简易时钟设计
可以调节时分秒
有程序
仿真原理图如下(proteus仿真工程文件可到本帖附件中下载)
单片机源程序如下:
- /************************************************************
- 作品名字:简易声光报时时钟(12小时和24小时可转换)
- 功能:六位数码管显示时分秒
- 二十四小时制和十二小时制可切换
- 十二小时制红灯亮,二十四小时制红灯灭
- 当时间在凌晨0点~中午12点之间时,黄灯亮,此时按切换按键数码管的示数不会变化
- 制作说明:P0接数码管段选,P2低6位接数码管位
-
- **************************************************************/
- #include
- #define uc unsigned char
- #define ui unsigned int
- sbit LED=P3^2;
- sbit key0=P3^3;
- sbit key1=P3^4;
- sbit key2=P3^5;
- sbit key3=P3^6;
- sbit wan=P3^7;
- uc min,hour,sec,num;
- bit bdata zhi;
- uc code tab[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};/*0,1,2,3,4,5,6,7,8,9,.*/
- void delay(ui x)
- {
- ui i,j;
- for(i=0;i
- for(j=0;j<121;j++);
- }
- void display()
- {
- P0=tab[hour/10];
- P2=0xdf;
- delay(1);
- P2=0xff;
- P0=(tab[hour%10])|0x80;
- P2=0xef;
- delay(1);
- P2=0xff;
- P0=tab[min/10];
- P2=0xf7;
- delay(1);
- P2=0xff;
- P0=(tab[min%10])|0x80;
- P2=0xfb;
- delay(1);
- P2=0xff;
- P0=tab[sec/10];
- P2=0xfd;
- delay(1);
- P2=0xff;
- P0=tab[sec%10];
- P2=0xfe;
- delay(1);
- P2=0xff;
- }
- void keyscan()
- {
- if(key0==0)
- {
- delay(5);
- if(key0==0)
- {
- LED=~LED;
- zhi=~zhi;
- if((hour>=12)&&(zhi==0))
- {
- hour=hour-12;
- }
- if((zhi==1)&&(wan==1))
- {
- hour=hour+12;
- }
- }
- while(!key0) display();
- }
- if(key1==0)
- {
- delay(5);
- if((key1==0)&&(zhi==0))
- {
- hour++;
- if(hour==12)
- {
- hour=0;
- wan=~wan;
- }
- }
- if((key1==0)&&(zhi==1))
- {
- hour++;
- if(hour==24)
- hour=0;
- }
- while(!key1) display();
- }
- if(key2==0)
- {
- delay(5);
- if(key2==0)
- {
- min++;
- if(min==60)
- min=0;
- }
- while(!key2) display();
- }
- if(key3==0)
- {
- delay(5);
- if(key3==0)
- {sec=0;}
- while(!key3) display();
- }
- }
- void init()
- {
- sec=0;
- min=30;
- hour=12;
- LED=1;
- zhi=1;
- wan=1;
- TMOD=0x01;
- TH0=(65536-50000)/256;
- TL0=(65536-50000)%256;
- EA=1;
- ET0=1;
- TR0=1;
- }
- void shizhi()
- {
- if(hour>=12)
- wan=1;
- if((hour<12)&&(zhi==1))
- wan=0;
- }
- void main()
- {
- init();
- while(1)
- {
- display();
- keyscan();
- shizhi();
- }
- }
- void time0() interrupt 1
- {
- TH0=(65536-50000)/256;
- TL0=(65536-50000)%256;
- num++;
- if(num==20)
- {
- num=0;
- sec++;
- if(sec==60)
- {
- sec=0;
- min++;
- if(min==60)
- {
- min=0;
- hour++;
- if((hour==12)&&(zhi==0))
- {
- wan=~wan;
- hour=0;
- }
- if((hour==24)&&(zhi==1))
- {
- hour=0;
- }
- }
- }
- }
- }
简易时钟.zip (64.62 KB, 下载次数: )