- #include<reg52.h>
- #define uint unsigned int
- #define uchar unsigned char
- sbit w=P2^5;
- sbit s=P2^6;
- sbit e=P2^7;
- sbit key1=P3^0; //功能键
- sbit key2=P3^1; //增加建
- sbit key3=P3^2; //减小键
- sbit key4=P3^3; //走动键
- uchar count,key1num,tianmax;
- long int miao=20,fen=59,shi=23,tian=27,yue=2,nian=2018;
- void delayms(uint xms)
- {
- uint i,j;
- for(i=xms;i>0;i--)
- for(j=110;j>0;j--);
- }
- void write_com(uchar com)//命令
- {
- s=0;
- w=0;
- e=0;
- P0=com;
- delayms(5);
- e=1;
- delayms(5);
- e=0;
- }
- void write_date(uchar date)//数据
- {
- s=1;
- w=0;
- e=0;
- P0=date;
- delayms(5);
- e=1;
- delayms(5);
- e=0;
- }
- void write_sfm(uchar add,uchar date) //写时分秒函数 add位置 date输入数据
- {
- uchar shi,ge;
- shi=date/10;
- ge=date%10;
- write_com(0xc0+add);
- write_date(0x30+shi);
- write_date(0x30+ge);
- }
- void write_ry(uchar add,uchar date) //写日月函数 add位置 date输入数据
- {
- uchar shi,ge;
- shi=date/10;
- ge=date%10;
- write_com(0x80+add);
- write_date(0x30+shi);
- write_date(0x30+ge);
- }
- void write_nian1()//写年函数
- {
- uchar ge,shi,bai,qian;
- qian=nian/1000%10;
- bai=nian/100%10;
- shi=nian/10%10;
- ge=nian%10;
- write_com(0x80+4);
- write_date(0x30+qian);
- write_date(0x30+bai);
- write_date(0x30+shi);
- write_date(0x30+ge);
-
- }
- void init()//初始化函数
- {
- count=0;
- key1num=0;
- write_com(0x38);
- write_com(0x08);
- write_com(0x01);
- write_com(0x06);
- write_com(0x0c);
- write_com(0xc0+6);
- write_date(':');
- delayms(1);
- write_com(0xc0+9);
- write_date(':');
- delayms(1);
- write_com(0x80+11);
- write_date('-');
- delayms(1);
- write_com(0x80+8);
- write_date('-');
- delayms(1);
- write_sfm(10,miao);
- write_sfm(7,fen);
- write_sfm(4,shi);
- write_ry(12,tian);
- write_ry(9,yue);
- write_nian1();
- TMOD=0x01;
- TH0=(65536-50000)/256;
- TL0=(65536-50000)%256;
- EA=1;
- ET0=1;
- TR0=1;
- }
- void keyscan()
- {
- if(key1==0)
- {
- delayms(5);
- if(key1==0)
- {
- key1num++;
- while(!key1);
- switch(key1num)
- {
- case 1:
- TR0=0;
- write_com(0xc0+10); //秒
- write_com(0x0f); //光标开始闪烁
- break;
- case 2:
- write_com(0xc0+7); //分
- break;
- case 3: //时
- write_com(0xc0+4);
- break;
- case 4:
- write_com(0x80+12); //天
- break;
- case 5:
- write_com(0x80+9); //月
- break;
- case 6:
- write_com(0x80+4); //年
- break;
- case 7:
- key1num=0;
- write_com(0x0c);//取消光标闪烁
- TR0=1;
- }
- }
- }
- if(key1num!=0)
- {
- if(key2==0)
- {
- delayms(5);
- if(key2==0)
- {
- while(!key2);
- if(key1num==1)
- {
- miao++;
- if(miao==60)
- miao=0;
- write_sfm(10,miao);
- }
- if(key1num==2)
- {
- fen++;
- if(fen==60)
- fen=0;
- write_sfm(7,fen);
- }
- if(key1num==3)
- {
- shi++;
- if(shi==24)
- shi=0;
- write_sfm(4,shi);
- }
- if(key1num==4)
- {
- tian++;
- if(tian==tianmax)
- tian=1;
- write_ry(12,tian);
- }
- if(key1num==5)
- {
- yue++;
- if(yue==13)
- yue=1;
- write_ry(9,yue);
- }
- if(key1num==6)
- {
- nian++;
- write_nian1();
- }
- }
- }
- if(key3==0)
- {
- delayms(5);
- if(key3==0)
- {
- while(!key3);
- if(key1num==1)
- {
- miao--;
- if(miao==-1)
- miao=59;
- write_sfm(10,miao);
- }
- if(key1num==2)
- {
- fen--;
- if(fen==-1)
- fen=59;
- write_sfm(7,fen);
- }
- if(key1num==3)
- {
- shi--;
- if(shi==-1)
- shi=23;
- write_sfm(4,shi);
- }
- if(key1num==4)
- {
- tian--;
- tianmax=tianmax-1;
- if(tian==0)
- tian=tianmax;
- write_ry(12,tian);
- }
- if(key1num==5)
- {
- yue--;
- if(yue==0)
- yue=12;
- write_ry(9,yue);
- }
- if(key1num==6)
- {
- nian--;
- write_nian1();
- }
- }
- }
- if(key4==0) //走动
- {
- delayms(5);
- if(key4==0)
- {
- key1num=0;
- write_com(0x0c);//取消光标闪烁
- TR0=1;
- }
- while(!key3);
- }
- }
- }
- void T0_time()interrupt 1
- {
-
- TH0=(65536-57000)/256;
- TL0=(65536-57000)%256;
- count++;
- if(count==20)
- {
- count=0;
- miao++;
- if(miao==60)
- {
- miao=0;
- fen++;
- if(fen==60)
- {
- fen=0;
- shi++;
- if(shi==24)
- {
- shi=0;
- tian++;
- if(tian==tianmax)
- {
- tian=1;
- yue++;
- if(yue==13)
- {
- yue=1;
- nian++;
- if(nian==9999)
- {
- nian=0;
- }
- write_nian1();
- }
- write_ry(9,yue);
- }
- write_ry(12,tian);
- }
- write_sfm(4,shi);
- }
- write_sfm(7,fen);
- }
- write_sfm(10,miao);
- }
- }
- void daymax()//最大天数
- {
- switch(yue)
- {
- case 1:
- case 3:
- case 5:
- case 7:
- case 8:
- case 10:
- case 12:
- tianmax=32;
- break;
- case 4:
- case 6:
- case 9:
- case 11:
- tianmax=31;
- break;
- case 2:
- if((nian%400==0)||(nian%4==0&&nian%100!=0))//判断润平年
- {
- tianmax=30;
- }
- else
- tianmax=29;
- break;
- }
- }
- void main()
- {
- init();
- while(1)
- {
- daymax();
- keyscan();
- }
- }
复制代码
|