Other Parts Discussed in Thread: MSP430F2616
Tool/software: Code Composer Studio
In the below code LCD is connected in 8 bit mode to port 4 and controls are provided in as defined. Now here I have used 8x2 lcd module. The display changes each time I powerup the module. Also as the LCD data is in the while loop now the display changes every time. So seems there is a loss of synchronisation. There seems to be a problem in the code itself. Please guide to reduce the randomness of the LCD.
#include <msp430F2616.h>
#define DR P5OUT = P5OUT | BIT7 // define RS high
#define CWR P5OUT = P5OUT & (~BIT7) // define RS low
#define READ P5OUT = P5OUT | BIT5 // define Read signal R/W = 1 for reading
#define WRITE P5OUT = P5OUT & (~BIT5) // define Write signal R/W = 0 for writing
#define ENABLE_HIGH P5OUT = P5OUT | BIT6 // define Enable high signal
#define ENABLE_LOW P5OUT = P5OUT & (~BIT6) // define Enable Low signal
void delay(unsigned char wt);
void init(void);
void init_lcd(void);
void wr_lcd_cw(unsigned char lcd_cmd);
void wr_lcd_dw(unsigned char lcd_data);
void lcd_msg(unsigned char *ch);
int s;
//PIN R/W =
/*
* main.c
*/
int main(void) {
WDTCTL = WDTPW | WDTHOLD; // Stop watchdog timer
init();
init_lcd();
while (1)
{
volatile unsigned int i ;
wr_lcd_cw(0x80);
lcd_msg("abCDE");
wr_lcd_cw(0xC0);
lcd_msg("Aefgh");
P1OUT ^= BIT5|BIT6|BIT7; // Toggle P1.0 using exclusive-OR
for( i = 0 ; i <= 50000 ; i++ );
}
}
void delay(unsigned char wt)
{
unsigned int k,j;
for(k=0;k<=wt;k++)
{
for(j=0;j<=3000;j++);
}
}
void lcd_msg(unsigned char *ch)
{
while(*ch !='\0')
{
wr_lcd_dw(*ch);
ch++;
}
}
void wr_lcd_cw(unsigned char lcd_cmd){
delay(10);
CWR;
WRITE;
P4OUT=lcd_cmd;
ENABLE_HIGH;//EN=1
delay(3);
ENABLE_LOW;//EN=0
}
void wr_lcd_dw(unsigned char lcd_data){
delay(10);
DR;
WRITE;
P4OUT=lcd_data;
ENABLE_HIGH;//EN=1
delay(3);
ENABLE_LOW;//EN=0
}
void init_lcd(void)
{
for(s=0;s<=5000;s++);
wr_lcd_cw(0x30);delay(10);//LCD in 8 bit mode
wr_lcd_cw(0x30);delay(160);//LCD in 8 bit mode
wr_lcd_cw(0x30);delay(160);//LCD in 8 bit mode
wr_lcd_cw(0x02);delay(12);//LCD in 8 bit mode
wr_lcd_cw(0x38);delay(10);//LCD in 8 bit mode
wr_lcd_cw(0x0C);delay(10);//Display ON & cursor off
wr_lcd_cw(0x14);delay(10);//shift data right by 1 character
wr_lcd_cw(0xc0);delay(10);
wr_lcd_cw(0x01);delay(160);//clear screen
//lcd_content(0x02);//return to home location
}
void init(void)
{
BCSCTL1 = 0xC7;
BCSCTL2 = 0x00;
BCSCTL3 = 0x20;
P1SEL=0;
P5SEL=0;
P4SEL=0;
P1DIR=0xff;
P4DIR=0xff;
P5DIR=0xff;
P1DIR |= BIT5|BIT6; // Set P1.0 to output direction
P4DIR = 0XFF; // Set Port 4 as output
P5DIR = 0XFF; // Set Port 5 as output
P4OUT = 0X00;
P5OUT = 0X00;
}