仿真原理圖如下(proteus仿真工程文件可到本帖附件中下載)
功能鍵,時或分+1,選擇調節時或分或秒,調節清零鍵.
單片機源程序如下:
- #include"reg52.h"
- #define uchar unsigned char
- #define uint unsigned int
- uchar code DuanMa[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
- uchar code DuanMa1[]={0x40,0x79,0x24,0x30,0x19,0x12,0x02,0x78,0x00,0x10};
- uchar hours=00,minutes=00,seconds=00;
- uchar Set_Time=0;
- uchar key_mode=0;
- bit impulse;
- uchar Time0_bit=0;
- bit flash;
- sbit LED=P1^0;
- sbit k1=P3^2;
- sbit k2=P3^3;
- sbit k3=P3^4;
- sbit k4=P3^5;
- uchar shi,fen,miao;
- void delay(uint n)
- {
- uint i;
- while(n--)
- {
- for(i=0;i<120;i++);
- }
- }
- void Mode_Choose()
- {
- if(k1==0)
- {
- delay(10);
- if(k1==0)
- {
- key_mode++;
- if(key_mode==2)
- key_mode=0;
- while(!k1);
- }
- }
- }
- void key_Time_Set()
- {
- if(key_mode==1)
- {
- if(k2==0)
- {
- delay(10);
- if(k2==0)
- {
- if(Set_Time==0)
- seconds=0;
- else if(Set_Time==1)
- {
- minutes++;
- if(minutes==60)
- minutes=0;
- }
- else
- {
- hours++;
- if(hours==24)
- hours=0;
- }
- while(!k2);
- }
- }
- if(k3==0)
- {
- delay(10);
- if(k3==0)
- {
- Set_Time++;
- if(Set_Time==3)
- Set_Time=0;
- while(!k3);
- }
- }
- if(k4==0)
- {
- delay(10);
- if(k4==0)
- {
- if(Set_Time==0)
- seconds=0;
- else if(Set_Time==1)
- minutes=0 ;
- else
- hours=0;
- while(!k4);
- }
- }
- } }
- void Display_ShiZhon()
- {
- P0=DuanMa[seconds%10];
- P2=0x80;
- delay(1);
- P0=0xff;
- P0=DuanMa[seconds/10];
- P2=0x40;
- delay(1);
- P0=0xff;
- if(flash)
- {
- P0=0xbf;
- P2=0x20;
- delay(1);
- P0=0xff;
- }
- else
- {
- P0=0xff;
- P2=0x20;
- delay(1);
- P0=0xff;
- }
- P0=DuanMa[minutes%10];
- P2=0x10;
- delay(1);
- P0=0xff;
- P0=DuanMa[minutes/10];
- P2=0x08;
- delay(1);
- P0=0xff;
- P0=0xbf;
- P2=0x04;
- delay(1);
- P0=0xff;
- P0=DuanMa[hours%10];
- P2=0x02;
- delay(1);
- P0=0xff;
- P0=DuanMa[hours/10];
- P2=0x01;
- delay(1);
- P0=0xff;
- }
- void Display_Time_Set()
- {
- if(Set_Time==0)
- {
- if(impulse)
- {
- P0=DuanMa[seconds%10];
- P2=0x80;
- delay(1);
- P0=0xff;
-
- P0=DuanMa[seconds/10];
- P2=0x40;
- delay(1);
- P0=0xff;
- }
- else
- {
- P0=0xff;
- P2=0x80;
- delay(1);
- P0=0xff;
-
- P0=0xff;
- P2=0x40;
- delay(1);
- P0=0xff;
- }
-
- if(flash)
- {
- P0=0xbf;
- P2=0x20;
- delay(1);
- P0=0xff;
- }
- else
- {
- P0=0xff;
- P2=0x20;
- delay(1);
- P0=0xff;
- }
-
- P0=DuanMa[minutes%10];
- P2=0x10;
- delay(1);
- P0=0xff;
-
- P0=DuanMa[minutes/10];
- P2=0x08;
- delay(1);
- P0=0xff;
-
- P0=0xbf;
- P2=0x04;
- delay(1);
- P0=0xff;
-
- P0=DuanMa[hours%10];
- P2=0x02;
- delay(1);
- P0=0xff;
-
- P0=DuanMa[hours/10];
- P2=0x01;
- delay(1);
- P0=0xff;
- }
- else if(Set_Time==1)
- {
- P0=DuanMa[seconds%10];
- P2=0x80;
- delay(1);
- P0=0xff;
-
- P0=DuanMa[seconds/10];
- P2=0x40;
- delay(1);
- P0=0xff;
-
- if(flash)
- {
- P0=0xbf;
- P2=0x20;
- delay(1);
- P0=0xff;
- }
- else
- {
- P0=0xff;
- P2=0x20;
- delay(1);
- P0=0xff;
- }
-
- if(impulse)
- {
- P0=DuanMa[minutes%10];
- P2=0x10;
- delay(1);
- P0=0xff;
-
- P0=DuanMa[minutes/10];
- P2=0x08;
- delay(1);
- P0=0xff;
- }
- else
- {
- P0=0xff;
- P2=0x10;
- delay(1);
- P0=0xff;
-
- P0=0xff;
- P2=0x08;
- delay(1);
- P0=0xff;
- }
-
- P0=0xbf;
- P2=0x04;
- delay(1);
- P0=0xff;
-
- P0=DuanMa[hours%10];
- P2=0x02;
- delay(1);
- P0=0xff;
-
- P0=DuanMa[hours/10];
- P2=0x01;
- delay(1);
- P0=0xff;
- }
- else
- {
- P0=DuanMa[seconds%10];
- P2=0x80;
- delay(1);
- P0=0xff;
-
- P0=DuanMa[seconds/10];
- P2=0x40;
- delay(1);
- P0=0xff;
-
- if(flash)
- {
- P0=0xbf;
- P2=0x20;
- delay(1);
- P0=0xff;
- }
- else
- {
- P0=0xff;
- P2=0x20;
- delay(1);
- P0=0xff;
- }
-
- P0=DuanMa[minutes%10];
- P2=0x10;
- delay(1);
- P0=0xff;
-
- P0=DuanMa[minutes/10];
- P2=0x08;
- delay(1);
- P0=0xff;
-
- P0=0xbf;
- P2=0x04;
- delay(1);
- P0=0xff;
-
- if(impulse)
- {
- P0=DuanMa[hours%10];
- P2=0x02;
- delay(1);
- P0=0xff;
-
- P0=DuanMa[hours/10];
- P2=0x01;
- delay(1);
- P0=0xff;
- }
- else
- {
- P0=0xff;
- P2=0x02;
- delay(1);
- P0=0xff;
-
- P0=0xff;
- P2=0x01;
- delay(1);
- P0=0xff;
- }
- }
- }
- void Time_Init()
- {
- TMOD=0x11;
- TH0=(65536-50000)/256;
- TL0=(65536-50000)%256;
- ET0=1;
- TR0=1;
- TH1=(65536-50000)/256;
- TL1=(65536-50000)%256;
- ET1=1;
- TR1=0;
- EA=1;
- }
- void Time0_Interrupt()interrupt 1
- {
- TH0=(65536-50000)/256;
- TL0=(65536-50000)%256;
- Time0_bit++;
- if((Time0_bit%10)==0)
- impulse=~impulse;
- if(Time0_bit==20)
- ……………………
- …………限于本文篇幅 余下代碼請從51黑下載附件…………
復制代碼
所有資料51hei提供下載:
可調時鐘.zip
(82.33 KB, 下載次數: 21)
2017-11-28 11:02 上傳
點擊文件名下載附件
完整的程序和仿真
|