標(biāo)題: 單片機(jī)按鍵識(shí)別程序與proteus仿真 [打印本頁]

作者: 51黑ff    時(shí)間: 2016-10-10 16:47
標(biāo)題: 單片機(jī)按鍵識(shí)別程序與proteus仿真
一個(gè)最簡單的單片機(jī)應(yīng)用 按鍵識(shí)別的proteus仿真,原理圖如下:

單片機(jī)源程序:
  1. /********************************************************************
  2.                             51黑電子論壇
  3. *********************************************************************/
  4. #include<reg52.h>                 //庫文件
  5. #define uchar unsigned char//宏定義無符號(hào)字符型
  6. #define uint unsigned int  //宏定義無符號(hào)整型
  7. /********************************************************************
  8.                             初始定義
  9. *********************************************************************/
  10. uchar count;   //按鍵計(jì)數(shù),每按一下,count 加1
  11. uchar a,b;           //定義字符型變量
  12. uchar temp;    //定義字符型變量
  13. sbit BY1=P2^1; //定義按鍵的輸入端(為單片機(jī)P2口的P2.1按鍵)
  14. /********************************************************************
  15.                             延時(shí)函數(shù)
  16. *********************************************************************/
  17. void delay10ms(void) //延時(shí)程序
  18. {
  19. uchar i,j;
  20. for(i=20;i>0;i--)
  21. for(j=248;j>0;j--);
  22. }
  23. /********************************************************************
  24.                            按鍵判斷函數(shù)
  25. *********************************************************************/
  26. void key() //按鍵判斷程序
  27. {
  28.         if(BY1==0) //判斷是否按下鍵盤
  29.         {
  30.                 delay10ms(); //延時(shí),軟件去干擾
  31.                 if(BY1==0) //確認(rèn)按鍵按下
  32.                 {
  33.                         count++; //按鍵計(jì)數(shù)加1
  34.                         if(count==8) //計(jì)8次重新計(jì)數(shù)
  35.                         {
  36.                                 count=0;//將count 清零
  37.                         }
  38.                 }
  39.         while(BY1==0);//按鍵鎖定,每按一次count 只加1.
  40.         }
  41. }
  42. /********************************************************************
  43.                             主函數(shù)
  44. *********************************************************************/
  45. void main()
  46. {
  47.         temp=0xfe; //定義為11111110
  48.         while(1)
  49.         {
  50.                 key(); //調(diào)用按鍵判斷函數(shù)
  51.                 a=temp<<count;//左移count位
  52.                 b=temp>>(8-count);//右移8-count位
  53.                 P0=a|b;        //求值
  54.         }
  55. }
  56.         
  57. /********************************************************************
  58.                               結(jié)束
  59. *********************************************************************/
復(fù)制代碼


所有資料下載:
按鍵識(shí)別.rar (56.31 KB, 下載次數(shù): 14)







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