我的设想是开启时先设置时钟,key1按下小时加1,key2按下分钟加1,key3长按清零且设置闹钟,key4长按清零且开始计时
但是运行时key1和key2一开始能够正常运行,但是key3长按时没有显示,按下key1key2也不会增加,key4长按时有显示但key1按下时依旧是小时+1不能进入计时模式,且两个都不会清零,请教大佬这是为什么啊
代码奉上- #include<reg52.h>
- #define uchar unsigned char
- #define uint unsigned int
- sbit P2_0=P2^0;
- sbit P2_1=P2^1;
- sbit P2_2=P2^2;
- sbit P2_3=P2^3;
- sbit key1=P3^5;
- sbit key2=P3^4;
- sbit key3=P3^3;
- sbit key4=P3^2;
- sbit beep=P3^6;
- uchar code table[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,
- 0xf8,0x80,0x90};
- uchar num1,num2,num3,num4,num5; //num1时,num2分 ,num3中断1中间数,num4中断2中间数,
- uchar num6,num7,num8,num9; //num5秒 ,num6计时器秒,num7计时器分 ,num8闹钟时,num9闹钟分
- uint a;
- void TIME0_init();
- void TIME1_init();
- void jishi_init();
- void naozhong_init();
- void delayms(uchar x);
- void display_shizhong(uchar num1,uchar num2);
- void display_jishu(uchar num7,uchar num6);
- void display_naozhong(uchar num8,uchar num9);
- void keyscan1();
- void keyscan2();
- void keyscan3();
- void clear_screen ();
- void alarm();
- void main()
- {
- TIME0_init();
- TIME1_init();
- while(1)
- {
- keyscan1();
- display_shizhong(num1,num2);
- if(num2==60)
- {
- num2=0;
- num1+=1;
- }
- alarm();
- }
-
- }
- void TIME0_init()
- {
- TMOD|=0x01;
- TH0=0;
- TL0=0;
- EA=1;
- ET0=1;
- TR0=1;
- }
- void TIME1_init()
- {
- TMOD|=0x10;
- TH1=0;
- TL1=0;
- EA=1;
- ET1=1;
- TR1=1;
- }
- void T0_time()interrupt 1//时钟
- {
- TH0=0;
- TL0=0;
- num4++;
- if(num4==14)
- {
- num4=0;
- num5++;
- if(num5==60)
- num2+=1;
- num5=0;
- }
- }
- void T1_time()interrupt 3//秒表
- {
- TH1=0;
- TL1=0;
- num3++;
- if(num3==14)
- {
- num3=0;
- num6++;
- if(num6==60)
- {
- num7+=1;
- num6=0;
- }
- }
-
- }
- void delayms(uchar x)
- {
- uchar j,k;
- for(j=x;j>0;j--)
- for(k=110;k>0;k--);
- }
- void display_shizhong(uchar num1,uchar num2)//时钟显示
- {
- uint shi1,shi2,fen1,fen2;
- shi1=num1/10;
- shi2=num1%10;
- fen1=num2/10;
- fen2=num2%10;
-
- P0=table[shi1];
- P2_0 = 0;
- delayms(5);
- P2_0 = 1;
-
- P0=table([shi2]& 0x7f);
- P2_1 = 0;
- delayms(5);
- P2_1 = 1;
-
- P0=table[fen1];
- P2_2 = 0;
- delayms(5);
- P2_2 = 1;
-
- P0=table[fen2];
- P2_3 = 0;
- delayms(5);
- P2_3 = 1;
-
- }
- void display_jishu(uchar num7,uchar num6)//秒表显示
- {
- uint shi3,shi4,fen3,fen4;
- shi3=num7/10;
- shi4=num7%10;
- fen3=num6/10;
- fen4=num6%10;
-
- P0=table[shi3];
- P2_0 = 0;
- delayms(5);
- P2_0 = 1;
-
- P0=table[shi4];
- P2_1 = 0;
- delayms(5);
- P2_1 = 1;
-
- P0=table[fen3];
- P2_2 = 0;
- delayms(5);
- P2_2 = 1;
-
- P0=table[fen4];
- P2_3 = 0;
- delayms(5);
- P2_3 = 1;
- }
- void display_naozhong(uchar num8,uchar num9)//闹钟显示
- {
- uint shi5,shi6,fen5,fen6;
- shi5=num8/10;
- shi6=num8%10;
- fen5=num9/10;
- fen6=num9%10;
-
- P0=table[shi5];
- P2_0 = 0;
- delayms(5);
- P2_0 = 1;
-
- P0=table[shi6];
- P2_1 = 0;
- delayms(5);
- P2_1 = 1;
-
- P0=table[fen5];
- P2_2 = 0;
- delayms(5);
- P2_2 = 1;
-
- P0=table[fen6];
- P2_3 = 0;
- delayms(5);
- P2_3 = 1;
- }
- void keyscan1()//key1按下小时加1,key2按下分钟加1,key3长按清零且设置闹钟,key4长按清零且开始计时
- {
- if(key1==0)
- {
- delayms(10);
- if(key1==0)
- {
- num1++;
- if(num1==24)
- {
- num1=0;
- }
- while(!key1);
- }
- }
- if(key2==0)
- {
- delayms(10);
- if(key2==0)
- {
- num2++;
- if(num2==60)
- {
- num2=0;
- num1+=1;
- }
- while(!key2);
- }
- }
-
- if(key3==0)
- {
- delayms(10);
- if(key3==0)
- {
- display_shizhong(num1,num2);
- while(!key3);
- }
- }
-
- if(key4==0)
- {
- delayms(10);
- if(key4==0)
- {
- clear_screen ();
- while(!key4)
- {
- display_jishu(num7,num6);
- keyscan2();
- }
- }
-
- if(key3==0)
- {
- delayms(10);
- if(key3==0)
- {
- clear_screen ();
- while(!key3)
- {
- display_jishu(num8,num9);
- keyscan3();
- while(!key2);
-
- }
- }
- }
- }
-
-
- }
- void clear_screen ()//清零
- {
- P0=table[0];
- P2_0 = 0;
- delayms(5);
- P2_0 = 1;
-
- P0=table[0];
- P2_1 = 0;
- delayms(5);
- P2_1 = 1;
-
- P0=table[0];
- P2_2 = 0;
- delayms(5);
- P2_2 = 1;
-
- P0=table[0];
- P2_3 = 0;
- delayms(5);
- P2_3 = 1;
- }
- void keyscan2()//开始计时
- {
- if(key1==0)
- {
- delayms(10);
- if(key1==0)
- {
- TR1=~TR1;
- while(!key1);
- }
- }
- }
- void keyscan3()//设置闹钟
- {
- if(key1==0)
- {
- delayms(10);
- if(key1==0)
- {
- num8++;
- if(num8==24)
- {
- num8=0;
- }
- while(!key1);
- }
- }
-
- if(key2==0)
- {
- delayms(10);
- if(key2==0)
- {
- num9++;
- if(num9==60)
- {
- num9=0;
- num8+=1;
- }
- while(!key2);
- }
- }
- }
-
- void alarm()//闹钟响
- {
- if(num1==num8&&num2==num9)
- while(a<1000)
- {
- beep=0;
- delayms(10) ;
- beep=1;
- delayms(10);
- a--;
- }
- }
复制代码
|