当前位置:   article > 正文

STM32之智能小车,手把手从0到1,模块化编程_智能小车编程

智能小车编程

小车介绍

本博文将会从0到1实现一个智能小车,该小车实现功能:1. 摇头避障模式、2. 跟随模式、3. 循迹模式、4. 小车测速并显示在OLED屏幕、5. 语音控制小车等等。

硬件组成

STM32F103开发板、小车套件、L9110S电机模块、超声波模块(HC-SR04)、sg90舵机、测速模块、循迹模块、红外避障模块等等(下面有详细介绍)

模块化编程

小车采用模块化编程循序渐进,即每实现新的功能小车需要复制前一种功能小车的文件夹来进行修改,否则会因为丢失某些文件导致小车功能缺失。

移动小车

让小车动起来。

硬件组成

L9110S电机模块

L9110S电机模块用来驱动两个电机,如果需要控制四个电机则需要两个L9110S电机模块。

  • 当B-1A为高电平,B-2A为低电平时,电机反转或正转。

  • 当B-1A为低电平,B-2A为高电平时,电机正转或反转。

  • 当B-1A为低电平,B-2A为低电平时,电机不转。

  • A-1A、A-1B同理。

  • 电机的正转和反转与跟电机的接线不同而不同,注意自己调试。

L9110S电机模块与STM32F103板子接线

  • B-1A <-> PB0

  • B-2A <-> PA0

  • A-1A <-> PB2

  • A-1B <-> PA1

STM32CubeMX相关配置

配置SYS

配置RCC

配置GPIO

配置PA0引脚、PA1引脚、PB0引脚、PB2引脚输出高电平。

文件编写

添加文件car.c

  1. #include "gpio.h"
  2. void car_goForward()
  3. {
  4. HAL_GPIO_WritePin(GPIOB, GPIO_PIN_0, GPIO_PIN_SET);
  5. HAL_GPIO_WritePin(GPIOA, GPIO_PIN_0, GPIO_PIN_RESET);
  6. HAL_GPIO_WritePin(GPIOB, GPIO_PIN_2, GPIO_PIN_SET);
  7. HAL_GPIO_WritePin(GPIOA, GPIO_PIN_1, GPIO_PIN_RESET);
  8. }
  9. void car_goBack()
  10. {
  11. HAL_GPIO_WritePin(GPIOB, GPIO_PIN_0, GPIO_PIN_RESET);
  12. HAL_GPIO_WritePin(GPIOA, GPIO_PIN_0, GPIO_PIN_SET);
  13. HAL_GPIO_WritePin(GPIOB, GPIO_PIN_2, GPIO_PIN_RESET);
  14. HAL_GPIO_WritePin(GPIOA, GPIO_PIN_1, GPIO_PIN_SET);
  15. }
  16. void car_goStop()
  17. {
  18. HAL_GPIO_WritePin(GPIOB, GPIO_PIN_0, GPIO_PIN_SET);
  19. HAL_GPIO_WritePin(GPIOA, GPIO_PIN_0, GPIO_PIN_SET);
  20. HAL_GPIO_WritePin(GPIOB, GPIO_PIN_2, GPIO_PIN_SET);
  21. HAL_GPIO_WritePin(GPIOA, GPIO_PIN_1, GPIO_PIN_SET);
  22. }
  23. void car_goRight()
  24. {
  25. HAL_GPIO_WritePin(GPIOB, GPIO_PIN_0, GPIO_PIN_SET);
  26. HAL_GPIO_WritePin(GPIOA, GPIO_PIN_0, GPIO_PIN_RESET);
  27. HAL_GPIO_WritePin(GPIOB, GPIO_PIN_2, GPIO_PIN_SET);
  28. HAL_GPIO_WritePin(GPIOA, GPIO_PIN_1, GPIO_PIN_SET);
  29. }
  30. void car_goLeft()
  31. {
  32. HAL_GPIO_WritePin(GPIOB, GPIO_PIN_0, GPIO_PIN_SET);
  33. HAL_GPIO_WritePin(GPIOA, GPIO_PIN_0, GPIO_PIN_SET);
  34. HAL_GPIO_WritePin(GPIOB, GPIO_PIN_2, GPIO_PIN_SET);
  35. HAL_GPIO_WritePin(GPIOA, GPIO_PIN_1, GPIO_PIN_RESET);
  36. }

添加文件car.h

  1. void car_goForward(void);
  2. void car_goBack(void);
  3. void car_goStop(void);
  4. void car_goRight(void);
  5. void car_goLeft(void);

main.c文件编写

  1. /* USER CODE BEGIN Header */
  2. /**
  3. ******************************************************************************
  4. * @file : main.c
  5. * @brief : Main program body
  6. ******************************************************************************
  7. * @attention
  8. *
  9. * Copyright (c) 2023 STMicroelectronics.
  10. * All rights reserved.
  11. *
  12. * This software is licensed under terms that can be found in the LICENSE file
  13. * in the root directory of this software component.
  14. * If no LICENSE file comes with this software, it is provided AS-IS.
  15. *
  16. ******************************************************************************
  17. */
  18. /* USER CODE END Header */
  19. /* Includes ------------------------------------------------------------------*/
  20. #include "main.h"
  21. #include "gpio.h"
  22. #include "car.h"
  23. /* Private includes ----------------------------------------------------------*/
  24. /* USER CODE BEGIN Includes */
  25. /* USER CODE END Includes */
  26. /* Private typedef -----------------------------------------------------------*/
  27. /* USER CODE BEGIN PTD */
  28. /* USER CODE END PTD */
  29. /* Private define ------------------------------------------------------------*/
  30. /* USER CODE BEGIN PD */
  31. /* USER CODE END PD */
  32. /* Private macro -------------------------------------------------------------*/
  33. /* USER CODE BEGIN PM */
  34. /* USER CODE END PM */
  35. /* Private variables ---------------------------------------------------------*/
  36. /* USER CODE BEGIN PV */
  37. /* USER CODE END PV */
  38. /* Private function prototypes -----------------------------------------------*/
  39. void SystemClock_Config(void);
  40. /* USER CODE BEGIN PFP */
  41. /* USER CODE END PFP */
  42. /* Private user code ---------------------------------------------------------*/
  43. /* USER CODE BEGIN 0 */
  44. /* USER CODE END 0 */
  45. /**
  46. * @brief The application entry point.
  47. * @retval int
  48. */
  49. int main(void)
  50. {
  51. /* USER CODE BEGIN 1 */
  52. /* USER CODE END 1 */
  53. /* MCU Configuration--------------------------------------------------------*/
  54. /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  55. HAL_Init();
  56. /* USER CODE BEGIN Init */
  57. /* USER CODE END Init */
  58. /* Configure the system clock */
  59. SystemClock_Config();
  60. /* USER CODE BEGIN SysInit */
  61. /* USER CODE END SysInit */
  62. /* Initialize all configured peripherals */
  63. MX_GPIO_Init();
  64. /* USER CODE BEGIN 2 */
  65. /* USER CODE END 2 */
  66. /* Infinite loop */
  67. /* USER CODE BEGIN WHILE */
  68. while (1)
  69. {
  70. /* USER CODE END WHILE */
  71. car_goForward();
  72. HAL_Delay(1000);
  73. car_goStop();
  74. HAL_Delay(1000);
  75. /* USER CODE BEGIN 3 */
  76. }
  77. /* USER CODE END 3 */
  78. }
  79. /**
  80. * @brief System Clock Configuration
  81. * @retval None
  82. */
  83. void SystemClock_Config(void)
  84. {
  85. RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  86. RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  87. /** Initializes the RCC Oscillators according to the specified parameters
  88. * in the RCC_OscInitTypeDef structure.
  89. */
  90. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  91. RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  92. RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
  93. RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  94. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  95. RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  96. RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;
  97. if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  98. {
  99. Error_Handler();
  100. }
  101. /** Initializes the CPU, AHB and APB buses clocks
  102. */
  103. RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2;
  104. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  105. RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  106. RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
  107. RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  108. if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
  109. {
  110. Error_Handler();
  111. }
  112. }
  113. /* USER CODE BEGIN 4 */
  114. /* USER CODE END 4 */
  115. /**
  116. * @brief This function is executed in case of error occurrence.
  117. * @retval None
  118. */
  119. void Error_Handler(void)
  120. {
  121. /* USER CODE BEGIN Error_Handler_Debug */
  122. /* User can add his own implementation to report the HAL error return state */
  123. __disable_irq();
  124. while (1)
  125. {
  126. }
  127. /* USER CODE END Error_Handler_Debug */
  128. }
  129. #ifdef USE_FULL_ASSERT
  130. /**
  131. * @brief Reports the name of the source file and the source line number
  132. * where the assert_param error has occurred.
  133. * @param file: pointer to the source file name
  134. * @param line: assert_param error line source number
  135. * @retval None
  136. */
  137. void assert_failed(uint8_t *file, uint32_t line)
  138. {
  139. /* USER CODE BEGIN 6 */
  140. /* User can add his own implementation to report the file name and line number,
  141. ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  142. /* USER CODE END 6 */
  143. }
  144. #endif /* USE_FULL_ASSERT */

USART小车

利用USART接收串口信息,实现蓝牙遥控小车等功能。

STM32CubeMX相关配置

配置USART1

配置NVIC

使用Micro库

只要映射了printf用来发送数据去串口都要使用这个库。

文件编写

修改文件usart.c

  1. /* USER CODE BEGIN Header */
  2. /**
  3. ******************************************************************************
  4. * @file usart.c
  5. * @brief This file provides code for the configuration
  6. * of the USART instances.
  7. ******************************************************************************
  8. * @attention
  9. *
  10. * Copyright (c) 2023 STMicroelectronics.
  11. * All rights reserved.
  12. *
  13. * This software is licensed under terms that can be found in the LICENSE file
  14. * in the root directory of this software component.
  15. * If no LICENSE file comes with this software, it is provided AS-IS.
  16. *
  17. ******************************************************************************
  18. */
  19. /* USER CODE END Header */
  20. /* Includes ------------------------------------------------------------------*/
  21. #include "usart.h"
  22. /* USER CODE BEGIN 0 */
  23. #include <stdio.h>
  24. #include <string.h>
  25. #include "car.h"
  26. #define USART_REC_LEN 200
  27. // 串口接收缓存(1字节)
  28. uint8_t buf = 0;
  29. uint8_t UART1_RX_Buffer[USART_REC_LEN]; // 接收缓冲,串口接收的数据存放地点
  30. // 串口接收状态,16位
  31. uint16_t UART1_RX_STA = 0;
  32. // bit15: 如果是1表示接收完成
  33. // bit14: 如果是1表示接收到回车(0x0d)
  34. // bit13~bit0: 接收到的有效字节数目
  35. /* USER CODE END 0 */
  36. UART_HandleTypeDef huart1;
  37. /* USART1 init function */
  38. void MX_USART1_UART_Init(void)
  39. {
  40. /* USER CODE BEGIN USART1_Init 0 */
  41. /* USER CODE END USART1_Init 0 */
  42. /* USER CODE BEGIN USART1_Init 1 */
  43. /* USER CODE END USART1_Init 1 */
  44. huart1.Instance = USART1;
  45. huart1.Init.BaudRate = 115200;
  46. huart1.Init.WordLength = UART_WORDLENGTH_8B;
  47. huart1.Init.StopBits = UART_STOPBITS_1;
  48. huart1.Init.Parity = UART_PARITY_NONE;
  49. huart1.Init.Mode = UART_MODE_TX_RX;
  50. huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  51. huart1.Init.OverSampling = UART_OVERSAMPLING_16;
  52. if (HAL_UART_Init(&huart1) != HAL_OK)
  53. {
  54. Error_Handler();
  55. }
  56. /* USER CODE BEGIN USART1_Init 2 */
  57. /* 开启串口1的接收中断 */
  58. HAL_UART_Receive_IT(&huart1, &buf, 1); /* 每接收一个串口数据调用一次串口接收完成回调函数 */
  59. /* USER CODE END USART1_Init 2 */
  60. }
  61. void HAL_UART_MspInit(UART_HandleTypeDef *uartHandle)
  62. {
  63. GPIO_InitTypeDef GPIO_InitStruct = {0};
  64. if (uartHandle->Instance == USART1)
  65. {
  66. /* USER CODE BEGIN USART1_MspInit 0 */
  67. /* USER CODE END USART1_MspInit 0 */
  68. /* USART1 clock enable */
  69. __HAL_RCC_USART1_CLK_ENABLE();
  70. __HAL_RCC_GPIOA_CLK_ENABLE();
  71. /**USART1 GPIO Configuration
  72. PA9 ------> USART1_TX
  73. PA10 ------> USART1_RX
  74. */
  75. GPIO_InitStruct.Pin = GPIO_PIN_9;
  76. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  77. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
  78. HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  79. GPIO_InitStruct.Pin = GPIO_PIN_10;
  80. GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
  81. GPIO_InitStruct.Pull = GPIO_NOPULL;
  82. HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  83. /* USART1 interrupt Init */
  84. HAL_NVIC_SetPriority(USART1_IRQn, 0, 0);
  85. HAL_NVIC_EnableIRQ(USART1_IRQn);
  86. /* USER CODE BEGIN USART1_MspInit 1 */
  87. /* USER CODE END USART1_MspInit 1 */
  88. }
  89. }
  90. void HAL_UART_MspDeInit(UART_HandleTypeDef *uartHandle)
  91. {
  92. if (uartHandle->Instance == USART1)
  93. {
  94. /* USER CODE BEGIN USART1_MspDeInit 0 */
  95. /* USER CODE END USART1_MspDeInit 0 */
  96. /* Peripheral clock disable */
  97. __HAL_RCC_USART1_CLK_DISABLE();
  98. /**USART1 GPIO Configuration
  99. PA9 ------> USART1_TX
  100. PA10 ------> USART1_RX
  101. */
  102. HAL_GPIO_DeInit(GPIOA, GPIO_PIN_9 | GPIO_PIN_10);
  103. /* USART1 interrupt Deinit */
  104. HAL_NVIC_DisableIRQ(USART1_IRQn);
  105. /* USER CODE BEGIN USART1_MspDeInit 1 */
  106. /* USER CODE END USART1_MspDeInit 1 */
  107. }
  108. }
  109. /* USER CODE BEGIN 1 */
  110. /* 重写stdio.h文件中的prinft()里的fputc()函数 */
  111. int fputc(int my_data, FILE *p)
  112. {
  113. unsigned char temp = my_data;
  114. // 改写后,使用printf()函数会将数据通过串口一发送出去
  115. HAL_UART_Transmit(&huart1, &temp, 1, 0xffff); // 0xfffff为最大超时时间
  116. return my_data;
  117. }
  118. /* 串口接收完成回调函数 */
  119. void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
  120. {
  121. // 判断中断是哪个串口触发的
  122. if (huart->Instance == USART1)
  123. {
  124. // 判断接收是否完成,即判断UART1_RX_STA的bit15是否为1
  125. if (!(UART1_RX_STA & 0x8000))
  126. { // 如果没接收完成就进入接收流程
  127. // 判断是否接收到回车0x0d
  128. if (UART1_RX_STA & 0x4000)
  129. {
  130. // 判断是否接收到换行0x0a
  131. if (buf == 0x0a)
  132. {
  133. // 如果回车和换行都接收到了,则表示接收完成,即把bit15拉高
  134. UART1_RX_STA |= 0x8000;
  135. }
  136. else
  137. { // 如果接收到回车0x0d没有接收到换行0x0a
  138. // 则认为接收错误,重新开始接收
  139. UART1_RX_STA = 0;
  140. }
  141. }
  142. else
  143. { // 如果没有接收到回车0x0d
  144. // 则判断收到的这个字符是否是回车0x0d
  145. if (buf == 0x0d)
  146. {
  147. // 如果这个字符是回车,则将将bit14拉高,表示接收到回车
  148. UART1_RX_STA |= 0x4000;
  149. }
  150. else
  151. { // 如果不是回车
  152. // 则将这个字符存放到缓存数组中
  153. UART1_RX_Buffer[UART1_RX_STA & 0x3ffff] = buf;
  154. UART1_RX_STA++;
  155. // 如果接收数据大于UART1_REC_LEN(200字节),则重新开始接收
  156. if (UART1_RX_STA > USART_REC_LEN - 1)
  157. {
  158. UART1_RX_STA = 0;
  159. }
  160. }
  161. }
  162. }
  163. // 如果接收完成则重新开启串口1的接收中断
  164. HAL_UART_Receive_IT(&huart1, &buf, 1);
  165. }
  166. }
  167. /* 对串口接收数据的处理 */
  168. void usart1_receive_data_handle()
  169. {
  170. /* 判断判断串口是否接收完成 */
  171. if (UART1_RX_STA & 0x8000)
  172. {
  173. printf("接收完成\r\n");
  174. // 串口接收完数据后,对串口数据进行处理
  175. if (!strcmp((const char *)UART1_RX_Buffer, "forward"))
  176. {
  177. printf("前进\r\n");
  178. car_goForward();
  179. }
  180. else if (!strcmp((const char *)UART1_RX_Buffer, "back"))
  181. {
  182. printf("后退\r\n");
  183. car_goBack();
  184. }
  185. else if (!strcmp((const char *)UART1_RX_Buffer, "left"))
  186. {
  187. printf("左转\r\n");
  188. car_goLeft();
  189. }
  190. else if (!strcmp((const char *)UART1_RX_Buffer, "right"))
  191. {
  192. printf("右转\r\n");
  193. car_goRight();
  194. }
  195. else if (!strcmp((const char *)UART1_RX_Buffer, "stop"))
  196. {
  197. printf("停止\r\n");
  198. car_goStop();
  199. }
  200. // 接收到其他数据,进行报错
  201. else
  202. {
  203. if (UART1_RX_Buffer[0] != '\0')
  204. {
  205. printf("%s\r\n", "输入错误,请重新输入");
  206. }
  207. }
  208. // 换行,重新开始下一次接收
  209. memset(UART1_RX_Buffer, 0, USART_REC_LEN);
  210. // printf("\r\n");
  211. UART1_RX_STA = 0;
  212. }
  213. }
  214. /* USER CODE END 1 */

