当前位置:   article > 正文

STM32基于hal库的智能小车—超声波避障_stm32超声波避障小车

stm32超声波避障小车

材料:

(1)stm32f407zgt6最小系统开发板

(2)l298n电机驱动模块1个

(3)四个电机

(4)超声波模块

一、组装

(1)L298N电机驱动模块与stm32开发板接线如下图:

watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA54K554Gv5Luj5biI,size_17,color_FFFFFF,t_70,g_se,x_16

(2)超声波模块接线:

watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA54K554Gv5Luj5biI,size_20,color_FFFFFF,t_70,g_se,x_16

说明: VCC接stm32开发板的3.3v~5v,GND接stm32开发板的GND,Trlg接单片机PB6 、Echo接单片机PA0.

 

二、主要程序

1、STM32CUBEMX配置如下:

(1)引脚配置:

watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA54K554Gv5Luj5biI,size_20,color_FFFFFF,t_70,g_se,x_16

 

 说明:

1)定义2个电机的引脚,都是GPIOB

2)motor11和motor12分别为电机(1)的两个引脚

3)motor21和motor22分别为电机(2)的两个引脚

(2)配置RCC时钟:

watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA54K554Gv5Luj5biI,size_20,color_FFFFFF,t_70,g_se,x_16

watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA54K554Gv5Luj5biI,size_20,color_FFFFFF,t_70,g_se,x_16

 (3) 时钟的配置:

watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA54K554Gv5Luj5biI,size_20,color_FFFFFF,t_70,g_se,x_16

(4)UART配置: 

1)选择所需UART
2)选择Mode为异步通讯方式(常用)
3)设置基础参数:波特率为115200 Bits/s;传输数据长度为8 Bit;奇偶检验无;停止位1;接收和发送都使能 。
注意 CubeMX默认打开的引脚确实为最常用的引脚,但有时与电路板并不相符。

watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA54K554Gv5Luj5biI,size_20,color_FFFFFF,t_70,g_se,x_16

 (4)预分频、分频和占空比配置:

TIM4配置 :watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA54K554Gv5Luj5biI,size_20,color_FFFFFF,t_70,g_se,x_16

watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA54K554Gv5Luj5biI,size_20,color_FFFFFF,t_70,g_se,x_16

 TIM5配置:

watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA54K554Gv5Luj5biI,size_20,color_FFFFFF,t_70,g_se,x_16

 

 三、程序

