先写好年份和日期,然后通过定时中断来控制时间的增加
按按键控制更改校准时间
单片机源程序如下:
- #include<reg52.h>
- #define uchar unsigned char
- #define uint unsigned int
- uchar miao =13, fen =12, shi = 11 ;
- uchar code Time_Table[] ="2023-4-17 1";
- uchar num,t,KEY1_num=0;
- sbit lcdrs=P1^0;
- sbit lcden=P1^1;
- sbit wela=P1^6;
- sbit KEY1=P2^4;
- sbit KEY2=P2^5;
- sbit KEY3=P2^6;
- sbit KEY4=P2^7;
- sbit beep=P3^5;
- void KEY_scan();
- void delay(uint z)
- {
- uint x,y;
- for(x=z;x>0;x--)
- for(y=110;y>0;y--);
- }
- void di()
- {
- beep = 0;
- delay(100);
- beep = 1;
- }
- void write_com(uchar com)
- {
- lcden=0;
- lcdrs=0;
- P0=com;
- delay(5);
- lcden=1;
- delay(5);
- lcden=0;
- }
- void write_date(uchar date)
- {
- lcden=0;
- lcdrs=1;
- P0=date;
- delay(5);
- lcden=1;
- delay(5);
- lcden=0;
- }
- void Display(uchar add,uchar date)
- {
- uchar shi,ge;
- shi=date/10;
- ge=date%10;
- write_com(0x80+0x40+add);
- write_date(0x30+shi);
- write_date(0x30+ge);
- }
- void lcdinit()
- {
- wela=0;
- write_com(0x38);
- write_com(0x0c);
- write_com(0x06);
- write_com(0x01);
- write_com(0x80+3);
- for(num = 0;Time_Table[num]!='\0'; num++)
- {
- write_date(Time_Table[num]);
- delay(5);
- }
- write_com(0x80+0x40+6);
- write_date(':');
- delay(5);
- write_com(0x80+0x40+9);
- write_date(':');
- delay(5);
- Display(4,shi);
- Display(7,fen);
- Display(10,miao);
- }
- void Time0_Init()
- {
- TMOD=0x01;
- TH0=(65536-45872)/256;
- TL0=(65536-45872)%256;
- EA=1;
- ET0=1;
- TR0=1;
- }
- void main()
- {
- lcdinit();
- Time0_Init();
- while(1)
- {
- KEY_scan();
- }
- }
- void timer0() interrupt 1
- {
- TH0=(65536-50000)/256;
- TL0=(65536-50000)%256;
- t++;
- if(t==20)
- {
- t=0;
- miao++;
- if(miao==60)
- {
- miao=0;
- fen++;
- if(fen==60)
- {
- fen=0;
- shi++;
- if(shi==24)
- {
- shi=0;
- }
- Display(4,shi);
- }
- Display(7,fen);
- }
- Display(10,miao);
- }
- }
- void KEY_scan()
- {
- if(KEY1==0)
- {
- delay(5);
- if(KEY1==0)
- {
- KEY1_num++;
- while(!KEY1);
- di();
- if(KEY1_num==1)
- {
- TR0=0;
- write_com(0x80+0x40+11);
- write_com(0x0f);
- }
- if(KEY1_num==2)
- {
- write_com(0x80+0x40+8);
- }
- if(KEY1_num==3)
- {
- write_com(0x80+0x40+5);
- }
- if(KEY1_num==4)
- {
- KEY1_num=0;
- write_com(0x0c);
- TR0=1;
- }
- }
- }
- if(KEY1_num!=0)
- {
- if(KEY2==0)
- {
- delay(5);
- if(KEY2==0)
- {
- while(!KEY2);
- di();
- if(KEY1_num==1)
- {
- miao++;
- if(miao==60)
- miao=0;
- Display(10,miao);
- write_com(0x80+0x40+10);
- }
- if(KEY1_num==2)
- {
- fen++;
- if(fen==60)
- fen=0;
- Display(7,fen);
- write_com(0x80+0x40+7);
- }
- if(KEY1_num==3)
- {
- shi++;
- if(shi==24)
- shi=0;
- Display(4,shi);
- write_com(0x80+0x40+4);
- }
- }
- }
- }
- if(KEY3==0)
- {
- delay(5);
- if(KEY3==0)
- {
- while(!KEY3);
- di();
- if(KEY1_num==1)
- {
- miao--;
- if(miao==-1)
- miao=59;
- Display(10,miao);
- write_com(0x80+0x40+10);
- }
- if(KEY1_num==2)
- {
- fen--;
- if(fen==-1)
- fen=59;
- Display(7,fen);
- write_com(0x80+0x40+7);
- }
- if(KEY1_num==3)
- {
- shi--;
- if(shi==-1)
- shi=23;
- Display(4,shi);
- write_com(0x80+0x40+4);
- }
- }
- }
- }
复制代码
|