欢迎来到天天文库
浏览记录
ID:37926112
大小:40.50 KB
页数:8页
时间:2019-06-02
《1602LCD时钟C程序》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、#include#includesbitrs=P2^0;sbitrw=P2^1;sbite=P2^2;//使能信号输入端sbitsset=P1^0;//设置按钮sbitsadd=P1^1;//加按钮sbitsdel=P1^2;//减按钮unsignedcharsecond,minute,hour,count,keycount=0;unsignedcharcodetable0[]="TheTimeIs:";//第一行显示unsignedcharcodetable1[]="::";//第二行
2、显示unsignedcharcodetable2[]="0123456789";voiddelay(unsignedintx)//延时{unsignedinta,b;for(a=x;a!=0;a--){for(b=20;b!=0;b--){;}}}voiddel10ms()//延时10ms{unsignedinta,b;for(a=20;a!=0;a--){for(b=248;b!=0;b--){;}}}voidwritecomm(unsignedcharcomm)//写入指令寄存器{rs=0;P0=comm;e=0;dela
3、y(5);//写,下降沿有效e=1;delay(5);e=0;}voidwritedat(unsignedchardat)//写入数据寄存器{rs=1;P0=dat;e=0;delay(5);//写,下降沿有效e=1;delay(5);e=0;}voidwritesmh(unsignedcharadd,unsignedchardate)//显示个位和十位数据{unsignedcharshi,ge;shi=date/10;ge=date%10;writecomm(0xc0+add);delay(5);writedat(table
4、2[shi]);delay(5);writedat(table2[ge]);delay(5);}voidkeyscan()//{if(sset==0){del10ms();if(sset==0){keycount++;while(sset==0){;}if(keycount==1){TR0=0;writecomm(0xcb);writecomm(0x0f);}if(keycount==2){writecomm(0xc8);writecomm(0x0f);}if(keycount==3){writecomm(0xc5);writ
5、ecomm(0x0f);}if(keycount==4){keycount=0;writecomm(0x0c);TR0=1;}}}if(keycount!=0){if(keycount==1)//编辑秒{if(sadd==0)//数值加1{del10ms();if(sadd==0){second++;if(second==60){second=0;}while(sadd==0){;}}writesmh(10,second);}if(sdel==0)//数值减1{del10ms();if(sdel==0){if(second==
6、0){second=60;}second--;while(sdel==0){;}}writesmh(10,second);}writecomm(0x0f);writecomm(0xcb);}if(keycount==2)//编辑分钟{if(sadd==0)//数值加1{del10ms();if(sadd==0){minute++;if(minute==60){minute=0;}while(sadd==0){;}}writesmh(7,minute);}if(sdel==0)//数值减1{del10ms();if(sdel==
7、0){if(minute==0){minute=60;}minute--;while(sdel==0){;}}writesmh(7,minute);}writecomm(0xc8);writecomm(0x0f);}if(keycount==3)//编辑小时{if(sadd==0)//数值加1{del10ms();if(sadd==0){hour++;if(hour==24){minute=0;}while(sadd==0){;}}writesmh(4,hour);}if(sdel==0)//数值减1{del10ms();if
8、(sdel==0){if(hour==0){hour=24;}hour--;while(sdel==0){;}}writesmh(4,hour);}writecomm(0xc5);writecomm(0x0f);}}}voidinit()//初始化{unsignedchari
此文档下载收益归作者所有