久久久久久久999_99精品久久精品一区二区爱城_成人欧美一区二区三区在线播放_国产精品日本一区二区不卡视频_国产午夜视频_欧美精品在线观看免费

 找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 7823|回復: 9
打印 上一主題 下一主題
收起左側

STM32兩輪自平衡小車資料

[復制鏈接]
跳轉到指定樓層
樓主
stm32單片機做的兩輪自平衡小車資料,帶完整的源碼。還附帶很多pdf教程 如pid控制和濾波的資料.

全部資料打包下載:
兩輪自平衡小車資料.rar (8.27 MB, 下載次數: 195)




主程序預覽:
  1. #include "sys.h"
  2. #include "usart.h"               
  3. //#include "delay.h"       

  4. // 編碼器:100線;電機減速比:150

  5. #include "ofme_led.h"


  6. #include "ofme_filter.h"
  7. #include "ofme_iic.h"
  8. #include "ofme_iic_dev.h"
  9. #include "ofme_pwm.h"
  10. #include "ofme_pid.h"
  11. #include "ofme_encoder.h"
  12. #include "ofme_io.h"
  13. #include "ofme_time.h"
  14. #include "ofme_ir_nec.h"

  15. #define PI (3.14159265)
  16. // 度數表示的角速度*1000
  17. #define MDPS (70)
  18. // 弧度表示的角速度
  19. #define RADPS ((float)MDPS*PI/180000)
  20. // 每個查詢周期改變的角度
  21. #define RADPT (RADPS/(-100))


  22. // 平衡的角度范圍;+-60度(由于角度計算采用一階展開,實際值約為46度)
  23. #define ANGLE_RANGE_MAX (60*PI/180)
  24. #define ANGLE_RANGE_MIN (-60*PI/180)

  25. // 全局變量
  26. pid_s sPID;                                        // PID控制參數結構體
  27. float radian_filted=0;                // 濾波后的弧度
  28. accelerometer_s acc;                // 加速度結構體,包含3維變量
  29. gyroscope_s gyr;                        // 角速度結構體,包含3維變量
  30. int speed=0, distance=0;        // 小車移動的速度,距離
  31. int tick_flag = 0;                        // 定時中斷標志
  32. int pwm_speed = 0;                        // 電機pwm控制的偏置值,兩個電機的大小、正負相同,使小車以一定的速度前進
  33. int pwm_turn = 0;                        // 電機pwm控制的差異值,兩個電機的大小相同,正負相反,使小車左、右轉向
  34. float angle_balance = 0;        // 小車的平衡角度。由于小車重心的偏移,小車的平衡角度不一定是radian_filted為零的時候


  35. // 定時器周期中斷,10ms
  36. void sys_tick_proc(void)
  37. {
  38.         static unsigned int i = 0;

  39.         tick_flag++;

  40.         i++;
  41.         if(i>=100) i=0;

  42.         if(i==0)                   // 綠燈的閃爍周期為1秒
  43.         {
  44.                 LED1_OFF();
  45.         }
  46.         else if(i==50)
  47.         {
  48.                 LED1_ON();
  49.         }
  50. }

  51. void control_proc(void)
  52. {
  53.         int i = ir_key_proc(); // 將紅外接收到的按鍵值,轉換為小車控制的相應按鍵值。

  54.         switch(i)
  55.         {
  56.                 case KEY_TURN_LEFT:
  57.                         if(pwm_turn<500) pwm_turn += 50;
  58.                         break;
  59.                 case KEY_TURN_RIGHT:
  60.                         if(pwm_turn>-500) pwm_turn -= 50;
  61.                         break;
  62.                 case KEY_TURN_STOP:
  63.                         pwm_turn = 0;
  64.                         distance = 0;
  65.                         pwm_speed = 0;
  66.                         break;
  67.                 case KEY_SPEED_UP:
  68.                         if(pwm_speed<500) pwm_speed+=100;
  69.                         break;
  70.                 case KEY_SPEED_DOWN:
  71.                         if(pwm_speed>-500) pwm_speed-=100;
  72.                         break;
  73.                 case KEY_SPEED_0:
  74.                         pwm_speed = 0;
  75.                         break;
  76.                 case KEY_SPEED_F1:
  77.                         pwm_speed = 150;
  78.                         break;
  79.                 case KEY_SPEED_F2:
  80.                         pwm_speed = 300;
  81.                         break;
  82.                 case KEY_SPEED_F3:
  83.                         pwm_speed = 450;
  84.                         break;
  85.                 case KEY_SPEED_F4:
  86.                         pwm_speed = 600;
  87.                         break;
  88.                 case KEY_SPEED_F5:
  89.                         pwm_speed = 750;
  90.                         break;
  91.                 case KEY_SPEED_F6:
  92.                         pwm_speed = 900;
  93.                         break;
  94.                 case KEY_SPEED_B1:
  95.                         pwm_speed = -150;
  96.                 case KEY_SPEED_B2:
  97.                         pwm_speed = -300;
  98.                 case KEY_SPEED_B3:
  99.                         pwm_speed = -450;
  100.                         break;
  101.                 default:
  102.                         break;
  103.         }

  104.         pwm_turn *= 0.9;        // pwm_turn的值以0.9的比例衰減,使小車在接收到一個轉向信號后只轉動一定的時間后停止轉動。


  105.         speed = speed*0.7 +0.3*(encoder_read());        // 定周期(10ms)讀取編碼器數值得到實時速度,再對速度進行平滑濾波
  106.         if(speed!=0)
  107.         {
  108.                 printf("speed: %d, dst: %d, pwm: %d \r\n", speed,distance,(int)(speed*6+distance*0.1));
  109.         }



  110.         encoder_write(0);                                                        // 編碼器值重新設為0

  111.         distance += speed;                                                        // 對速度進行積分,得到移動距離

  112.         if(distance>6000) distance = 6000;                        // 減少小車懸空、空轉對控制的影響
  113.         else if(distance<-6000) distance = -6000;

  114. }

  115. void balance_proc(void)
  116. {
  117.         static unsigned int err_cnt=0;

  118. //        float tFloat;
  119.         int pwm_balance;
  120. //        static float angle;
  121. //        float angle_t;
  122.         float radian, radian_pt;          // 當前弧度及弧度的微分(角速度,角度值用弧度表示)

  123.         adxl345_read(&acc);                        // 讀取當前加速度。由于傳感器按照的位置原因,傳感器的值在函數內部經過處理,變為小車的虛擬坐標系。
  124.         l3g4200d_read(&gyr);                // 讀取當前角速度。同樣經過坐標系變換。


  125. // 此段程序用于傳感器出錯時停止小車
  126.         err_cnt = err_cnt*115>>7;        // err_cnt以0.9的比例系數衰減(115>>7的值約為0.9,避免浮點數,提高速度)
  127.         if(acc.flag != 0x0F || gyr.flag != 0x0F)   // 讀取的角度、角速度值有誤?赡苁请姶鸥蓴_、iic線太長等導致出錯。
  128.         {
  129.                 LED0_ON();                // 亮紅燈
  130.                 err_cnt +=100;        // 等比數列,比例系數0.9(115>>7),常數項100;根據公式,連續10項的和約為657
  131.                 if(err_cnt>657) goto err;        // 當連續發生約10次(約0.1秒)錯誤則超過657而溢出。
  132.         }


  133. // 此段程序用于倒立或失重時停止小車
  134.         if(acc.z<=0)
  135.         {
  136.                 goto err;
  137.         }


  138. // 小車的虛擬x軸方向為小車前進方向,虛擬y軸為小車左邊,虛擬z軸為小車上升方向。
  139. // 前傾角度為負,后傾角度為正。
  140.         // 通過計算加速度分量,得到小車傾斜弧度(未濾波)
  141.         radian = (float)(acc.x)/acc.z;        //  一階展開:Q =f(x)=x-x^3/3+x^5/5+...+(-1)^k*x^(2k+1)/(2k+1)+...
  142.         // 通過角速度傳感器,得到小車的角速度(單位為 弧度/秒)
  143.         radian_pt = gyr.y*RADPT;
  144.         radian_filted = ofme_filter(radian_filted, radian, radian_pt);                // 互補濾波得到小車的傾斜角度

  145. // 此段程序用于小車傾斜角度過大時,停止小車
  146.         if(radian_filted> ANGLE_RANGE_MAX || radian_filted<ANGLE_RANGE_MIN)
  147.         {
  148.                 goto err;
  149.         }

  150. // 通過PID計算,得到保持小車角度為零所需要的電機pwm輸出
  151.         pwm_balance = pid_proc(&sPID, radian_filted, radian_pt);
  152.         //        printf("%d\r\n",speed);
  153. // 通過小車移動速度與移動距離,調整小車平衡所需的pwm輸出
  154.         pwm_balance += (speed*6+distance*0.1);

  155. // 在pwm_balance的基礎上,加上速度分量與轉動分量,調整小車兩個電機的轉速。
  156.         pwm_control(pwm_balance+pwm_speed+pwm_turn, pwm_balance+pwm_speed-pwm_turn);

  157. // 如果pwm超出有效值,紅燈亮。用于調試,了解系統狀態。
  158.         if(pwm_balance>=1000||pwm_balance<=-1000) LED1_ON();
  159.         LED0_OFF();
  160.         return;
  161. err:
  162.         puts("balance error.\r\n");
  163.         pwm_control(0, 0);                                   // 關閉電機
  164.         return;
  165. }


  166. int main(void)
  167. {
  168. //        int i=0, t;
  169. //        int pwm;
  170. //        float radian, radian_pt;

  171.           Stm32_Clock_Init(9);//系統時鐘設置
  172.         uart_init(72,57600); //串口1初始化   
  173.         hw_tick_start();   // 定時器周期性中斷,用于提供系統脈搏
  174. ////////////////////////////////////////////////////////////////////////////////
  175.         led_init();
  176.         pwm_init();
  177.         iic_init();
  178.         adxl345_init(&acc);
  179.         l3g4200d_init(&gyr);
  180.         hw_ir_init();
  181.         encoder_init();
  182.         while(0)
  183.         {
  184.                 if(tick_flag>100)
  185.                 {
  186.                         tick_flag = 0;
  187.                         printf("count: %d\r\n",encoder_read());

  188.                 }
  189.         }



  190. ////////////////////////////////////////////////////////////////////////////////
  191. //        pid_init(&sPID, 4500,0,-300);6000--350        ;8000--350;11000--350;
  192. //        pid_init(&sPID, 6000,0,-35000);
  193.         pid_init(&sPID, 7500,0,-35000);          // 調節PID參數,后3個形參分別為:比例系數P,積分系數I,微分系數D
  194.         sPID.target = -3.5*PI/180;

  195.         radian_filted = 0;
  196.         adxl345_init(&acc);
  197.         l3g4200d_init(&gyr);
  198.         while(1)
  199.         {
  200.                 if(tick_flag)
  201.                 {
  202.                         tick_flag = 0;
  203.                         balance_proc();        // 調節小車,保持平衡
  204.                         control_proc();        // 根據遙控接收到的數據,調整電機輸出參數
  205.                 }
  206.         }
  207. }

