標(biāo)題: 利用單片機中斷延時消除按鍵抖動 [打印本頁]

作者: 883    時間: 2019-9-5 12:37
標(biāo)題: 利用單片機中斷延時消除按鍵抖動
利用delay()的延時消除按鍵抖動,用while(!K)等語句檢測松手動作,這種按鍵處理程序,簡單易懂,許多初學(xué)者喜歡使用。但如果程序端口有檢測信號的話,十幾二十毫秒的延時錯過了不少檢測信號。如果按鍵一直按著或按鍵損壞短接了,程序就會一直停留在這里不能運行了。設(shè)置2毫秒的中斷延時,利用中斷延時避開主程序過長的周期。將按鍵檢測分為兩種狀態(tài),在檢測按鍵按下狀態(tài)中完成延時消抖,后進入檢測按鍵松開。
水平有限,貼上程序大家共同探討。

單片機源程序如下:
  1. #include <reg52.h>

  2. typedef signed long slong;
  3. typedef signed int sint;
  4. typedef  signed char schar

  5. typedef unsigned long ulong;
  6. typedef unsigned int uint;
  7. typedef unsigned char uchar;

  8. #define SEG_DB P0                 //數(shù)碼管
  9. sbit SEG_WE = P2^7;                 //數(shù)碼管位選
  10. sbit SEG_DU = P2^6;                 //數(shù)碼管段選
  11. sbit Keyin1 = P3^4;
  12. //sbit Keyin2 = P3^5;
  13. //sbit Keyin3 = P3^6;
  14. //sbit Keyin4 = P3^7;
  15. sbit Keyout1 = P3^0;
  16. //sbit Keyout2 = P3^1;
  17. //sbit Keyout3 = P3^2;
  18. //sbit Keyout4 = P3^3;

  19. bit Keytan=1;
  20. uchar T0RH,T0RL;
  21. uchar Ledtmp[6];
  22. uint cou;
  23. uchar code Ledchar[] = {0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,
  24.                                         0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};

  25. void ConfigTimer0(uchar ms);
  26. void KeyDrive();

  27. void main()
  28. {
  29.         schar j;
  30.         uchar buf[6];

  31.         Keyout1 = 0;
  32.         ConfigTimer0(2);
  33.         Ledtmp[0] = Ledchar[2];
  34.         while (1)
  35.         {
  36.                 KeyDrive();                                         
  37.                 buf[0] = cou%10;
  38.                 buf[1] = cou/10%10;
  39.                 buf[2] = cou/100%10;                    
  40.                 buf[3] = cou/1000%10;
  41.                 buf[4] = cou/10000%10;
  42.                 buf[5] = cou/100000%10;
  43.                 for (j=5;j>=1;j--)                                                //消隱高位數(shù)的0
  44.                 {
  45.                         if (buf[j] == 0)
  46.                                 Ledtmp[j] = 0x00;
  47.                         else
  48.                                 break;
  49.                 }
  50.                 for (;j>=0;j--)
  51.                 {
  52.                         Ledtmp[j] = Ledchar[buf[j]];
  53.                 }                                                                                
  54.         }
  55. }

  56. void KeyDrive()
  57. {
  58.         static Keybuf = 1;

  59.         if (Keybuf != Keytan)
  60.         {
  61.                 if (Keybuf == 1)
  62.                 {
  63.                         cou++;               
  64.                 }
  65.                 Keybuf = Keytan;
  66.         }
  67. }

  68. void LedPlay()
  69. {
  70.         static uchar i=0;

  71.         SEG_DB = 0xff;
  72.         SEG_WE = 1;
  73.         SEG_WE = 0;
  74.         SEG_DB = Ledtmp[i];
  75.         SEG_DU = 1;
  76.         SEG_DU = 0;
  77.         SEG_DB = ~(0x20>>i);
  78.         SEG_WE = 1;
  79.         SEG_WE = 0;
  80.         i++;
  81.         if (i == 6)
  82.                 i = 0;
  83. }

  84. void KeyScan()
  85. {
  86.         static uchar kmode=0;
  87.         static uchar i;

  88.         switch (kmode)
  89.         {
  90.                 case 0:        if (Keyin1 == 0)          //檢測按鍵按下
  91.                                 {
  92.                                         i++;
  93.                                         if (i == 7)                  //7次相當(dāng)于延時2msx7=14毫秒
  94.                                         {
  95.                                                 i = 0;
  96.                                                 kmode = 1;          //按鍵穩(wěn)定,轉(zhuǎn)換進入檢測按鍵松手模式
  97.                                                 Keytan = 0;          //按鍵動作標(biāo)志
  98.                                         }
  99.                                 }
  100.                                 else
  101.                                         i = 0;                          //沒有7次連續(xù)檢測到按鍵按下,說明沒有穩(wěn)定,時間清零
  102.                 case 1:        if (Keyin1 == 1)          //檢測按鍵松開                                         
  103.                                  {
  104.                                         Keytan = 1;                 //按鍵松開標(biāo)志
  105.                                         kmode = 0;                 //進入檢測按鍵按下模式
  106.                                 }        
  107.         }               
  108. }

  109. void ConfigTimer0(uchar ms)
  110. {
  111.         ulong tmp;

  112.         tmp = 11059200/12;
  113.         tmp = (tmp*ms)/1000;
  114.         tmp = 65536 - tmp;
  115.         T0RH = (uchar)(tmp>>8);
  116.         T0RL = (uchar)tmp;
  117.         TH0 = T0RH;
  118.         TL0 = T0RL;
  119.         TMOD &= 0xf0;
  120.         TMOD |= 0x01;
  121.         TR0 = 1;
  122.         ET0 = 1;
  123.         EA = 1;
  124. }

  125. void InterruptTimer0() interrupt 1
  126. {
  127.         TH0 = T0RH;
  128.         TL0 = T0RH;
  129.         LedPlay();
  130.         KeyScan();
  131. }
復(fù)制代碼

全部資料51hei下載地址:
按鍵.zip (39.04 KB, 下載次數(shù): 11)






歡迎光臨 (http://www.torrancerestoration.com/bbs/) Powered by Discuz! X3.1