修改文件usart.h

  1. /* USER CODE BEGIN Header */
  2. /**
  3. ******************************************************************************
  4. * @file usart.h
  5. * @brief This file contains all the function prototypes for
  6. * the usart.c file
  7. ******************************************************************************
  8. * @attention
  9. *
  10. * Copyright (c) 2023 STMicroelectronics.
  11. * All rights reserved.
  12. *
  13. * This software is licensed under terms that can be found in the LICENSE file
  14. * in the root directory of this software component.
  15. * If no LICENSE file comes with this software, it is provided AS-IS.
  16. *
  17. ******************************************************************************
  18. */
  19. /* USER CODE END Header */
  20. /* Define to prevent recursive inclusion -------------------------------------*/
  21. #ifndef __USART_H__
  22. #define __USART_H__
  23. #ifdef __cplusplus
  24. extern "C"
  25. {
  26. #endif
  27. /* Includes ------------------------------------------------------------------*/
  28. #include "main.h"
  29. /* USER CODE BEGIN Includes */
  30. /* USER CODE END Includes */
  31. extern UART_HandleTypeDef huart1;
  32. /* USER CODE BEGIN Private defines */
  33. /* USER CODE END Private defines */
  34. void MX_USART1_UART_Init(void);
  35. /* USER CODE BEGIN Prototypes */
  36. void usart1_receive_data_handle(void);
  37. /* USER CODE END Prototypes */
  38. #ifdef __cplusplus
  39. }
  40. #endif
  41. #endif /* __USART_H__ */

main.c文件编写

  1. /* USER CODE BEGIN Header */
  2. /**
  3. ******************************************************************************
  4. * @file : main.c
  5. * @brief : Main program body
  6. ******************************************************************************
  7. * @attention
  8. *
  9. * Copyright (c) 2023 STMicroelectronics.
  10. * All rights reserved.
  11. *
  12. * This software is licensed under terms that can be found in the LICENSE file
  13. * in the root directory of this software component.
  14. * If no LICENSE file comes with this software, it is provided AS-IS.
  15. *
  16. ******************************************************************************
  17. */
  18. /* USER CODE END Header */
  19. /* Includes ------------------------------------------------------------------*/
  20. #include "main.h"
  21. #include "usart.h"
  22. #include "gpio.h"
  23. /* Private includes ----------------------------------------------------------*/
  24. /* USER CODE BEGIN Includes */
  25. #include <stdio.h>
  26. #include "car.h"
  27. /* USER CODE END Includes */
  28. /* Private typedef -----------------------------------------------------------*/
  29. /* USER CODE BEGIN PTD */
  30. /* USER CODE END PTD */
  31. /* Private define ------------------------------------------------------------*/
  32. /* USER CODE BEGIN PD */
  33. /* USER CODE END PD */
  34. /* Private macro -------------------------------------------------------------*/
  35. /* USER CODE BEGIN PM */
  36. /* USER CODE END PM */
  37. /* Private variables ---------------------------------------------------------*/
  38. /* USER CODE BEGIN PV */
  39. /* USER CODE END PV */
  40. /* Private function prototypes -----------------------------------------------*/
  41. void SystemClock_Config(void);
  42. /* USER CODE BEGIN PFP */
  43. /* USER CODE END PFP */
  44. /* Private user code ---------------------------------------------------------*/
  45. /* USER CODE BEGIN 0 */
  46. /* USER CODE END 0 */
  47. /**
  48. * @brief The application entry point.
  49. * @retval int
  50. */
  51. int main(void)
  52. {
  53. /* USER CODE BEGIN 1 */
  54. /* USER CODE END 1 */
  55. /* MCU Configuration--------------------------------------------------------*/
  56. /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  57. HAL_Init();
  58. /* USER CODE BEGIN Init */
  59. /* USER CODE END Init */
  60. /* Configure the system clock */
  61. SystemClock_Config();
  62. /* USER CODE BEGIN SysInit */
  63. /* USER CODE END SysInit */
  64. /* Initialize all configured peripherals */
  65. MX_GPIO_Init();
  66. MX_USART1_UART_Init();
  67. /* USER CODE BEGIN 2 */
  68. printf("haozige\r\n");
  69. /* USER CODE END 2 */
  70. /* Infinite loop */
  71. /* USER CODE BEGIN WHILE */
  72. while (1)
  73. {
  74. /* USER CODE END WHILE */
  75. /* USER CODE BEGIN 3 */
  76. usart1_receive_data_handle(); /* 对串口读取的数据进行处理 */
  77. HAL_Delay(40);
  78. }
  79. /* USER CODE END 3 */
  80. }
  81. /**
  82. * @brief System Clock Configuration
  83. * @retval None
  84. */
  85. void SystemClock_Config(void)
  86. {
  87. RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  88. RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  89. /** Initializes the RCC Oscillators according to the specified parameters
  90. * in the RCC_OscInitTypeDef structure.
  91. */
  92. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  93. RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  94. RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
  95. RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  96. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  97. RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  98. RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;
  99. if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  100. {
  101. Error_Handler();
  102. }
  103. /** Initializes the CPU, AHB and APB buses clocks
  104. */
  105. RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2;
  106. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  107. RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  108. RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
  109. RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  110. if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
  111. {
  112. Error_Handler();
  113. }
  114. }
  115. /* USER CODE BEGIN 4 */
  116. /* USER CODE END 4 */
  117. /**
  118. * @brief This function is executed in case of error occurrence.
  119. * @retval None
  120. */
  121. void Error_Handler(void)
  122. {
  123. /* USER CODE BEGIN Error_Handler_Debug */
  124. /* User can add his own implementation to report the HAL error return state */
  125. __disable_irq();
  126. while (1)
  127. {
  128. }
  129. /* USER CODE END Error_Handler_Debug */
  130. }
  131. #ifdef USE_FULL_ASSERT
  132. /**
  133. * @brief Reports the name of the source file and the source line number
  134. * where the assert_param error has occurred.
  135. * @param file: pointer to the source file name
  136. * @param line: assert_param error line source number
  137. * @retval None
  138. */
  139. void assert_failed(uint8_t *file, uint32_t line)
  140. {
  141. /* USER CODE BEGIN 6 */
  142. /* User can add his own implementation to report the file name and line number,
  143. ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  144. /* USER CODE END 6 */
  145. }
  146. #endif /* USE_FULL_ASSERT */

调速并测速小车

使用TIM2的通道1、通道2给左右电机输入不同占空比的PWM来达到轮子调速的目的,并使用测速模块将轮子当前速度显示在OLED屏,OLED屏使用软件模拟的I2C来驱动。

硬件组成

测速模块

  • 发射的红外线被物体遮挡时,输出高电平,发射的红外线没被物体遮挡时,输出低电平。

  • 即有物体高电平,没物体低电平。

  • 当搭配小车测速盘,会形成下降沿(有遮挡高电平,没遮挡低电平)。

模块与STM32F103板子接线

  • 测速模块DO引脚 <-> PB13

  • OLED屏SCL引脚 <-> PB6

  • OLED屏SDA引脚 <-> PB7

STM32CubeMX相关配置

配置GPIO

  • 配置PB13引脚为引脚中断。

  • 配置PB6引脚输出高电平。

  • 重置PA0、PA1引脚的状态,防止待会配置定时器2通道输出PWM时映射到其他引脚。

配置定时器2和定时器3

配置定时器2的通道1和通道2输出PWM,用来给电机调速。

配置定时器3定时时间为1s。

配置NVIC

  • 打开定时器3和PB13引脚的中断。

  • 修改定时器3响应中断优先级为4,PB13引脚中断响应优先级为3。

文件编写

修改文件car.c

利用PWM修改在一个调速周期内低电平占用的时间来达到轮子调速的效果。

  1. #include "gpio.h"
  2. #include "tim.h"
  3. /* B_1A、B-2A控制左边电机,A-1A、A-1B控制右边电机 */
  4. #define B_1A_LOW HAL_GPIO_WritePin(GPIOB, GPIO_PIN_0, GPIO_PIN_RESET) /* PB0 */
  5. #define B_1A_HIGH HAL_GPIO_WritePin(GPIOB, GPIO_PIN_0, GPIO_PIN_SET)
  6. #define A_1A_LOW HAL_GPIO_WritePin(GPIOB, GPIO_PIN_2, GPIO_PIN_RESET) /* PB2 */
  7. #define A_1A_HIGH HAL_GPIO_WritePin(GPIOB, GPIO_PIN_2, GPIO_PIN_SET)
  8. /* 轮子速度:0~19 */
  9. void car_goForward()
  10. {
  11. B_1A_HIGH;
  12. __HAL_TIM_SetCompare(&htim2, TIM_CHANNEL_1, 19); // B_2A_LOW,PA0
  13. A_1A_HIGH;
  14. __HAL_TIM_SetCompare(&htim2, TIM_CHANNEL_2, 19); // A_1B_LOW,PA1
  15. }
  16. void car_goBack()
  17. {
  18. B_1A_LOW;
  19. __HAL_TIM_SetCompare(&htim2, TIM_CHANNEL_1, 0); // B_2A_HIGH;
  20. A_1A_LOW;
  21. __HAL_TIM_SetCompare(&htim2, TIM_CHANNEL_2, 0); // A_1B_HIGH;
  22. }
  23. void car_goStop()
  24. {
  25. B_1A_HIGH;
  26. __HAL_TIM_SetCompare(&htim2, TIM_CHANNEL_1, 0); // B_2A_HIGH;
  27. A_1A_HIGH;
  28. __HAL_TIM_SetCompare(&htim2, TIM_CHANNEL_2, 0); // A_1B_HIGH;
  29. }
  30. void car_goRight()
  31. {
  32. B_1A_HIGH;
  33. __HAL_TIM_SetCompare(&htim2, TIM_CHANNEL_1, 19); // B_2A_LOW;
  34. A_1A_HIGH;
  35. __HAL_TIM_SetCompare(&htim2, TIM_CHANNEL_2, 0); // A_1B_HIGH;
  36. }
  37. void car_goLeft()
  38. {
  39. B_1A_HIGH;
  40. __HAL_TIM_SetCompare(&htim2, TIM_CHANNEL_1, 0); // B_2A_HIGH;
  41. A_1A_HIGH;
  42. __HAL_TIM_SetCompare(&htim2, TIM_CHANNEL_2, 19); // A_1B_LOW;
  43. }

修改文件tim.c

启动TIM2的通道1、通道2的PWM,启动TIM3并使能中断。

  1. /* USER CODE BEGIN Header */
  2. /**
  3. ******************************************************************************
  4. * @file tim.c
  5. * @brief This file provides code for the configuration
  6. * of the TIM instances.
  7. ******************************************************************************
  8. * @attention
  9. *
  10. * Copyright (c) 2023 STMicroelectronics.
  11. * All rights reserved.
  12. *
  13. * This software is licensed under terms that can be found in the LICENSE file
  14. * in the root directory of this software component.
  15. * If no LICENSE file comes with this software, it is provided AS-IS.
  16. *
  17. ******************************************************************************
  18. */
  19. /* USER CODE END Header */
  20. /* Includes ------------------------------------------------------------------*/
  21. #include "tim.h"
  22. /* USER CODE BEGIN 0 */
  23. /* USER CODE END 0 */
  24. TIM_HandleTypeDef htim2;
  25. TIM_HandleTypeDef htim3;
  26. /* TIM2 init function */
  27. void MX_TIM2_Init(void)
  28. {
  29. /* USER CODE BEGIN TIM2_Init 0 */
  30. /* USER CODE END TIM2_Init 0 */
  31. TIM_ClockConfigTypeDef sClockSourceConfig = {0};
  32. TIM_MasterConfigTypeDef sMasterConfig = {0};
  33. TIM_OC_InitTypeDef sConfigOC = {0};
  34. /* USER CODE BEGIN TIM2_Init 1 */
  35. /* USER CODE END TIM2_Init 1 */
  36. htim2.Instance = TIM2;
  37. htim2.Init.Prescaler = 71;
  38. htim2.Init.CounterMode = TIM_COUNTERMODE_UP;
  39. htim2.Init.Period = 19;
  40. htim2.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
  41. htim2.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_ENABLE;
  42. if (HAL_TIM_Base_Init(&htim2) != HAL_OK)
  43. {
  44. Error_Handler();
  45. }
  46. sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
  47. if (HAL_TIM_ConfigClockSource(&htim2, &sClockSourceConfig) != HAL_OK)
  48. {
  49. Error_Handler();
  50. }
  51. if (HAL_TIM_PWM_Init(&htim2) != HAL_OK)
  52. {
  53. Error_Handler();
  54. }
  55. sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
  56. sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
  57. if (HAL_TIMEx_MasterConfigSynchronization(&htim2, &sMasterConfig) != HAL_OK)
  58. {
  59. Error_Handler();
  60. }
  61. sConfigOC.OCMode = TIM_OCMODE_PWM1;
  62. sConfigOC.Pulse = 0;
  63. sConfigOC.OCPolarity = TIM_OCPOLARITY_LOW;
  64. sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
  65. if (HAL_TIM_PWM_ConfigChannel(&htim2, &sConfigOC, TIM_CHANNEL_1) != HAL_OK)
  66. {
  67. Error_Handler();
  68. }
  69. if (HAL_TIM_PWM_ConfigChannel(&htim2, &sConfigOC, TIM_CHANNEL_2) != HAL_OK)
  70. {
  71. Error_Handler();
  72. }
  73. /* USER CODE BEGIN TIM2_Init 2 */
  74. /* 启动B-2A、A-1B引脚的PWM */
  75. HAL_TIM_PWM_Start(&htim2, TIM_CHANNEL_1);
  76. HAL_TIM_PWM_Start(&htim2, TIM_CHANNEL_2);
  77. /* USER CODE END TIM2_Init 2 */
  78. HAL_TIM_MspPostInit(&htim2);
  79. }
  80. /* TIM3 init function */
  81. void MX_TIM3_Init(void)
  82. {
  83. /* USER CODE BEGIN TIM3_Init 0 */
  84. /* USER CODE END TIM3_Init 0 */
  85. TIM_ClockConfigTypeDef sClockSourceConfig = {0};
  86. TIM_MasterConfigTypeDef sMasterConfig = {0};
  87. /* USER CODE BEGIN TIM3_Init 1 */
  88. /* USER CODE END TIM3_Init 1 */
  89. htim3.Instance = TIM3;
  90. htim3.Init.Prescaler = 7199;
  91. htim3.Init.CounterMode = TIM_COUNTERMODE_UP;
  92. htim3.Init.Period = 9999;
  93. htim3.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
  94. htim3.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_ENABLE;
  95. if (HAL_TIM_Base_Init(&htim3) != HAL_OK)
  96. {
  97. Error_Handler();
  98. }
  99. sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
  100. if (HAL_TIM_ConfigClockSource(&htim3, &sClockSourceConfig) != HAL_OK)
  101. {
  102. Error_Handler();
  103. }
  104. sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
  105. sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
  106. if (HAL_TIMEx_MasterConfigSynchronization(&htim3, &sMasterConfig) != HAL_OK)
  107. {
  108. Error_Handler();
  109. }
  110. /* USER CODE BEGIN TIM3_Init 2 */
  111. HAL_TIM_Base_Start_IT(&htim3); /* 启动定时器3,并使能中断 */
  112. /* USER CODE END TIM3_Init 2 */
  113. }
  114. void HAL_TIM_Base_MspInit(TIM_HandleTypeDef *tim_baseHandle)
  115. {
  116. if (tim_baseHandle->Instance == TIM2)
  117. {
  118. /* USER CODE BEGIN TIM2_MspInit 0 */
  119. /* USER CODE END TIM2_MspInit 0 */
  120. /* TIM2 clock enable */
  121. __HAL_RCC_TIM2_CLK_ENABLE();
  122. /* USER CODE BEGIN TIM2_MspInit 1 */
  123. /* USER CODE END TIM2_MspInit 1 */
  124. }
  125. else if (tim_baseHandle->Instance == TIM3)
  126. {
  127. /* USER CODE BEGIN TIM3_MspInit 0 */
  128. /* USER CODE END TIM3_MspInit 0 */
  129. /* TIM3 clock enable */
  130. __HAL_RCC_TIM3_CLK_ENABLE();
  131. /* TIM3 interrupt Init */
  132. HAL_NVIC_SetPriority(TIM3_IRQn, 4, 0);
  133. HAL_NVIC_EnableIRQ(TIM3_IRQn);
  134. /* USER CODE BEGIN TIM3_MspInit 1 */
  135. /* USER CODE END TIM3_MspInit 1 */
  136. }
  137. }
  138. void HAL_TIM_MspPostInit(TIM_HandleTypeDef *timHandle)
  139. {
  140. GPIO_InitTypeDef GPIO_InitStruct = {0};
  141. if (timHandle->Instance == TIM2)
  142. {
  143. /* USER CODE BEGIN TIM2_MspPostInit 0 */
  144. /* USER CODE END TIM2_MspPostInit 0 */
  145. __HAL_RCC_GPIOA_CLK_ENABLE();
  146. /**TIM2 GPIO Configuration
  147. PA0-WKUP ------> TIM2_CH1
  148. PA1 ------> TIM2_CH2
  149. */
  150. GPIO_InitStruct.Pin = GPIO_PIN_0 | GPIO_PIN_1;
  151. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  152. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  153. HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  154. /* USER CODE BEGIN TIM2_MspPostInit 1 */
  155. /* USER CODE END TIM2_MspPostInit 1 */
  156. }
  157. }
  158. void HAL_TIM_Base_MspDeInit(TIM_HandleTypeDef *tim_baseHandle)
  159. {
  160. if (tim_baseHandle->Instance == TIM2)
  161. {
  162. /* USER CODE BEGIN TIM2_MspDeInit 0 */
  163. /* USER CODE END TIM2_MspDeInit 0 */
  164. /* Peripheral clock disable */
  165. __HAL_RCC_TIM2_CLK_DISABLE();
  166. /* USER CODE BEGIN TIM2_MspDeInit 1 */
  167. /* USER CODE END TIM2_MspDeInit 1 */
  168. }
  169. else if (tim_baseHandle->Instance == TIM3)
  170. {
  171. /* USER CODE BEGIN TIM3_MspDeInit 0 */
  172. /* USER CODE END TIM3_MspDeInit 0 */
  173. /* Peripheral clock disable */
  174. __HAL_RCC_TIM3_CLK_DISABLE();
  175. /* TIM3 interrupt Deinit */
  176. HAL_NVIC_DisableIRQ(TIM3_IRQn);
  177. /* USER CODE BEGIN TIM3_MspDeInit 1 */
  178. /* USER CODE END TIM3_MspDeInit 1 */
  179. }
  180. }
  181. /* USER CODE BEGIN 1 */
  182. /* USER CODE END 1 */

