• 让Pegasus天马座开发板实现超声波测距


    在完成《让Pegasus天马座开发板用上OLED屏》后,我觉得可以把超声波测距功能也在Pegasus天马座开发板上实现。于是在箱子里找到了,Grove - Ultrasonic Ranger 这一超声波测传感器。

    官方地址: https://wiki.seeedstudio.com/Grove-Ultrasonic_Ranger

     超声波传感器使用

    Grove - Ultrasonic Ranger测距模块一般配备有一个标准的Grove接口,使得接线变得简单。通常,这个模块有4个引脚:

    1. VCC:供电引脚,通常连接到5V。
    2. GND:地引脚,连接到微控制器的GND。
    3. SIG:信号引脚,用于传输数据。
    4. NC:未连接(No Connection),通常不用

     其中SIG信号引脚,是触发信号引脚,也是发送测距时长反馈引脚(高电平的时长为超声波往返距离所以使用的时间)。

    • 触发信号: 发送一个大于10us的脉冲信号到SIG引脚
    • 回复信号: 接收脉冲及得到高电平时间长度

    按照初中物理声音传播公式:

    S = \frac{\frac{}{}T * 340m/s}{2}

    我们只要测出公式中T所用的时间,即可得到距离。

    硬件接线

    为了方便捕获SIG信号高脉冲的宽度,为使用芯片的TIMER2的通道3作为输入捕获口。此通道对应的是PA3这个IO口。如下图所示:

    功能实现思路 

    1. 发送出发信号到SIG引脚
      配置PA3推挽输出口,并拉高10us的高电平
    2. 接收SIG引脚并检测高电平时间
      配置PA3为悬浮输入并将TIMER2配置为在通道3启用输入捕获模式。然后,先后捕获上升沿及下降沿时的时间点。通过这两个参数,即可计算得到超声波往返点时间。
    3. 通过得到时间后,套用物理公式即可得到转换后的距离
    4. 得到距离数据后,即可将数值显示到OLED上,实现动态测距

     main.c代码

    1. #include "config.h"
    2. #include "delay.h"
    3. #include "ultrasonic.h"
    4. #include "oled.h"
    5. #define LED_GPIO_PORT GPIOD
    6. #define LED_PIN GPIO_PIN_4
    7. #define BUTTON_GPIO_PORT GPIOD
    8. #define BUTTON_PIN GPIO_PIN_3
    9. static void init_gpio()
    10. {
    11. GPIO_Init(LED_GPIO_PORT, LED_PIN, GPIO_MODE_OUT_PP_LOW_FAST);
    12. GPIO_Init(BUTTON_GPIO_PORT, BUTTON_PIN, GPIO_MODE_IN_PU_NO_IT);
    13. OLED_Init();
    14. }
    15. void set_screen_color_and_display(uint8_t cfg)
    16. {
    17. // BIT0作为屏幕颜色控制 0: 正常显示, 1: 反色显示
    18. OLED_ColorTurn(cfg & 0x01);
    19. // BIT1作为屏幕颜色控制 0: 正常显示 1: 屏幕翻转显示
    20. OLED_DisplayTurn((cfg >> 1) & 0x01);
    21. }
    22. void main(void)
    23. {
    24. static uint8_t testBuffer[32] = {0};
    25. static uint8_t k = 0;
    26. bool isKeyDown = FALSE;
    27. uint8_t screenCfg = 0;
    28. uint8_t t = ' ';
    29. uint32_t range = 0;
    30. disable_interrupts();
    31. for (uint16_t i = 0; i < sizeof(testBuffer); i++)
    32. {
    33. testBuffer[i] = i;
    34. }
    35. enable_interrupts();
    36. LOG("Startup...\r\nss");
    37. DUMP(testBuffer,sizeof(testBuffer));
    38. init_gpio();
    39. OLED_ColorTurn(0);//0正常显示,1 反色显示
    40. OLED_DisplayTurn(0);//0正常显示 1 屏幕翻转显示
    41. while (1)
    42. {
    43. GPIO_WriteReverse(LED_GPIO_PORT,LED_PIN);
    44. isKeyDown = GPIO_ReadInputPin(BUTTON_GPIO_PORT,BUTTON_PIN) == RESET;
    45. range = ultrasonic_get_range_in_centimeters();
    46. LOG(" app_sdcc == %u key %s , range == %lu cm\r\n",k++, isKeyDown ? "down" : "up",range);
    47. if (isKeyDown == TRUE)
    48. {
    49. if (++screenCfg > 3)
    50. {
    51. screenCfg = 0;
    52. }
    53. set_screen_color_and_display(screenCfg);
    54. OLED_Clear();
    55. }
    56. OLED_ShowString(25,0,"Pegasus Board",8);
    57. OLED_ShowString(35,2,"2023/09/20",8);
    58. OLED_ShowString(20,4,isKeyDown ? "Button Down" : "Button Up ",8);
    59. OLED_ShowNum(90,4,range,3,8);
    60. OLED_ShowString(108,4," cm",8);
    61. OLED_ShowString(0,6,"ASCII:",8);
    62. OLED_ShowString(63,6,"CODE:",8);
    63. OLED_ShowChar(48,6,t,8);
    64. t++;
    65. if(t>'~')t=' ';
    66. OLED_ShowNum(103,6,t,3,8);
    67. delay(500);
    68. }
    69. }
    70. #ifdef USE_FULL_ASSERT
    71. /**
    72. * @brief Reports the name of the source file and the source line number
    73. * where the assert_param error has occurred.
    74. * @param file: pointer to the source file name
    75. * @param line: assert_param error line source number
    76. * @retval None
    77. */
    78. void assert_failed(uint8_t* file, uint32_t line)
    79. {
    80. /* User can add his own implementation to report the file name and line number,
    81. ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
    82. LOG("Wrong parameters value: file %s on line %d\r\n", file, line);
    83. /* Infinite loop */
    84. while (1)
    85. {
    86. }
    87. }
    88. #endif

    运行效果

    让Pegasus天马座开发板实现超声波测距

    工程分享

    工程下载路径

        

  • 相关阅读:
    拿到第一个用户并提权
    解析 FXML 表格的使用,图书列表功能的实现
    【LeetCode】Day148-目标和
    LLaVa大模型关键技术及在线演示
    R语言查看版本 R包查看版本
    uni-app 之 picker选择器
    django项目实战基于Python实现的在线办公系统
    flink对状态ttl进行单元测试
    Jenkins教程-20-常用插件-Parameterized Trigger
    1.git基础使用
  • 原文地址:https://blog.csdn.net/lan120576664/article/details/133143179