main.c

  1. /* USER CODE BEGIN Header */
  2. /**
  3. ******************************************************************************
  4. * @file : main.c
  5. * @brief : Main program body
  6. ******************************************************************************
  7. * @attention
  8. *
  9. * <h2><center>&copy; Copyright (c) 2021 STMicroelectronics.
  10. * All rights reserved.</center></h2>
  11. *
  12. * This software component is licensed by ST under BSD 3-Clause license,
  13. * the "License"; You may not use this file except in compliance with the
  14. * License. You may obtain a copy of the License at:
  15. * opensource.org/licenses/BSD-3-Clause
  16. *
  17. ******************************************************************************
  18. */
  19. /* USER CODE END Header */
  20. /* Includes ------------------------------------------------------------------*/
  21. #include "main.h"
  22. #include "tim.h"
  23. #include "usart.h"
  24. #include "gpio.h"
  25. /* Private includes ----------------------------------------------------------*/
  26. /* USER CODE BEGIN Includes */
  27. #include <stdio.h>
  28. #include <string.h>
  29. #include "motor.h"
  30. /* USER CODE END Includes */
  31. /* Private typedef -----------------------------------------------------------*/
  32. /* USER CODE BEGIN PTD */
  33. /* USER CODE END PTD */
  34. /* Private define ------------------------------------------------------------*/
  35. /* USER CODE BEGIN PD */
  36. /* USER CODE END PD */
  37. /* Private macro -------------------------------------------------------------*/
  38. /* USER CODE BEGIN PM */
  39. /* USER CODE END PM */
  40. /* Private variables ---------------------------------------------------------*/
  41. /* USER CODE BEGIN PV */
  42. uint8_t isCapUp=1;
  43. uint16_t valueUp=0;
  44. uint16_t valueDown=0;
  45. uint16_t width=0;
  46. uint8_t updateCount=0;
  47. uint8_t echoFlag=0;
  48. /* USER CODE END PV */
  49. /* Private function prototypes -----------------------------------------------*/
  50. void SystemClock_Config(void);
  51. /* USER CODE BEGIN PFP */
  52. /* USER CODE END PFP */
  53. /* Private user code ---------------------------------------------------------*/
  54. /* USER CODE BEGIN 0 */
  55. void HAL_TIM_IC_CaptureCallback(TIM_HandleTypeDef *htim)
  56. {
  57. if(isCapUp )
  58. {
  59. valueUp =HAL_TIM_ReadCapturedValue (htim,TIM_CHANNEL_1 );
  60. __HAL_TIM_SET_CAPTUREPOLARITY (htim,TIM_CHANNEL_1 ,TIM_ICPOLARITY_FALLING );
  61. isCapUp =0;
  62. }
  63. else
  64. {
  65. valueDown = HAL_TIM_ReadCapturedValue (htim,TIM_CHANNEL_1 );
  66. __HAL_TIM_SET_CAPTUREPOLARITY (htim,TIM_CHANNEL_1 ,TIM_ICPOLARITY_RISING );
  67. isCapUp =1;
  68. width =valueDown + updateCount*65535 - valueUp ;
  69. echoFlag=1;
  70. }
  71. }
  72. void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
  73. {
  74. updateCount++;
  75. }
  76. uint32_t vidth2dist(uint16_t width)
  77. {
  78. return width*75/1000;//测距离公式
  79. }
  80. /* USER CODE END 0 */
  81. /**
  82. * @brief The application entry point.
  83. * @retval int
  84. */
  85. int main(void)
  86. {
  87. /* USER CODE BEGIN 1 */
  88. uint32_t dist=0;
  89. uint32_t tick=0;
  90. char printString[64]={0};
  91. /* USER CODE END 1 */
  92. /* MCU Configuration--------------------------------------------------------*/
  93. /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  94. HAL_Init();
  95. /* USER CODE BEGIN Init */
  96. /* USER CODE END Init */
  97. /* Configure the system clock */
  98. SystemClock_Config();
  99. /* USER CODE BEGIN SysInit */
  100. /* USER CODE END SysInit */
  101. /* Initialize all configured peripherals */
  102. MX_GPIO_Init();
  103. MX_TIM4_Init();
  104. MX_TIM5_Init();
  105. MX_USART1_UART_Init();
  106. /* USER CODE BEGIN 2 */
  107. /* USER CODE END 2 */
  108. /* Infinite loop */
  109. /* USER CODE BEGIN WHILE */
  110. HAL_UART_Transmit(&huart1,"hello\r\n",7,HAL_MAX_DELAY );
  111. HAL_TIM_PWM_Start(&htim4 ,TIM_CHANNEL_1 );
  112. HAL_TIM_IC_Start_IT(&htim5,TIM_CHANNEL_1 );
  113. while (1)
  114. {
  115. if(echoFlag )
  116. {
  117. dist =vidth2dist(width);
  118. sprintf (printString,"%u\r\n",dist);
  119. HAL_UART_Transmit(&huart1 ,printString ,strlen (printString ),HAL_MAX_DELAY );
  120. echoFlag =0;
  121. }
  122. /小于25厘米
  123. if(dist<250)
  124. {
  125. car_go_ahead();//停止
  126. HAL_Delay (300);
  127. car_go_after();//后退
  128. HAL_Delay (250);
  129. car_go_left();//左转
  130. HAL_Delay (250);
  131. }
  132. else
  133. {
  134. car_go_straight();//直走
  135. }
  136. /* USER CODE END WHILE */
  137. /* USER CODE BEGIN 3 */
  138. }
  139. /* USER CODE END 3 */
  140. }
  141. /**
  142. * @brief System Clock Configuration
  143. * @retval None
  144. */
  145. void SystemClock_Config(void)
  146. {
  147. RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  148. RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  149. /** Configure the main internal regulator output voltage
  150. */
  151. __HAL_RCC_PWR_CLK_ENABLE();
  152. __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
  153. /** Initializes the RCC Oscillators according to the specified parameters
  154. * in the RCC_OscInitTypeDef structure.
  155. */
  156. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  157. RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  158. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  159. RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  160. RCC_OscInitStruct.PLL.PLLM = 4;
  161. RCC_OscInitStruct.PLL.PLLN = 168;
  162. RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
  163. RCC_OscInitStruct.PLL.PLLQ = 4;
  164. if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  165. {
  166. Error_Handler();
  167. }
  168. /** Initializes the CPU, AHB and APB buses clocks
  169. */
  170. RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  171. |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  172. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  173. RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  174. RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;
  175. RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;
  176. if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5) != HAL_OK)
  177. {
  178. Error_Handler();
  179. }
  180. }
  181. /* USER CODE BEGIN 4 */
  182. /* USER CODE END 4 */
  183. /**
  184. * @brief This function is executed in case of error occurrence.
  185. * @retval None
  186. */
  187. void Error_Handler(void)
  188. {
  189. /* USER CODE BEGIN Error_Handler_Debug */
  190. /* User can add his own implementation to report the HAL error return state */
  191. __disable_irq();
  192. while (1)
  193. {
  194. }
  195. /* USER CODE END Error_Handler_Debug */
  196. }
  197. #ifdef USE_FULL_ASSERT
  198. /**
  199. * @brief Reports the name of the source file and the source line number
  200. * where the assert_param error has occurred.
  201. * @param file: pointer to the source file name
  202. * @param line: assert_param error line source number
  203. * @retval None
  204. */
  205. void assert_failed(uint8_t *file, uint32_t line)
  206. {
  207. /* USER CODE BEGIN 6 */
  208. /* User can add his own implementation to report the file name and line number,
  209. ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  210. /* USER CODE END 6 */
  211. }
  212. #endif /* USE_FULL_ASSERT */
  213. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/