添加文件my_i2c.c

软件模拟I2C来驱动OLED屏显示,也可以使用硬件I2C,就不用此文件了。

  1. #include "gpio.h"
  2. #define SCL_HIGH HAL_GPIO_WritePin(GPIOB, GPIO_PIN_6, GPIO_PIN_SET)
  3. #define SCL_LOW HAL_GPIO_WritePin(GPIOB, GPIO_PIN_6, GPIO_PIN_RESET)
  4. #define SDA_INPUT SDA_GPIO_PB7_INIT(GPIO_MODE_INPUT)
  5. #define SDA_OUTPUT SDA_GPIO_PB7_INIT(GPIO_MODE_OUTPUT_PP)
  6. #define SDA_HIGH HAL_GPIO_WritePin(GPIOB, GPIO_PIN_7, GPIO_PIN_SET)
  7. #define SDA_LOW HAL_GPIO_WritePin(GPIOB, GPIO_PIN_7, GPIO_PIN_RESET)
  8. #define SDA_READ HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_7)
  9. /* 微秒延时函数:只适用F1系列72M主频 */
  10. void delay_us(uint32_t us)
  11. {
  12. uint32_t delay = (HAL_RCC_GetHCLKFreq() / 4000000 * us);
  13. while (delay--)
  14. {
  15. ;
  16. }
  17. }
  18. /* 配置PB7为输入引脚或者输出引脚 */
  19. void SDA_GPIO_PB7_INIT(uint32_t mode)
  20. {
  21. // 打开时钟
  22. __HAL_RCC_GPIOB_CLK_ENABLE();
  23. // 配置引脚
  24. GPIO_InitTypeDef GPIO_InitStruct = {0};
  25. GPIO_InitStruct.Pin = GPIO_PIN_7;
  26. GPIO_InitStruct.Mode = mode; // 配置为输入(GPIO_MODE_INPUT)或输出(GPIO_MODE_OUTPUT_PP)
  27. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  28. HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
  29. }
  30. /* 起始信号 */
  31. void IIC_start()
  32. {
  33. SDA_OUTPUT; /* 配置SDA引脚为输出引脚 */
  34. SCL_HIGH;
  35. SDA_HIGH;
  36. delay_us(5);
  37. SDA_LOW;
  38. delay_us(5);
  39. SCL_LOW;
  40. delay_us(5);
  41. }
  42. /* 终止信号 */
  43. void IIC_stop()
  44. {
  45. SDA_OUTPUT; /* 配置SDA引脚为输出引脚 */
  46. SDA_LOW;
  47. delay_us(5);
  48. SCL_HIGH;
  49. delay_us(5);
  50. SDA_HIGH;
  51. delay_us(5);
  52. }
  53. /* 检测应答信号:ACK返回0,NACK返回1 */
  54. uint8_t IIC_wait_ack()
  55. {
  56. SDA_OUTPUT; /* 配置SDA引脚为输出引脚 */
  57. SDA_HIGH; /* 释放数据线 */
  58. delay_us(5);
  59. SCL_HIGH; /* 从机返回ACK */
  60. delay_us(5);
  61. SDA_INPUT; /* 配置SDA引脚为输入引脚 */
  62. /* 读取SDA的电平 */
  63. if (SDA_READ == GPIO_PIN_SET)
  64. {
  65. /* 如果是高电平则为NACK */
  66. IIC_stop();
  67. return 1;
  68. }
  69. SCL_LOW; /* 结束应答信号的检测 */
  70. delay_us(5);
  71. return 0;
  72. }
  73. /* 发送一个字节数据 */
  74. void IIC_send_byte(uint8_t data)
  75. {
  76. SDA_OUTPUT; /* 配置SDA引脚为输出引脚 */
  77. for (uint8_t i = 0; i < 8; i++)
  78. {
  79. /* 从最高位开始发送 */
  80. if ((data & 0x80) >> 7)
  81. {
  82. SDA_HIGH;
  83. }
  84. else
  85. {
  86. SDA_LOW;
  87. }
  88. delay_us(5);
  89. SCL_HIGH;
  90. delay_us(5);
  91. SCL_LOW;
  92. data <<= 1; /* 将下一位移至最高位 */
  93. }
  94. SCL_HIGH; /* 发送完成,释放数据线*/
  95. }

添加文件my_i2c.h

  1. #include "main.h"
  2. void delay_us(uint32_t us);
  3. void SDA_GPIO_PB7_INIT(uint32_t mode);
  4. void IIC_start(void);
  5. void IIC_stop(void);
  6. uint8_t IIC_wait_ack(void);
  7. void IIC_send_byte(uint8_t data);

