单片机电子时钟设计(含源代码和仿真图)
单片机源程序如下:
- #include<reg52.h>
- #include<CZJ_FUN.h>
- /*********************************************
- clock写入
- **********************************************/
- void write_clock(uchar address,uchar date)
- {
- uchar s,g;
- s=date/10;
- g=date%10;
- write_com(0x80+address);
- write_data(0x30+s);
- write_data(0x30+g);
- }
- /***********************************************
- 显示时钟
- ***********************************************/
- void display_clock()
- {
- int j;
- write_com(0x01);
- write_com(0x06);
- write_com(0x80+0x03);
- for(j=0;j<11;j++)
- {
- write_data(table2[j]);
- }
- write_com(0x80+0x45);
- for(j=0;j<8;j++)
- {
- write_data(table3[j]);
- }
- TMOD=0x01;
- TH0=(65536-50000)/256;
- TL0=(65536-50000)%256;
- EA=1;
- ET0=1;
- TR0=1;
-
- }
- /***********************************************
- 中断加时间
- ************************************************/
- void time0() interrupt 1
- {
- TH0=(65536-50000)/256;
- TL1=(65536-50000)%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;
- day++;
- if(mon==1||mon==3||mon==5||mon==7||mon==8||mon==10||mon==12)
- {
- temp=1;
- }
- if(mon==4||mon==6||mon==9||mon==11)
- {
- temp=0;
- }
-
- if(mon==2)
- {
- if(y==0)
- {
- if(day==30)
- {
- day=1;
- mon++ ;
- }
- write_clock(0x0B,day);
- write_clock(0x08,mon);
- }
- else
- {
- {
- if(day==29)
- {
- day=1;
- mon++ ;
- }
- write_clock(0x0B,day);
- write_clock(0x08,mon);
- }
- }
- }
-
-
- if(day==32&&temp==1)
- {
- day=1;
- mon++ ;
- if(mon==13)
- {
- mon=1;
- year1++;
- if(year1==100)
- {
- year1=0;
- year2++;
- write_clock(0x03,year1);
-
- }
- write_clock(0x05,year1);
- }
- write_clock(0x08,mon);
- }
- write_clock(0x0B,day);
- if(day==31&&temp==0)
- {
- day=1;
- mon++ ;
- if(mon==13)
- {
- mon=1;
- year1++;
- if(year1==100)
- {
- year1=0;
- year2++;
- write_clock(0x03,year1);
-
- }
- write_clock(0x05,year1);
- }
- write_clock(0x08,mon);
- }
- write_clock(0x0B,day);
- }
- write_clock(0x45,shi);
- }
- write_clock(0x48,fen);
- }
- write_clock(0x4B,miao);
- }
- }
- /**********************************************************
- clock 修改
- **********************************************************/
- void keyscan()
- {
-
- if(s1==0)
- {
- delayx(5);
- if(s1==0)
- {
- num1++;
- while(!s1);
- if(num1==1)
- {
- TR0=0;
- write_com(0x80+0x40+11);
- write_com(0x0f);
- }
- if(num1==2)
- {
- write_com(0x80+0x40+8);
- write_com(0x0f);
- }
- if(num1==3)
- {
- write_com(0x80+0x40+5);
- write_com(0x0f);
- }
- if(num1==4)
- {
- write_com(0x80+0x0B);
- write_com(0x0f);
- }
- if(num1==5)
- {
- write_com(0x80+0x08);
- write_com(0x0f);
- }
- if(num1==6)
- {
- write_com(0x80+0x03);
- write_com(0x0f);
- }
- if(num1==7)
- {
- TR0=1;
- num1=0;
- write_com(0x0C);
- }
- }
- }
- if(s2==0)
- {
- delayx(5);
- if(s2==0)
- {
- while(!s2);
- if(num1==1)
- {
- miao++;
- if(miao==60)
- {
- miao=0;
- }
- write_clock(0x4B,miao);
- write_com(0x80+0x40+11);
- }
- if(num1==2)
- {
- fen++;
- if(fen==60)
- {
- fen=0;
- }
- write_clock(0x48,fen);
- write_com(0x80+0x40+8);
- }
- if(num1==3)
- {
- shi++;
- if(shi==24)
- {
- shi=0;
- }
- write_clock(0x45,shi);
- write_com(0x80+0x40+5);
- }
- if(num1==4)
- {
- if(mon==1||mon==3||mon==5||mon==7||mon==8||mon==10||mon==12)
- {
- temp=0;
- }
- if(mon==4||mon==6||mon==9||mon==11)
- {
- temp=1;
- }
- if(mon==2)
- {
- if(y==0)
- {
- temp=3;
- }
- else
- {
- temp=4;
- }
- }
- if(temp==0)
- {
- day++;
- if(day==32)
- {
- day=1;
- }
- write_clock(0x0B,day);
- write_com(0x80+0x0B);
- }
- if(temp==1)
- {
- day++;
- if(day==31)
- {
- day=1;
- }
- write_clock(0x0B,day);
- write_com(0x80+0x0B);
- }
- if(temp==3)
- {
- day++;
- if(day==30)
- {
- day=1;
- }
- write_clock(0x0B,day);
- write_com(0x80+0x0B);
- }
- if(temp==4)
- {
- day++;
- if(day==29)
- {
- day=1;
- }
- write_clock(0x0B,day);
- write_com(0x80+0x0B);
- }
-
- }
- if(num1==5)
- {
- mon++;
- if(mon==13)
- {
- mon=1;
- }
- write_clock(0x08,mon);
- write_com(0x80+0x08);
-
- }
- if(num1==6)
- {
- year1++;
- if(year1==100)
- {
- year1=1;
- year2++;
- }
- write_clock(0x05,year1);
- write_clock(0x03,year2);
- write_com(0x80+0x03);
-
- }
-
-
- }
- }
- if(s3==0)
- {
- delayx(5);
- if(s3==0)
- {
- while(!s3);
- if(num1==1)
- {
- miao--;
- if(miao==-1)
- {
- miao=59;
- }
- write_clock(0x4B,miao);
- write_com(0x80+0x40+11);
- }
- if(num1==2)
- {
- fen--;
- if(fen==-1)
- {
- fen=59;
- }
- write_clock(0x48,fen);
- write_com(0x80+0x40+8);
- }
- if(num1==3)
- {
- shi--;
- if(shi==-1)
- {
- shi=23;
- }
- write_clock(0x45,shi);
- write_com(0x80+0x40+5);
- }
- if(num1==4)
- {
- if(mon==1||mon==3||mon==5||mon==7||mon==8||mon==10||mon==12)
- {
- temp=0;
- }
- if(mon==4||mon==6||mon==9||mon==11)
- {
- temp=1;
- }
- if(mon==2)
- {
- if(y==0)
- {
- temp=3;
- }
- else
- {
- temp=4;
- }
- }
- if(temp==0)
- {
- day--;
- if(day==0)
- {
- day=31;
- }
- write_clock(0x0B,day);
- write_com(0x80+0x0B);
- }
- if(temp==1)
- {
- day--;
- if(day==0)
- {
- day=30;
- }
- write_clock(0x0B,day);
- write_com(0x80+0x0B);
- }
- if(temp==3)
- {
- day--;
- if(day==0)
- {
- day=29;
- }
- write_clock(0x0B,day);
- write_com(0x80+0x0B);
- }
- if(temp==4)
- {
- day--;
- if(day==0)
- {
- day=28;
- }
- write_clock(0x0B,day);
- write_com(0x80+0x0B);
- }
-
- }
- if(num1==5)
- {
- mon--;
- if(mon==0)
- {
- mon=12;
- }
- write_clock(0x08,mon);
- write_com(0x80+0x08);
-
- }
- if(num1==6)
- {
- year1--;
- if(year1==-1)
- {
- year1=99;
- year2--;
- }
- write_clock(0x05,year1);
- write_clock(0x03,year2);
- write_com(0x80+0x03);
- ……………………
- …………限于本文篇幅 余下代码请从51黑下载附件…………
复制代码
所有资料51hei提供下载:
单片机电子时钟51hei设计(含源代码和仿真图).rar
(58.75 KB, 下载次数: 32)
|