电机程序:

motor.c 

  1. #include "motor.h"
  2. //前进
  3. void car_go_straight(void)
  4. {
  5. HAL_GPIO_WritePin(motor11_GPIO_Port,motor11_Pin,GPIO_PIN_SET);
  6. HAL_GPIO_WritePin(motor12_GPIO_Port,motor12_Pin,GPIO_PIN_RESET);
  7. HAL_GPIO_WritePin(motor21_GPIO_Port,motor21_Pin,GPIO_PIN_SET);
  8. HAL_GPIO_WritePin(motor22_GPIO_Port,motor22_Pin,GPIO_PIN_RESET);
  9. }
  10. //右转
  11. void car_go_right(void)
  12. {
  13. HAL_GPIO_WritePin(motor11_GPIO_Port,motor11_Pin,GPIO_PIN_SET);
  14. HAL_GPIO_WritePin(motor12_GPIO_Port,motor12_Pin,GPIO_PIN_RESET);
  15. HAL_GPIO_WritePin(motor21_GPIO_Port,motor21_Pin,GPIO_PIN_RESET);
  16. HAL_GPIO_WritePin(motor22_GPIO_Port,motor22_Pin,GPIO_PIN_SET);
  17. }
  18. //左转
  19. void car_go_left(void)
  20. {
  21. HAL_GPIO_WritePin(motor11_GPIO_Port,motor11_Pin,GPIO_PIN_RESET);
  22. HAL_GPIO_WritePin(motor12_GPIO_Port,motor12_Pin,GPIO_PIN_SET);
  23. HAL_GPIO_WritePin(motor21_GPIO_Port,motor21_Pin,GPIO_PIN_SET);
  24. HAL_GPIO_WritePin(motor22_GPIO_Port,motor22_Pin,GPIO_PIN_RESET);
  25. }
  26. //停止
  27. void car_go_ahead(void)
  28. {
  29. HAL_GPIO_WritePin(motor11_GPIO_Port,motor11_Pin,GPIO_PIN_RESET);
  30. HAL_GPIO_WritePin(motor12_GPIO_Port,motor12_Pin,GPIO_PIN_RESET);
  31. HAL_GPIO_WritePin(motor21_GPIO_Port,motor21_Pin,GPIO_PIN_RESET);
  32. HAL_GPIO_WritePin(motor22_GPIO_Port,motor22_Pin,GPIO_PIN_RESET);
  33. }
  34. //后退
  35. void car_go_after(void)
  36. {
  37. HAL_GPIO_WritePin(motor11_GPIO_Port,motor11_Pin,GPIO_PIN_RESET);
  38. HAL_GPIO_WritePin(motor12_GPIO_Port,motor12_Pin,GPIO_PIN_SET);
  39. HAL_GPIO_WritePin(motor21_GPIO_Port,motor21_Pin,GPIO_PIN_RESET);
  40. HAL_GPIO_WritePin(motor22_GPIO_Port,motor22_Pin,GPIO_PIN_SET);
  41. }

 motor.h

  1. #ifndef __MOTOR_H_
  2. #define __MOTOR_H_
  3. #include "main.h"
  4. void car_go_straight(void);
  5. void car_go_right(void);
  6. void car_go_left(void);
  7. void car_go_ahead(void);
  8. void car_go_after(void);
  9. #endif

 

 


————————————————
版权声明:本文为CSDN博主「点灯代师」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/weixin_65866701/article/details/122180377

 

 

 

 

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/article/detail/46297
推荐阅读
相关标签
  

闽ICP备14008679号