添加文件oled.c

  1. #include "my_i2c.h"
  2. #include <string.h>
  3. // OLED的字符构造点阵
  4. unsigned char oledFont[] =
  5. {
  6. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0
  7. 0x00, 0x00, 0x00, 0xF8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x33, 0x30, 0x00, 0x00, 0x00, //! 1
  8. 0x00, 0x10, 0x0C, 0x06, 0x10, 0x0C, 0x06, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, //" 2
  9. 0x40, 0xC0, 0x78, 0x40, 0xC0, 0x78, 0x40, 0x00, 0x04, 0x3F, 0x04, 0x04, 0x3F, 0x04, 0x04, 0x00, // # 3
  10. 0x00, 0x70, 0x88, 0xFC, 0x08, 0x30, 0x00, 0x00, 0x00, 0x18, 0x20, 0xFF, 0x21, 0x1E, 0x00, 0x00, //$ 4
  11. 0xF0, 0x08, 0xF0, 0x00, 0xE0, 0x18, 0x00, 0x00, 0x00, 0x21, 0x1C, 0x03, 0x1E, 0x21, 0x1E, 0x00, //% 5
  12. 0x00, 0xF0, 0x08, 0x88, 0x70, 0x00, 0x00, 0x00, 0x1E, 0x21, 0x23, 0x24, 0x19, 0x27, 0x21, 0x10, //& 6
  13. 0x10, 0x16, 0x0E, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, //' 7
  14. 0x00, 0x00, 0x00, 0xE0, 0x18, 0x04, 0x02, 0x00, 0x00, 0x00, 0x00, 0x07, 0x18, 0x20, 0x40, 0x00, //( 8
  15. 0x00, 0x02, 0x04, 0x18, 0xE0, 0x00, 0x00, 0x00, 0x00, 0x40, 0x20, 0x18, 0x07, 0x00, 0x00, 0x00, //) 9
  16. 0x40, 0x40, 0x80, 0xF0, 0x80, 0x40, 0x40, 0x00, 0x02, 0x02, 0x01, 0x0F, 0x01, 0x02, 0x02, 0x00, //* 10
  17. 0x00, 0x00, 0x00, 0xF0, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x01, 0x1F, 0x01, 0x01, 0x01, 0x00, //+ 11
  18. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0xB0, 0x70, 0x00, 0x00, 0x00, 0x00, 0x00, //, 12
  19. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, //- 13
  20. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x30, 0x30, 0x00, 0x00, 0x00, 0x00, 0x00, //. 14
  21. 0x00, 0x00, 0x00, 0x00, 0x80, 0x60, 0x18, 0x04, 0x00, 0x60, 0x18, 0x06, 0x01, 0x00, 0x00, 0x00, /// 15
  22. 0x00, 0xE0, 0x10, 0x08, 0x08, 0x10, 0xE0, 0x00, 0x00, 0x0F, 0x10, 0x20, 0x20, 0x10, 0x0F, 0x00, // 0 16
  23. 0x00, 0x10, 0x10, 0xF8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0x20, 0x3F, 0x20, 0x20, 0x00, 0x00, // 1 17
  24. 0x00, 0x70, 0x08, 0x08, 0x08, 0x88, 0x70, 0x00, 0x00, 0x30, 0x28, 0x24, 0x22, 0x21, 0x30, 0x00, // 2 18
  25. 0x00, 0x30, 0x08, 0x88, 0x88, 0x48, 0x30, 0x00, 0x00, 0x18, 0x20, 0x20, 0x20, 0x11, 0x0E, 0x00, // 3 19
  26. 0x00, 0x00, 0xC0, 0x20, 0x10, 0xF8, 0x00, 0x00, 0x00, 0x07, 0x04, 0x24, 0x24, 0x3F, 0x24, 0x00, // 4 20
  27. 0x00, 0xF8, 0x08, 0x88, 0x88, 0x08, 0x08, 0x00, 0x00, 0x19, 0x21, 0x20, 0x20, 0x11, 0x0E, 0x00, // 5 21
  28. 0x00, 0xE0, 0x10, 0x88, 0x88, 0x18, 0x00, 0x00, 0x00, 0x0F, 0x11, 0x20, 0x20, 0x11, 0x0E, 0x00, // 6 22
  29. 0x00, 0x38, 0x08, 0x08, 0xC8, 0x38, 0x08, 0x00, 0x00, 0x00, 0x00, 0x3F, 0x00, 0x00, 0x00, 0x00, // 7 23
  30. 0x00, 0x70, 0x88, 0x08, 0x08, 0x88, 0x70, 0x00, 0x00, 0x1C, 0x22, 0x21, 0x21, 0x22, 0x1C, 0x00, // 8 24
  31. 0x00, 0xE0, 0x10, 0x08, 0x08, 0x10, 0xE0, 0x00, 0x00, 0x00, 0x31, 0x22, 0x22, 0x11, 0x0F, 0x00, // 9 25
  32. 0x00, 0x00, 0x00, 0xC0, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x30, 0x30, 0x00, 0x00, 0x00, //: 26
  33. 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x60, 0x00, 0x00, 0x00, 0x00, //; 27
  34. 0x00, 0x00, 0x80, 0x40, 0x20, 0x10, 0x08, 0x00, 0x00, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x00, //< 28
  35. 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x00, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x00, //= 29
  36. 0x00, 0x08, 0x10, 0x20, 0x40, 0x80, 0x00, 0x00, 0x00, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01, 0x00, //> 30
  37. 0x00, 0x70, 0x48, 0x08, 0x08, 0x08, 0xF0, 0x00, 0x00, 0x00, 0x00, 0x30, 0x36, 0x01, 0x00, 0x00, //? 31
  38. 0xC0, 0x30, 0xC8, 0x28, 0xE8, 0x10, 0xE0, 0x00, 0x07, 0x18, 0x27, 0x24, 0x23, 0x14, 0x0B, 0x00, //@ 32
  39. 0x00, 0x00, 0xC0, 0x38, 0xE0, 0x00, 0x00, 0x00, 0x20, 0x3C, 0x23, 0x02, 0x02, 0x27, 0x38, 0x20, // A 33
  40. 0x08, 0xF8, 0x88, 0x88, 0x88, 0x70, 0x00, 0x00, 0x20, 0x3F, 0x20, 0x20, 0x20, 0x11, 0x0E, 0x00, // B 34
  41. 0xC0, 0x30, 0x08, 0x08, 0x08, 0x08, 0x38, 0x00, 0x07, 0x18, 0x20, 0x20, 0x20, 0x10, 0x08, 0x00, // C 35
  42. 0x08, 0xF8, 0x08, 0x08, 0x08, 0x10, 0xE0, 0x00, 0x20, 0x3F, 0x20, 0x20, 0x20, 0x10, 0x0F, 0x00, // D 36
  43. 0x08, 0xF8, 0x88, 0x88, 0xE8, 0x08, 0x10, 0x00, 0x20, 0x3F, 0x20, 0x20, 0x23, 0x20, 0x18, 0x00, // E 37
  44. 0x08, 0xF8, 0x88, 0x88, 0xE8, 0x08, 0x10, 0x00, 0x20, 0x3F, 0x20, 0x00, 0x03, 0x00, 0x00, 0x00, // F 38
  45. 0xC0, 0x30, 0x08, 0x08, 0x08, 0x38, 0x00, 0x00, 0x07, 0x18, 0x20, 0x20, 0x22, 0x1E, 0x02, 0x00, // G 39
  46. 0x08, 0xF8, 0x08, 0x00, 0x00, 0x08, 0xF8, 0x08, 0x20, 0x3F, 0x21, 0x01, 0x01, 0x21, 0x3F, 0x20, // H 40
  47. 0x00, 0x08, 0x08, 0xF8, 0x08, 0x08, 0x00, 0x00, 0x00, 0x20, 0x20, 0x3F, 0x20, 0x20, 0x00, 0x00, // I 41
  48. 0x00, 0x00, 0x08, 0x08, 0xF8, 0x08, 0x08, 0x00, 0xC0, 0x80, 0x80, 0x80, 0x7F, 0x00, 0x00, 0x00, // J 42
  49. 0x08, 0xF8, 0x88, 0xC0, 0x28, 0x18, 0x08, 0x00, 0x20, 0x3F, 0x20, 0x01, 0x26, 0x38, 0x20, 0x00, // K 43
  50. 0x08, 0xF8, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0x3F, 0x20, 0x20, 0x20, 0x20, 0x30, 0x00, // L 44
  51. 0x08, 0xF8, 0xF8, 0x00, 0xF8, 0xF8, 0x08, 0x00, 0x20, 0x3F, 0x00, 0x3F, 0x00, 0x3F, 0x20, 0x00, // M 45
  52. 0x08, 0xF8, 0x30, 0xC0, 0x00, 0x08, 0xF8, 0x08, 0x20, 0x3F, 0x20, 0x00, 0x07, 0x18, 0x3F, 0x00, // N 46
  53. 0xE0, 0x10, 0x08, 0x08, 0x08, 0x10, 0xE0, 0x00, 0x0F, 0x10, 0x20, 0x20, 0x20, 0x10, 0x0F, 0x00, // O 47
  54. 0x08, 0xF8, 0x08, 0x08, 0x08, 0x08, 0xF0, 0x00, 0x20, 0x3F, 0x21, 0x01, 0x01, 0x01, 0x00, 0x00, // P 48
  55. 0xE0, 0x10, 0x08, 0x08, 0x08, 0x10, 0xE0, 0x00, 0x0F, 0x18, 0x24, 0x24, 0x38, 0x50, 0x4F, 0x00, // Q 49
  56. 0x08, 0xF8, 0x88, 0x88, 0x88, 0x88, 0x70, 0x00, 0x20, 0x3F, 0x20, 0x00, 0x03, 0x0C, 0x30, 0x20, // R 50
  57. 0x00, 0x70, 0x88, 0x08, 0x08, 0x08, 0x38, 0x00, 0x00, 0x38, 0x20, 0x21, 0x21, 0x22, 0x1C, 0x00, // S 51
  58. 0x18, 0x08, 0x08, 0xF8, 0x08, 0x08, 0x18, 0x00, 0x00, 0x00, 0x20, 0x3F, 0x20, 0x00, 0x00, 0x00, // T 52
  59. 0x08, 0xF8, 0x08, 0x00, 0x00, 0x08, 0xF8, 0x08, 0x00, 0x1F, 0x20, 0x20, 0x20, 0x20, 0x1F, 0x00, // U 53
  60. 0x08, 0x78, 0x88, 0x00, 0x00, 0xC8, 0x38, 0x08, 0x00, 0x00, 0x07, 0x38, 0x0E, 0x01, 0x00, 0x00, // V 54
  61. 0xF8, 0x08, 0x00, 0xF8, 0x00, 0x08, 0xF8, 0x00, 0x03, 0x3C, 0x07, 0x00, 0x07, 0x3C, 0x03, 0x00, // W 55
  62. 0x08, 0x18, 0x68, 0x80, 0x80, 0x68, 0x18, 0x08, 0x20, 0x30, 0x2C, 0x03, 0x03, 0x2C, 0x30, 0x20, // X 56
  63. 0x08, 0x38, 0xC8, 0x00, 0xC8, 0x38, 0x08, 0x00, 0x00, 0x00, 0x20, 0x3F, 0x20, 0x00, 0x00, 0x00, // Y 57
  64. 0x10, 0x08, 0x08, 0x08, 0xC8, 0x38, 0x08, 0x00, 0x20, 0x38, 0x26, 0x21, 0x20, 0x20, 0x18, 0x00, // Z 58
  65. 0x00, 0x00, 0x00, 0xFE, 0x02, 0x02, 0x02, 0x00, 0x00, 0x00, 0x00, 0x7F, 0x40, 0x40, 0x40, 0x00, //[ 59
  66. 0x00, 0x0C, 0x30, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x06, 0x38, 0xC0, 0x00, //\ 60
  67. 0x00, 0x02, 0x02, 0x02, 0xFE, 0x00, 0x00, 0x00, 0x00, 0x40, 0x40, 0x40, 0x7F, 0x00, 0x00, 0x00, //] 61
  68. 0x00, 0x00, 0x04, 0x02, 0x02, 0x02, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, //^ 62
  69. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, //_ 63
  70. 0x00, 0x02, 0x02, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, //` 64
  71. 0x00, 0x00, 0x80, 0x80, 0x80, 0x80, 0x00, 0x00, 0x00, 0x19, 0x24, 0x22, 0x22, 0x22, 0x3F, 0x20, // a 65
  72. 0x08, 0xF8, 0x00, 0x80, 0x80, 0x00, 0x00, 0x00, 0x00, 0x3F, 0x11, 0x20, 0x20, 0x11, 0x0E, 0x00, // b 66
  73. 0x00, 0x00, 0x00, 0x80, 0x80, 0x80, 0x00, 0x00, 0x00, 0x0E, 0x11, 0x20, 0x20, 0x20, 0x11, 0x00, // c 67
  74. 0x00, 0x00, 0x00, 0x80, 0x80, 0x88, 0xF8, 0x00, 0x00, 0x0E, 0x11, 0x20, 0x20, 0x10, 0x3F, 0x20, // d 68
  75. 0x00, 0x00, 0x80, 0x80, 0x80, 0x80, 0x00, 0x00, 0x00, 0x1F, 0x22, 0x22, 0x22, 0x22, 0x13, 0x00, // e 69
  76. 0x00, 0x80, 0x80, 0xF0, 0x88, 0x88, 0x88, 0x18, 0x00, 0x20, 0x20, 0x3F, 0x20, 0x20, 0x00, 0x00, // f 70
  77. 0x00, 0x00, 0x80, 0x80, 0x80, 0x80, 0x80, 0x00, 0x00, 0x6B, 0x94, 0x94, 0x94, 0x93, 0x60, 0x00, // g 71
  78. 0x08, 0xF8, 0x00, 0x80, 0x80, 0x80, 0x00, 0x00, 0x20, 0x3F, 0x21, 0x00, 0x00, 0x20, 0x3F, 0x20, // h 72
  79. 0x00, 0x80, 0x98, 0x98, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0x20, 0x3F, 0x20, 0x20, 0x00, 0x00, // i 73
  80. 0x00, 0x00, 0x00, 0x80, 0x98, 0x98, 0x00, 0x00, 0x00, 0xC0, 0x80, 0x80, 0x80, 0x7F, 0x00, 0x00, // j 74
  81. 0x08, 0xF8, 0x00, 0x00, 0x80, 0x80, 0x80, 0x00, 0x20, 0x3F, 0x24, 0x02, 0x2D, 0x30, 0x20, 0x00, // k 75
  82. 0x00, 0x08, 0x08, 0xF8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0x20, 0x3F, 0x20, 0x20, 0x00, 0x00, // l 76
  83. 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x00, 0x20, 0x3F, 0x20, 0x00, 0x3F, 0x20, 0x00, 0x3F, // m 77
  84. 0x80, 0x80, 0x00, 0x80, 0x80, 0x80, 0x00, 0x00, 0x20, 0x3F, 0x21, 0x00, 0x00, 0x20, 0x3F, 0x20, // n 78
  85. 0x00, 0x00, 0x80, 0x80, 0x80, 0x80, 0x00, 0x00, 0x00, 0x1F, 0x20, 0x20, 0x20, 0x20, 0x1F, 0x00, // o 79
  86. 0x80, 0x80, 0x00, 0x80, 0x80, 0x00, 0x00, 0x00, 0x80, 0xFF, 0xA1, 0x20, 0x20, 0x11, 0x0E, 0x00, // p 80
  87. 0x00, 0x00, 0x00, 0x80, 0x80, 0x80, 0x80, 0x00, 0x00, 0x0E, 0x11, 0x20, 0x20, 0xA0, 0xFF, 0x80, // q 81
  88. 0x80, 0x80, 0x80, 0x00, 0x80, 0x80, 0x80, 0x00, 0x20, 0x20, 0x3F, 0x21, 0x20, 0x00, 0x01, 0x00, // r 82
  89. 0x00, 0x00, 0x80, 0x80, 0x80, 0x80, 0x80, 0x00, 0x00, 0x33, 0x24, 0x24, 0x24, 0x24, 0x19, 0x00, // s 83
  90. 0x00, 0x80, 0x80, 0xE0, 0x80, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1F, 0x20, 0x20, 0x00, 0x00, // t 84
  91. 0x80, 0x80, 0x00, 0x00, 0x00, 0x80, 0x80, 0x00, 0x00, 0x1F, 0x20, 0x20, 0x20, 0x10, 0x3F, 0x20, // u 85
  92. 0x80, 0x80, 0x80, 0x00, 0x00, 0x80, 0x80, 0x80, 0x00, 0x01, 0x0E, 0x30, 0x08, 0x06, 0x01, 0x00, // v 86
  93. 0x80, 0x80, 0x00, 0x80, 0x00, 0x80, 0x80, 0x80, 0x0F, 0x30, 0x0C, 0x03, 0x0C, 0x30, 0x0F, 0x00, // w 87
  94. 0x00, 0x80, 0x80, 0x00, 0x80, 0x80, 0x80, 0x00, 0x00, 0x20, 0x31, 0x2E, 0x0E, 0x31, 0x20, 0x00, // x 88
  95. 0x80, 0x80, 0x80, 0x00, 0x00, 0x80, 0x80, 0x80, 0x80, 0x81, 0x8E, 0x70, 0x18, 0x06, 0x01, 0x00, // y 89
  96. 0x00, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x00, 0x00, 0x21, 0x30, 0x2C, 0x22, 0x21, 0x30, 0x00, // z 90
  97. 0x00, 0x00, 0x00, 0x00, 0x80, 0x7C, 0x02, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3F, 0x40, 0x40, //{ 91
  98. 0x00, 0x00, 0x00, 0x00, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0x00, 0x00, 0x00, //| 92
  99. 0x00, 0x02, 0x02, 0x7C, 0x80, 0x00, 0x00, 0x00, 0x00, 0x40, 0x40, 0x3F, 0x00, 0x00, 0x00, 0x00, //} 93
  100. 0x00, 0x06, 0x01, 0x01, 0x02, 0x02, 0x04, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, //~ 94
  101. };
  102. /* OLED写入一条指令 */
  103. void oledWriteCmd(char writeCmd)
  104. {
  105. IIC_start();
  106. IIC_send_byte(0x78); // 选择一个OLED屏,写模式
  107. IIC_wait_ack();
  108. IIC_send_byte(0x00); // 写入命令,D/C位为0
  109. IIC_wait_ack();
  110. IIC_send_byte(writeCmd);
  111. IIC_wait_ack();
  112. IIC_stop();
  113. }
  114. /* OLED写入一个数据 */
  115. void oledWriteData(char writeData)
  116. {
  117. IIC_start();
  118. IIC_send_byte(0x78); // 选择一个OLED屏,写模式
  119. IIC_wait_ack();
  120. IIC_send_byte(0x40); // 写入命令,D/C位为1
  121. IIC_wait_ack();
  122. IIC_send_byte(writeData);
  123. IIC_wait_ack();
  124. IIC_stop();
  125. }
  126. // OLCD初始化
  127. void oledInit()
  128. {
  129. oledWriteCmd(0xAE);
  130. oledWriteCmd(0x00);
  131. oledWriteCmd(0x10);
  132. oledWriteCmd(0x40);
  133. oledWriteCmd(0xB0);
  134. oledWriteCmd(0x81);
  135. oledWriteCmd(0xFF);
  136. oledWriteCmd(0xA1);
  137. oledWriteCmd(0xA6);
  138. oledWriteCmd(0xA8);
  139. oledWriteCmd(0x3F);
  140. oledWriteCmd(0xC8);
  141. oledWriteCmd(0xD3);
  142. oledWriteCmd(0x00);
  143. oledWriteCmd(0xD5);
  144. oledWriteCmd(0x80);
  145. oledWriteCmd(0xD8);
  146. oledWriteCmd(0x05);
  147. oledWriteCmd(0xD9);
  148. oledWriteCmd(0xF1);
  149. oledWriteCmd(0xDA);
  150. oledWriteCmd(0x12);
  151. oledWriteCmd(0xDB);
  152. oledWriteCmd(0x30);
  153. oledWriteCmd(0x8D);
  154. oledWriteCmd(0x14);
  155. oledWriteCmd(0xAF);
  156. }
  157. // OLED全屏清屏
  158. void oledClean()
  159. {
  160. int i, j;
  161. for (i = 0; i < 8; i++)
  162. {
  163. oledWriteCmd(0xB0 + i); // 选择PAGE
  164. // 选择PAGE的第0列开始显示
  165. oledWriteCmd(0x00);
  166. oledWriteCmd(0x10);
  167. for (j = 0; j < 128; j++)
  168. {
  169. oledWriteData(0); // 写入字符0
  170. }
  171. }
  172. }
  173. // OLED行清屏
  174. void oled_rowClean(char rows)
  175. {
  176. unsigned char i, j;
  177. for (i = 0; i < 2; i++)
  178. {
  179. oledWriteCmd(0xB0 + (rows * 2 - (2 - i))); // 选择PAGE
  180. // 选择PAGE的第0列开始显示
  181. oledWriteCmd(0x00);
  182. oledWriteCmd(0x10);
  183. for (j = 0; j < 128; j++)
  184. {
  185. oledWriteData(0); // 写入字符0
  186. }
  187. }
  188. }
  189. // OLED显示一个字符
  190. void oledShowByte(char rows, char columns, char oledByte)
  191. {
  192. unsigned int i;
  193. // 显示字符的上半部分
  194. oledWriteCmd(0xb0 + (rows * 2 - 2)); // 选择行
  195. // 选择列
  196. oledWriteCmd(0x00 + (columns & 0x0f));
  197. oledWriteCmd(0x10 + (columns >> 4));
  198. // 显示数据
  199. for (i = ((oledByte - 32) * 16); i < ((oledByte - 32) * 16 + 8); i++)
  200. {
  201. oledWriteData(oledFont[i]);
  202. }
  203. // 显示字符的上半部分
  204. oledWriteCmd(0xb0 + (rows * 2 - 1)); // 选择行
  205. // 选择列
  206. oledWriteCmd(0x00 + (columns & 0x0f));
  207. oledWriteCmd(0x10 + (columns >> 4));
  208. // 显示数据
  209. for (i = ((oledByte - 32) * 16 + 8); i < ((oledByte - 32) * 16 + 8 + 8); i++)
  210. {
  211. oledWriteData(oledFont[i]);
  212. }
  213. }
  214. // OLED显示一个字符串
  215. void oledShowString(char rows, char columns, char *str)
  216. {
  217. while (*str != '\0')
  218. {
  219. oledShowByte(rows, columns, *str);
  220. str++;
  221. columns += 8;
  222. }
  223. }

添加文件oled.h

  1. #include "main.h"
  2. // OLED写入一条指令
  3. void oledWriteCmd(uint8_t writeCmd);
  4. // OLED写入一个数据
  5. void oledWriteData(uint8_t writeData);
  6. // OLCD初始化
  7. void oledInit(void);
  8. // OLED清屏
  9. void oledClean(void);
  10. // OLED行清屏
  11. void oled_rowClean(char rows);
  12. // OLED显示一个字符
  13. void oledShowByte(char rows, char columns, char oledByte);
  14. // OLED显示一个字符串
  15. void oledShowString(char rows, char columns, char *str);

