標(biāo)題: 關(guān)于單片機按鍵Key1,每按一次數(shù)碼管加一遇到的問題,請大神老師指導(dǎo) [打印本頁]

作者: bai850107    時間: 2017-11-17 23:19
標(biāo)題: 關(guān)于單片機按鍵Key1,每按一次數(shù)碼管加一遇到的問題,請大神老師指導(dǎo)
  1. #include<reg52.h>

  2. #define uchar unsigned char
  3. #define uint unsigned int

  4. sbit LSA=P2^2;
  5. sbit LSB=P2^3;
  6. sbit LSC=P2^4;

  7. sbit Key1=P3^1;
  8. sbit Key2=P3^0;
  9. sbit Key3=P3^2;
  10. sbit Key4=P3^3;

  11. uchar num,num1,shi,ge;
  12. uint z,i,j;

  13. uchar code smgdisplay[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,
  14. 0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};

  15. void delayms(uint z);

  16. void keydown()
  17. {
  18.         if(Key1==0)
  19.         {
  20.                 delayms(5);
  21.                 if(Key1==0)     //再次確認(rèn)按鍵按下
  22.                 {
  23.                         num++;
  24.                         if(num==60)
  25.                         num=0;
  26.                          while(!Key1);  //此處等待按鍵釋放 ,但不加該語句,程序依然能夠?qū)崿F(xiàn)。這是為什么呢?
  27.                 }
  28.                         

  29.         }

  30. }   //關(guān)于該處語句的理解如此:按鍵按下松開,數(shù)碼管數(shù)值加一。開發(fā)板上的現(xiàn)象是按下數(shù)碼管不顯示任何數(shù)字,松開數(shù)值加一。

  31. void display(uchar num1)
  32. {
  33.         shi=num1/10;
  34.         ge=num1%10;
  35.         
  36.         LSA=1;
  37.         LSB=0;
  38.         LSC=0;
  39.         
  40.         P0=smgdisplay[shi];
  41.         delayms(10);
  42.         P0=0X00;

  43.         LSA=0;
  44.         LSB=0;
  45.         LSC=0;
  46.         
  47.         P0=smgdisplay[ge];
  48.         delayms(10);
  49.         P0=0X00;
  50.         
  51.                

  52. }

  53. void main()
  54. {
  55.         while(1)
  56.         {
  57.                 keydown();
  58.                 display(num);
  59.         }

  60.                

  61. }

  62. void delayms(uint z)
  63. {
  64.         for(i=z;i>0;i--)
  65.         for(j=110;j>0;j--);

  66. }




復(fù)制代碼

HC6800-ES V2.0 新版.pdf

759.71 KB, 下載次數(shù): 7


作者: 不倒翁.WZT    時間: 2017-11-18 04:35
標(biāo)題: RE: 關(guān)于按鍵Key1,每按一次數(shù)碼管加一遇到的問題,請大神老師指導(dǎo)
while(!Key1);是將鍵值取反,如果手已松開,鍵值等于1,取反后,鍵值等于0,while(0);跳出大循環(huán),執(zhí)行后面的程序。如果此時手沒有松開,鍵值等于0,取反后,鍵值等于1,while(1);繼續(xù)進(jìn)行大循環(huán),什么時候鍵值等于1,取反后,鍵值等于0,那時候跳出大循環(huán)。這也是為什么手松開數(shù)碼管才顯示加1的原因。
作者: bai850107    時間: 2017-11-18 20:39
不倒翁.WZT 發(fā)表于 2017-11-18 04:35
while(!Key1);是將鍵值取反,如果手已松開,鍵值等于1,取反后,鍵值等于0,while(0);跳出大循環(huán),執(zhí)行后 ...

是,謝謝。但如果不加這一句程序依然能夠?qū)崿F(xiàn),不明白為什么
作者: wblsx    時間: 2017-11-21 13:50
bai850107 發(fā)表于 2017-11-18 20:39
是,謝謝。但如果不加這一句程序依然能夠?qū)崿F(xiàn),不明白為什么

不加這一句就沒有松手檢測,那么按下去數(shù)碼管就會加一,但如果不加這句按鍵有抖動數(shù)碼管可能會連續(xù)加上




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