復制代碼


分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏 分享淘帖 頂 踩
回復

使用道具 舉報

沙發
ID:1 發表于 2017-3-18 21:26 | 只看該作者
iic部分源碼:
  1. #include "ofme_iic.h"

  2. ////////////////////////////////////////////////////////////////////////////////
  3. // ofme_iic.c
  4. // v2012.12.20
  5. // Copyright(C) ofourme@163.com
  6. // All rights reserved
  7. ////////////////////////////////////////////////////////////////////////////////
  8. // iic 主機模擬程序 for F/S mode
  9. // 不可重入
  10. // 所有的函數結束后都是占有scl總線的,故不會有別的主機與之競爭(除非與別的主...
  11. // 機處于完全同步),但別的主機也可能在程序運行期間加入競爭。
  12. ////////////////////////////////////////////////////////////////////////////////
  13. // 詳見《I2C總線規范(周立功翻譯版)》
  14. // 傳輸格式:P8~P10
  15. // 同步與仲裁:P10~P11
  16. // 時序要求:P27~P28
  17. // tag: 同步發生于主機活躍的所有時間
  18. //      仲裁發生于主機發送數據的情況,包括ACK位
  19. ////////////////////////////////////////////////////////////////////////////////
  20. // 全局變量,用于保存IIC_TIME_OUT等錯誤信息。通過iic_error_check()獲取。
  21. static int _IIC_ERROR_CODE = IIC_OK;
  22. ////////////////////////////////////////////////////////////////////////////////
  23. #ifndef F_SCL
  24.     #err "F_SCL not defined."
  25. #elif (F_SCL==100)
  26. ////////////////////////////////////////////////////////////////////////////////
  27. // (重復)起始條件的保持時間。在這個周期后,產生第一個時間脈沖...
  28. //  4.0us<t_hd_sta or 0.6us<t_hd_sta
  29. #define T_HD_STA (4)
  30. // SCL時鐘的低電平周期 4.7us<t or 1.3us<t
  31. // SDA should hold at least 300ns while SCL is falling
  32. #define T_LOW1  (1)
  33. #define T_LOW2  (4)
  34. #define T_LOW   (T_LOW1+T_LOW2)
  35. // SCL時鐘的高電平周期 4.0us<t or 0.6us<t
  36. #define T_HIGH  (4)
  37. // 重復起始條件的建立時間 4.7us<t or 0.6us<t
  38. #define T_SU_STA (5)
  39. // 數據保持時間:
  40. // IIC總線器件:0<t<3.45us or 0<t<0.9us
  41. // 兼容CUBS的主機:5.0us<t<NULL or NULL<t<NULL
  42. // SDA should hold at least 300ns while SCL is falling
  43. #define T_HD_DAT (1)
  44. // 數據建立時間:250ns<t or 100ns<t
  45. #define T_SU_DAT (1)
  46. // 停止條件的建立時間:4.0us<t or 0.6us<t
  47. #define T_SU_STO (4)
  48. // 停止和啟動條件之間的總線空閑時間 4.7us<t_buf or 1.3us<t_buf
  49. #define T_BUF   (5)
  50. ////////////////////////////////////////////////////////////////////////////////
  51. #elif (F_SCL==400)
  52. ////////////////////////////////////////////////////////////////////////////////
  53. #define T_HD_STA    (1)
  54. #define T_LOW1      (1)
  55. #define T_LOW2      (1)
  56. #define T_LOW       (T_LOW1+T_LOW2)
  57. #define T_HIGH      (1)
  58. #define T_SU_STA    (1)
  59. #define T_HD_DAT    (0)
  60. #define T_SU_DAT    (1)
  61. #define T_SU_STO    (1)
  62. #define T_BUF       (2)
  63. ////////////////////////////////////////////////////////////////////////////////
  64. #else
  65. #err "F_SCL value error."
  66. #endif
  67. ////////////////////////////////////////////////////////////////////////////////
  68. // 提供給iic的延時函數,單位為1微秒。
  69. #ifndef iic_delay
  70.     #err "iic_delay() not defined."
  71. #endif
  72. ////////////////////////////////////////////////////////////////////////////////
  73. // 主機釋放SCL總線,而且等待外部主機、設備釋放SCL總線。用于SCL同步。
  74. /* "IIC SCL SYNCHRONIZE." 不屬于異常錯誤,故大寫表示。*/
  75. #define IIC_SCL_RELEASE(t) \
  76. {\
  77.     SCL_H();\
  78.         t = 0;\
  79.         while(SCL==0)\
  80.         {\
  81.                 t++;\
  82.                 if(t==IIC_RAISING_COUNT) IIC_DEBUG("IIC SCL SYNCHRONIZE.\r\n");\
  83.                 if(t>=IIC_TIME_OUT_COUNT)\
  84.                 {\
  85.                         _IIC_ERROR_CODE = IIC_TIME_OUT;\
  86.                         IIC_DEBUG("iic scl synchronize time out.\r\n");\
  87.                         break;\
  88.                 }\
  89.         }\
  90. }
  91. ////////////////////////////////////////////////////////////////////////////////
  92. // 保持時間T的高電平。但是如果總線電平被外部拉低,則提前退出。用于SCL同步。
  93. #define IIC_SCL_HOLD(T, t) \
  94. {\
  95.     for(t=0; t<T; t++)\
  96.     {\
  97.         iic_delay(1);\
  98.         if(SCL==0) break;\
  99.     }\
  100. }
  101. ////////////////////////////////////////////////////////////////////////////////
  102. void iic_init(void)
  103. {
  104.         hw_iic_init(); // 外部函數。配置端口為開漏輸出。
  105. //        _IIC_ERROR_CODE = IIC_OK;        // 延后到iic_start()里面設置。
  106.         SCL_H();                // 釋放端口
  107.         SDA_H();
  108. }
  109. ////////////////////////////////////////////////////////////////////////////////
  110. // iic_start()函數在總線忙碌的情況下返回失敗值
  111. int iic_start(void)
  112. {
  113. // 其它主機可能處于<1>start、<2>restart、<3>stop、<4>讀寫SDA且SDA為高電平。
  114. // 有可能獨占總線或與別的主機同步占有總線,這是我們希望的最好結果。
  115. // 但iic協議是否有可能導致不同步地占有總線?
  116. //
  117. // 程序實際上應該檢查總線在T_BUF期間是否被占用,保證起始標志時序不被打斷,...
  118. // 但使用軟件查詢方式無法確切認定在延時期間總線電平沒有被外部主機拉低,...
  119. // 本程序的缺陷有可能導致不同步地占有總線。!
  120. // 只能寄希望于程序在后面的多主機競爭中失敗退出而避免錯誤了。


  121.         _IIC_ERROR_CODE = IIC_OK;

  122.     SCL_H();
  123.     SDA_H();
  124.     iic_delay(T_BUF+T_BUF_ALT);        // 保證SCL與SDA線的高電平維持時間

  125.     if( SCL==0 )            // SCL總線忙
  126.         {
  127.         return IIC_SCL_BUSY;
  128.         }
  129.     if( SDA==0 )            // SDA總線忙
  130.         {
  131.                 return IIC_SDA_BUSY;
  132.         }
  133.        
  134.     SDA_L();
  135.     iic_delay(T_HD_STA);
  136.     SCL_L();    // get the SCL & SDA bus

  137.     return IIC_OK;
  138. }

  139. ////////////////////////////////////////////////////////////////////////////////
  140. // 在傳輸完數據后可立即調用iic_restart(),與iic_start()類似。
  141. void iic_restart(void)
  142. {
  143.         int t;
  144. // scl==0
  145.     SDA_H();
  146.     iic_delay(T_LOW);
  147.     IIC_SCL_RELEASE(t);
  148.     iic_delay(T_SU_STA);
  149.     SDA_L();
  150.     iic_delay(T_HD_STA);
  151.     SCL_L();    // get the SCL & SDA bus
  152. }
  153. ////////////////////////////////////////////////////////////////////////////////
  154. void iic_stop(void)
  155. {
  156. // scl==0
  157.     SDA_L();
  158.     iic_delay(T_LOW);
  159.         SCL_H();        // release SCL, ignore pulling down by other device.
  160.     iic_delay(T_SU_STO);
  161.     SDA_H();    // release SDA
  162. }

  163. ////////////////////////////////////////////////////////////////////////////////
  164. // 主機接收數據發送ack, 比較響應位進行多主機仲裁,由于ack為低電平,故實際不仲裁
  165. void iic_ack(void)
  166. {
  167.     int t;

  168. // scl==0
  169.     SDA_L();    // ack
  170.     iic_delay(T_LOW);
  171.     IIC_SCL_RELEASE(t);         // SCL SYNCHRONIZE
  172.     IIC_SCL_HOLD(T_HIGH, t);   // SCL SYNCHRONIZE
  173.     SCL_L();    // get the SCL bus
  174. }

  175. ////////////////////////////////////////////////////////////////////////////////
  176. // 主機接收數據發送nack, 比較響應位進行多主機仲裁
  177. int iic_nack(void)
  178. {
  179.     int t;

  180. // scl==0
  181.     SDA_H();  // nack
  182.     iic_delay(T_LOW);
  183.     IIC_SCL_RELEASE(t);         // SCL SYNCHRONIZE
  184.     if(SDA==0)
  185.     {   // scl & sda had been released before.
  186.                 IIC_DEBUG("iic_nack() arbitrate failed.\r\n");
  187.                 // 應該不用再發送時鐘直到nack周期結束?
  188.         return IIC_AARB_FAIL;
  189.     }
  190.     IIC_SCL_HOLD(T_HIGH, t);         // SCL SYNCHRONIZE
  191.     SCL_L();    // get the SCL bus

  192.     return IIC_OK;
  193. }
  194. ////////////////////////////////////////////////////////////////////////////////
  195. // 主機發送數據完等待從機響應ack or nack,不進行多主機仲裁
  196. int iic_wait_ack(void)
  197. {
  198.     int t, data;

  199. // scl==0
  200.     SDA_H();            // release SDA
  201.     iic_delay(T_LOW);   // wait for SDA to be change
  202.     IIC_SCL_RELEASE(t);        // SCL SYNCHRONIZE
  203.         data = SDA;
  204.     IIC_SCL_HOLD(T_HIGH, t);  // SCL SYNCHRONIZE
  205.     SCL_L();    // get the SCL bus

  206.     if(data) return IIC_NACK;
  207.     else     return IIC_ACK;
  208. }
  209. ////////////////////////////////////////////////////////////////////////////////
  210. // 主機讀取數據,不比較數據位進行多主機仲裁
  211. u8 iic_read(void)
  212. {
  213.     u8 d;
  214.     int i, t;

  215. // sda==0, scl==0;
  216.     SDA_H(); // release SDA, wait for SDA to be change
  217.     for(i=0, d=0; i<8; i++)
  218.     {
  219.         iic_delay(T_LOW);
  220.         IIC_SCL_RELEASE(t);          // SCL SYNCHRONIZE
  221. //      read_bit();
  222.         d<<=1;
  223.         if(SDA) d++;
  224. // 理論上read函數和write函數在這里收發字節的第1位時,應不斷檢測SCL高電平期間,...
  225. // SDA的電平有無變化以識別restart()或stop()標志,但同時還要檢測SCL有無被外部拉低...
  226. // 在不使用中斷而采用純粹查詢手段的情況下,實現起來有困難,故不做判斷。
  227.         IIC_SCL_HOLD(T_HIGH, t);    // SCL SYNCHRONIZE
  228.         SCL_L();    // get the SCL bus
  229.     }

  230.     return d;
  231. }

  232. ////////////////////////////////////////////////////////////////////////////////
  233. // 主機發送數據,比較數據位進行多主機仲裁
  234. // 主機在發送數據的第一位且第一位為1時,其它主機可能在SCL高電平期間發送...
  235. // restart()或是stop()標志,也即電平0->1或是1->0,理論上程序應該檢測這種...
  236. // 情況的發生,并停止發送數據而發送一樣的restart或是stop標志(見P11)。
  237. // 為簡化程序,一旦遇到這種情況既轉化為IIC_ARB_FAIL處理。
  238. int iic_write(u8 data)
  239. {
  240.     int i, t, err = IIC_OK;

  241. // sda==0, scl==0;
  242.     for(i=0; i<8; i++, data<<=1)
  243.     {
  244.         iic_delay(T_LOW1);

  245. //                send_bit();
  246.                if(data&0x80)
  247.                    SDA_H();
  248.                else
  249.                    SDA_L();
  250. //
  251.             iic_delay(T_LOW2);
  252.                IIC_SCL_RELEASE(t);          // SCL SYNCHRONIZE
  253.                if( data&0x80 && (SDA==0) )//仲裁失敗
  254.                {   // scl & sda had been released before.
  255.                         // 理論上仲裁失敗就由其它主機接管控制器,程序可以停止產生SCL...
  256.                         // 在這里我們應該可以直接返回 IIC_DARB_FAIL
  257.             // return IIC_DARB_FAIL;
  258.                         // 但我選擇發送0xff直到字節結束
  259.                         err = IIC_DARB_FAIL;
  260.                         data = 0xFF;
  261.         }
  262.         IIC_SCL_HOLD(T_HIGH, t);    // SCL SYNCHRONIZE
  263.         SCL_L();    // get the SCL bus
  264.     }

  265.     return err;
  266. }

  267. ////////////////////////////////////////////////////////////////////////////////
  268. #if 0
  269. int iic_dev_read(u8 dev, u8 addr, u8* data)
  270. {
  271. // 注意將IIC_DEBUG()放iic_stop()后面,以免影響總線時序。

  272.     int i;

  273.     i = iic_start();  // select the device and set address
  274.     if( i != IIC_OK ) goto err_bus_busy;
  275.     i = iic_write(dev);
  276.     if( i != IIC_OK ) goto err_arb_fail;
  277.     i = iic_wait_ack();
  278.     if( i != IIC_ACK) goto err_dev_fail;
  279.     i = iic_write(addr);
  280.     if( i != IIC_OK ) goto err_arb_fail;
  281.     i = iic_wait_ack();
  282.     if( i != IIC_ACK) goto err_tar_fail;

  283.     iic_restart();
  284.     i = iic_write(dev|1);  // start read
  285.     if( i != IIC_OK ) goto err_arb_fail;
  286.     i = iic_wait_ack();
  287.     if( i != IIC_ACK) goto err_dev_fail;
  288.     *data = iic_read();
  289.     i = iic_nack();// write nack to tell the slave stop transfer data.
  290.     if( i != IIC_OK ) goto err_arb_fail;

  291. //end:
  292.     iic_stop();
  293. //        IIC_DEBUG("R: IIC READ DONE.\r\n");
  294.         if(_IIC_ERROR_CODE & IIC_TIME_OUT)
  295.         {
  296.                 IIC_DEBUG("r: iic time out.\r\n");
  297.                 return _IIC_ERROR_CODE;
  298.         }
  299.     return IIC_OK;
  300. err_bus_busy:
  301.         if(i == IIC_SCL_BUSY)
  302.                 IIC_DEBUG("r: iic scl bus busy.\r\n");
  303.         else
  304.                 IIC_DEBUG("r: iic sda bus busy.\r\n");
  305.         return i | _IIC_ERROR_CODE;
  306. err_arb_fail:
  307. //  總線仲裁失敗可能是由于硬件錯誤或是多主機競爭。如果是硬件錯誤,應繼續產生...
  308. //  時鐘到字節傳輸結束,然后釋放總線?不管怎樣,都不應該再調用iic_stop();
  309.         SDA_H();
  310.         SCL_H();
  311.         IIC_DEBUG("r: iic bus arbitrate failed.\r\n");
  312.         return i | _IIC_ERROR_CODE;        // IIC_ARB_FAIL
  313. err_dev_fail:
  314.         iic_stop();
  315.         IIC_DEBUG("r: iic device not respond.\r\n");
  316.         return IIC_DEVICE_FAIL | _IIC_ERROR_CODE;
  317. err_tar_fail:
  318.         iic_stop();
  319.         IIC_DEBUG("r: device target not respond.\r\n");
  320.         return IIC_TARGET_FAIL | _IIC_ERROR_CODE;
  321. }
  322. #else
  323. int iic_dev_read(u8 dev, u8 addr, u8* data)
  324. {
  325.         return iic_dev_gets(dev, addr, data, 1);
  326. }

  327. #endif
  328. ////////////////////////////////////////////////////////////////////////////////

  329. int iic_dev_gets(u8 dev, u8 addr, u8* data, u16 n)
  330. {
  331.     int i;

  332.     i = iic_start();  // select the device and set address
  333.     if( i != IIC_OK ) goto err_bus_busy;
  334.     i = iic_write(dev);
  335.     if( i != IIC_OK ) goto err_arb_fail;
  336.     i = iic_wait_ack();
  337.     if( i != IIC_ACK) goto err_dev_fail;
  338.     i = iic_write(addr);
  339.     if( i != IIC_OK ) goto err_arb_fail;
  340.     i = iic_wait_ack();
  341.     if( i != IIC_ACK) goto err_tar_fail;

  342.     iic_restart();
  343.     i = iic_write(dev|1);  // start read
  344.     if( i != IIC_OK ) goto err_arb_fail;
  345.     i = iic_wait_ack();
  346.     if( i != IIC_ACK) goto err_dev_fail;
  347.         if(n<1) n=1;
  348.         while(--n)
  349.     {
  350.                 *data++ = iic_read();
  351.                 iic_ack();
  352.         }
  353.         *data = iic_read();
  354.     i = iic_nack();// write nack to tell the slave stop transfer data.
  355.     if( i != IIC_OK ) goto err_arb_fail;

  356. //end:
  357.     iic_stop();
  358. //        IIC_DEBUG("R: IIC READ DONE.\r\n");
  359.         if(_IIC_ERROR_CODE & IIC_TIME_OUT)
  360.         {
  361.                 IIC_DEBUG("r: iic time out.\r\n");
  362.                 return _IIC_ERROR_CODE;
  363.         }
  364.     return IIC_OK;
  365. err_bus_busy:
  366.         if(i == IIC_SCL_BUSY)
  367.                 IIC_DEBUG("r: iic scl bus busy.\r\n");
  368.         else
  369.                 IIC_DEBUG("r: iic sda bus busy.\r\n");
  370.         return i | _IIC_ERROR_CODE;
  371. err_arb_fail:
  372. //  總線仲裁失敗可能是由于硬件錯誤或是多主機競爭。如果是硬件錯誤,應繼續產生...
  373. //  時鐘到字節傳輸結束,然后釋放總線?不管怎樣,都不應該再調用iic_stop();
  374.         SDA_H();
  375.         SCL_H();
  376.         IIC_DEBUG("r: iic bus arbitrate failed.\r\n");
  377.         return i | _IIC_ERROR_CODE;        // IIC_ARB_FAIL
  378. err_dev_fail:
  379.         iic_stop();
  380.         IIC_DEBUG("r: iic device not respond.\r\n");
  381.         return IIC_DEVICE_FAIL | _IIC_ERROR_CODE;
  382. err_tar_fail:
  383.         iic_stop();
  384.         IIC_DEBUG("r: device target not respond.\r\n");
  385.         return IIC_TARGET_FAIL | _IIC_ERROR_CODE;
  386. }


  387. ////////////////////////////////////////////////////////////////////////////////
  388. #if 0
  389. int iic_dev_write(u8 dev, u8 addr, u8 data)
  390. {
  391. // 注意將IIC_DEBUG()放iic_stop()后面,以免影響總線時序。

  392.     int i;

  393.     i = iic_start();
  394.     if( i != IIC_OK ) goto err_bus_busy;
  395.     i = iic_write(dev);
  396.     if( i != IIC_OK ) goto err_arb_fail;
  397.     i = iic_wait_ack();
  398.     if( i != IIC_ACK) goto err_dev_fail;
  399.     i = iic_write(addr);
  400.     if( i != IIC_OK ) goto err_arb_fail;
  401.     i = iic_wait_ack();
  402.     if( i != IIC_ACK) goto err_tar_fail;
  403.     i = iic_write(data);
  404.     if( i != IIC_OK ) goto err_arb_fail;
  405. // 如果返回IIC_NACK,則不能再繼續往從機寫數據。本函數只寫一字節的數據,故忽略。
  406.     i = iic_wait_ack();
  407. //end:
  408.     iic_stop();
  409. //        IIC_DEBUG("W: IIC WRITE DONE.\r\n");
  410.         if( i != IIC_ACK)
  411.                 IIC_DEBUG("w: IIC DEVICE NO ACK.\r\n");
  412.         if(_IIC_ERROR_CODE & IIC_TIME_OUT)
  413.         {
  414.                 IIC_DEBUG("w: iic time out.\r\n");
  415.                 return _IIC_ERROR_CODE;
  416.         }
  417.     return IIC_OK;
  418. err_bus_busy:
  419.         if(i == IIC_SCL_BUSY)
  420.                 IIC_DEBUG("w: iic scl bus busy.\r\n");
  421.         else
  422.                 IIC_DEBUG("w: iic sda bus busy.\r\n");
  423.         return i | _IIC_ERROR_CODE;
  424. err_arb_fail:
  425. //  總線仲裁失敗可能是由于硬件錯誤或是多主機競爭。如果是硬件錯誤,應繼續產生...
  426. //  時鐘到字節傳輸結束,然后釋放總線?不管怎樣,都不應該再調用iic_stop();
  427.         SDA_H();
  428.         SCL_H();
  429.         IIC_DEBUG("w: iic bus arbitrate failed.\r\n");
  430.         return i | _IIC_ERROR_CODE;        // IIC_ARB_FAIL
  431. err_dev_fail:
  432.         iic_stop();
  433.         IIC_DEBUG("w: iic device not respond.\r\n");
  434.         return IIC_DEVICE_FAIL | _IIC_ERROR_CODE;
  435. err_tar_fail:
  436.         iic_stop();
  437.         IIC_DEBUG("w: device target not respond.\r\n");
  438.         return IIC_TARGET_FAIL | _IIC_ERROR_CODE;
  439. }

  440. #else

  441. int iic_dev_write(u8 dev, u8 addr, u8 data)
  442. {
  443.         u8 buf = data;
  444.         return iic_dev_puts(dev, addr, &buf, 1);
  445. }

  446. #endif

  447. ////////////////////////////////////////////////////////////////////////////////

  448. int iic_dev_puts(u8 dev, u8 addr, u8* data, u16 n)
  449. {
  450. // 注意將IIC_DEBUG()放iic_stop()后面,以免影響總線時序。

  451.     int i;

  452.     i = iic_start();
  453.     if( i != IIC_OK ) goto err_bus_busy;
  454.     i = iic_write(dev);
  455.     if( i != IIC_OK ) goto err_arb_fail;
  456.     i = iic_wait_ack();
  457.     if( i != IIC_ACK) goto err_dev_fail;
  458.     i = iic_write(addr);
  459.     if( i != IIC_OK ) goto err_arb_fail;
  460.     i = iic_wait_ack();
  461.     if( i != IIC_ACK) goto err_tar_fail;

  462.         if(n<1) n=1;
  463.         while(--n)
  464.         {
  465.                 i = iic_write(*data++);
  466.             if( i != IIC_OK ) goto err_arb_fail;
  467.             i = iic_wait_ack();
  468.                 if( i != IIC_ACK) goto err_tar_fail;        //could not write data.
  469.         }
  470.         i = iic_write(*data);
  471.            if( i != IIC_OK ) goto err_arb_fail;
  472.            iic_wait_ack();        // 最后一個字節,忽略ack。


  473. //end:
  474.     iic_stop();
  475. //        IIC_DEBUG("W: IIC WRITE DONE.\r\n");

  476.         if(_IIC_ERROR_CODE & IIC_TIME_OUT)
  477.         {
  478.                 IIC_DEBUG("w: iic time out.\r\n");
  479.                 return _IIC_ERROR_CODE;
  480.         }
  481.     return IIC_OK;
  482. err_bus_busy:
  483.         if(i == IIC_SCL_BUSY)
  484.                 IIC_DEBUG("w: iic scl bus busy.\r\n");
  485.         else
  486.                 IIC_DEBUG("w: iic sda bus busy.\r\n");
  487.         return i | _IIC_ERROR_CODE;
  488. err_arb_fail:
  489. //  總線仲裁失敗可能是由于硬件錯誤或是多主機競爭。如果是硬件錯誤,應繼續產生...
  490. //  時鐘到字節傳輸結束,然后釋放總線?不管怎樣,都不應該再調用iic_stop();
  491.         SDA_H();
  492.         SCL_H();
  493.         IIC_DEBUG("w: iic bus arbitrate failed.\r\n");
  494.         return i | _IIC_ERROR_CODE;        // IIC_ARB_FAIL
  495. err_dev_fail:
  496.         iic_stop();
  497.         IIC_DEBUG("w: iic device not respond.\r\n");
  498.         return IIC_DEVICE_FAIL | _IIC_ERROR_CODE;
  499. err_tar_fail:
  500.         iic_stop();
  501.         IIC_DEBUG("w: device target not respond.\r\n");
  502.         return IIC_TARGET_FAIL | _IIC_ERROR_CODE;
  503. }

  504. ////////////////////////////////////////////////////////////////////////////////