main.c文件编写

  1. /* USER CODE BEGIN Header */
  2. /**
  3. ******************************************************************************
  4. * @file : main.c
  5. * @brief : Main program body
  6. ******************************************************************************
  7. * @attention
  8. *
  9. * Copyright (c) 2023 STMicroelectronics.
  10. * All rights reserved.
  11. *
  12. * This software is licensed under terms that can be found in the LICENSE file
  13. * in the root directory of this software component.
  14. * If no LICENSE file comes with this software, it is provided AS-IS.
  15. *
  16. ******************************************************************************
  17. */
  18. /* USER CODE END Header */
  19. /* Includes ------------------------------------------------------------------*/
  20. #include "main.h"
  21. #include "tim.h"
  22. #include "usart.h"
  23. #include "gpio.h"
  24. /* Private includes ----------------------------------------------------------*/
  25. /* USER CODE BEGIN Includes */
  26. #include <stdio.h>
  27. #include <string.h>
  28. #include "car.h"
  29. #include "oled.h"
  30. uint8_t speed_cnt = 0;
  31. uint8_t speedMsg[24] = {0};
  32. /* USER CODE END Includes */
  33. /* Private typedef -----------------------------------------------------------*/
  34. /* USER CODE BEGIN PTD */
  35. /* USER CODE END PTD */
  36. /* Private define ------------------------------------------------------------*/
  37. /* USER CODE BEGIN PD */
  38. /* USER CODE END PD */
  39. /* Private macro -------------------------------------------------------------*/
  40. /* USER CODE BEGIN PM */
  41. /* USER CODE END PM */
  42. /* Private variables ---------------------------------------------------------*/
  43. /* USER CODE BEGIN PV */
  44. /* USER CODE END PV */
  45. /* Private function prototypes -----------------------------------------------*/
  46. void SystemClock_Config(void);
  47. /* USER CODE BEGIN PFP */
  48. /* USER CODE END PFP */
  49. /* Private user code ---------------------------------------------------------*/
  50. /* USER CODE BEGIN 0 */
  51. /* PB13引脚中断的回调函数 */
  52. void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
  53. {
  54. if (GPIO_Pin == GPIO_PIN_13)
  55. { /* 如果是PB13引脚产生的中断 */
  56. // HAL_Delay(50); /* 按键防抖,这个delay函数不知道为什么引起OLED显示失败 */
  57. if (HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_13) == GPIO_PIN_RESET)
  58. {
  59. speed_cnt++;
  60. }
  61. }
  62. }
  63. /* 定时器3中断的回调函数 */
  64. void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
  65. {
  66. // 判断是否是定时器3产生的中断
  67. if (htim3.Instance == TIM3)
  68. {
  69. if (speed_cnt < 10)
  70. {
  71. sprintf((char *)speedMsg, "speed: %d cm/s ", speed_cnt);
  72. }
  73. else
  74. {
  75. sprintf((char *)speedMsg, "speed: %d cm/s ", speed_cnt);
  76. }
  77. printf("%s\r\n", speedMsg);
  78. oledShowString(3, 1, (char *)speedMsg);
  79. speed_cnt = 0;
  80. }
  81. }
  82. /* USER CODE END 0 */
  83. /**
  84. * @brief The application entry point.
  85. * @retval int
  86. */
  87. int main(void)
  88. {
  89. /* USER CODE BEGIN 1 */
  90. /* USER CODE END 1 */
  91. /* MCU Configuration--------------------------------------------------------*/
  92. /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  93. HAL_Init();
  94. /* USER CODE BEGIN Init */
  95. /* USER CODE END Init */
  96. /* Configure the system clock */
  97. SystemClock_Config();
  98. /* USER CODE BEGIN SysInit */
  99. /* USER CODE END SysInit */
  100. /* Initialize all configured peripherals */
  101. MX_GPIO_Init();
  102. MX_USART1_UART_Init();
  103. MX_TIM2_Init();
  104. MX_TIM3_Init();
  105. /* USER CODE BEGIN 2 */
  106. oledInit(); // OLED初始化
  107. oledClean(); /* 清屏函数 */
  108. // 设置寻址模式
  109. oledWriteCmd(0x20); // 设置内存
  110. oledWriteCmd(0x02); // 选择页寻址模式
  111. oledShowString(1, 1, "diedie_car");
  112. oledShowString(2, 1, "move: stop");
  113. oledShowString(4, 1, "mode: bizhang");
  114. /* USER CODE END 2 */
  115. /* Infinite loop */
  116. /* USER CODE BEGIN WHILE */
  117. while (1)
  118. {
  119. /* USER CODE END WHILE */
  120. /* USER CODE BEGIN 3 */
  121. usart1_receive_data_handle(); /* 对串口读取的数据进行处理 */
  122. }
  123. /* USER CODE END 3 */
  124. }
  125. /**
  126. * @brief System Clock Configuration
  127. * @retval None
  128. */
  129. void SystemClock_Config(void)
  130. {
  131. RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  132. RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  133. /** Initializes the RCC Oscillators according to the specified parameters
  134. * in the RCC_OscInitTypeDef structure.
  135. */
  136. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  137. RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  138. RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
  139. RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  140. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  141. RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  142. RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;
  143. if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  144. {
  145. Error_Handler();
  146. }
  147. /** Initializes the CPU, AHB and APB buses clocks
  148. */
  149. RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2;
  150. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  151. RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  152. RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
  153. RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  154. if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
  155. {
  156. Error_Handler();
  157. }
  158. }
  159. /* USER CODE BEGIN 4 */
  160. /* USER CODE END 4 */
  161. /**
  162. * @brief This function is executed in case of error occurrence.
  163. * @retval None
  164. */
  165. void Error_Handler(void)
  166. {
  167. /* USER CODE BEGIN Error_Handler_Debug */
  168. /* User can add his own implementation to report the HAL error return state */
  169. __disable_irq();
  170. while (1)
  171. {
  172. }
  173. /* USER CODE END Error_Handler_Debug */
  174. }
  175. #ifdef USE_FULL_ASSERT
  176. /**
  177. * @brief Reports the name of the source file and the source line number
  178. * where the assert_param error has occurred.
  179. * @param file: pointer to the source file name
  180. * @param line: assert_param error line source number
  181. * @retval None
  182. */
  183. void assert_failed(uint8_t *file, uint32_t line)
  184. {
  185. /* USER CODE BEGIN 6 */
  186. /* User can add his own implementation to report the file name and line number,
  187. ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  188. /* USER CODE END 6 */
  189. }
  190. #endif /* USE_FULL_ASSERT */

摇头避障小车

利用sg90舵机转动不同的角度配合超声波获取小车前方的障碍物的距离来达到避障的效果。

硬件组成

超声波模块HC-SR04

  • 发送超声波:当Trig引脚接收到一个10微秒以上的高电平后开始发送超声波,当开始发送超声波后,Echo引脚会从低电平跳转到高电平。

  • 接收超声波:当发出去的超声波返回来并被接收后,Echo引脚会从高电平跳转到低电平。

  • 超声波从发出到被接收的时间:Echo持续高电平的时间,当超声波发出去的瞬间启动定时器,超声波被接收的瞬间停止定时器,获取中间经过的时间。

  • 测距:距离 = 声音速度(340m/s)* 时间 / 2,除以2是因为超声波经过了两倍距离。

sg90舵机

驱动sg90舵机时,PWM信号的频率不能太高,大概为50HZ,即设置一个周期为20ms左右的PWM。

  • 周期内0.5ms高电平 --> 舵机转动45°,即占空比为2.5%。

  • 周期内1.0ms高电平 --> 舵机转动90°,即占空比为5.0%。

  • 周期内1.5ms高电平 --> 舵机转动135°,即占空比为7.5%。

  • 周期内2.0ms高电平 --> 舵机转动45°,即占空比为10.0%。

  • 周期内2.5ms高电平 --> 舵机转动180°,即占空比为12.5%。

模块与STM32F103板子接线

  • 超声波Trig引脚 <-> PB3

  • 超声波Echo引脚 <-> PB4

  • 舵机PWM信号引脚 <-> PB9

STM32CubeMX相关配置

配置GPIO

  • 配置PB3引脚为输出高电平。

  • 配置PB4引脚为输入引脚。

配置定时器1和定时器4

配置定时器1作为计数器,PSC值为71,ARR值为65535,这样定时器每计数一下就经过1us。

配置定时器4的通道4输出PWM,用来驱动舵机转动不同的角度。

文件编写

修改文件tim.c

  1. /* USER CODE BEGIN Header */
  2. /**
  3. ******************************************************************************
  4. * @file tim.c
  5. * @brief This file provides code for the configuration
  6. * of the TIM instances.
  7. ******************************************************************************
  8. * @attention
  9. *
  10. * Copyright (c) 2023 STMicroelectronics.
  11. * All rights reserved.
  12. *
  13. * This software is licensed under terms that can be found in the LICENSE file
  14. * in the root directory of this software component.
  15. * If no LICENSE file comes with this software, it is provided AS-IS.
  16. *
  17. ******************************************************************************
  18. */
  19. /* USER CODE END Header */
  20. /* Includes ------------------------------------------------------------------*/
  21. #include "tim.h"
  22. /* USER CODE BEGIN 0 */
  23. /* USER CODE END 0 */
  24. TIM_HandleTypeDef htim1;
  25. TIM_HandleTypeDef htim2;
  26. TIM_HandleTypeDef htim3;
  27. TIM_HandleTypeDef htim4;
  28. /* TIM1 init function */
  29. void MX_TIM1_Init(void)
  30. {
  31. /* USER CODE BEGIN TIM1_Init 0 */
  32. /* USER CODE END TIM1_Init 0 */
  33. TIM_ClockConfigTypeDef sClockSourceConfig = {0};
  34. TIM_MasterConfigTypeDef sMasterConfig = {0};
  35. /* USER CODE BEGIN TIM1_Init 1 */
  36. /* USER CODE END TIM1_Init 1 */
  37. htim1.Instance = TIM1;
  38. htim1.Init.Prescaler = 71;
  39. htim1.Init.CounterMode = TIM_COUNTERMODE_UP;
  40. htim1.Init.Period = 65535;
  41. htim1.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
  42. htim1.Init.RepetitionCounter = 0;
  43. htim1.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_ENABLE;
  44. if (HAL_TIM_Base_Init(&htim1) != HAL_OK)
  45. {
  46. Error_Handler();
  47. }
  48. sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
  49. if (HAL_TIM_ConfigClockSource(&htim1, &sClockSourceConfig) != HAL_OK)
  50. {
  51. Error_Handler();
  52. }
  53. sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
  54. sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
  55. if (HAL_TIMEx_MasterConfigSynchronization(&htim1, &sMasterConfig) != HAL_OK)
  56. {
  57. Error_Handler();
  58. }
  59. /* USER CODE BEGIN TIM1_Init 2 */
  60. /* USER CODE END TIM1_Init 2 */
  61. }
  62. /* TIM2 init function */
  63. void MX_TIM2_Init(void)
  64. {
  65. /* USER CODE BEGIN TIM2_Init 0 */
  66. /* USER CODE END TIM2_Init 0 */
  67. TIM_ClockConfigTypeDef sClockSourceConfig = {0};
  68. TIM_MasterConfigTypeDef sMasterConfig = {0};
  69. TIM_OC_InitTypeDef sConfigOC = {0};
  70. /* USER CODE BEGIN TIM2_Init 1 */
  71. /* USER CODE END TIM2_Init 1 */
  72. htim2.Instance = TIM2;
  73. htim2.Init.Prescaler = 71;
  74. htim2.Init.CounterMode = TIM_COUNTERMODE_UP;
  75. htim2.Init.Period = 19;
  76. htim2.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
  77. htim2.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_ENABLE;
  78. if (HAL_TIM_Base_Init(&htim2) != HAL_OK)
  79. {
  80. Error_Handler();
  81. }
  82. sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
  83. if (HAL_TIM_ConfigClockSource(&htim2, &sClockSourceConfig) != HAL_OK)
  84. {
  85. Error_Handler();
  86. }
  87. if (HAL_TIM_PWM_Init(&htim2) != HAL_OK)
  88. {
  89. Error_Handler();
  90. }
  91. sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
  92. sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
  93. if (HAL_TIMEx_MasterConfigSynchronization(&htim2, &sMasterConfig) != HAL_OK)
  94. {
  95. Error_Handler();
  96. }
  97. sConfigOC.OCMode = TIM_OCMODE_PWM1;
  98. sConfigOC.Pulse = 0;
  99. sConfigOC.OCPolarity = TIM_OCPOLARITY_LOW;
  100. sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
  101. if (HAL_TIM_PWM_ConfigChannel(&htim2, &sConfigOC, TIM_CHANNEL_1) != HAL_OK)
  102. {
  103. Error_Handler();
  104. }
  105. if (HAL_TIM_PWM_ConfigChannel(&htim2, &sConfigOC, TIM_CHANNEL_2) != HAL_OK)
  106. {
  107. Error_Handler();
  108. }
  109. /* USER CODE BEGIN TIM2_Init 2 */
  110. /* 启动B-2A、A-1B引脚的PWM */
  111. HAL_TIM_PWM_Start(&htim2, TIM_CHANNEL_1);
  112. HAL_TIM_PWM_Start(&htim2, TIM_CHANNEL_2);
  113. /* USER CODE END TIM2_Init 2 */
  114. HAL_TIM_MspPostInit(&htim2);
  115. }
  116. /* TIM3 init function */
  117. void MX_TIM3_Init(void)
  118. {
  119. /* USER CODE BEGIN TIM3_Init 0 */
  120. /* USER CODE END TIM3_Init 0 */
  121. TIM_ClockConfigTypeDef sClockSourceConfig = {0};
  122. TIM_MasterConfigTypeDef sMasterConfig = {0};
  123. /* USER CODE BEGIN TIM3_Init 1 */
  124. /* USER CODE END TIM3_Init 1 */
  125. htim3.Instance = TIM3;
  126. htim3.Init.Prescaler = 7199;
  127. htim3.Init.CounterMode = TIM_COUNTERMODE_UP;
  128. htim3.Init.Period = 9999;
  129. htim3.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
  130. htim3.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_ENABLE;
  131. if (HAL_TIM_Base_Init(&htim3) != HAL_OK)
  132. {
  133. Error_Handler();
  134. }
  135. sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
  136. if (HAL_TIM_ConfigClockSource(&htim3, &sClockSourceConfig) != HAL_OK)
  137. {
  138. Error_Handler();
  139. }
  140. sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
  141. sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
  142. if (HAL_TIMEx_MasterConfigSynchronization(&htim3, &sMasterConfig) != HAL_OK)
  143. {
  144. Error_Handler();
  145. }
  146. /* USER CODE BEGIN TIM3_Init 2 */
  147. HAL_TIM_Base_Start_IT(&htim3);
  148. /* USER CODE END TIM3_Init 2 */
  149. }
  150. /* TIM4 init function */
  151. void MX_TIM4_Init(void)
  152. {
  153. /* USER CODE BEGIN TIM4_Init 0 */
  154. /* USER CODE END TIM4_Init 0 */
  155. TIM_ClockConfigTypeDef sClockSourceConfig = {0};
  156. TIM_MasterConfigTypeDef sMasterConfig = {0};
  157. TIM_OC_InitTypeDef sConfigOC = {0};
  158. /* USER CODE BEGIN TIM4_Init 1 */
  159. /* USER CODE END TIM4_Init 1 */
  160. htim4.Instance = TIM4;
  161. htim4.Init.Prescaler = 7199;
  162. htim4.Init.CounterMode = TIM_COUNTERMODE_UP;
  163. htim4.Init.Period = 199;
  164. htim4.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
  165. htim4.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_ENABLE;
  166. if (HAL_TIM_Base_Init(&htim4) != HAL_OK)
  167. {
  168. Error_Handler();
  169. }
  170. sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
  171. if (HAL_TIM_ConfigClockSource(&htim4, &sClockSourceConfig) != HAL_OK)
  172. {
  173. Error_Handler();
  174. }
  175. if (HAL_TIM_PWM_Init(&htim4) != HAL_OK)
  176. {
  177. Error_Handler();
  178. }
  179. sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
  180. sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
  181. if (HAL_TIMEx_MasterConfigSynchronization(&htim4, &sMasterConfig) != HAL_OK)
  182. {
  183. Error_Handler();
  184. }
  185. sConfigOC.OCMode = TIM_OCMODE_PWM1;
  186. sConfigOC.Pulse = 0;
  187. sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
  188. sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
  189. if (HAL_TIM_PWM_ConfigChannel(&htim4, &sConfigOC, TIM_CHANNEL_4) != HAL_OK)
  190. {
  191. Error_Handler();
  192. }
  193. /* USER CODE BEGIN TIM4_Init 2 */
  194. /* 启动sg90引脚的PWM */
  195. HAL_TIM_PWM_Start(&htim4, TIM_CHANNEL_4);
  196. /* USER CODE END TIM4_Init 2 */
  197. HAL_TIM_MspPostInit(&htim4);
  198. }
  199. void HAL_TIM_Base_MspInit(TIM_HandleTypeDef *tim_baseHandle)
  200. {
  201. if (tim_baseHandle->Instance == TIM1)
  202. {
  203. /* USER CODE BEGIN TIM1_MspInit 0 */
  204. /* USER CODE END TIM1_MspInit 0 */
  205. /* TIM1 clock enable */
  206. __HAL_RCC_TIM1_CLK_ENABLE();
  207. /* USER CODE BEGIN TIM1_MspInit 1 */
  208. /* USER CODE END TIM1_MspInit 1 */
  209. }
  210. else if (tim_baseHandle->Instance == TIM2)
  211. {
  212. /* USER CODE BEGIN TIM2_MspInit 0 */
  213. /* USER CODE END TIM2_MspInit 0 */
  214. /* TIM2 clock enable */
  215. __HAL_RCC_TIM2_CLK_ENABLE();
  216. /* USER CODE BEGIN TIM2_MspInit 1 */
  217. /* USER CODE END TIM2_MspInit 1 */
  218. }
  219. else if (tim_baseHandle->Instance == TIM3)
  220. {
  221. /* USER CODE BEGIN TIM3_MspInit 0 */
  222. /* USER CODE END TIM3_MspInit 0 */
  223. /* TIM3 clock enable */
  224. __HAL_RCC_TIM3_CLK_ENABLE();
  225. /* TIM3 interrupt Init */
  226. HAL_NVIC_SetPriority(TIM3_IRQn, 4, 0);
  227. HAL_NVIC_EnableIRQ(TIM3_IRQn);
  228. /* USER CODE BEGIN TIM3_MspInit 1 */
  229. /* USER CODE END TIM3_MspInit 1 */
  230. }
  231. else if (tim_baseHandle->Instance == TIM4)
  232. {
  233. /* USER CODE BEGIN TIM4_MspInit 0 */
  234. /* USER CODE END TIM4_MspInit 0 */
  235. /* TIM4 clock enable */
  236. __HAL_RCC_TIM4_CLK_ENABLE();
  237. /* USER CODE BEGIN TIM4_MspInit 1 */
  238. /* USER CODE END TIM4_MspInit 1 */
  239. }
  240. }
  241. void HAL_TIM_MspPostInit(TIM_HandleTypeDef *timHandle)
  242. {
  243. GPIO_InitTypeDef GPIO_InitStruct = {0};
  244. if (timHandle->Instance == TIM2)
  245. {
  246. /* USER CODE BEGIN TIM2_MspPostInit 0 */
  247. /* USER CODE END TIM2_MspPostInit 0 */
  248. __HAL_RCC_GPIOA_CLK_ENABLE();
  249. /**TIM2 GPIO Configuration
  250. PA0-WKUP ------> TIM2_CH1
  251. PA1 ------> TIM2_CH2
  252. */
  253. GPIO_InitStruct.Pin = GPIO_PIN_0 | GPIO_PIN_1;
  254. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  255. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  256. HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  257. /* USER CODE BEGIN TIM2_MspPostInit 1 */
  258. /* USER CODE END TIM2_MspPostInit 1 */
  259. }
  260. else if (timHandle->Instance == TIM4)
  261. {
  262. /* USER CODE BEGIN TIM4_MspPostInit 0 */
  263. /* USER CODE END TIM4_MspPostInit 0 */
  264. __HAL_RCC_GPIOB_CLK_ENABLE();
  265. /**TIM4 GPIO Configuration
  266. PB9 ------> TIM4_CH4
  267. */
  268. GPIO_InitStruct.Pin = GPIO_PIN_9;
  269. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  270. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  271. HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
  272. /* USER CODE BEGIN TIM4_MspPostInit 1 */
  273. /* USER CODE END TIM4_MspPostInit 1 */
  274. }
  275. }
  276. void HAL_TIM_Base_MspDeInit(TIM_HandleTypeDef *tim_baseHandle)
  277. {
  278. if (tim_baseHandle->Instance == TIM1)
  279. {
  280. /* USER CODE BEGIN TIM1_MspDeInit 0 */
  281. /* USER CODE END TIM1_MspDeInit 0 */
  282. /* Peripheral clock disable */
  283. __HAL_RCC_TIM1_CLK_DISABLE();
  284. /* USER CODE BEGIN TIM1_MspDeInit 1 */
  285. /* USER CODE END TIM1_MspDeInit 1 */
  286. }
  287. else if (tim_baseHandle->Instance == TIM2)
  288. {
  289. /* USER CODE BEGIN TIM2_MspDeInit 0 */
  290. /* USER CODE END TIM2_MspDeInit 0 */
  291. /* Peripheral clock disable */
  292. __HAL_RCC_TIM2_CLK_DISABLE();
  293. /* USER CODE BEGIN TIM2_MspDeInit 1 */
  294. /* USER CODE END TIM2_MspDeInit 1 */
  295. }
  296. else if (tim_baseHandle->Instance == TIM3)
  297. {
  298. /* USER CODE BEGIN TIM3_MspDeInit 0 */
  299. /* USER CODE END TIM3_MspDeInit 0 */
  300. /* Peripheral clock disable */
  301. __HAL_RCC_TIM3_CLK_DISABLE();
  302. /* TIM3 interrupt Deinit */
  303. HAL_NVIC_DisableIRQ(TIM3_IRQn);
  304. /* USER CODE BEGIN TIM3_MspDeInit 1 */
  305. /* USER CODE END TIM3_MspDeInit 1 */
  306. }
  307. else if (tim_baseHandle->Instance == TIM4)
  308. {
  309. /* USER CODE BEGIN TIM4_MspDeInit 0 */
  310. /* USER CODE END TIM4_MspDeInit 0 */
  311. /* Peripheral clock disable */
  312. __HAL_RCC_TIM4_CLK_DISABLE();
  313. /* USER CODE BEGIN TIM4_MspDeInit 1 */
  314. /* USER CODE END TIM4_MspDeInit 1 */
  315. }
  316. }
  317. /* USER CODE BEGIN 1 */
  318. /* USER CODE END 1 */

