我这个是0~9999的循环按键加减,按一下加/减一,长按连续加减,但是短按加的时候会连续加几下,减的话会反应慢很多。不确定是不是消抖没做好,求各位大神指教!!!!!
- #includ<reg51.h>
- #define uchar unsigned char
- #define uint unsigned int
- sbit Seg_W1 = P2^1;
- sbit Seg_W4 = P2^2;
- sbit Seg_W3 = P2^3;
- sbit Seg_W2 = P1^7;
- sbit key1 = P3^4;//加
- sbit key2 = P2^6;//减
- code uint biao[]= {0x40,0x79,0x24,0x30,0x19,0x12,0x02,0x78,0x00,0x10};
- uint array[]={0,1,2,3,4,5,6,7,8,9};
- uchar displayDelayCount = 0;
- uchar displayIndex = 0;
- uint displayData =240;
- uint delay = 0;
- void delayms(unsigned int x)
- {
- unsigned int i;
- while(x--)
- {
- for(i=0;i<113;i++);
- }
- }
- void xianshi(void
- {
-
- if(displayDelayCount >0
- {
- displayDelayCount = 0;
- if(displayIndex < 3)
- {
- displayIndex ++;
- }
- else
- {
- displayIndex = 0;
- }
- if(displayIndex == 0)
- {
- Seg_W1 = Seg_W2 = Seg_W3 = Seg_W4 = 0 ;
- P1=0X7F;
- P1=biao[displayData%10]
- Seg_W1=1;
- }
- if(displayIndex == 1)
- {
- Seg_W1 = Seg_W2 = Seg_W3 = Seg_W4 = 0 ;
- P1=0X7F;
- P1=biao[displayData/10%10];
- Seg_W2=1;
- }
- if(displayIndex == 2)
- {
- Seg_W1 = Seg_W2 = Seg_W3 = Seg_W4 = 0 ;
- P1=0X7F;
- P1=biao[displayData/100%10];
- Seg_W3=1;
- }
-
- if(displayIndex == 3)
- {
- Seg_W1 = Seg_W2 = Seg_W3 = Seg_W4 = 0 ;
- P1=0X7F;
- P1=biao[displayData/1000%10];
- Seg_W4=1;
- }
- }
- }
- #define NO_AJ 1
- #define UP 2
- #define DOWM 3
- uchar anjian_yansi = 0;
- uchar anjianhuoqu(void)
- {
- {
- if(!key1)
- {
- }
- else if(!key2)
- {
- }
- else
- anjian_yansi = 0;
- }
-
- if(anjian_yansi == 0)
- {
- if(!key1)
- {
- anjian_yansi = 100;
- return UP;
- }
- else if(!key2)
- {
- anjian_yansi = 100;
- return DOWM;
- }
- else
- return NO_AJ;
- }
- else
- return NO_AJ;
- }
- void anjian ()
- {
- if(anjianhuoqu() == UP)
- {
- displayData++;
- }
- else if(anjianhuoqu() == DOWM)
- {
- displayData--;
- }
- }
- void time0()interrupt 1 //¶¨ê±¼ÆêyÆ÷£¬èë¿ú1
- {
- TH0=(65535-5000)/256;
- TL0=(65535-5000)%256;
- displayDelayCount++;
- delay++;
- if(anjian_yansi > 0)
- anjian_yansi --;
- }
- void main()
- {
- EA=1;
- ET0=1;
- TMOD=0X01;
- TH0=(65535-5000)/256;
- TL0=(65535-5000)%256;
- TR0=1;
-
- while(1)
- { key1=key2=key3=key4=1;
- xianshi();
- anjian ();
- }
- }
复制代码
|