仿真原理圖如下(proteus仿真工程文件可到本帖附件中下載)
0.png (15.83 KB, 下載次數: 33)
下載附件
2018-11-28 16:46 上傳
單片機源程序如下:
- #include<reg51.h>
- typedef unsigned int u16; //對數據類型進行聲明定義
- typedef unsigned char u8;
- u8 code smgduan[17]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,
- 0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};//顯示0~F的值
- sbit Key1 = P3^7; //計時停止
- sbit Key2 = P3^6; //調位
- sbit Key3 = P3^0; //加一
- sbit Key4 = P3^2; //切換
- sbit LED1 = P1^7;
- sbit LSA = P2^5;
- sbit LSB = P2^6;
- sbit LSC = P2^7;
- u8 second=00,minute=00,hour=00,count;
- void Delayms(u16 t)
- {
- u16 i,j;
- for(i=0;i<t;i++)
- for(j=0;j<120;j++);
- }
-
- void DigDisplay()
- {
- u8 i;
- for(i=0;i<8;i++)
- {
- switch(i) //位選,選擇點亮的數碼管,
- {
- case(0):
- LSA=0;LSB=0;LSC=0; P0=smgduan[second%10];break;//顯示第0位
- case(1):
- LSA=0;LSB=0;LSC=1; P0=smgduan[second/10];break;//顯示第1位
- case(2):
- LSA=0;LSB=1;LSC=0; P0=0x40;break;//顯示第2位
- case(3):
- LSA=0;LSB=1;LSC=1; P0=smgduan[minute%10];break;//顯示第3位
- case(4):
- LSA=1;LSB=0;LSC=0; P0=smgduan[minute/10];break;//顯示第4位
- case(5):
- LSA=1;LSB=0;LSC=1; P0=0x40;break;//顯示第5位
- case(6):
- LSA=1;LSB=1;LSC=0;
- P0=smgduan[hour%10];break;//顯示第6位
- case(7):
- LSA=1;LSB=1;LSC=1; P0=smgduan[hour/10];break;//顯示第7位
- }
- Delayms(1); //間隔一段時間掃描
- P0=0x00;//消隱
- }
- }
-
- void Keyscan1()
- {
- static u8 i=0,j=0;
- if(Key1==0)
- {
- Delayms(10); //消抖
- if(Key1==0)
- while(!Key1); //等待按鍵彈起
- i++;
- }
- if(i%2==1)
- {
- LED1=0;
- TR0=0;
- }
- if(i%2==0)
- {
- LED1=1;
- TR0=1;
- }
- if(Key2==0)
- {
- Delayms(10);
- if(Key2==0)
- while(!Key2);
- j++;
- }
- if(j%4==1)
- {
- if(Key3==0)
- {
- Delayms(10);
- if(Key3==0)
- while(!Key3);
- second++;
- if(second==60)
- second=0;
- }
- }
- if(j%4==2)
- {
- if(Key3==0)
- {
- Delayms(10);
- if(Key3==0)
- while(!Key3);
- minute++;
- if(minute==60)
- minute=0;
- }
- }
- if(j%4==3)
- {
- if(Key3==0)
- {
- Delayms(10);
- if(Key3==0)
- while(!Key3);
- hour++;
- if(hour==24)
- hour=0;
- }
- }
- }
- void main()
- {
- TMOD=0x01;
- TH0=(65536-10000)/256;
- TL0=(65536-10000)%256;
- EA=1;
- ET0=1;
- TR0=1;
- while(1)
- {
- static u8 h=0;
- if(Key4==0)
- {
- Delayms(10);
- if(Key4==0)
- while(!Key4);
- h++;
- }
- if(h%2==0)
- {
- DigDisplay();
- Keyscan1();
- }
- }
- }
- void time0_int(void) interrupt 1
- {
- TH0=(65536-10000)/256;
- TL0=(65536-10000)%256;
- count++;
- if(count==100)
- {
- count=0;
- second++;
- if(second==60)
- {
- second=0;
- minute++;
- if(minute==60)
- {
- minute=0;
- hour++;
- if(hour==24)
- {
- hour=0;
- }
- }
- }
- }
- }
復制代碼
所有資料51hei提供下載:
18-11-18按鍵數碼管時鐘.rar
(64.49 KB, 下載次數: 39)
2018-11-28 11:05 上傳
點擊文件名下載附件
下載積分: 黑幣 -5
|