添加文件hc_sr04.c

  1. #include "tim.h"
  2. #include "gpio.h"
  3. #include "my_i2c.h"
  4. /* 定时器1 */
  5. // 获取超声波的距离
  6. double getDistance()
  7. {
  8. int cnt = 0;
  9. // 给Trig端口至少10us的高电平
  10. HAL_GPIO_WritePin(GPIOB, GPIO_PIN_3, GPIO_PIN_SET);
  11. delay_us(15);
  12. HAL_GPIO_WritePin(GPIOB, GPIO_PIN_3, GPIO_PIN_RESET);
  13. __HAL_TIM_SetCounter(&htim1, 0); // 将定时器1的计数值设置为0
  14. // echo端口从低电平跳到高电平,开始发波,启动定时器1
  15. while (HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_4) == GPIO_PIN_RESET);
  16. __HAL_TIM_ENABLE(&htim1); // 启动定时器1
  17. // echo端口从高电平跳到低电平,停止发波,关闭定时器1
  18. while (HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_4) == GPIO_PIN_SET);
  19. __HAL_TIM_DISABLE(&htim1); // 关闭定时器1
  20. // 计算时间
  21. cnt = __HAL_TIM_GetCounter(&htim1); // 读取定时器1的计数值
  22. // 计算距离
  23. return cnt * 340 / 2 * 0.000001 * 100;
  24. }

添加文件hc_sr04.h

  1. //获取超声波的距离
  2. double getDistance(void);

添加文件sg90.c

  1. #include "sg90.h"
  2. #include "main.h"
  3. #include "tim.h"
  4. #define LEFT 1
  5. #define MIDDLE 2
  6. #define RIGHT 3
  7. uint8_t flag = LEFT;
  8. void sg90Left()
  9. {
  10. if (flag != LEFT)
  11. {
  12. flag = LEFT;
  13. __HAL_TIM_SetCompare(&htim4, TIM_CHANNEL_4, 25); // 设置舵机转动角度为180°
  14. }
  15. }
  16. void sg90Middle()
  17. {
  18. if (flag != MIDDLE)
  19. {
  20. flag = MIDDLE;
  21. __HAL_TIM_SetCompare(&htim4, TIM_CHANNEL_4, 15); // 设置舵机转动角度为90°
  22. }
  23. }
  24. void sg90Right()
  25. {
  26. if (flag != RIGHT)
  27. {
  28. flag = RIGHT;
  29. __HAL_TIM_SetCompare(&htim4, TIM_CHANNEL_4, 5); // 设置舵机转动角度为0°
  30. }
  31. }

添加文件sg90.h

  1. void sg90Left(void);
  2. void sg90Middle(void);
  3. void sg90Right(void);

添加文件bizhang.c

  1. #include "tim.h"
  2. #include "car.h"
  3. #include "sg90.h"
  4. #include "hc_sr04.h"
  5. extern double leftDistance;
  6. extern double rightDistance;
  7. extern double middleDistance;
  8. /* 避障模式 */
  9. void bizhang_mode()
  10. {
  11. sg90Middle();
  12. HAL_Delay(400);
  13. middleDistance = getDistance();
  14. if (middleDistance > 35)
  15. {
  16. car_goForward();
  17. }
  18. else
  19. {
  20. car_goStop();
  21. HAL_Delay(50);
  22. sg90Left();
  23. HAL_Delay(400);
  24. leftDistance = getDistance();
  25. sg90Middle();
  26. HAL_Delay(400);
  27. sg90Right();
  28. HAL_Delay(400);
  29. rightDistance = getDistance();
  30. if (leftDistance < 15 && rightDistance < 15)
  31. {
  32. car_goBack();
  33. HAL_Delay(500);
  34. car_goStop();
  35. HAL_Delay(50);
  36. }
  37. else
  38. {
  39. if (leftDistance > rightDistance)
  40. {
  41. car_goLeft();
  42. HAL_Delay(500);
  43. car_goStop();
  44. HAL_Delay(50);
  45. }
  46. if (rightDistance > leftDistance)
  47. {
  48. car_goRight();
  49. HAL_Delay(500);
  50. car_goStop();
  51. HAL_Delay(50);
  52. }
  53. }
  54. }
  55. }

添加文件bizhang.h

  1. /* 避障模式 */
  2. void bizhang_mode(void);

main.c文件编写

  1. /* USER CODE BEGIN Header */
  2. /**
  3. ******************************************************************************
  4. * @file : main.c
  5. * @brief : Main program body
  6. ******************************************************************************
  7. * @attention
  8. *
  9. * Copyright (c) 2023 STMicroelectronics.
  10. * All rights reserved.
  11. *
  12. * This software is licensed under terms that can be found in the LICENSE file
  13. * in the root directory of this software component.
  14. * If no LICENSE file comes with this software, it is provided AS-IS.
  15. *
  16. ******************************************************************************
  17. */
  18. /* USER CODE END Header */
  19. /* Includes ------------------------------------------------------------------*/
  20. #include "main.h"
  21. #include "tim.h"
  22. #include "usart.h"
  23. #include "gpio.h"
  24. /* Private includes ----------------------------------------------------------*/
  25. /* USER CODE BEGIN Includes */
  26. #include <stdio.h>
  27. #include <string.h>
  28. #include "car.h"
  29. #include "oled.h"
  30. #include "sg90.h"
  31. #include "hc_sr04.h"
  32. #include "my_i2c.h"
  33. #include "bizhang.h"
  34. uint8_t speed_cnt = 0;
  35. uint8_t speedMsg[24] = {0};
  36. double leftDistance = 0;
  37. double rightDistance = 0;
  38. double middleDistance = 0;
  39. /* USER CODE END Includes */
  40. /* Private typedef -----------------------------------------------------------*/
  41. /* USER CODE BEGIN PTD */
  42. /* USER CODE END PTD */
  43. /* Private define ------------------------------------------------------------*/
  44. /* USER CODE BEGIN PD */
  45. /* USER CODE END PD */
  46. /* Private macro -------------------------------------------------------------*/
  47. /* USER CODE BEGIN PM */
  48. /* USER CODE END PM */
  49. /* Private variables ---------------------------------------------------------*/
  50. /* USER CODE BEGIN PV */
  51. /* USER CODE END PV */
  52. /* Private function prototypes -----------------------------------------------*/
  53. void SystemClock_Config(void);
  54. /* USER CODE BEGIN PFP */
  55. /* USER CODE END PFP */
  56. /* Private user code ---------------------------------------------------------*/
  57. /* USER CODE BEGIN 0 */
  58. /* 引脚中断的回调函数 */
  59. void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
  60. {
  61. if (GPIO_Pin == GPIO_PIN_13)
  62. { /* 如果是PB13引脚产生的中断 */
  63. // HAL_Delay(50); /* 按键防抖,这个delay函数不知道为什么引起OLED显示失败 */
  64. if (HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_13) == GPIO_PIN_RESET)
  65. {
  66. speed_cnt++;
  67. }
  68. }
  69. }
  70. /* 定时器中断的回调函数 */
  71. void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
  72. {
  73. // 判断是否是定时器3产生的中断
  74. if (htim3.Instance == TIM3)
  75. {
  76. if (speed_cnt < 10)
  77. {
  78. sprintf((char *)speedMsg, "speed: %d cm/s ", speed_cnt);
  79. }
  80. else
  81. {
  82. sprintf((char *)speedMsg, "speed: %d cm/s ", speed_cnt);
  83. }
  84. // printf("%s\r\n",speedMsg);
  85. oledShowString(3, 1, (char *)speedMsg);
  86. speed_cnt = 0;
  87. }
  88. }
  89. /* USER CODE END 0 */
  90. /**
  91. * @brief The application entry point.
  92. * @retval int
  93. */
  94. int main(void)
  95. {
  96. /* USER CODE BEGIN 1 */
  97. /* USER CODE END 1 */
  98. /* MCU Configuration--------------------------------------------------------*/
  99. /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  100. HAL_Init();
  101. /* USER CODE BEGIN Init */
  102. /* USER CODE END Init */
  103. /* Configure the system clock */
  104. SystemClock_Config();
  105. /* USER CODE BEGIN SysInit */
  106. /* USER CODE END SysInit */
  107. /* Initialize all configured peripherals */
  108. MX_GPIO_Init();
  109. MX_USART1_UART_Init();
  110. MX_TIM2_Init();
  111. MX_TIM3_Init();
  112. MX_TIM1_Init();
  113. MX_TIM4_Init();
  114. /* USER CODE BEGIN 2 */
  115. oledInit(); // OLED初始化
  116. oledClean(); /* 清屏函数 */
  117. // 设置寻址模式
  118. oledWriteCmd(0x20); // 设置内存
  119. oledWriteCmd(0x02); // 选择页寻址模式
  120. oledShowString(1, 1, "diedie_car");
  121. oledShowString(2, 1, "move: stop");
  122. oledShowString(4, 1, "mode: bizhang");
  123. /* USER CODE END 2 */
  124. /* Infinite loop */
  125. /* USER CODE BEGIN WHILE */
  126. while (1)
  127. {
  128. /* USER CODE END WHILE */
  129. /* USER CODE BEGIN 3 */
  130. usart1_receive_data_handle(); /* 对串口读取的数据进行处理 */
  131. bizhang_mode();
  132. ;
  133. }
  134. /* USER CODE END 3 */
  135. }
  136. /**
  137. * @brief System Clock Configuration
  138. * @retval None
  139. */
  140. void SystemClock_Config(void)
  141. {
  142. RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  143. RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  144. /** Initializes the RCC Oscillators according to the specified parameters
  145. * in the RCC_OscInitTypeDef structure.
  146. */
  147. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  148. RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  149. RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
  150. RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  151. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  152. RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  153. RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;
  154. if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  155. {
  156. Error_Handler();
  157. }
  158. /** Initializes the CPU, AHB and APB buses clocks
  159. */
  160. RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2;
  161. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  162. RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  163. RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
  164. RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  165. if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
  166. {
  167. Error_Handler();
  168. }
  169. }
  170. /* USER CODE BEGIN 4 */
  171. /* USER CODE END 4 */
  172. /**
  173. * @brief This function is executed in case of error occurrence.
  174. * @retval None
  175. */
  176. void Error_Handler(void)
  177. {
  178. /* USER CODE BEGIN Error_Handler_Debug */
  179. /* User can add his own implementation to report the HAL error return state */
  180. __disable_irq();
  181. while (1)
  182. {
  183. }
  184. /* USER CODE END Error_Handler_Debug */
  185. }
  186. #ifdef USE_FULL_ASSERT
  187. /**
  188. * @brief Reports the name of the source file and the source line number
  189. * where the assert_param error has occurred.
  190. * @param file: pointer to the source file name
  191. * @param line: assert_param error line source number
  192. * @retval None
  193. */
  194. void assert_failed(uint8_t *file, uint32_t line)
  195. {
  196. /* USER CODE BEGIN 6 */
  197. /* User can add his own implementation to report the file name and line number,
  198. ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  199. /* USER CODE END 6 */
  200. }
  201. #endif /* USE_FULL_ASSERT */

跟随小车

硬件组成

红外避障模块

  • 当发射出的红外线没有被反射回来或被反射回来但强度不够大,DO输出高电平,灯灭。没有障碍物。

  • 当发射出的红外线被反射回来,DO输出低电平,灯亮。有障碍物。

  • 即有障碍物DO输出低电平,红外避障模块灯亮,没有障碍物DO输出高电平,红外避障模块灯灭。

模块与STM32F103板子接线

  • 左红外避障模块 <-> PA4

  • 右红外避障模块 <-> PA5

STM32CubeMX相关配置

GPIO配置

配置PA4引脚、PA5引脚为输入引脚。

文件编写

添加文件gensui.c

  1. #include "gpio.h"
  2. #include "car.h"
  3. #include "oled.h"
  4. /* PA4:左红外避障模块,PA5:右红外避障模块 */
  5. void followMode()
  6. {
  7. oledShowString(4, 1, "mode: gensui");
  8. if (HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_4) == GPIO_PIN_RESET && HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_5) == GPIO_PIN_RESET)
  9. { //
  10. car_goForward();
  11. }
  12. if (HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_4) == GPIO_PIN_RESET && HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_5) == GPIO_PIN_SET)
  13. {
  14. car_goLeft();
  15. }
  16. if (HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_4) == GPIO_PIN_SET && HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_5) == GPIO_PIN_RESET)
  17. {
  18. car_goRight();
  19. }
  20. if (HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_4) == GPIO_PIN_SET && HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_5) == GPIO_PIN_SET)
  21. {
  22. car_goStop();
  23. }
  24. }

