发贴仅为黑币,非原创,已表明出处,如有冒犯,还望海涵
所选用器材单片机最小系统选用AT89C51),排阻,四个按钮开关,8位共阴数码管,蜂鸣器; 有protues仿真电路原理图源代码:
- #include<reg51.h>
- #define uChar unsigned char
- #define uInt unsigned int
- uChar a[10]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
- uChar b[8]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};
- uChar second=50,minute=59,hour=12,year=18,month=07,day=21,count;
- sbit Key1 = P3^0; //计时停止
- sbit Key2 = P3^1; //调位
- sbit Key3 = P3^2; //加一
- sbit Key4 = P3^3; //切换
- sbit Buzzer=P1^1;
- /*********************延迟函数********************/
- void Delay(uInt t)
- {
- while(t)
- {
- t--;
- }
- }
- /*********************时分秒显示函数*******************/
- void Dispaly1(uChar second,uChar minute,uChar hour)
- {
- /*********************第一位数码管*********************/
- P2=b[0];
- P0=a[hour/10];
- Delay(10);
- /*********************第二位数码管*********************/
- P2=b[1];
- P0=a[hour%10];
- Delay(10);
- /*********************第三位数码管*********************/
- P2=b[2];
- P0=0x40;
- Delay(10);
- /*********************第四位数码管*********************/
- P2=b[3];
- P0=a[minute/10];
- Delay(10);
- /*********************第五位数码管*********************/
- P2=b[4];
- P0=a[minute%10];
- Delay(10);
- /*********************第六位数码管*********************/
- P2=b[5];
- P0=0x40;
- Delay(10);
- /*********************第七位数码管*********************/
- P2=b[6];
- P0=a[second/10];
- Delay(10);
- /*********************第八位数码管*********************/
- P2=b[7];;
- P0=a[second%10];
- Delay(10);
- }
- /*********************年月日显示函数********************/
- void Dispaly2(uChar day,uChar month,uChar year)
- {
- P2=b[0];
- P0=a[day/10];
- Delay(10);
- P2=b[1];
- P0=a[day%10];
- Delay(10);
- P2=b[2];
- P0=0x40;
- Delay(10);
- P2=b[3];
- P0=a[month/10];
- Delay(10);
- P2=b[4];
- P0=a[month%10];
- Delay(10);
- P2=b[5];
- P0=0x40;
- Delay(10);
- P2=b[6];
- P0=a[year/10];
- Delay(10);
- P2=b[7];
- P0=a[year%10];
- Delay(10);
- }
- /*********************时钟按键扫描函数*********************/
- void Keyscan1()
- {
- static uChar i=0,j=0;
- if(Key1==0)
- {
- Delay(10); //消抖
- if(Key1==0)
- while(!Key1); //等待按键弹
- i++;
- }
- /*时钟暂停功能*/
- if(i%2==1)
- {
- TR0=0;/*如果是奇数次按键自然关闭定时器0*/
- }
- if(i%2==0)
- {
- TR0=1;/*如果是偶数次按键则打开定时器0*/
- }
- /*时钟调位和数值加一功能*/
- if(Key2==0)
- {
- Delay(10);
- if(Key2==0)
- while(!Key2);
- j++;
- }
- if(j%4==1)
- {
- if(Key3==0)
- {
- Delay(10);
- if(Key3==0)
- while(!Key3);
- second++;
- if(second==60)
- second=0;
- }
- }
- if(j%4==2)
- {
- if(Key3==0)
- {
- Delay(10);
- if(Key3==0)
- while(!Key3);
- minute++;
- if(minute==60)
- minute=0;
- }
- }
- if(j%4==3)
- {
- if(Key3==0)
- {
- Delay(10);
- if(Key3==0)
- while(!Key3);
- hour++;
- if(hour==24)
- hour=0;
- }
- }
- }
- /*日期按键扫描函数*/
- void Keyscan2()
- {
- static uChar m=0,n=0;
- if(Key1==0)
- {
- Delay(10);
- if(Key1==0)
- while(!Key3);
- m++;
- }
- if(m%2==1)
- {
- TR0=0;/*奇数次按键则关闭定时器0*/
- }
- if(m%2==0)
- {
- TR0=1;/*偶数次按键则打开定时器0*/
- }
- if(Key2==0)
- {
- Delay(10);
- if(Key2==0)
- while(!Key2);
- n++;
- }
- /*日期调位和日期加一功能*/
- if(n%4==1)
- {
- if(Key3==0)
- {
- Delay(10);
- if(Key3==0)
- while(!Key3);
- day++;
- if(day==30)
- day=0;
- }
- }
- if(n%4==2)
- {
- if(Key3==0)
- {
- Delay(10);
- if(Key3==0)
- while(!Key3);
- month++;
- if(month==12)
- month=0;
- }
- }
- if(n%4==3)
- {
- if(Key3==0)
- {
- Delay(10);
- if(Key3==0)
- while(!Key3);
- year++;
- if(year==99)
- year=0;
- }
- }
- }
- /************************************************/
- /***************主函数***************************/
- /************************************************/
- void main()
- {
- TMOD=0x01; /*定时器以方式一工作*/
- TH0=(65536-10000)/256;
- TL0=(65536-10000)%256;/*10ms计时*/
- EA=1;
- ET0=1;/*允许定时器0中断*/
- TR0=1;/*打开定时器0*/
- while(1)
- {
- static uChar h=0;
- /*时钟和日期切换功能*/
- if(Key4==0)
- {
- Delay(10);
- if(Key4==0)
- while(!Key4);
- h++;
- }
- if(h%2==0)/*如果按键偶数次则显示时钟*/
- {
- Dispaly1(second,minute,hour);
- Keyscan1();
- }
- if(h%2==1)/*如果按键奇数次则显示日期*/
- {
- Dispaly2(year,month,day);
- Keyscan2();
- }
- }
- }
- /**********************中断函数**************************/
- void time0_int(void) interrupt 1
- {
- TH0=(65536-10000)/256;
- TL0=(65536-10000)%256;
- count++;
- if(count==100)/*10ms??ê±£???100′??ò??o?1s*/
- {
- count=0;
- second++;
- if(second==60)
- {
- second=0;
- minute++;
- if(minute==60)
- {
- minute=0;
- hour++;
- if(hour==24)
- {
- hour=0;
- day++;
- if(day==30)
- {
- day=0;
- month++;
- if(month==12)
- {
- month=0;
- year++;
- if(year==99)
- {
- year=0;
- }
- }
- }
- }
- }
- }
- }
- /*判断整点提醒*/
- if(second==00&&minute==00)
- Buzzer=0;
- else
- Buzzer=1;
- }
复制代码
———————————————— 版权声明:本文为CSDN博主「MoreMbb」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。 原文链接:https://blog.csdn.net/weixin_421 ... le/details/89765975 最后实现的效果: 2.png (36.36 KB, 下载次数: 0)
proteus仿真结果 
3.png (38.14 KB, 下载次数: 0)
最终结果实现 
|