復制代碼
回復

使用道具 舉報

板凳
ID:1 發表于 2017-3-18 21:26 | 只看該作者
紅外遙控部分:
  1. #include "ofme_ir_nec.h"
  2. #include "ofme_time.h"

  3. #define NEC_HEAD_PLUSE_MAX                (9000+900)
  4. #define NEC_HEAD_PLUSE_MIN                (9000-630)
  5. #define NEC_HEAD_SPACE_MAX                (4500+450)
  6. #define NEC_HEAD_SPACE_MIN                (4500-315)
  7. #define NEC_HEAD_MAX                        (NEC_HEAD_PLUSE_MAX+NEC_HEAD_SPACE_MAX)
  8. #define NEC_HEAD_MIN                        (NEC_HEAD_PLUSE_MIN+NEC_HEAD_SPACE_MIN)
  9. #define NEC_DATA_PLUSE_MAX                (560+56)
  10. #define NEC_DATA_PLUSE_MIN                (560-56)
  11. #define NEC_LOG0_SPACE_MAX                (560+56)
  12. #define NEC_LOG0_SPACE_MIN                (560-56)
  13. #define NEC_LOG0_MAX                        (NEC_DATA_PLUSE_MAX+NEC_LOG0_SPACE_MAX)
  14. #define NEC_LOG0_MIN                        (NEC_DATA_PLUSE_MIN+NEC_LOG0_SPACE_MIN)
  15. #define NEC_LOG1_SPACE_MAX                (1680+168)
  16. #define NEC_LOG1_SPACE_MIN                (1680-168)
  17. #define NEC_LOG1_MAX                        (NEC_DATA_PLUSE_MAX+NEC_LOG1_SPACE_MAX)
  18. #define NEC_LOG1_MIN                        (NEC_DATA_PLUSE_MIN+NEC_LOG1_SPACE_MIN)
  19. #define NEC_REPEAT_PLUSE_MAX        (9000+630)
  20. #define NEC_REPEAT_PLUSE_MIN        (9000-900)
  21. #define NEC_REPEAT_SPACE_MAX        (2500+175)
  22. #define NEC_REPEAT_SPACE_MIN        (2500-250)
  23. #define NEC_REPEAT_DELAY_MAX        (97940+9794+NEC_DATA_PLUSE_MAX)
  24. #define NEC_REPEAT_DELAY_MIN        (97940-9794+NEC_DATA_PLUSE_MIN)
  25. #define NEC_REPEAT_MAX                        (NEC_REPEAT_PLUSE_MAX+NEC_REPEAT_SPACE_MAX)
  26. #define NEC_REPEAT_MIN                        (NEC_REPEAT_PLUSE_MIN+NEC_REPEAT_SPACE_MIN)

  27. #define IR_INT_CLR()                        EXTI->PR = 1<<1

  28. #define IR_NEC_DEBUG

  29. // 接收到的數值
  30. u32 ir_data;
  31. // <0: ir_data無效; 0:ir_data有效,但已經被處理過;>0: 連發次數;當>0,外部程序可以進行-1操作表示讀取數據
  32. int        ir_repeat = -1;
  33. // 0: OK; >0: error count; 外部程序可讀取此數值了解有無干擾信號或用于debug
  34. int ir_err_cnt = 0;

  35. void hw_ir_init(void)
  36. {

  37. //初始化紅外接收引腳的設置
  38. //開啟中斷,并映射
  39.         RCC->APB2ENR|=1<<4;       //PC時鐘使能                  
  40.         GPIOC->CRL&=0XFFFFFF0F;
  41.         GPIOC->CRL|=0X00000080;        //PC1輸入         
  42.         GPIOC->ODR|=1<<1;                //PC.1上拉      
  43.         Ex_NVIC_Config(GPIO_C,1,FTIR);//將line1映射到PC.1,下降沿觸發.
  44.         MY_NVIC_Init(2,1,EXTI1_IRQChannel,2);

  45. }
  46. // 一體化紅外接收頭只能通過38kHz左右的載波(抗干擾&節能),并轉化為TTL低電平
  47. // 下降沿中斷
  48. void EXTI1_IRQHandler(void)
  49. //void ir_nec_receive(void)
  50. {
  51. //        step(<=-1:表示重復幀結束; 0:表示數據幀的開頭;32:表示連續幀之間的間隔;>=33:表示重復幀的開頭)
  52.         static int step=-1;
  53.         static int time1=0;
  54.         int        time2;
  55.         int interval;

  56.         time2 = hw_time_get();
  57.         interval = hw_interval_get(time1,time2);
  58.         time1 = time2;

  59.         if(interval>NEC_REPEAT_DELAY_MAX)//連發碼之間的最大間隔
  60.         {
  61.                 step = -1;
  62.                 goto err;
  63.         }
  64.         else if(interval>NEC_HEAD_MAX)
  65.         {
  66.                 goto err;
  67.         }
  68.         else if(interval>NEC_HEAD_MIN)
  69.         {
  70.                 ir_repeat=-1; // 表示ir_data無效
  71.                 ir_data = 0;
  72.                 step = 0;
  73. #ifdef IR_NEC_DEBUG
  74.                 putchar('[');
  75. #endif
  76.                 IR_INT_CLR();
  77.                 return;
  78.         }
  79.         else if(interval>NEC_REPEAT_MAX)
  80.         {
  81.                 goto err;
  82.         }
  83.         else if(interval>NEC_REPEAT_MIN)
  84.         {
  85.                 if(step != 33) goto err;
  86.                 step = 32;
  87.                 ir_repeat++;
  88. #ifdef IR_NEC_DEBUG
  89.                 putchar('-');
  90.                 putchar('R');
  91.                 printf("-%d*%d.", (ir_data>>16)&0x0FF, ir_repeat);
  92. #endif
  93.                 IR_INT_CLR();
  94.                 return;
  95.         }
  96.         else if(interval>NEC_LOG1_MAX)
  97.         {
  98.                 goto err;
  99.         }
  100.         else if(interval>NEC_LOG1_MIN)
  101.         {
  102.                 goto decode;
  103.         }
  104.         else if(interval>NEC_LOG0_MAX)
  105.         {
  106.                 goto err;
  107.         }
  108.         else if(interval>NEC_LOG0_MIN)
  109.         {
  110.                 goto decode;
  111.         }
  112.         else
  113.         {
  114.                 goto err;
  115.         }

  116. // 只有長度為0或1的脈沖才能執行到這里
  117. decode:
  118.         if(step<0 || step>=32) goto err;
  119.         ir_data>>= 1;
  120.         if(interval>NEC_LOG1_MIN)
  121.         {
  122.                 ir_data |= 0x80000000UL;
  123.         }
  124.         step++;
  125.         if(step==32)
  126.         {
  127.                 ir_repeat = 1;
  128. #ifdef IR_NEC_DEBUG
  129.                 putchar(']');
  130.                 printf("-%d*%d.", (ir_data>>16)&0x0FF, ir_repeat);
  131. #endif
  132.         }
  133.         IR_INT_CLR();
  134.         return;

  135. err:
  136. #ifdef IR_NEC_DEBUG
  137.         putchar('\r');
  138.         putchar('\n');
  139. #endif
  140.         if(step == 32)
  141.         {
  142.                 step = 33;
  143. #ifdef IR_NEC_DEBUG
  144.                 putchar('R');
  145. #endif
  146.         }
  147.         else if(step>=0) // 數據接收出錯
  148.         {
  149.                 ir_err_cnt++;
  150.                 step = -1;
  151. #ifdef IR_NEC_DEBUG
  152.                 putchar('E');
  153. #endif
  154.         }
  155.         else
  156.         {
  157. #ifdef IR_NEC_DEBUG
  158.                 putchar('S');
  159. #endif
  160.         }

  161.         IR_INT_CLR();
  162.         return;
  163. }
