Advertisement
Guest User

Untitled

a guest
Jan 29th, 2015
416
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.89 KB | None | 0 0
  1. #include "msp430g2553.h"
  2.  
  3. #define DIR P2DIR
  4. #define POUT P2OUT
  5.  
  6. //following maps bits to lcd pins
  7. //note: remember to set p5 to gnd to set r/w to data write
  8.  
  9. #define RS BIT5                 //p4;; high sets data input;; low sets instruction input
  10. #define EN BIT4                 //p6;; pulled high to enable device
  11. #define D4 BIT0                 //p11
  12. #define D5 BIT1                 //p12
  13. #define D6 BIT2                 //p13
  14. #define D7 BIT3                 //p14
  15.  
  16. #define PMSK (RS|EN|D7|D6|D5|D4)
  17.  
  18. #define FALSE 0
  19. #define TRUE 1
  20.  
  21.  
  22. void enable(){
  23.   //POUT &= ~EN;                  //pulls en bit low
  24.   //__delay_cycles(5000);
  25.   POUT |= EN;                   //pulls en bit high
  26.   __delay_cycles(200);
  27.   POUT &= ~EN;
  28.   __delay_cycles(200);
  29. }
  30.  
  31. //function to send a byte to the display
  32. //might just use bool as second argument of this function
  33. void sendbyte(char bsend, int isdata){
  34.   POUT &= ~PMSK;                 //clears all output pins
  35.   //now need to send high nibble first
  36.   //right shift 4 due to xmitting on pins 0->3
  37.   POUT |=((bsend >> 4) & 0x0F);
  38.   if(isdata==TRUE){
  39.     POUT |= RS;
  40.   }
  41.   else{
  42.     POUT &= (~RS);
  43.   }
  44.   enable();                     //sends enable signal.
  45.   POUT &= ~PMSK;              //clears all output pins
  46.   //now need to send low nibble
  47.   //no shifting required, already lined up.
  48.   POUT |= (bsend & 0x0F);
  49.   if(isdata==TRUE){
  50.     POUT |= RS;
  51.   }
  52.   else{
  53.     POUT &= (~RS);
  54.   }
  55.  enable();
  56. }
  57.  
  58. //the following function sets the cursor position
  59. void setcursor(char row, char col){
  60.   char address;
  61.   //make address from row, col coords.
  62.   if(row==0){
  63.     address=0;
  64.   }
  65.   else{
  66.     address=0x40;               //address of the second line in memory
  67.   }
  68.   address |= col;               //sets the offset from the row
  69.   sendbyte((0x80|address), FALSE);
  70. }
  71.  
  72. //clears the screen and returns cursor to home
  73. void clrscreen(){
  74.   sendbyte(0x01, FALSE);
  75.   sendbyte(0x02, FALSE);
  76. }
  77.  
  78. //initialization subroutine, only run once on startup
  79. void init(void){
  80.   P1DIR |= 0x01;
  81.   P1OUT = 0x00;
  82.   DIR |= PMSK;                  //sets all pmask pins to output
  83.   POUT &= (~PMSK);              //sets all pins low
  84.   __delay_cycles(1000000);       //lcd needs delay to power up
  85.  
  86.   //following are magical commands to reset for 4 bit mode..
  87.   //set to 8 bit 3 times then initialize as 4 bit
  88.   POUT |= 0x03;
  89.   enable();
  90.   enable();
  91.   enable();
  92.   //send 4 bit input - second time? supposedly but
  93.   POUT &= ~PMSK;
  94.   POUT |= (0x02);
  95.   enable();
  96.   sendbyte(0x28, FALSE);
  97.   //display on
  98.   sendbyte(0x01, FALSE);
  99.   //set cursor behavior (display and blink)
  100.   sendbyte(0x0F, FALSE);
  101.   //allow cursor auto increment
  102.   sendbyte(0x06, FALSE);
  103.   //set to home
  104.   sendbyte(0x02, FALSE);
  105. }
  106.  
  107. void printstr(char* str){
  108.   char* c;
  109.   c=str;
  110.   while((c!=0)&&(*c!=00)){
  111.     sendbyte(*c, TRUE);
  112.     c++;
  113.     P1OUT ^= BIT0;
  114.   }
  115.   P1OUT &= ~BIT0;
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement