程序:
- #include<reg52.h>
- #define uchar unsigned char
- sbit rd=P2^6;
- sbit wr=P2^5;
- sbit lcde=P2^7;
- sbit k1=P3^2;
- sbit k2=P3^3;
- sbit k4=P3^4;
- sbit k8=P3^5;
- uchar code table1[]=" 2020-12-21 SAT";
- uchar code table2[]="00:00:00";
- uchar t,s1num;
- char shi,fen,miao;
- void delay(uchar z) //延迟zms函数
- {
- uchar x,y;
- for(x=z;x>0;x--)
- for(y=110;y>0;y--);
- }
- void write_com(uchar com) //液晶写指令操作
- {
- rd=0;
- wr=0;
- delay(5);
- lcde=1;
- P0=com;
- lcde=0;
- }
- void write_date(uchar date) //液晶写数据操作
- {
- rd=1;
- wr=0;
- delay(5);
- lcde=1;
- P0=date;
- lcde=0;
- }
- void write_sfm(uchar add,uchar date) //更新时分秒
- {
- uchar shi,ge;
- shi=date/10;
- ge=date;
- write_com(0x80+0x40+add);
- write_date(0x30+shi);
- write_date(0x30+ge);
- }
- void init() //初始化函数
- {
- uchar i;
- write_com(0x38);
- write_com(0x0c);
- write_com(0x06);
- write_com(0x01);
- write_com(0x80+0x00);
- for(i=0;i<15;i++)
- write_date(table1[i]);
- write_com(0x80+0x46);
- for(i=0;i<8;i++)
- write_date(table2[i]);
- TMOD=0x01; //定时器工作方式
- TH0=(65536-50000)/256; //设定定时器初值
- TL0=(65536-50000)%6; //12M晶振时ms数为
- EA=1;
- ET0=1;
- TR0=1;
- }
- void keyscan() //键盘扫描函数
- {
- if(k1==0) //按下k1键确定调时分秒
- {
- delay(5);
- if(k1==0)
- {
- s1num++;
- while(!k1)
- ;
- delay(5);
- while(!k1);
- if(s1num==1)
- {
- TR0=0; //暂停计时器
- write_com(0x80+0x4d); //光标在秒的位置闪烁
- write_com(0x0f);
- }
- if(s1num==2) //光标在分的位置
- {
- TR0=0;
- write_com(0x80+0x4a);
- write_com(0x0f);
- }
- if(s1num==3)
- {
- TR0=0;
- write_com(0x80+0x47);
- write_com(0x0f);
- }
- if(s1num==4) //按下第四下时接着走时
- {
- s1num=0;
- write_com(0x0c);
- TR0=1;
- }
- }
- }
- if(s1num!=0)
- {
- if(s1num==1)
- {
- if(k2==0) //按k2键秒加
- {
- delay(5);
- if(k2==0)
- {
- while(!k2);
- delay(5);
- while(!k2);
- miao++;
- if(miao==60)
- miao=0;
- write_sfm(0x0c,miao);
- write_com(0x80+0x4d);
- }
- }
- }
- if(s1num==2)
- {
- if(k2==0) //分加
- {
- delay(5);
- if(k2==0)
- {
- while(!k2);
- delay(5);
- while(!k2);
- fen++;
- if(fen==60)
- fen=0;
- write_sfm(0x09,fen);
- write_com(0x80+0x4a);
- }
- }
- }
- if(s1num==3)
- {
- if(k2==0) //时加
- {
- delay(5);
- if(k2==0)
- {
- while(!k2);
- delay(5);
- while(!k2);
- shi++;
- if(shi==24)
- shi=0;
- write_sfm(0x06,shi);
- write_com(0x80+0x47);
- }
- }
- }
- if(k4==0) //判断k3是否被按下
- {
- delay(5);
- if(k4==0)
- {
- while(!k4);
- delay(5);
- while(!k4);
- if(s1num==1)
- {
- miao--; //秒减
- if(miao==-1)
- miao=59;
- write_sfm(0x0c,miao);
- write_com(0x80+0x4d);
- }
- if(s1num==2)
- {
- fen--; //分减
- if(fen==-1)
- fen=59;
- write_sfm(0x09,fen);
- write_com(0x80+0x4a);
- }
- if(s1num==3)
- {
- shi--; //时减
- if(shi==-1)
- shi=23;
- write_sfm(0x06,shi);
- write_com(0x80+0x47);
- }
- }
- }
- }
- if(k8==0) // 按下k8退出调时
- {
- delay(5);
- if(k8==0)
- {
- while(!k8);
- delay(5);
- while(!k8);
- s1num=0;
- write_com(0x0c);
- TR0=1;
- }
- }
- }
- void main()
- {
- init();
- while(1)
- {
- keyscan();
- }
- }
- void timer0() interrupt 1 //中断服务程序
- {
- TH0=(65536-50000)/256;
- TL0=(65536-50000)%6; //重新设定定时器初值
- t++;
- if(t==20) //定时器中断次为秒
- {
- t=0;
- miao++;
- if(miao==60)
- {
- miao=0;
- fen++;
- if(fen==60)
- {
- fen=0;
- shi++;
- if(shi==24)
- {
- shi=0;
- }
- write_sfm(0x06,shi); //显示时
- }
- write_sfm(0x09,fen); //显示分
- }
- write_sfm(0x0c,miao); //显示秒
- }
- }
复制代码
|