把按键和显示函数改一下。
- #include <REG51.H>
- #define uchar unsigned char
- #define uint unsigned int
- sbit k1=P3^1;
- sbit k2=P3^0;
- sbit LSA=P2^2;
- sbit LSB=P2^3;
- sbit LSC=P2^4;
- uint num;
- uint count;
- uchar code smgduan[17]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,
- 0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};//显示0~F的值
- void delay(uint xms)
- {
- unsigned int i,j;
- for(i=0;i<xms;i++)
- for(j=0;j<120;j++);
- }
- void key()
- {
- if(!k1||!k2)
- {
- count++;
- if(count>10)
- count=11;
- if(k1==0&&count==10)
- {
- if(num<9999)
- num++;
- }
- if(k2==0&&count==10)
- {
- if(num>0)
- num--;
- }
- }
- else count=0;
- }
- void display()
- {
- static uchar i=0;
- uchar dis_buf[4];
- dis_buf[0]=smgduan[num/1000];
- dis_buf[1]=smgduan[num/100%10];
- dis_buf[2]=smgduan[num/10%10];
- dis_buf[3]=smgduan[num%10];
- P0=0x00;
- switch(i)
- {
- case 0: LSC=0;LSB=1;LSA=1; break;
- case 1: LSC=0;LSB=1;LSA=0; break;
- case 2: LSC=0;LSB=0;LSA=1; break;
- case 3: LSC=0;LSB=0;LSA=0; break;
- }
- P0=dis_buf[i];
- i++;i%=4;
- }
- void main(void)
- {
- while(1)
- {
- key();
- display();
- delay(1);
- }
- }
复制代码
|