1、51单片机4乘4矩阵和1602屏幕的计算器代码51单片机4*4矩阵和1602屏幕的计算器代码一、要求:液晶显示器第一行显示“Hello World!”;第二行显示键值和按键次数,且按键时间大于1.5秒时,识别为2次按键。单片机型号:STC-12C5A16AD二、程序代码:#include #include #define uchar unsigned char#define uint unsigned intunsigned char code dis=Hello World!;unsigned char code dis1=KEY:;unsigned char code dis2=TIME:
2、;uchar key_val=0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,G;uchar code key_code=0x77,0x7B,0x7D,0x7E,0xB7,0xBB,0xBD,0xBE,0xD7,0xDB,0xDD,0xDE,0xE7,0xEB,0xED,0xEE;uchar key,x,count;uint time=0;sbit U3_DS=P15;sbit U3_STCP=P14;sbit U3_SHCP=P13;sbit U4_DS=P12;sbit U4_STCP=P11;sbit U4_SHCP=P10;void delay(unsigned int
3、 n);/74HC595void U3_595(unsigned char num) unsigned char count1; for (count1=0;count1=7;count1+) if (num&0x80)=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;void U4_595(unsigned char num)/发送指令到RS,RW,E(4,5,6位) unsigned char count2; for (count2=0
4、;count2=7;count2+) if(num&0x80)=0x80) U4_DS=1; else U4_DS=0; U4_SHCP=0; U4_SHCP=1; num0;n-) for(i=0;i255;i+) _nop_();/写指令到LCDvoid wcmd(unsigned char cmd) U4_595(0x00); U3_595(cmd); U4_595(0x40); U4_595(0x00);/写要显示的数据到LCDvoid wdat(unsigned char dat) U4_595(0x10); U3_595(dat); U4_595(0x50); U4_595(0x1
5、0);/初始化LCDvoid init() wcmd(0x38);/设置8位总线双行显示,5*7点阵 delay(20); wcmd(0x0C);/开显示,开光标,不闪烁 delay(20); wcmd(0x06);/读写字符时地址加1 delay(20); wcmd(0x01);/清屏 delay(20); wcmd(0x80+2); for(x=0;x12;x+) /第一行显示hello world! wdat(disx); delay(20); wcmd(0xC2); for(x=0;x4;x+)/第二行显示按键和次数 wdat(dis1x); wcmd(0xC8); for(x=0;x
6、=0;i-) if(keycode=key_codei) /查找键码 key=i; return(key); else P0=0xff; /按键弹起则关闭定时器 TR0=0; count=0; return (16); void keydown() /判断按键按下和显示程序 P0=0xf0;if(P0&0xf0)!=0xf0) TR0=1; /开启定时器 while(P0!=0xf0) keyscan(); /获得键码 if(count30) time+; count=0; else /超过1.5秒计数2次 time+=2; count=0; wcmd(0xC6); /设置键值显示位置 wdat(key_val16-key); wcmd(0xCD); /设置次数显示位置 if(time9&time99&time1000) wdat(0x30+time/100); wdat(0x30+time/10-(time/100)*10); wdat(0x30+time%10); /中断函数void timer() interrupt 1 TH0=0x3C; TL0=0xB0; count+;void main(void)init();for(;) keydown();
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1