atk_ms6050_iic(7502).c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. #include "atk_ms6050_iic.h"
  2. //#include "./SYSTEM/delay/delay.h"
  3. void delay_us(uint32_t us) {
  4. uint32_t total_cycles = us * 72; // 假设1个指令周期为1/72MHz秒
  5. for (uint32_t i = 0; i < total_cycles; i++) {
  6. __NOP(); // 空操作指令,用于消耗一个指令周期
  7. }
  8. }
  9. /**
  10. * @brief IIC接口延时函数,用于控制IIC读写速度
  11. * @param 无
  12. * @retval 无
  13. */
  14. static inline void atk_ms6050_iic_delay(void)
  15. {
  16. delay_us(2);
  17. }
  18. /**
  19. * @brief 产生IIC起始信号
  20. * @param 无
  21. * @retval 无
  22. */
  23. void atk_ms6050_iic_start(void)
  24. {
  25. ATK_MS6050_IIC_SDA(1);
  26. ATK_MS6050_IIC_SCL(1);
  27. atk_ms6050_iic_delay();
  28. ATK_MS6050_IIC_SDA(0);
  29. atk_ms6050_iic_delay();
  30. ATK_MS6050_IIC_SCL(0);
  31. atk_ms6050_iic_delay();
  32. }
  33. /**
  34. * @brief 产生IIC停止信号
  35. * @param 无
  36. * @retval 无
  37. */
  38. void atk_ms6050_iic_stop(void)
  39. {
  40. ATK_MS6050_IIC_SDA(0);
  41. atk_ms6050_iic_delay();
  42. ATK_MS6050_IIC_SCL(1);
  43. atk_ms6050_iic_delay();
  44. ATK_MS6050_IIC_SDA(1);
  45. atk_ms6050_iic_delay();
  46. }
  47. /**
  48. * @brief 等待IIC应答信号
  49. * @param 无
  50. * @retval 0: 应答信号接收成功
  51. * 1: 应答信号接收失败
  52. */
  53. uint8_t atk_ms6050_iic_wait_ack(void)
  54. {
  55. uint8_t waittime = 0;
  56. uint8_t rack = 0;
  57. ATK_MS6050_IIC_SDA(1);
  58. atk_ms6050_iic_delay();
  59. ATK_MS6050_IIC_SCL(1);
  60. atk_ms6050_iic_delay();
  61. while (ATK_MS6050_IIC_READ_SDA())
  62. {
  63. waittime++;
  64. if (waittime > 250)
  65. {
  66. atk_ms6050_iic_stop();
  67. rack = 1;
  68. break;
  69. }
  70. }
  71. ATK_MS6050_IIC_SCL(0);
  72. atk_ms6050_iic_delay();
  73. return rack;
  74. }
  75. /**
  76. * @brief 产生ACK应答信号
  77. * @param 无
  78. * @retval 无
  79. */
  80. void atk_ms6050_iic_ack(void)
  81. {
  82. ATK_MS6050_IIC_SDA(0);
  83. atk_ms6050_iic_delay();
  84. ATK_MS6050_IIC_SCL(1);
  85. atk_ms6050_iic_delay();
  86. ATK_MS6050_IIC_SCL(0);
  87. atk_ms6050_iic_delay();
  88. ATK_MS6050_IIC_SDA(1);
  89. atk_ms6050_iic_delay();
  90. }
  91. /**
  92. * @brief 不产生ACK应答信号
  93. * @param 无
  94. * @retval 无
  95. */
  96. void atk_ms6050_iic_nack(void)
  97. {
  98. ATK_MS6050_IIC_SDA(1);
  99. atk_ms6050_iic_delay();
  100. ATK_MS6050_IIC_SCL(1);
  101. atk_ms6050_iic_delay();
  102. ATK_MS6050_IIC_SCL(0);
  103. atk_ms6050_iic_delay();
  104. }
  105. /**
  106. * @brief IIC发送一个字节
  107. * @param dat: 要发送的数据
  108. * @retval 无
  109. */
  110. void atk_ms6050_iic_send_byte(uint8_t dat)
  111. {
  112. uint8_t t;
  113. for (t=0; t<8; t++)
  114. {
  115. ATK_MS6050_IIC_SDA((dat & 0x80) >> 7);
  116. atk_ms6050_iic_delay();
  117. ATK_MS6050_IIC_SCL(1);
  118. atk_ms6050_iic_delay();
  119. ATK_MS6050_IIC_SCL(0);
  120. dat <<= 1;
  121. }
  122. ATK_MS6050_IIC_SDA(1);
  123. }
  124. /**
  125. * @brief IIC接收一个字节
  126. * @param ack: ack=1时,发送ack; ack=0时,发送nack
  127. * @retval 接收到的数据
  128. */
  129. uint8_t atk_ms6050_iic_read_byte(uint8_t ack)
  130. {
  131. uint8_t i;
  132. uint8_t dat = 0;
  133. for (i = 0; i < 8; i++ )
  134. {
  135. dat <<= 1;
  136. ATK_MS6050_IIC_SCL(1);
  137. atk_ms6050_iic_delay();
  138. if (ATK_MS6050_IIC_READ_SDA())
  139. {
  140. dat++;
  141. }
  142. ATK_MS6050_IIC_SCL(0);
  143. atk_ms6050_iic_delay();
  144. }
  145. if (ack == 0)
  146. {
  147. atk_ms6050_iic_nack();
  148. }
  149. else
  150. {
  151. atk_ms6050_iic_ack();
  152. }
  153. return dat;
  154. }
  155. /**
  156. * @brief 初始化IIC接口
  157. * @param 无
  158. * @retval 无
  159. */
  160. void atk_ms6050_iic_init(void)
  161. {
  162. GPIO_InitTypeDef gpio_init_struct = {0};
  163. /* 使能SCL、SDA引脚GPIO的时钟 */
  164. ATK_MS6050_IIC_SCL_GPIO_CLK_ENABLE();
  165. ATK_MS6050_IIC_SDA_GPIO_CLK_ENABLE();
  166. /* 初始化SCL引脚 */
  167. gpio_init_struct.Pin = ATK_MS6050_IIC_SCL_GPIO_PIN; /* SCL引脚 */
  168. gpio_init_struct.Mode = GPIO_MODE_OUTPUT_PP; /* 推挽输出 */
  169. gpio_init_struct.Pull = GPIO_PULLUP; /* 上拉 */
  170. gpio_init_struct.Speed = GPIO_SPEED_FREQ_HIGH; /* 高速 */
  171. HAL_GPIO_Init(ATK_MS6050_IIC_SCL_GPIO_PORT, &gpio_init_struct);
  172. /* 初始化SDA引脚 */
  173. gpio_init_struct.Pin = ATK_MS6050_IIC_SDA_GPIO_PIN; /* SDA引脚 */
  174. gpio_init_struct.Mode = GPIO_MODE_OUTPUT_OD; /* 开漏输出 */
  175. HAL_GPIO_Init(ATK_MS6050_IIC_SDA_GPIO_PORT, &gpio_init_struct);
  176. atk_ms6050_iic_stop();
  177. }