SR04.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /* include ---------------------------------------------------------*/
  2. #include "main.h"
  3. /* private define -------------------------------------------------*/
  4. // ECHO
  5. #define ECHO_GPIO_PORT GPIOA /* GPIO端口 */
  6. #define ECHO_GPIO_CLK RCC_APB2Periph_GPIOA /* GPIO端口时钟 */
  7. #define ECHO_GPIO_PIN GPIO_Pin_8 /* 连接到SCL时钟线的GPIO */
  8. #define ECHO_GPIO_TOGGLE ECHO_GPIO_PORT->BSRR = ((ECHO_GPIO_PORT->ODR & ECHO_GPIO_PIN) << 16) | (~ECHO_GPIO_PORT->ODR & ECHO_GPIO_PIN)
  9. #define ECHO_GPIO_SET ECHO_GPIO_PORT->BSRR = (uint32_t)ECHO_GPIO_PIN // 置1
  10. #define ECHO_GPIO_RESET ECHO_GPIO_PORT->BSRR = (uint32_t)ECHO_GPIO_PIN<<16 // 置0
  11. #define ECHO_GPIO_READ ((ECHO_GPIO_PORT->IDR & ECHO_GPIO_PIN) != 0)
  12. // TRIG
  13. #define TRIG_GPIO_PORT GPIOB /* GPIO端口 */
  14. #define TRIG_GPIO_CLK RCC_APB2Periph_GPIOB /* GPIO端口时钟 */
  15. #define TRIG_GPIO_PIN GPIO_Pin_15 /* 连接到SCL时钟线的GPIO */
  16. #define TRIG_GPIO_TOGGLE TRIG_GPIO_PORT->BSRR = ((TRIG_GPIO_PORT->ODR & TRIG_GPIO_PIN) << 16) | (~TRIG_GPIO_PORT->ODR & TRIG_GPIO_PIN)
  17. #define TRIG_GPIO_SET TRIG_GPIO_PORT->BSRR = (uint32_t)TRIG_GPIO_PIN // 置1
  18. #define TRIG_GPIO_RESET TRIG_GPIO_PORT->BSRR = (uint32_t)TRIG_GPIO_PIN<<16 // 置0
  19. #define TRIG_GPIO_READ ((TRIG_GPIO_PORT->IDR & TRIG_GPIO_PIN) != 0)
  20. /* private variables ----------------------------------------------*/
  21. /* private function prototypes ------------------------------------*/
  22. static void Init(void);
  23. static uint16_t Read(void);
  24. /* public variables -----------------------------------------------*/
  25. SR04_t SR04 =
  26. {
  27. Init,
  28. Read
  29. };
  30. /**
  31. * @brief SR04 Init
  32. * @param None
  33. * @retval None
  34. * @note None
  35. */
  36. static void Init(void)
  37. {
  38. GPIO_InitTypeDef GPIO_InitStructer;
  39. RCC_APB2PeriphClockCmd(TRIG_GPIO_CLK, ENABLE);
  40. /*TRIG触发信号*/
  41. GPIO_InitStructer.GPIO_Speed=GPIO_Speed_50MHz;
  42. GPIO_InitStructer.GPIO_Mode=GPIO_Mode_Out_PP;
  43. GPIO_InitStructer.GPIO_Pin=TRIG_GPIO_PIN;
  44. GPIO_Init(TRIG_GPIO_PORT, &GPIO_InitStructer);
  45. GPIO_ResetBits(TRIG_GPIO_PORT,TRIG_GPIO_PIN);
  46. RCC_APB2PeriphClockCmd(ECHO_GPIO_CLK, ENABLE);
  47. /*ECOH回响信号*/
  48. GPIO_InitStructer.GPIO_Mode=GPIO_Mode_IN_FLOATING;
  49. GPIO_InitStructer.GPIO_Pin=ECHO_GPIO_PIN;
  50. GPIO_Init(ECHO_GPIO_PORT, &GPIO_InitStructer);
  51. }
  52. /**
  53. * @brief Read IO口控制
  54. * @param None
  55. * @retval cm
  56. * @note None
  57. */
  58. static uint16_t Read(void)
  59. {
  60. uint32_t tim_time = 0;
  61. uint16_t length = 0;
  62. TRIG_GPIO_SET;
  63. DELAYClass.DelayUs(20);
  64. TRIG_GPIO_RESET;
  65. //等待返回信号
  66. while(ECHO_GPIO_READ == RESET)
  67. {
  68. tim_time++;
  69. DELAYClass.DelayUs(1);
  70. if(tim_time >= 10000)
  71. {
  72. return 10000;
  73. }
  74. }
  75. tim_time = 0;
  76. //等待返回信号
  77. while(ECHO_GPIO_READ)
  78. {
  79. tim_time++;
  80. DELAYClass.DelayUs(1);
  81. if(tim_time >= 10000)
  82. {
  83. return 10000;
  84. }
  85. }
  86. //关闭定时器
  87. length = (tim_time * 0.12)/2;
  88. return length;
  89. }