/* for EnableInterrupts macro */ #include \"derivative.h\" /* include peripheral declarations */#define Senddata PTGD_PTGD2 #define sclk PTGD_PTGD1 #define rclk PTGD_PTGD0
#define LCDE PTDD_PTDD0 #define LCDRW PTBD_PTBD2 #define LCDRS PTBD_PTBD3
void delay (unsigned int a);
void LCD_writedata(unsigned char WDLCD);
void LCD_writecommand(unsigned char WCLCD); void LCD_init(void);
void displayone (unsigned char x, unsigned char y,unsigned char data); void displaystring(unsigned char x, unsigned char y,unsigned char *data); void shift595(unsigned char a);
void main(void) {
unsigned char title[]=\"lcd1602\" ; EnableInterrupts; /* enable interrupts */ /* include your code here */
PTGDD=0x07; PTBDD=0x0c; PTDDD=0x01;
LCD_init(); delay (100);
//displayone (0,0,'x'); for(;;) {
displaystring(0,0,title);
__RESET_WATCHDOG(); /* feeds the dog */ } /* loop forever */
/* please make sure that you never leave main */ }
void LCD_writedata(unsigned char WDLCD) {
LCDRS=1; LCDRW=0;
shift595(WDLCD); delay(30); LCDE=0; LCDE=1; LCDE=0; delay(30); }
void LCD_writecommand(unsigned char WCLCD) {
LCDRS=0; LCDRW=0;
shift595(WCLCD); delay(30); LCDE=0; LCDE=1; LCDE=0; delay(30); }
void LCD_init(void) {
shift595(0x00);
LCD_writecommand(0x38); delay(30);
LCD_writecommand(0x38);
delay(30);
LCD_writecommand(0x08); delay(30);
LCD_writecommand(0x08); delay(30);
LCD_writecommand(0x06); delay(30);
LCD_writecommand(0x0c); delay(30); }
void displayone (unsigned char x, unsigned char y,unsigned char data) { x&=0x0f; y&=0x01; if(y) x|=0x40; x|=0x80;
LCD_writecommand(x); LCD_writedata(data);
}
void displaystring(unsigned char x, unsigned char y,unsigned char *data) { unsigned char stringlength; x&=0x0f; y&=0x01;
stringlength=0;
while(data[stringlength]!='\\0') {
if(x<0x0f)
displayone( x,y,data[stringlength]); else { x=x-0x10; y=y+1;
displayone( x,y,data[stringlength]); }
stringlength++; x++; } }
void delay (unsigned int a) {
unsigned int i; unsigned char j; for(i=0;ifor(j=0;j<200;j++); __RESET_WATCHDOG(); } }void shift595(unsigned char a) { unsigned char j; for(j=0;j<8;j++) { if(a&0x80) Senddata=1;
else
Senddata=0; a=a<<1; sclk=0;
sclk=1; }
rclk=0; rclk=1; }