添加文件gensui.h

  1. void followMode(void);

main.c文件编写

  1. /* USER CODE BEGIN Header */
  2. /**
  3. ******************************************************************************
  4. * @file : main.c
  5. * @brief : Main program body
  6. ******************************************************************************
  7. * @attention
  8. *
  9. * Copyright (c) 2023 STMicroelectronics.
  10. * All rights reserved.
  11. *
  12. * This software is licensed under terms that can be found in the LICENSE file
  13. * in the root directory of this software component.
  14. * If no LICENSE file comes with this software, it is provided AS-IS.
  15. *
  16. ******************************************************************************
  17. */
  18. /* USER CODE END Header */
  19. /* Includes ------------------------------------------------------------------*/
  20. #include "main.h"
  21. #include "tim.h"
  22. #include "usart.h"
  23. #include "gpio.h"
  24. /* Private includes ----------------------------------------------------------*/
  25. /* USER CODE BEGIN Includes */
  26. #include <stdio.h>
  27. #include <string.h>
  28. #include "car.h"
  29. #include "oled.h"
  30. #include "sg90.h"
  31. #include "hc-08.h"
  32. #include "my_i2c.h"
  33. #include "bizhang.h"
  34. #include "gensui.h"
  35. uint8_t speed_cnt = 0;
  36. uint8_t speedMsg[24] = {0};
  37. double leftDistance = 0;
  38. double rightDistance = 0;
  39. double middleDistance = 0;
  40. /* USER CODE END Includes */
  41. /* Private typedef -----------------------------------------------------------*/
  42. /* USER CODE BEGIN PTD */
  43. /* USER CODE END PTD */
  44. /* Private define ------------------------------------------------------------*/
  45. /* USER CODE BEGIN PD */
  46. /* USER CODE END PD */
  47. /* Private macro -------------------------------------------------------------*/
  48. /* USER CODE BEGIN PM */
  49. /* USER CODE END PM */
  50. /* Private variables ---------------------------------------------------------*/
  51. /* USER CODE BEGIN PV */
  52. /* USER CODE END PV */
  53. /* Private function prototypes -----------------------------------------------*/
  54. void SystemClock_Config(void);
  55. /* USER CODE BEGIN PFP */
  56. /* USER CODE END PFP */
  57. /* Private user code ---------------------------------------------------------*/
  58. /* USER CODE BEGIN 0 */
  59. /* 引脚中断的回调函数 */
  60. void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
  61. {
  62. if (GPIO_Pin == GPIO_PIN_13)
  63. { /* 如果是PB13引脚产生的中断 */
  64. // HAL_Delay(50); /* 按键防抖,这个delay函数不知道为什么引起OLED显示失败 */
  65. if (HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_13) == GPIO_PIN_RESET)
  66. {
  67. speed_cnt++;
  68. }
  69. }
  70. }
  71. /* 定时器中断的回调函数 */
  72. void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
  73. {
  74. // 判断是否是定时器3产生的中断
  75. if (htim3.Instance == TIM3)
  76. {
  77. memset(speedMsg, 0, 24);
  78. sprintf((char *)speedMsg, "speed:%d cm/s", speed_cnt);
  79. printf("%s\r\n", speedMsg);
  80. oledShowString(3, 1, (char *)speedMsg);
  81. speed_cnt = 0;
  82. }
  83. }
  84. /* USER CODE END 0 */
  85. /**
  86. * @brief The application entry point.
  87. * @retval int
  88. */
  89. int main(void)
  90. {
  91. /* USER CODE BEGIN 1 */
  92. /* USER CODE END 1 */
  93. /* MCU Configuration--------------------------------------------------------*/
  94. /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  95. HAL_Init();
  96. /* USER CODE BEGIN Init */
  97. /* USER CODE END Init */
  98. /* Configure the system clock */
  99. SystemClock_Config();
  100. /* USER CODE BEGIN SysInit */
  101. /* USER CODE END SysInit */
  102. /* Initialize all configured peripherals */
  103. MX_GPIO_Init();
  104. MX_USART1_UART_Init();
  105. MX_TIM2_Init();
  106. MX_TIM3_Init();
  107. MX_TIM1_Init();
  108. MX_TIM4_Init();
  109. /* USER CODE BEGIN 2 */
  110. oledInit(); // OLED初始化
  111. oledClean(); /* 清屏函数 */
  112. // 设置寻址模式
  113. oledWriteCmd(0x20); // 设置内存
  114. oledWriteCmd(0x02); // 选择页寻址模式
  115. oledShowString(1, 1, "diedie_car");
  116. oledShowString(2, 1, "move: stop");
  117. oledShowString(4, 1, "mode: gensui");
  118. /* USER CODE END 2 */
  119. /* Infinite loop */
  120. /* USER CODE BEGIN WHILE */
  121. while (1)
  122. {
  123. /* USER CODE END WHILE */
  124. /* USER CODE BEGIN 3 */
  125. // printf("jiangxiaoya\r\n");
  126. // HAL_Delay(1000);
  127. // followMode();
  128. usart1_receive_data_handle(); /* 对串口读取的数据进行处理 */
  129. // bizhang_mode();
  130. // printf("middle:%f\r\n",middleDistance);
  131. }
  132. /* USER CODE END 3 */
  133. }
  134. /**
  135. * @brief System Clock Configuration
  136. * @retval None
  137. */
  138. void SystemClock_Config(void)
  139. {
  140. RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  141. RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  142. /** Initializes the RCC Oscillators according to the specified parameters
  143. * in the RCC_OscInitTypeDef structure.
  144. */
  145. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  146. RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  147. RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
  148. RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  149. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  150. RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  151. RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;
  152. if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  153. {
  154. Error_Handler();
  155. }
  156. /** Initializes the CPU, AHB and APB buses clocks
  157. */
  158. RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2;
  159. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  160. RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  161. RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
  162. RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  163. if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
  164. {
  165. Error_Handler();
  166. }
  167. }
  168. /* USER CODE BEGIN 4 */
  169. /* USER CODE END 4 */
  170. /**
  171. * @brief This function is executed in case of error occurrence.
  172. * @retval None
  173. */
  174. void Error_Handler(void)
  175. {
  176. /* USER CODE BEGIN Error_Handler_Debug */
  177. /* User can add his own implementation to report the HAL error return state */
  178. __disable_irq();
  179. while (1)
  180. {
  181. }
  182. /* USER CODE END Error_Handler_Debug */
  183. }
  184. #ifdef USE_FULL_ASSERT
  185. /**
  186. * @brief Reports the name of the source file and the source line number
  187. * where the assert_param error has occurred.
  188. * @param file: pointer to the source file name
  189. * @param line: assert_param error line source number
  190. * @retval None
  191. */
  192. void assert_failed(uint8_t *file, uint32_t line)
  193. {
  194. /* USER CODE BEGIN 6 */
  195. /* User can add his own implementation to report the file name and line number,
  196. ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  197. /* USER CODE END 6 */
  198. }
  199. #endif /* USE_FULL_ASSERT */

循迹小车

硬件组成

循迹模块(TCRT5000传感器

  • 当发射出的红外线没有被反射回来或被反射回来但强度不够大时,DO输出高电平,灯灭。 黑色吸收红外线,DO输出高电平,灯亮。

  • 当发射出的红外线被反射回来或被反射回来且强度足够大,DO输出低电平,灯亮。 白色反射红外线,DO输出低电平,灯亮。

  • 即黑色DO输出高电平,模块灯灭,白色DO输出低电平,模块灯亮。

模块与STM32F103板子接线

  • 左循迹模块 <-> PA6

  • 右循迹模块 <-> PA7

STM32CubeMX相关配置

GPIO配置

配置PA6引脚、PA7引脚为输入引脚。

文件编写

添加文件xunji.c

  1. #include "gpio.h"
  2. #include "car.h"
  3. /* PA6:左红外循迹模块,PA7:右红外循迹模块 */
  4. void tracingMode()
  5. {
  6. // oledShowString(4, 1, "mode: gensui");
  7. if (HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_6) == GPIO_PIN_RESET && HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_7) == GPIO_PIN_RESET)
  8. { //
  9. car_goForward();
  10. }
  11. if (HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_6) == GPIO_PIN_RESET && HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_7) == GPIO_PIN_SET)
  12. {
  13. car_goLeft();
  14. }
  15. if (HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_6) == GPIO_PIN_SET && HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_7) == GPIO_PIN_RESET)
  16. {
  17. car_goRight();
  18. }
  19. if (HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_6) == GPIO_PIN_SET && HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_7) == GPIO_PIN_SET)
  20. {
  21. car_goStop();
  22. }
  23. }

添加文件xunji.h

  1. void tracingMode(void);

main.c文件编写

  1. /* USER CODE BEGIN Header */
  2. /**
  3. ******************************************************************************
  4. * @file : main.c
  5. * @brief : Main program body
  6. ******************************************************************************
  7. * @attention
  8. *
  9. * Copyright (c) 2023 STMicroelectronics.
  10. * All rights reserved.
  11. *
  12. * This software is licensed under terms that can be found in the LICENSE file
  13. * in the root directory of this software component.
  14. * If no LICENSE file comes with this software, it is provided AS-IS.
  15. *
  16. ******************************************************************************
  17. */
  18. /* USER CODE END Header */
  19. /* Includes ------------------------------------------------------------------*/
  20. #include "main.h"
  21. #include "tim.h"
  22. #include "usart.h"
  23. #include "gpio.h"
  24. /* Private includes ----------------------------------------------------------*/
  25. /* USER CODE BEGIN Includes */
  26. #include <stdio.h>
  27. #include <string.h>
  28. #include "car.h"
  29. #include "oled.h"
  30. #include "sg90.h"
  31. #include "hc_sr04.h"
  32. #include "my_i2c.h"
  33. #include "bizhang.h"
  34. #include "gensui.h"
  35. #include "xunji.h"
  36. uint8_t speed_cnt = 0;
  37. uint8_t speedMsg[24] = {0};
  38. double leftDistance = 0;
  39. double rightDistance = 0;
  40. double middleDistance = 0;
  41. /* USER CODE END Includes */
  42. /* Private typedef -----------------------------------------------------------*/
  43. /* USER CODE BEGIN PTD */
  44. /* USER CODE END PTD */
  45. /* Private define ------------------------------------------------------------*/
  46. /* USER CODE BEGIN PD */
  47. /* USER CODE END PD */
  48. /* Private macro -------------------------------------------------------------*/
  49. /* USER CODE BEGIN PM */
  50. /* USER CODE END PM */
  51. /* Private variables ---------------------------------------------------------*/
  52. /* USER CODE BEGIN PV */
  53. /* USER CODE END PV */
  54. /* Private function prototypes -----------------------------------------------*/
  55. void SystemClock_Config(void);
  56. /* USER CODE BEGIN PFP */
  57. /* USER CODE END PFP */
  58. /* Private user code ---------------------------------------------------------*/
  59. /* USER CODE BEGIN 0 */
  60. /* 引脚中断的回调函数 */
  61. void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
  62. {
  63. if (GPIO_Pin == GPIO_PIN_13)
  64. { /* 如果是PB13引脚产生的中断 */
  65. // HAL_Delay(50); /* 按键防抖,这个delay函数不知道为什么引起OLED显示失败 */
  66. if (HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_13) == GPIO_PIN_RESET)
  67. {
  68. speed_cnt++;
  69. }
  70. }
  71. }
  72. /* 定时器中断的回调函数 */
  73. void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
  74. {
  75. // 判断是否是定时器3产生的中断
  76. if (htim3.Instance == TIM3)
  77. {
  78. if (speed_cnt < 10)
  79. {
  80. sprintf((char *)speedMsg, "speed: %d cm/s ", speed_cnt);
  81. }
  82. else
  83. {
  84. sprintf((char *)speedMsg, "speed: %d cm/s ", speed_cnt);
  85. }
  86. // printf("%s\r\n",speedMsg);
  87. oledShowString(3, 1, (char *)speedMsg);
  88. speed_cnt = 0;
  89. }
  90. }
  91. /* USER CODE END 0 */
  92. /**
  93. * @brief The application entry point.
  94. * @retval int
  95. */
  96. int main(void)
  97. {
  98. /* USER CODE BEGIN 1 */
  99. /* USER CODE END 1 */
  100. /* MCU Configuration--------------------------------------------------------*/
  101. /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  102. HAL_Init();
  103. /* USER CODE BEGIN Init */
  104. /* USER CODE END Init */
  105. /* Configure the system clock */
  106. SystemClock_Config();
  107. /* USER CODE BEGIN SysInit */
  108. /* USER CODE END SysInit */
  109. /* Initialize all configured peripherals */
  110. MX_GPIO_Init();
  111. MX_USART1_UART_Init();
  112. MX_TIM2_Init();
  113. MX_TIM3_Init();
  114. MX_TIM1_Init();
  115. MX_TIM4_Init();
  116. /* USER CODE BEGIN 2 */
  117. oledInit(); // OLED初始化
  118. oledClean(); /* 清屏函数 */
  119. // 设置寻址模式
  120. oledWriteCmd(0x20); // 设置内存
  121. oledWriteCmd(0x02); // 选择页寻址模式
  122. oledShowString(1, 1, "diedie_car");
  123. oledShowString(2, 1, "move: stop");
  124. oledShowString(4, 1, "mode: xunji");
  125. /* USER CODE END 2 */
  126. /* Infinite loop */
  127. /* USER CODE BEGIN WHILE */
  128. while (1)
  129. {
  130. /* USER CODE END WHILE */
  131. /* USER CODE BEGIN 3 */
  132. tracingMode();
  133. usart1_receive_data_handle(); /* 对串口读取的数据进行处理 */
  134. }
  135. /* USER CODE END 3 */
  136. }
  137. /**
  138. * @brief System Clock Configuration
  139. * @retval None
  140. */
  141. void SystemClock_Config(void)
  142. {
  143. RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  144. RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  145. /** Initializes the RCC Oscillators according to the specified parameters
  146. * in the RCC_OscInitTypeDef structure.
  147. */
  148. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  149. RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  150. RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
  151. RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  152. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  153. RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  154. RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;
  155. if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  156. {
  157. Error_Handler();
  158. }
  159. /** Initializes the CPU, AHB and APB buses clocks
  160. */
  161. RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2;
  162. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  163. RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  164. RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
  165. RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  166. if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
  167. {
  168. Error_Handler();
  169. }
  170. }
  171. /* USER CODE BEGIN 4 */
  172. /* USER CODE END 4 */
  173. /**
  174. * @brief This function is executed in case of error occurrence.
  175. * @retval None
  176. */
  177. void Error_Handler(void)
  178. {
  179. /* USER CODE BEGIN Error_Handler_Debug */
  180. /* User can add his own implementation to report the HAL error return state */
  181. __disable_irq();
  182. while (1)
  183. {
  184. }
  185. /* USER CODE END Error_Handler_Debug */
  186. }
  187. #ifdef USE_FULL_ASSERT
  188. /**
  189. * @brief Reports the name of the source file and the source line number
  190. * where the assert_param error has occurred.
  191. * @param file: pointer to the source file name
  192. * @param line: assert_param error line source number
  193. * @retval None
  194. */
  195. void assert_failed(uint8_t *file, uint32_t line)
  196. {
  197. /* USER CODE BEGIN 6 */
  198. /* User can add his own implementation to report the file name and line number,
  199. ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  200. /* USER CODE END 6 */
  201. }
  202. #endif /* USE_FULL_ASSERT */

语音控制小车,循迹、跟随、避障三种功能切换

硬件组成

语音模块SU-03T

可以通过智能公元平台配置语音词条。

智能公元产品使用方法

  1. 创建产品

  1. 选择对应的模块

  1. 配置相应的信息

  1. 根据需要将某些引脚配置成串口或GPIO口

  1. 添加SU-03T语音模块的唤醒词

  1. 添加命令词

  1. 为某个命令词添加控制,通过某个引脚输出电平或者通过串口输出信息

  1. 配置其他信息

  1. 生成SDK

  1. 下载固件压缩包

  1. 将固件包解压到没中文路径的文件夹

  1. 去智能公元官方下载烧录软件

  1. 打开烧录软件,烧录固件

  1. 选择刚刚生成的固件烧录到语音

模块与STM32F103板子接线

  • SU-03T的PB6引脚 <-> PA9(USART1_TX引脚)

  • SU-03T的PB7引脚 <-> PA10(USART1_RX引脚)

文件编写

