找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索

STM32兩輪自平衡小車資料

查看數(shù): 8036 | 評論數(shù): 9 | 收藏 0
關(guān)燈 | 提示:支持鍵盤翻頁<-左 右->
    組圖打開中,請稍候......
發(fā)布時間: 2017-3-18 11:47

正文摘要:

stm32單片機做的兩輪自平衡小車資料,帶完整的源碼。還附帶很多pdf教程 如pid控制和濾波的資料. 主程序預(yù)覽: #include "sys.h" #include "usart.h"          &nbs ...

回復(fù)

ID:87766 發(fā)表于 2018-9-7 11:02
謝謝分享
ID:243161 發(fā)表于 2018-8-4 14:26
感謝分享
ID:363726 發(fā)表于 2018-7-18 08:31
學(xué)習(xí)
ID:313772 發(fā)表于 2018-4-22 15:46
適合初學(xué)者  做嘛
ID:134810 發(fā)表于 2017-9-27 15:54
是這樣的
ID:95059 發(fā)表于 2017-8-13 21:22
tam1974 發(fā)表于 2017-4-30 17:03
兩輪的人玩的不多呀

難度比較大,所以玩得人少
ID:146110 發(fā)表于 2017-4-30 17:03
兩輪的人玩的不多呀
ID:1 發(fā)表于 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. // 接收到的數(shù)值
  30. u32 ir_data;
  31. // <0: ir_data無效; 0:ir_data有效,但已經(jīng)被處理過;>0: 連發(fā)次數(shù);當(dāng)>0,外部程序可以進行-1操作表示讀取數(shù)據(jù)
  32. int        ir_repeat = -1;
  33. // 0: OK; >0: error count; 外部程序可讀取此數(shù)值了解有無干擾信號或用于debug
  34. int ir_err_cnt = 0;

  35. void hw_ir_init(void)
  36. {

  37. //初始化紅外接收引腳的設(shè)置
  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,下降沿觸發(fā).
  44.         MY_NVIC_Init(2,1,EXTI1_IRQChannel,2);

  45. }
  46. // 一體化紅外接收頭只能通過38kHz左右的載波(抗干擾&節(jié)能),并轉(zhuǎn)化為TTL低電平
  47. // 下降沿中斷
  48. void EXTI1_IRQHandler(void)
  49. //void ir_nec_receive(void)
  50. {
  51. //        step(<=-1:表示重復(fù)幀結(jié)束; 0:表示數(shù)據(jù)幀的開頭;32:表示連續(xù)幀之間的間隔;>=33:表示重復(fù)幀的開頭)
  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)//連發(fā)碼之間的最大間隔
  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的脈沖才能執(zhí)行到這里
  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) // 數(shù)據(jù)接收出錯
  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. }
復(fù)制代碼


