我要达到目的是按k2,数码管数值加1,(初値是60),按k3,数码管数值减1,当按下k1时,led灯第一个亮,并且数码管数值从当前显示的値一秒减一个,减到0为止且led熄灭,我现在写了一程序,就是k2,k3的功能能实现,但是当我按下k1时,led亮了,但数码管要过一会儿才开始减,怎么回事儿?
程序如下,请高手给指点下迷津
- #include<reg52.h>
- #define uint unsigned int
- #define uchar unsigned char
- uchar temp,aa,bai,shi,ge,x,yushe;
- sbit dula=P2^6;
- sbit wela=P2^7;
- sbit k1=P3^4;
- sbit k2=P3^5;
- sbit k3=P3^6;
- sbit d1=P1^0;
- uchar code table[]={
- 0x3f,0x06,0x5b,0x4f,
- 0x66,0x6d,0x7d,0x07,
- 0x7f,0x6f,0x77,0x7c,
- 0x39,0x5e,0x79,0x71};
- void display(uchar bai,uchar shi,uchar ge);
- void delay(uint z);
- void init();
- void keys2();
- void keys3();
- void jishi();
- void main()
- {
- init();//初始化子程序
- yushe=60;
- while(1)
- {
- bai=yushe/100;
- shi=yushe%100/10;
- ge=yushe%10;
- display(bai,shi,ge);
- keys2();
- keys3();
- jishi();
-
-
- }
- }
- void delay(uint z)//延时子程序
- {
- uint x,y;
- for(x=z;x>0;x--)
- for(y=110;y>0;y--);
- }
- void display(uchar bai,uchar shi,uchar ge) //数码管显示子程序
- {
- dula=1;
- P0=table[bai];
- dula=0;
- P0=0xff;
- wela=1;
- P0=0xfe;
- wela=0;
- delay(1);
- dula=1;
- P0=table[shi];
- dula=0;
- P0=0xff;
- wela=1;
- P0=0xfd;
- wela=0;
- delay(1);
- dula=1;
- P0=table[ge];
- dula=0;
- P0=0xff;
- wela=1;
- P0=0xfb;
- wela=0;
- delay(1);
- }
- void init()//初始化子程序
- {
- wela=0;
- dula=0;
- temp=0;
- TMOD=0x01;
- TH0=(65536-50000)/256;
- TL0=(65536-50000)%256;
- EA=1;
- ET0=1;
- TR0=1;
- }
- void timer0() interrupt 1 //定时器中断
- {
- TH0=(65536-50000)/256;
- TL0=(65536-50000)%256;
- aa++;
- }
- void keys2() //按键+函数
- {
- if(k2==0)
- {
- delay(50);
- if(k2==0)
- {
- yushe++;
- while(!k2)
- {
- bai=yushe/100;
- shi=yushe%100/10;
- ge=yushe%10;
- display(bai,shi,ge);
- }
- }
- }
- }
- void keys3() //按键-函数
- {
- if(k3==0)
- {
- delay(50);
- if(k3==0)
- {
- yushe--;
- while(!k3)
- {
- bai=yushe/100;
- shi=yushe%100/10;
- ge=yushe%10;
- display(bai,shi,ge);
- }
- }
- }
- }
- void jishi()//开始倒计时子程序
- {
- if(k1==0)
- {
- delay(50);
- if(k1==0)
- {
- temp=yushe;
- d1=0;
- while(temp>0)
- {
- if(aa==20)
- {
- aa=0;
- temp--;
- if(temp==0)
- {
- d1=1;
- }
- bai=temp/100;
- shi=temp%100/10;
- ge=temp%10;
- }
- display(bai,shi,ge);
- }
- }
- }
- }
复制代码
一周热门 更多>