仿真原理图如下(proteus仿真工程文件可到本帖附件中下载)
单片机源程序如下:
- #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)
|