单片机时钟程序,第一个数码管特别亮,按键按下时恢复正常,松开又变亮,这要怎么改?
- #include <reg52.h>
- #include <intrins.h>
- sbit key1=P3^0;
- sbit key2=P3^1;
- sbit key3=P3^2;
- sbit key4=P3^3;
- sbit wela=P2^1;
- sbit dula=P2^0;
- unsigned char num=-1;
- unsigned char tabledsq[]={0,1};
- unsigned char code tabledu[]=
- {0x3F,0x06,0x5B,0x4F,
- 0x66,0x6D,0x7D,0x07,
- 0x7F,0x6F,0x77,0x7C,
- 0x39,0x5E,0x79,0x71,};
- long s=0,m=0,h=0,shi1=0,ge1=0,shi2=0,ge2=0,shi3=0,ge3=0;
- void timer_set();
- void smg_display(long a,long b,long c,long d,long e,long f);
- void delay(unsigned int z)
- {
- unsigned int x,y;
- for(x=z;x>0;x--)
- for(y=110;y>0;y--);
- }
- void main()
- {
- timer_set();
- while(1)
- {
- shi1=s/10;
- ge1=s%10;
- shi2=m/10;
- ge2=m%10;
- shi3=h/10;
- ge3=h%10;
- smg_display(ge1,shi1,ge2,shi2,ge3,shi3);
- if(key1==0)
- {
- delay(10);
- if (key1==0)
- {
- num++;
- if(num==2)
- {
- num=0;
- }
- TR0=tabledsq[num];
- while(!key1)
- smg_display(ge1,shi1,ge2,shi2,ge3,shi3);
- }
- }
- if(key2==0)
- {
- delay(10);
- if (key2==0)
- {
- s++;
- if(s==60)
- {
- s=0;
- }
- while(!key2)
- smg_display(ge1,shi1,ge2,shi2,ge3,shi3);
- }
- }
- if(key3==0)
- {
- delay(10);
- if (key3==0)
- {
- m++;
- if(m==60)
- {
- m=0;
- }
- while(!key3)
- smg_display(ge1,shi1,ge2,shi2,ge3,shi3);
- }
- }
- if(key4==0)
- {
- delay(10);
- if (key4==0)
- {
- h++;
- if(h==24)
- {
- h=0;
- }
- while(!key4)
- smg_display(ge1,shi1,ge2,shi2,ge3,shi3);
- }
- }
- }
- }
- void timer0() interrupt 1
- {
- static long t=0;
- TH0=(65536-50000)/256;
- TL0=(65536-50000)%256;
- t++;
- if(t==20)
- {
- t=0;
- s++;
- if(s==60)
- {
- s=0;
- m++;
- if(m==60)
- {
- m=0;
- h++;
- if(h==24)
- {
- h=0;
- }
- }
- }
- }
- }
- void timer_set()
- {
- TMOD |=0x01;
- TH0=(65536-50000)/256;
- TL0=(65536-50000)%256;
- ET0=1;
- TR0=1;
- EA=1;
- }
- void smg_display(long a,long b,long c,long d,long e,long f)
- {
- wela=1;
- P1=0x7F;
- wela=0;
- dula=1;
- P1=tabledu[a];
- dula=0;
- delay(2);
-
- wela=1;
- P1=0xBF;
- wela=0;
- dula=1;
- P1=tabledu[b];
- dula=0;
- delay(2);
-
- wela=1;
- P1=0xDF;
- wela=0;
- dula=1;
- P1=0x40;
- dula=0;
- delay(2);
-
- wela=1;
- P1=0xEF;
- wela=0;
- dula=1;
- P1=tabledu[c];
- dula=0;
- delay(2);
-
- wela=1;
- P1=0xF7;
- wela=0;
- dula=1;
- P1=tabledu[d];
- dula=0;
- delay(2);
-
- wela=1;
- P1=0xFB;
- wela=0;
- dula=1;
- P1=0x40;
- dula=0;
- delay(2);
-
- wela=1;
- P1=0xFD;
- wela=0;
- dula=1;
- P1=tabledu[e];
- dula=0;
- delay(2);
-
- wela=1;
- P1=0xFE;
- wela=0;
- dula=1;
- P1=tabledu[f];
- dula=0;
- delay(2);
- }
复制代码 |