#include <msp430.h>				

#define rs  BIT0 //2.0
#define en  BIT2 // 2.2
#define LCD_Port  P1OUT //1.2 1.3 1.4 1.5

void PulseLcm(){
    P2OUT &= ~en;
    __delay_cycles(200);
    P2OUT |= en;
    __delay_cycles(200);
    P2OUT &= ~en;
    __delay_cycles(200);
}

void LCD_Command (char cmd)
{
    LCD_Port |= 0x00;

    LCD_Port |= (cmd & 0xF0);
    P2OUT &= ~rs;   // rs = 0 
    PulseLcm();


    LCD_Port |=  ((cmd & 0x0F)<<4); /* Send lower nibble */
    P2OUT &= ~rs;   // rs = 0
    PulseLcm();
}

void LCD_Char (char data)  /* LCD data write function */
{
    LCD_Port |= 0x00;

    LCD_Port |=(data & 0xF0);/* Send upper nibble */
    P2OUT |= rs;     // rs = 1
    PulseLcm();

    LCD_Port |= ((data & 0x0F)<<4); /* Send lower nibble */
    P2OUT |= rs;     // rs = 1
    PulseLcm();

}

void LCD_String (char *str) 
{
 int i;
 for(i=0;str[i]!=0;i++) 
 {
  LCD_Char (str[i]); 
 }
}

void LCD_String_xy (char row, char pos, char *str) 
{
 if (row == 0)
 LCD_Command((pos & 0x0F)|0x80);
 else if (row == 1)
 LCD_Command((pos & 0x0F)|0xC0);
 LCD_String(str);   /* Call LCD string function */
}

void LCD_Init (void) 
{
    LCD_Port |= 0x00;
    __delay_cycles(100000);
    P2OUT &= ~rs;
    P2OUT &= ~en;
    PulseLcm();

     LCD_Command (0x28); /* Initialization of 16X2 LCD in 4bit mode */
     LCD_Command (0x0E); /* Display ON Cursor OFF */
     LCD_Command (0x06); /* Auto Increment cursor */
}

void ClearLCD(){
    LCD_Command (0x01);
    LCD_Command (0x02);
}



void main (void)
{
    WDTCTL = WDTPW+WDTHOLD;

    P1DIR |= BIT2 + BIT3 + BIT4 + BIT5;
    P2DIR |= BIT0 + BIT2;
    P1OUT |= 0x00;
    P2OUT |= 0x00;

    LCD_Init();  /* Initialization of LCD*/
    ClearLCD();

    LCD_String("AB");


    while(1);
}