復制代碼


回復

使用道具 舉報

地板
ID:146110 發表于 2017-4-30 17:03 | 只看該作者
兩輪的人玩的不多呀
回復

使用道具 舉報

5#
ID:95059 發表于 2017-8-13 21:22 | 只看該作者
tam1974 發表于 2017-4-30 17:03
兩輪的人玩的不多呀

難度比較大,所以玩得人少
回復

使用道具 舉報

6#
ID:134810 發表于 2017-9-27 15:54 來自手機 | 只看該作者
是這樣的
回復

使用道具 舉報

7#
ID:313772 發表于 2018-4-22 15:46 | 只看該作者
適合初學者  做嘛
回復

使用道具 舉報

8#
ID:363726 發表于 2018-7-18 08:31 | 只看該作者
學習
回復

使用道具 舉報

9#
ID:243161 發表于 2018-8-4 14:26 | 只看該作者
感謝分享
回復

使用道具 舉報

10#
ID:87766 發表于 2018-9-7 11:02 | 只看該作者
謝謝分享
回復

使用道具 舉報

您需要登錄后才可以回帖 登錄 | 立即注冊

本版積分規則

手機版|小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術交流QQ群281945664

Powered by 單片機教程網

快速回復 返回頂部 返回列表
主站蜘蛛池模板: 在线观看中文字幕视频 | 一区精品在线观看 | 中文字幕亚洲一区 | 成人一区二区三区在线观看 | 国产三级国产精品 | www.国产视频 | 亚洲播放 | 精品一区二区三区四区 | 国产一级免费视频 | 在线2区 | 久久成人免费 | 日韩一区二区三区视频 | 久久夜视频 | 日韩欧美精品一区 | 亚洲精品成人在线 | 久久成人在线视频 | 欧美在线视频一区二区 | 国产精品久久久久久久免费观看 | 插插插干干干 | 国产69精品久久99不卡免费版 | 伊人精品国产 | 最新黄色在线观看 | 涩涩视频在线观看 | 欧美一级视频免费看 | 欧美精品日韩精品 | 婷婷在线视频 | 婷婷久| 日韩久久久久久 | 欧美一级二级三级视频 | 欧美高清一区 | 日韩电影免费在线观看中文字幕 | 亚洲精品一区二区三区四区高清 | 色.com| 色999日韩 | 国产视频三区 | 日韩精品一区二区三区中文字幕 | 欧美一级黄色网 | 天天久久 | 在线久草 | 视频在线一区二区 | 国产精品美女在线观看 |