修改usart.c文件

  1. /* USER CODE BEGIN Header */
  2. /**
  3. ******************************************************************************
  4. * @file usart.c
  5. * @brief This file provides code for the configuration
  6. * of the USART instances.
  7. ******************************************************************************
  8. * @attention
  9. *
  10. * Copyright (c) 2023 STMicroelectronics.
  11. * All rights reserved.
  12. *
  13. * This software is licensed under terms that can be found in the LICENSE file
  14. * in the root directory of this software component.
  15. * If no LICENSE file comes with this software, it is provided AS-IS.
  16. *
  17. ******************************************************************************
  18. */
  19. /* USER CODE END Header */
  20. /* Includes ------------------------------------------------------------------*/
  21. #include "usart.h"
  22. /* USER CODE BEGIN 0 */
  23. #include <stdio.h>
  24. #include <string.h>
  25. #include "car.h"
  26. #include "oled.h"
  27. #define USART_REC_LEN 200
  28. extern uint8_t mode_mark;
  29. // 串口接收缓存(1字节)
  30. uint8_t buf = 0;
  31. uint8_t UART1_RX_Buffer[USART_REC_LEN]; // 接收缓冲,串口接收的数据存放地点
  32. // 串口接收状态,16位
  33. uint16_t UART1_RX_STA = 0;
  34. // bit15: 如果是1表示接收完成
  35. // bit14: 如果是1表示接收到回车(0x0d)
  36. // bit13~bit0: 接收到的有效字节数目
  37. /* USER CODE END 0 */
  38. UART_HandleTypeDef huart1;
  39. /* USART1 init function */
  40. void MX_USART1_UART_Init(void)
  41. {
  42. /* USER CODE BEGIN USART1_Init 0 */
  43. /* USER CODE END USART1_Init 0 */
  44. /* USER CODE BEGIN USART1_Init 1 */
  45. /* USER CODE END USART1_Init 1 */
  46. huart1.Instance = USART1;
  47. huart1.Init.BaudRate = 115200;
  48. huart1.Init.WordLength = UART_WORDLENGTH_8B;
  49. huart1.Init.StopBits = UART_STOPBITS_1;
  50. huart1.Init.Parity = UART_PARITY_NONE;
  51. huart1.Init.Mode = UART_MODE_TX_RX;
  52. huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  53. huart1.Init.OverSampling = UART_OVERSAMPLING_16;
  54. if (HAL_UART_Init(&huart1) != HAL_OK)
  55. {
  56. Error_Handler();
  57. }
  58. /* USER CODE BEGIN USART1_Init 2 */
  59. /* 开启串口1的接收中断 */
  60. HAL_UART_Receive_IT(&huart1, &buf, 1); /* 每接收一个串口数据调用一次串口接收完成回调函数 */
  61. /* USER CODE END USART1_Init 2 */
  62. }
  63. void HAL_UART_MspInit(UART_HandleTypeDef *uartHandle)
  64. {
  65. GPIO_InitTypeDef GPIO_InitStruct = {0};
  66. if (uartHandle->Instance == USART1)
  67. {
  68. /* USER CODE BEGIN USART1_MspInit 0 */
  69. /* USER CODE END USART1_MspInit 0 */
  70. /* USART1 clock enable */
  71. __HAL_RCC_USART1_CLK_ENABLE();
  72. __HAL_RCC_GPIOA_CLK_ENABLE();
  73. /**USART1 GPIO Configuration
  74. PA9 ------> USART1_TX
  75. PA10 ------> USART1_RX
  76. */
  77. GPIO_InitStruct.Pin = GPIO_PIN_9;
  78. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  79. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
  80. HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  81. GPIO_InitStruct.Pin = GPIO_PIN_10;
  82. GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
  83. GPIO_InitStruct.Pull = GPIO_NOPULL;
  84. HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  85. /* USART1 interrupt Init */
  86. HAL_NVIC_SetPriority(USART1_IRQn, 0, 0);
  87. HAL_NVIC_EnableIRQ(USART1_IRQn);
  88. /* USER CODE BEGIN USART1_MspInit 1 */
  89. /* USER CODE END USART1_MspInit 1 */
  90. }
  91. }
  92. void HAL_UART_MspDeInit(UART_HandleTypeDef *uartHandle)
  93. {
  94. if (uartHandle->Instance == USART1)
  95. {
  96. /* USER CODE BEGIN USART1_MspDeInit 0 */
  97. /* USER CODE END USART1_MspDeInit 0 */
  98. /* Peripheral clock disable */
  99. __HAL_RCC_USART1_CLK_DISABLE();
  100. /**USART1 GPIO Configuration
  101. PA9 ------> USART1_TX
  102. PA10 ------> USART1_RX
  103. */
  104. HAL_GPIO_DeInit(GPIOA, GPIO_PIN_9 | GPIO_PIN_10);
  105. /* USART1 interrupt Deinit */
  106. HAL_NVIC_DisableIRQ(USART1_IRQn);
  107. /* USER CODE BEGIN USART1_MspDeInit 1 */
  108. HAL_UART_Receive_IT(&huart1, &buf, 1);
  109. /* USER CODE END USART1_MspDeInit 1 */
  110. }
  111. }
  112. /* USER CODE BEGIN 1 */
  113. /* 重写stdio.h文件中的prinft()里的fputc()函数 */
  114. int fputc(int my_data, FILE *p)
  115. {
  116. unsigned char temp = my_data;
  117. // 改写后,使用printf()函数会将数据通过串口一发送出去
  118. HAL_UART_Transmit(&huart1, &temp, 1, 0xffff); // 0xfffff为最大超时时间
  119. return my_data;
  120. }
  121. /* 串口接收完成回调函数 */
  122. void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
  123. {
  124. // 判断中断是哪个串口触发的
  125. if (huart->Instance == USART1)
  126. {
  127. // 判断接收是否完成,即判断UART1_RX_STA的bit15是否为1
  128. if (!(UART1_RX_STA & 0x8000))
  129. { // 如果没接收完成就进入接收流程
  130. // 判断是否接收到回车0x0d
  131. if (UART1_RX_STA & 0x4000)
  132. {
  133. // 判断是否接收到换行0x0a
  134. if (buf == 0x0a)
  135. {
  136. // 如果回车和换行都接收到了,则表示接收完成,即把bit15拉高
  137. UART1_RX_STA |= 0x8000;
  138. }
  139. else
  140. { // 如果接收到回车0x0d没有接收到换行0x0a
  141. // 则认为接收错误,重新开始接收
  142. UART1_RX_STA = 0;
  143. }
  144. }
  145. else
  146. { // 如果没有接收到回车0x0d
  147. // 则判断收到的这个字符是否是回车0x0d
  148. if (buf == 0x0d)
  149. {
  150. // 如果这个字符是回车,则将将bit14拉高,表示接收到回车
  151. UART1_RX_STA |= 0x4000;
  152. }
  153. else
  154. { // 如果不是回车
  155. // 则将这个字符存放到缓存数组中
  156. UART1_RX_Buffer[UART1_RX_STA & 0x3ffff] = buf;
  157. UART1_RX_STA++;
  158. // 如果接收数据大于UART1_REC_LEN(200字节),则重新开始接收
  159. if (UART1_RX_STA > USART_REC_LEN - 1)
  160. {
  161. UART1_RX_STA = 0;
  162. }
  163. }
  164. }
  165. }
  166. // 如果接收完成则重新开启串口1的接收中断
  167. HAL_UART_Receive_IT(&huart1, &buf, 1);
  168. }
  169. }
  170. /* 对串口接收数据的处理 */
  171. void usart1_receive_data_handle()
  172. {
  173. /* 判断判断串口是否接收完成 */
  174. if (UART1_RX_STA & 0x8000)
  175. {
  176. printf("接收完成\r\n");
  177. // 串口接收完数据后,对串口数据进行处理
  178. if (!strcmp((const char *)UART1_RX_Buffer, "bizhang"))
  179. {
  180. printf("避障模式\r\n");
  181. mode_mark = 0;
  182. }
  183. else if (!strcmp((const char *)UART1_RX_Buffer, "gensui"))
  184. {
  185. printf("跟随模式\r\n");
  186. mode_mark = 1;
  187. }
  188. else if (!strcmp((const char *)UART1_RX_Buffer, "xunji"))
  189. {
  190. printf("循迹模式\r\n");
  191. mode_mark = 2;
  192. }
  193. // 接收到其他数据,进行报错
  194. else
  195. {
  196. if (UART1_RX_Buffer[0] != '\0')
  197. {
  198. printf("%s\r\n", "输入错误,请重新输入");
  199. }
  200. }
  201. // 换行,重新开始下一次接收
  202. memset(UART1_RX_Buffer, 0, USART_REC_LEN);
  203. // printf("\r\n");
  204. UART1_RX_STA = 0;
  205. }
  206. }
  207. /* USER CODE END 1 */

main.c文件编写

  1. /* USER CODE BEGIN Header */
  2. /**
  3. ******************************************************************************
  4. * @file : main.c
  5. * @brief : Main program body
  6. ******************************************************************************
  7. * @attention
  8. *
  9. * Copyright (c) 2023 STMicroelectronics.
  10. * All rights reserved.
  11. *
  12. * This software is licensed under terms that can be found in the LICENSE file
  13. * in the root directory of this software component.
  14. * If no LICENSE file comes with this software, it is provided AS-IS.
  15. *
  16. ******************************************************************************
  17. */
  18. /* USER CODE END Header */
  19. /* Includes ------------------------------------------------------------------*/
  20. #include "main.h"
  21. #include "tim.h"
  22. #include "usart.h"
  23. #include "gpio.h"
  24. /* Private includes ----------------------------------------------------------*/
  25. /* USER CODE BEGIN Includes */
  26. #include <stdio.h>
  27. #include <string.h>
  28. #include "car.h"
  29. #include "oled.h"
  30. #include "sg90.h"
  31. #include "hc-08.h"
  32. #include "my_i2c.h"
  33. #include "bizhang.h"
  34. #include "gensui.h"
  35. #include "xunji.h"
  36. #define BZ 0
  37. #define GS 1
  38. #define XJ 2
  39. uint8_t speed_cnt = 0;
  40. uint8_t speedMsg[24] = {0};
  41. uint8_t mode_mark = 0;
  42. uint8_t mark = 0;
  43. double leftDistance = 0;
  44. double rightDistance = 0;
  45. double middleDistance = 0;
  46. /* USER CODE END Includes */
  47. /* Private typedef -----------------------------------------------------------*/
  48. /* USER CODE BEGIN PTD */
  49. /* USER CODE END PTD */
  50. /* Private define ------------------------------------------------------------*/
  51. /* USER CODE BEGIN PD */
  52. /* USER CODE END PD */
  53. /* Private macro -------------------------------------------------------------*/
  54. /* USER CODE BEGIN PM */
  55. /* USER CODE END PM */
  56. /* Private variables ---------------------------------------------------------*/
  57. /* USER CODE BEGIN PV */
  58. /* USER CODE END PV */
  59. /* Private function prototypes -----------------------------------------------*/
  60. void SystemClock_Config(void);
  61. /* USER CODE BEGIN PFP */
  62. /* USER CODE END PFP */
  63. /* Private user code ---------------------------------------------------------*/
  64. /* USER CODE BEGIN 0 */
  65. /* 引脚中断的回调函数 */
  66. void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
  67. {
  68. if (GPIO_Pin == GPIO_PIN_13)
  69. { /* 如果是PB13引脚产生的中断 */
  70. // HAL_Delay(50); /* 按键防抖,这个delay函数不知道为什么引起OLED显示失败 */
  71. if (HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_13) == GPIO_PIN_RESET)
  72. {
  73. speed_cnt++;
  74. }
  75. }
  76. }
  77. /* 定时器中断的回调函数 */
  78. void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
  79. {
  80. // 判断是否是定时器3产生的中断
  81. if (htim3.Instance == TIM3)
  82. {
  83. if (speed_cnt < 10)
  84. {
  85. sprintf((char *)speedMsg, "speed: %d cm/s ", speed_cnt);
  86. }
  87. else
  88. {
  89. sprintf((char *)speedMsg, "speed: %d cm/s ", speed_cnt);
  90. }
  91. // printf("%s\r\n",speedMsg);
  92. oledShowString(3, 1, (char *)speedMsg);
  93. speed_cnt = 0;
  94. }
  95. }
  96. void voice_handle()
  97. {
  98. if (mode_mark == 0) /* 避障模式 */
  99. {
  100. if (mark != BZ)
  101. {
  102. oled_rowClean(4);
  103. oledShowString(4, 1, "mode: bizhang");
  104. }
  105. mark = BZ;
  106. avoidMode();
  107. }
  108. else if (mode_mark == 1) /* 跟随模式 */
  109. {
  110. if (mark != GS)
  111. {
  112. oled_rowClean(4);
  113. oledShowString(4, 1, "mode: gensui");
  114. }
  115. mark = GS;
  116. followMode();
  117. }
  118. else if (mode_mark == 2) /* 循迹模式 */
  119. {
  120. if (mark != XJ)
  121. {
  122. oled_rowClean(4);
  123. oledShowString(4, 1, "mode: xunji");
  124. }
  125. mark = XJ;
  126. tracingMode();
  127. }
  128. else
  129. {
  130. oled_rowClean(4);
  131. oledShowString(4, 1, "voice error");
  132. }
  133. }
  134. /* USER CODE END 0 */
  135. /**
  136. * @brief The application entry point.
  137. * @retval int
  138. */
  139. int main(void)
  140. {
  141. /* USER CODE BEGIN 1 */
  142. /* USER CODE END 1 */
  143. /* MCU Configuration--------------------------------------------------------*/
  144. /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  145. HAL_Init();
  146. /* USER CODE BEGIN Init */
  147. /* USER CODE END Init */
  148. /* Configure the system clock */
  149. SystemClock_Config();
  150. /* USER CODE BEGIN SysInit */
  151. /* USER CODE END SysInit */
  152. /* Initialize all configured peripherals */
  153. MX_GPIO_Init();
  154. MX_USART1_UART_Init();
  155. MX_TIM2_Init();
  156. MX_TIM3_Init();
  157. MX_TIM1_Init();
  158. MX_TIM4_Init();
  159. /* USER CODE BEGIN 2 */
  160. oledInit(); // OLED初始化
  161. oledClean(); /* 清屏函数 */
  162. // 设置寻址模式
  163. oledWriteCmd(0x20); // 设置内存
  164. oledWriteCmd(0x02); // 选择页寻址模式
  165. oledShowString(1, 1, "diedie_car");
  166. oledShowString(2, 1, "move: stop");
  167. oledShowString(4, 1, "mode: bizhang");
  168. /* USER CODE END 2 */
  169. /* Infinite loop */
  170. /* USER CODE BEGIN WHILE */
  171. while (1)
  172. {
  173. /* USER CODE END WHILE */
  174. /* USER CODE BEGIN 3 */
  175. voice_handle();
  176. usart1_receive_data_handle(); /* 对串口读取的数据进行处理 */
  177. }
  178. /* USER CODE END 3 */
  179. }
  180. /**
  181. * @brief System Clock Configuration
  182. * @retval None
  183. */
  184. void SystemClock_Config(void)
  185. {
  186. RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  187. RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  188. /** Initializes the RCC Oscillators according to the specified parameters
  189. * in the RCC_OscInitTypeDef structure.
  190. */
  191. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  192. RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  193. RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
  194. RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  195. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  196. RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  197. RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;
  198. if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  199. {
  200. Error_Handler();
  201. }
  202. /** Initializes the CPU, AHB and APB buses clocks
  203. */
  204. RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2;
  205. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  206. RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  207. RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
  208. RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  209. if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
  210. {
  211. Error_Handler();
  212. }
  213. }
  214. /* USER CODE BEGIN 4 */
  215. /* USER CODE END 4 */
  216. /**
  217. * @brief This function is executed in case of error occurrence.
  218. * @retval None
  219. */
  220. void Error_Handler(void)
  221. {
  222. /* USER CODE BEGIN Error_Handler_Debug */
  223. /* User can add his own implementation to report the HAL error return state */
  224. __disable_irq();
  225. while (1)
  226. {
  227. }
  228. /* USER CODE END Error_Handler_Debug */
  229. }
  230. #ifdef USE_FULL_ASSERT
  231. /**
  232. * @brief Reports the name of the source file and the source line number
  233. * where the assert_param error has occurred.
  234. * @param file: pointer to the source file name
  235. * @param line: assert_param error line source number
  236. * @retval None
  237. */
  238. void assert_failed(uint8_t *file, uint32_t line)
  239. {
  240. /* USER CODE BEGIN 6 */
  241. /* User can add his own implementation to report the file name and line number,
  242. ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  243. /* USER CODE END 6 */
  244. }
  245. #endif /* USE_FULL_ASSERT */
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/article/detail/46230?site
推荐阅读
相关标签
  

闽ICP备14008679号