#include #include #define uint unsigned int//键盘扫描的思想是将行设置为低,列设置为高,来读取P3口的值,就能知道是哪个按键按下了 #define uchar unsigned char #define LCDdata P0 sbit E = P2^7; sbit RW = P2^6; sbit RS = P2^5; void init(); void delayms(uint x); void displaykey(); void write_com(uchar com);//写命令 void write_data(uchar date);//写数据 uchar temp; //--------------主函数----------------- void main() { init();// P3=0xfe;//P3=0xfd;//P3=0xfb;//P3=0xf7; while(1) { displaykey(); } } //-------------液晶初始化---------------- void init() { write_com(0x01); write_com(0x02); write_com(0x06); write_com(0x0e); } //------------毫秒延时--------------- void delayms(uint x) { uchar i; while(x--) { for(i=0;i<120;i++); } } //------------写命令----------------- void write_com(uchar com) { RS = 0; RW = 0; E = 0; LCDdata = com; E = 1; delayms(5); E = 0; } //-------------写数据------------------ void write_data(uchar date) { RS = 1; RW = 0; E = 0; LCDdata = date; E = 1; delayms(5); E = 0; } //---- void displaykey() { //****************************判断第一行的键盘 P3=0xfe; temp=P3; temp=temp&0xf0; while(temp!=0xf0)//判断是否有键盘按下 为真有键盘按下 { delayms(50); temp=P3; temp=temp&0xf0; while(temp!=0xf0)//再次判断是否有键盘按下 为真有键盘按下 { temp=P3; switch(temp) { case ');break; 0xee:write_com(0x80);write_data('0');write_com(0x81);write_data(' case ');break; 0xde:write_com(0x80);write_data('1');write_com(0x81);write_data(' case ');break; 0xbe:write_com(0x80);write_data('2');write_com(0x81);write_data(' case 0x7e:write_com(0x80);write_data('3');write_com(0x81);write_data(' ');break; }break;//这个break很重要不能丢下,若丢下会这这个中退步出来 } } //******************************判断第二行的键盘 P3=0xfd; temp=P3; temp=temp&0xf0; while(temp!=0xf0) { delayms(50); temp=P3; temp=temp&0xf0; while(temp!=0xf0) { temp=P3; switch(temp) { case ');break; 0xed:write_com(0x80);write_data('4');write_com(0x81);write_data(' case ');break; 0xdd:write_com(0x80);write_data('5');write_com(0x81);write_data(' case ');break; 0xbd:write_com(0x80);write_data('6');write_com(0x81);write_data(' case ');break; 0x7d:write_com(0x80);write_data('7');write_com(0x81);write_data(' } break; } } //******************************判断第三行的键盘 P3=0xfb; temp=P3; temp=temp&0xf0; while(temp!=0xf0) { delayms(50); temp=P3; temp=temp&0xf0; while(temp!=0xf0) { temp=P3; switch(temp) { case ');break; 0xeb:write_com(0x80);write_data('8');write_com(0x81);write_data(' case ');break; 0xdb:write_com(0x80);write_data('9');write_com(0x81);write_data(' case 0xbb:write_com(0x80);write_data('1');write_com(0x81);write_data('0');break; case 0x7b:write_com(0x80);write_data('1');write_com(0x81);write_data('1');break; }break; } } //******************************判断第四行的键盘 P3=0xf7; temp=P3; temp=temp&0xf0; while(temp!=0xf0) { delayms(50); temp=P3; temp=temp&0xf0; while(temp!=0xf0) { temp=P3; switch(temp) { case 0xe7:write_com(0x80);write_data('1');write_com(0x81);write_data('2');break; case 0xd7:write_com(0x80);write_data('1');write_com(0x81);write_data('3');break; case 0xb7:write_com(0x80);write_data('1');write_com(0x81);write_data('4');break; case 0x77:write_com(0x80);write_data('1');write_com(0x81);write_data('5');break; }break; } } } 因篇幅问题不能全部显示,请点此查看更多更全内容