资源描述:
《lcd1602液晶显示与4×4矩阵键盘》由会员上传分享,免费在线阅读,更多相关内容在应用文档-天天文库。
1、一、要求: 液晶显示器第一行显示“HelloWorld!”; 第二行显示键盘的键值。二、程序代码:#include#include#defineucharunsignedchar#defineuintunsignedintunsignedcharcodedis1[]={"HelloWorld!"};ucharkey_val[]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F
2、','G'};sbitU3_DS=P1^5;sbitU3_STCP=P1^4;sbitU3_SHCP=P1^3;sbitU4_DS=P1^2;sbitU4_STCP=P1^1;sbitU4_SHCP=P1^0;voiddelay(unsignedintn);//74HC595voidU3_595(unsignedcharnum) { unsignedcharcount1; for(count1=0;count1<=7;count1++) { if((num&0x80)
3、==0x80)//最高位为1,则向SDATA_595发送1 { U3_DS=1; } else { U3_DS=0; } U3_SHCP=0; U3_SHCP=1; num<<=1;//左移 }U3_STCP=0;U3_STCP=1;}voidU4_595(unsignedcharnum)//发送指令到RS,RW,E(
4、4,5,6位) { unsignedcharcount2; for(count2=0;count2<=7;count2++) { if((num&0x80)==0x80) { U4_DS=1; } else { U4_DS=0; } U4_SHCP=0; U4_SHCP=1;
5、 num<<=1; } U4_STCP=0; U4_STCP=1;}//LCD延时子程序n=1时延时1msvoiddelay(unsignedintn) { unsignedinti; for(;n>0;n--) for(i=0;i<125;i++) _nop_(); }//写指令到LCDvoidwcmd(unsignedcharcmd){ U4_595(0x00); U3_595(cmd); U4_595(0x
6、40); U4_595(0x00);}//写要显示的数据到LCDvoidwdat(unsignedchardat){ U4_595(0x10); U3_595(dat); U4_595(0x50); U4_595(0x10);}//初始化LCD子程序voidinit(){ wcmd(0x38);//设置8位总线双行显示,5*7点阵 delay(20); wcmd(0x0C);//开显示,开光标,不闪烁 delay(20); wcmd(0x06);//读写字符
7、时地址加1 delay(20); wcmd(0x01);//清屏 delay(20);}//键盘扫描子程序voidkeyscan(void){unsignedcharn;//扫描第一行 P0=0xEF; n=P0; n&=0x0F;if(n!=0x0F){ delay(1);//去抖动 P0=0xEF; n=P0; n&=0x0F; if(n!=0x0F) { switch(n) { case(0x0E):wdat(key_val[0x01
8、]);break; case(0x0D):wdat(key_val[0x02]);break; case(0x0B):wdat(key_val[0x03]);break; case(0x07):wdat(key_val[0x04]);break; } } } P0=0xDF;//扫描第二行 n=P0; n&=0x0F;if(n!=0x0F) { delay(1); P0=0xDF; n=P0; n&=0x