Date : 2012.9.18
----------------------------------------*/ # include # include "macroandconst.h" # define LcdData P0 sbit EN=P3^5; sbit RW=P3^6; sbit RS=P3^7; //sfr LcdData=0x80; /********************* 延时函数 *********************/ void Delay(uint16 t) { while(t--); } /********************* LCD1602驱动程序 *********************/ void //完成一个字节命令的写入LcdWriteCmd(uint8 cmd) { Delay(1000); EN=0; RW=0; RS=0; LcdData=cmd; EN=1; Delay(20); EN=0; } void //完成一个字节数据的写入LcdWriteDat(uint8 dat) { Delay(1000); EN=0; RW=0; RS=1; EN=1; LcdData=dat; EN=0; } void //清屏 LcdClear() { LcdWriteCmd(0x01); } void //初始化液晶屏 LcdInit() { LcdWriteCmd(0x38); Delay(200); LcdWriteCmd(0x0e); Delay(200); LcdWriteCmd(0x06); Delay(200); } void //完成字符串的写入,如 果第一行写满,自动切换到第 二行 LcdWriteStr(uint8 addr,uint8 *p) { LcdWriteCmd(addr); while(*p!=0x00) { LcdWriteDat(*p); p++; if(addr++==0x8f) { LcdWriteCmd(0xc0); } } } //主函数 void main() { LcdClear(); LcdInit(); LcdWriteStr(0x80,"HeHuiB o"); LcdWriteStr(0xc0,"1010107 2054"); while(1); } Created By : Paul He Date : 2012.9.19 ----------------------------------------*/ # include # include "macroandconst.h" # define LcdData P0 sbit EN=P3^5; sbit RW=P3^6; sbit RS=P3^7; //sfr LcdData=0x80; /********************* 延时函数 *********************/ void Delay(uint16 t) { while(t--); } /********************* LCD128驱动程序 *********************/ void LcdWriteCmd(uint8 cmd) { Delay(5000); EN=0; RW=0; RS=0; EN=1; LcdData=cmd; Delay(50); EN=0; } void LcdWriteDat(uint8 dat) { Delay(5000); EN=0; RW=0; RS=1; EN=1; LcdData=dat; Delay(50); EN=0; } void LcdClear() { LcdWriteCmd(0x01); } void LcdInit() { Delay(0xffff); Delay(0xffff); LcdWriteCmd(0x30); Delay(1000); LcdWriteCmd(0x30); Delay(1000); LcdWriteCmd(0x0c); Delay(1000); LcdWriteCmd(0x01); Delay(1000); LcdWriteCmd(0x06); Delay(1000); } void LcdShowGroup(uint8 x,uint8 y,uint8 *p) { switch(y) { case 1: LcdWriteCmd(0x80+x-1);brea k; case 2: LcdWriteCmd(0x90+x-1);brea k; case 3: LcdWriteCmd(0x88+x-1);break; case 4: LcdWriteCmd(0x98+x-1);break; default :break; } while(*p) { LcdWriteDat(*p++); } } //主函数 void main() { LcdClear(); LcdInit(); LcdShowGroup(1,1,"128 液晶实验"); LcdShowGroup(3,2,"何辉波 "); LcdShowGroup(2,3,"101010 72054"); while(1); } # include "macroandconst.h" # define LcdData P0 sbit EN=P3^5; sbit RW=P3^6; sbit RS=P3^7; uint8 lcdcount=0; /********************* 延时函数 *********************/ void Delay(uint16 t) { while(t--); } /********************* LCD1602驱动程序 *********************/ void LcdWriteCmd(uint8 cmd) { Delay(1000); EN=0; RW=0; RS=0; LcdData=cmd; EN=1; Delay(20); EN=0; } void LcdWriteDat(uint8 dat) { Delay(1000); EN=0; RW=0; RS=1; EN=1; LcdData=dat; EN=0; } void LcdClear() { LcdWriteCmd(0x01); } void LcdInit() { LcdWriteCmd(0x38); Delay(200); LcdWriteCmd(0x0e); Delay(200); LcdWriteCmd(0x06); Delay(200); } /*void LcdWriteStr(uint8 addr,uint8 *p) { LcdWriteCmd(addr); while(*p!=0x00) { LcdWriteDat(*p); p++; if(addr++==0x8f) { LcdWriteCmd(0xc0); } } } */ void UART0_Init() { SCON=0x50; TMOD|=0x20; PCON|=0x80; TH1=0xFD; TL1=0xFD; IE|=0x90; TR1=1; } void UART0_SendByte(uint8 dat) { EA=0; SBUF=dat; while(TI==0); TI=0; EA=1; } void UART0_SendStr(uint8 *Str) { uint8 i; i=0; while(*(Str+i)!='\\0') { UART0_SendByte(*(Str+i)); i=i+1; } } void UART0_SendUint16(uint16 dat) { uint8 flag; uint8 temp; flag=0; temp=dat/10000; if (temp!=0||flag!=0) { flag=1; UART0_SendByte(temp+'0'); } dat=dat%10000; temp=dat/1000; if (temp!=0||flag!=0) {flag=1; UART0_SendByte(temp+'0'); } dat=dat%1000; temp=dat/100; if (temp!=0||flag!=0) { flag=1; UART0_SendByte(temp+'0'); } dat=dat%100; temp=dat/10; if (temp!=0||flag!=0) { flag=1; UART0_SendByte(temp+'0'); } dat=dat%10; UART0_SendByte(temp+'0'); } void UART_Rcv()interrupt 4 { uint8 temp; EA=0; if(RI) { RI=0; temp=SBUF; UART0_SendByte(temp); UART0_SendByte(0x0d); UART0_SendByte(0x0a); lcdcount++; if(lcdcount==16) { LcdWriteCmd(0xc0); } if(lcdcount==32) { lcdcount=0; LcdClear(); } LcdWriteDat(temp); } EA=1; } void main() { LcdClear(); UART0_Init(); LcdInit(); UART0_SendStr("串口程序欢迎您!"); UART0_SendByte(0x0d); UART0_SendByte(0x0a); UART0_SendUint16(0x020 ); UART0_SendByte(0x0d); UART0_SendByte(0x0a); while(1); } # include "macroandconst.h" sbit EN=P3^5; sbit RW=P3^6; sbit RS=P3^7; //sfr LcdData=0x80; # define LcdData P0 # define keyport P2 //键盘驱动程序 bit ReadKey(void) { bit dat; keyport =0xf0;//键盘口高四位列线为1,低四位行线为0 if(keyport==0xf0) { dat=0;//无按键按下 } else { dat=1; } return dat; } uint8 IdentifyKey(void) { uint8 i=1,j=0xfe,m; while(j!=0x7f) { keyport=j; m=keyport&0xf0; switch(m) { case 0xe0 : { i=i+0; j=0x7f; break; } case 0xd0 : { i=i+4; j=0x7f; break; } case 0xb0 : { i=i+8; j=0x7f; break; } case 0x70 : { i=i+12; j=0x7f; break; } default : { i=i+1; j=(j<<1)+1; break; } } } return i; } /********************* 延时函数 *********************/ void Delay(uint16 t) { while(t--); } /********************* LCD1602驱动程序 *********************/ void //完成一个字节命令的写 入操作 LcdWriteCmd(uint8 cmd) { Delay(1000); EN=0; RW=0; RS=0; LcdData=cmd; EN=1; Delay(20); EN=0; } void //完成一个字节数据的写 入操作 LcdWriteDat(uint8 dat) { Delay(1000); EN=0; RW=0; RS=1; EN=1; LcdData=dat; EN=0; } void //完成液晶的清屏操作 LcdClear() { LcdWriteCmd(0x01); } void //完成液晶初始化操作 LcdInit() { LcdWriteCmd(0x38); Delay(200); LcdWriteCmd(0x0e); Delay(200); LcdWriteCmd(0x06); Delay(200); } void //完成字符串雪乳操作,如果第一行写满,自动切换到第二行。LcdWriteStr(uint8 addr,uint8 *p) { LcdWriteCmd(addr); while(*p!=0x00) { LcdWriteDat(*p); p++; if(addr++==0x8f) { LcdWriteCmd(0xc0); } } } //键盘与液晶程序应用 void DisplayKey(uint8 i,uint8 k) { LcdWriteCmd(i); if(k>=10) { LcdWriteDat(k%10+'A'); LcdWriteDat(k*0+' '); } else { LcdWriteDat(k/10+0x30); LcdWriteDat(k%10+0x30); } } void ScanKey(void) { if(ReadKey()) { uint8 key; key=IdentifyKey(); while(ReadKey()); DisplayKey(0xca,key); } } void main() { LcdClear(); LcdInit(); LcdWriteStr(0x81,"KeyB oard Test"); LcdWriteStr(0xc0,"KeyN umber="); while(1) { ScanKey(); Delay(0xffff); } } # include # include "macroandconst.h" uint8 count50=0; uint8 second=0,minute=0,hour=0; /*-------------------------------- LCD端口定义 ---------------------------------*/ sfr LcdData=0x80; sbit EN=P3^5; sbit RW=P3^6; sbit RS=P3^7; /********************* 延时函数 *********************/ void Delay(uint16 t) { while(t--); } /********************* LCD1602驱动程序 *********************/ void LcdWriteCmd(uint8 cmd) { Delay(1000); EN=0; RW=0; RS=0; LcdData=cmd; EN=1; Delay(20); EN=0; } void LcdWriteDat(uint8 dat) { Delay(1000); EN=0; RW=0; RS=1; EN=1; LcdData=dat; EN=0; } void LcdClear() { LcdWriteCmd(0x01); } void LcdInit() { LcdWriteCmd(0x38); Delay(200); LcdWriteCmd(0x0e); Delay(200); LcdWriteCmd(0x06); Delay(200); } void LcdWriteStr(uint8 addr,uint8 *p) { LcdWriteCmd(addr); while(*p!=0x00) { LcdWriteDat(*p); p++; if(addr++==0x8f) { LcdWriteCmd(0xc0); } } } /*---------------------------------- -------- 液晶显示函数 --------------------------------------- ----*/ void display(uint8 addr,uint8 dat) { LcdWriteCmd(addr); LcdWriteDat(dat/10+0x30 ); LcdWriteDat(dat%10+0x 30); } /*------------------------------------- ----- 定时器中断0 --------------------------------------- ----*/ void TimerInit()interrupt 1 { TL0=0x00; TH0=0xb8; count50++; if(count50==50) { count50=0; second++; if(second==60) { second=0; minute++; if(minute==60) { minute=0; hour++; if(hour==24) { hour=0; } display(0xc3,hour); } } display(0xc9,second); } } void main() { LcdClear(); LcdInit(); Delay(0xffff); LcdWriteStr(0x85,"clock"); display(0xc3,hour); LcdWriteDat(':'); display(0xc6,minute); LcdWriteDat(':'); display(0xc9,second); TMOD=0x01; EA=1; ET0=1; TL0=0x00; TH0=0xB8; TR0=1; while(1); } # include "1602.h" /********************* 延时函数 *********************/ void Delay(uint16 t) { while(t--); } /********************* LCD1602驱动程序 *********************/ void LcdWriteCmd(uint8 cmd) { Delay(1000); EN=0; RW=0; RS=0; LcdData=cmd; EN=1; Delay(20); EN=0; } void LcdWriteDat(uint8 dat) { Delay(1000); EN=0; RW=0; RS=1; EN=1; LcdData=dat; EN=0; } void LcdClear() { LcdWriteCmd(0x01); } void LcdInit() { LcdWriteCmd(0x38); Delay(200); LcdWriteCmd(0x0e); Delay(200); LcdWriteCmd(0x06); Delay(200); } void LcdWriteStr(uint8 addr,uint8 *p) { LcdWriteCmd(addr); while(*p!=0x00) { LcdWriteDat(*p); p++; if(addr++==0x8f) { LcdWriteCmd(0xc0); } } } # include "macroandconst.h" # include "1602.h" # include "uart.h" uint8 lcdcount=0; /********************* 延时函数 *********************/ void Delay(uint16 t) { while(t--); } /********************* LCD1602驱动程序 *********************/ void LcdWriteCmd(uint8 cmd) { Delay(1000); EN=0; RW=0; RS=0; LcdData=cmd; EN=1; Delay(20); EN=0; } void LcdWriteDat(uint8 dat) { Delay(1000); EN=0; RW=0; RS=1; EN=1; LcdData=dat; EN=0; } void LcdClear() { LcdWriteCmd(0x01); } void LcdInit() { LcdWriteCmd(0x38); Delay(200); LcdWriteCmd(0x0e); Delay(200); LcdWriteCmd(0x06); Delay(200); } void LcdWriteStr(uint8 addr,uint8 *p) { LcdWriteCmd(addr); while(*p!=0x00) { LcdWriteDat(*p); p++; if(addr++==0x8f) { LcdWriteCmd(0xc0); } } } void UART_Rcv()interrupt 4 { uint8 temp; EA=0; if(RI) { RI=0; temp=SBUF; UART0_SendByte(temp); UART0_SendByte(0x0d); UART0_SendByte(0x0a); lcdcount++; if(lcdcount==16) { LcdWriteCmd(0xc0); } if(lcdcount==32) { lcdcount=0; LcdClear(); } LcdWriteDat(temp); } EA=1; } void main() { LcdClear(); UART0_Init(); LcdInit(); UART0_SendStr("串口程 序欢迎您!"); UART0_SendByte(0x0d); UART0_SendByte(0x0a); UART0_SendUint16(0x02 0); UART0_SendByte(0x0d); UART0_SendByte(0x0a); while(1); }下载本文