/*-- LCD v1.5 - non ASCII for command entry v1.4 - better timing microproccesssor pins ---- RST -| |- VCC RX P3_0 -| 2 |- P1_7 D7 TX P3_1 -| 0 |- P1_6 D6 XTAL -| 5 |- P1_5 D5 XTAL -| 1 |- P1_4 D4 P3_2 -| |- P1_3 D3 E P3_3 -| |- P1_2 D2 RS P3_4 -| |- P1_1 * D1 RW P3_5 -| |- P1_0 * D0 GND -| |- P3_7 ---- LCD pins 1 ground 2 Vcc 3 contrast 4 RS 5 RW 6 E 7 D0 8 D1 9 D2 10 D3 11 D4 12 D5 13 D6 14 D7 --*/ #include "reg.h" #include #define DATA P1 #define RS P3_4 #define RW P3_5 #define E P3_3 #define LCD_FUNCTION_CODE 0x38 // 1 line:30, 2 lines:38 void pause(int); // select pause in milliseconds void loop(int); // pauses 9us per count void serial_init(void); // void lcd_init(void); // void lcd_command(char); // void lcd_send(char); // char gethex2(void); // void sendhex2(char); // /*----------------------------------*/ main(){ char a; serial_init(); lcd_init(); lcd_send('V'); lcd_send('1'); lcd_send('.'); lcd_send('5'); while(1){ putchar(':'); a=_getkey(); switch (a) { case 0x1B: //ESC, to enter a command from the keyboard putchar('\n'); putchar('*'); a=gethex2(); lcd_command(a); break; case 0x03: //ctrl C a=_getkey(); lcd_command(a); break; case 0x08: //backspace lcd_command(0x10); lcd_send(' '); lcd_command(0x10); break; case 0x0D: //return, home lcd_command(0x02); break; case 0x18: // ctrl X, clear screen lcd_command(0x01); break; case 0x0A: // second line lcd_command(0xc0); break; case 0x12: // ctrl R, read data RS=1; RW=1; E=1; a=DATA; E=0; sendhex2(a); pause(2); break; case 0x11: // ctrl Q, read busy flag and address RS=0; RW=1; E=1; a=DATA; E=0; sendhex2(a); pause(2); break; default: putchar(a); lcd_send(a); break; } } //while } //main /*----------------------------*/ void pause(int ms) { int n; while(ms!=0){ n=125; while(n!=0){ n=n-1; } ms=ms-1; } } /*---------------------------*/ void serial_init(void){ SM0=0; SM1=1; SM2=1; REN=1; SCON = 0x50; /* SCON: mode 1, 8-bit UART, enable rcvr */ TMOD &= 0x0f; TMOD |= 0x20; /* TMOD: timer 1, mode 2, 8-bit reload */ TH1 = 0xfd; /* TH1: reload value for 9600 baud */ //TH1 = 0xfa; /* TH1: reload value for 4800 baud */ //TH1 = 0xf4; /* TH1: reload value for 2400 baud */ TR1 = 1; /* TR1: timer 1 run */ TI = 1; /* TI: set TI to send first char of UART */ return; } /*----------------------------*/ void lcd_init(void){ pause(1000); E=0; lcd_command(LCD_FUNCTION_CODE); lcd_command(LCD_FUNCTION_CODE); lcd_command(LCD_FUNCTION_CODE); lcd_command(0x06); lcd_command(0x0E); } /*-------------------------------*/ void lcd_command(char a) { RS=0; RW=0; DATA=a; E=1; E=0; loop(190); // creates more than the needed 1.64ms delay return; } /*-----------------------------*/ void lcd_send(char a) { RS=1; RW=0; DATA=a; E=1; E=0; loop(5); // creates more than the needed 40us delay return; } /*-------------------------------*/ char gethex2(void) { char a, high, low; first: high = _getkey(); if(high > 0x66) {goto first;} if(high >= 0x61) {putchar(high); high=high-0x57; goto second;} if(high > 0x39) {goto first;} if(high >= 0x30) {putchar(high); high=high-0x30; goto second;} goto first; second: low = _getkey(); if(low > 0x66) {goto second;} if(low >= 0x61) {putchar(low); low=low-0x57; goto join;} if(low > 0x39) {goto second;} if(low >= 0x30) {putchar(low); low=low-0x30; goto join;} goto second; join: a=high*16+low; return a; } /*--------------------------------*/ void sendhex2(char a) { char high, low; high = a/16 + 0x30; low = a%16 + 0x30; if(high > 0x39) {high = high + 0x27;} if(low > 0x39) {low = low + 0x27;} putchar(high); putchar(low); return; } /*----------------------------------*/ // creates a time delay of about 9us per count void loop (int n) { while(n != 0){n=n-1;} return; }