標(biāo)題: 我調(diào)的小車前進(jìn)后退左轉(zhuǎn)右轉(zhuǎn)的簡單代碼 新手開始做小車,請多多指教 [打印本頁]

作者: King_Dan    時間: 2016-5-29 01:35
標(biāo)題: 我調(diào)的小車前進(jìn)后退左轉(zhuǎn)右轉(zhuǎn)的簡單代碼 新手開始做小車,請多多指教
這是我調(diào)的小車前進(jìn)后退左轉(zhuǎn)右轉(zhuǎn)的簡單代碼。

  1. #include"reg52.h"

  2. #define key_vaule1 1
  3. #define key_vaule2 2
  4. #define key_vaule3 3
  5. #define key_vaule4 4

  6. sbit key1=P3^2;
  7. sbit key2=P3^4;
  8. sbit key3=P3^6;
  9. sbit key4=P3^7;

  10. sbit IA1=P0^0;
  11. sbit IB1=P0^1;
  12. sbit IA2=P0^2;
  13. sbit IB2=P0^3;

  14. int ms_10=0;
  15. int keypress = 0;

  16. //按鍵抬起的任務(wù)執(zhí)行函數(shù)
  17. void HandleKeyPress()
  18. {
  19.         switch(keypress)
  20.         {
  21.                 case key_vaule1:
  22.                         IA1=1;IB1=0;
  23.                         IA2=1;IB2=0;
  24.                         break;
  25.                 case key_vaule2:
  26.                         IA1=0;IB1=1;
  27.                         IA2=0;IB2=1;
  28.                         break;
  29.                 case key_vaule3:
  30.                         IA1=1;IB1=0;
  31.                         IA2=0;IB2=0;
  32.                         break;
  33.                 case key_vaule4:
  34.                         IA1=0;IB1=0;
  35.                         IA2=1;IB2=0;
  36.                         break;
  37.         }
  38. }

  39. void keyScan()
  40. {
  41.         static unsigned char state=0;
  42.         unsigned char temp;

  43.     if(key1==0) temp=key_vaule1;
  44.     else if (key2==0) temp=key_vaule2;
  45.     else if (key3==0) temp=key_vaule3;
  46.     else if (key4==0) temp=key_vaule4;
  47.     else temp=0;

  48.     switch (state)
  49.     {
  50.                 case 0: if(temp!=0) state=1;break;  //檢測是否有低電平出現(xiàn)
  51.                 case 1: state=2;  break; //延時10ms
  52.                           //case 2:按鍵已經(jīng)被按下,此時把temp的數(shù)值付給keypress就可以知道是那個按鍵被按下
  53.                 case 2: if(temp!=0) {state=3;keypress=temp;HandleKeyPress();} else state=0; break;
  54.                 case 3: if(temp==0) {state=0;IA1=0;IB1=0;IA2=0;IB2=0;} break;       
  55.     }
  56. }

  57. void main()
  58. {
  59.         TMOD = 0x01;
  60.         TH0 = (65536-1000)/256;
  61.         TL0 = (65536-1000)%256;
  62.         EA = 1;
  63.         ET0 = 1;
  64.         TR0 = 1;
  65.        
  66.         while(1)
  67.         {
  68.                 if(ms_10>10)
  69.                 {
  70.                         ms_10 = 0;
  71.                         keyScan();
  72.                 }
  73.         }
  74. }

  75. void timer0() interrupt 1
  76. {
  77.         TH0 = (65536-1000)/256;
  78.         TL0 = (65536-1000)%256;       
  79.           ms_10++;
  80. }
復(fù)制代碼


新建 Microsoft Word 文檔.doc

30.5 KB, 下載次數(shù): 8, 下載積分: 黑幣 -5


作者: 黑色天空》》    時間: 2019-11-10 13:18
樓主  這里的按鍵是什么模塊啊




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