ID:1 發(fā)表于 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. // 所有的函數(shù)結(jié)束后都是占有scl總線的,故不會有別的主機與之競爭(除非與別的主...
  11. // 機處于完全同步),但別的主機也可能在程序運行期間加入競爭。
  12. ////////////////////////////////////////////////////////////////////////////////
  13. // 詳見《I2C總線規(guī)范(周立功翻譯版)》
  14. // 傳輸格式:P8~P10
  15. // 同步與仲裁:P10~P11
  16. // 時序要求:P27~P28
  17. // tag: 同步發(fā)生于主機活躍的所有時間
  18. //      仲裁發(fā)生于主機發(fā)送數(shù)據(jù)的情況,包括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. // (重復(fù))起始條件的保持時間。在這個周期后,產(chǎn)生第一個時間脈沖...
  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. // 重復(fù)起始條件的建立時間 4.7us<t or 0.6us<t
  38. #define T_SU_STA (5)
  39. // 數(shù)據(jù)保持時間:
  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. // 數(shù)據(jù)建立時間: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的延時函數(shù),單位為1微秒。
  69. #ifndef iic_delay
  70.     #err "iic_delay() not defined."
  71. #endif
  72. ////////////////////////////////////////////////////////////////////////////////
  73. // 主機釋放SCL總線,而且等待外部主機、設(shè)備釋放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(); // 外部函數(shù)。配置端口為開漏輸出。
  105. //        _IIC_ERROR_CODE = IIC_OK;        // 延后到iic_start()里面設(shè)置。
  106.         SCL_H();                // 釋放端口
  107.         SDA_H();
  108. }
  109. ////////////////////////////////////////////////////////////////////////////////
  110. // iic_start()函數(shù)在總線忙碌的情況下返回失敗值
  111. int iic_start(void)
  112. {
  113. // 其它主機可能處于<1>start、<2>restart、<3>stop、<4>讀寫SDA且SDA為高電平。
  114. // 有可能獨占總線或與別的主機同步占有總線,這是我們希望的最好結(jié)果。
  115. // 但iic協(xié)議是否有可能導(dǎo)致不同步地占有總線?
  116. //
  117. // 程序?qū)嶋H上應(yīng)該檢查總線在T_BUF期間是否被占用,保證起始標(biāo)志時序不被打斷,...
  118. // 但使用軟件查詢方式無法確切認(rèn)定在延時期間總線電平?jīng)]有被外部主機拉低,...
  119. // 本程序的缺陷有可能導(dǎo)致不同步地占有總線。。
  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. // 在傳輸完數(shù)據(jù)后可立即調(diào)用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. // 主機接收數(shù)據(jù)發(fā)送ack, 比較響應(yīng)位進行多主機仲裁,由于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. // 主機接收數(shù)據(jù)發(fā)送nack, 比較響應(yīng)位進行多主機仲裁
  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.                 // 應(yīng)該不用再發(fā)送時鐘直到nack周期結(jié)束?
  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. // 主機發(fā)送數(shù)據(jù)完等待從機響應(yīng)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. // 主機讀取數(shù)據(jù),不比較數(shù)據(jù)位進行多主機仲裁
  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函數(shù)和write函數(shù)在這里收發(fā)字節(jié)的第1位時,應(yīng)不斷檢測SCL高電平期間,...
  225. // SDA的電平有無變化以識別restart()或stop()標(biāo)志,但同時還要檢測SCL有無被外部拉低...
  226. // 在不使用中斷而采用純粹查詢手段的情況下,實現(xiàn)起來有困難,故不做判斷。
  227.         IIC_SCL_HOLD(T_HIGH, t);    // SCL SYNCHRONIZE
  228.         SCL_L();    // get the SCL bus
  229.     }

  230.     return d;
  231. }

  232. ////////////////////////////////////////////////////////////////////////////////
  233. // 主機發(fā)送數(shù)據(jù),比較數(shù)據(jù)位進行多主機仲裁
  234. // 主機在發(fā)送數(shù)據(jù)的第一位且第一位為1時,其它主機可能在SCL高電平期間發(fā)送...
  235. // restart()或是stop()標(biāo)志,也即電平0->1或是1->0,理論上程序應(yīng)該檢測這種...
  236. // 情況的發(fā)生,并停止發(fā)送數(shù)據(jù)而發(fā)送一樣的restart或是stop標(biāo)志(見P11)。
  237. // 為簡化程序,一旦遇到這種情況既轉(zhuǎn)化為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.                         // 理論上仲裁失敗就由其它主機接管控制器,程序可以停止產(chǎn)生SCL...
  256.                         // 在這里我們應(yīng)該可以直接返回 IIC_DARB_FAIL
  257.             // return IIC_DARB_FAIL;
  258.                         // 但我選擇發(fā)送0xff直到字節(jié)結(jié)束
  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. //  總線仲裁失敗可能是由于硬件錯誤或是多主機競爭。如果是硬件錯誤,應(yīng)繼續(xù)產(chǎn)生...
  308. //  時鐘到字節(jié)傳輸結(jié)束,然后釋放總線?不管怎樣,都不應(yīng)該再調(diào)用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. //  總線仲裁失敗可能是由于硬件錯誤或是多主機競爭。如果是硬件錯誤,應(yīng)繼續(xù)產(chǎn)生...
  373. //  時鐘到字節(jié)傳輸結(jié)束,然后釋放總線?不管怎樣,都不應(yīng)該再調(diào)用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,則不能再繼續(xù)往從機寫數(shù)據(jù)。本函數(shù)只寫一字節(jié)的數(shù)據(jù),故忽略。
  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. //  總線仲裁失敗可能是由于硬件錯誤或是多主機競爭。如果是硬件錯誤,應(yīng)繼續(xù)產(chǎn)生...
  426. //  時鐘到字節(jié)傳輸結(jié)束,然后釋放總線?不管怎樣,都不應(yīng)該再調(diào)用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();        // 最后一個字節(jié),忽略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. //  總線仲裁失敗可能是由于硬件錯誤或是多主機競爭。如果是硬件錯誤,應(yīng)繼續(xù)產(chǎn)生...
  490. //  時鐘到字節(jié)傳輸結(jié)束,然后釋放總線?不管怎樣,都不應(yīng)該再調(diào)用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. ////////////////////////////////////////////////////////////////////////////////
復(fù)制代碼

小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機教程網(wǎng)

快速回復(fù) 返回頂部 返回列表