From 89b22e459b33a493d5325702f2ae57fb81ff304a Mon Sep 17 00:00:00 2001 From: EN Date: Thu, 18 Jul 2024 11:29:28 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=AE=9A=E6=97=B6=E5=99=A8?= =?UTF-8?q?=E6=A8=A1=E5=9D=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Core/Src/main.c | 103 +++--- MDK-ARM/CAR.uvoptx | 448 ++++++++++++------------ MDK-ARM/CAR.uvprojx | 145 ++++---- Peripheral/Inc/buzzer.h | 13 + Peripheral/Inc/line_seek.h | 2 +- Peripheral/Src/buzzer.c | 20 ++ Peripheral/Src/line_seek.c | 1 + {Peripheral => Service}/Inc/path_plan.h | 0 Service/Inc/timer.h | 16 + {Peripheral => Service}/Src/path_plan.c | 0 Service/Src/timer.c | 63 ++++ 11 files changed, 472 insertions(+), 339 deletions(-) create mode 100644 Peripheral/Inc/buzzer.h create mode 100644 Peripheral/Src/buzzer.c rename {Peripheral => Service}/Inc/path_plan.h (100%) create mode 100644 Service/Inc/timer.h rename {Peripheral => Service}/Src/path_plan.c (100%) create mode 100644 Service/Src/timer.c diff --git a/Core/Src/main.c b/Core/Src/main.c index 90d221e..dfa645b 100644 --- a/Core/Src/main.c +++ b/Core/Src/main.c @@ -27,11 +27,13 @@ /* USER CODE BEGIN Includes */ #include "app_line_seek.h" #include "bluetooth.h" +#include "buzzer.h" #include "hcsr04.h" #include "led.h" #include "motor.h" #include "path_plan.h" #include "syscalls.h" +#include "timer.h" /* USER CODE END Includes */ /* Private typedef -----------------------------------------------------------*/ @@ -70,15 +72,15 @@ void SystemClock_Config(void); /* USER CODE END 0 */ /** - * @brief The application entry point. - * @retval int - */ + * @brief The application entry point. + * @retval int + */ int main(void) { /* USER CODE BEGIN 1 */ - uint8_t pData[10] = {1, 2, 3}; + uint8_t pData[10] = {1, 2, 3}; /* USER CODE END 1 */ /* MCU Configuration--------------------------------------------------------*/ @@ -105,56 +107,58 @@ int main(void) MX_TIM3_Init(); MX_TIM4_Init(); // MX_USART1_UART_Init(); - MX_USART2_UART_Init(); + // MX_USART2_UART_Init(); /* USER CODE BEGIN 2 */ - HC_SR04_Init(); - BLUETOOTH_Init(); - MOTOR_Init(); - PathPlanner_Init(); - LineSeek_Init(); + // HC_SR04_Init(); + // BLUETOOTH_Init(); + // MOTOR_Init(); + // PathPlanner_Init(); + // LineSeek_Init(); + TIMER_Init(); /* USER CODE END 2 */ /* Infinite loop */ /* USER CODE BEGIN WHILE */ - while (1) - { - // my_printf(HUART1, "%s\r\n", *pData); + while (1) + { - // if (HAL_OK == HAL_UART_Receive(&huart2, (uint8_t *)pData, 2, 1000)) - // { - // HAL_UART_Transmit(&huart2, (uint8_t *)pData, sizeof(pData), 1000); - // HAL_Delay(500); - // } + BUZZER_StartTimed(3); + HAL_Delay(1000); + // my_printf(HUART1, "%s\r\n", *pData); - // HAL_Delay(2000); + // if (HAL_OK == HAL_UART_Receive(&huart2, (uint8_t *)pData, 2, 1000)) + // { + // HAL_UART_Transmit(&huart2, (uint8_t *)pData, sizeof(pData), 1000); + // HAL_Delay(500); + // } - //App_LineSeek(); - SYN_FrameInfo(2, "[v9][m0][t1]????ok"); - HAL_Delay(3000); + // HAL_Delay(2000); - // MOTOR_SetDuty(-20,20); + // App_LineSeek(); - // HAL_Delay(300); // ???300?? + // MOTOR_SetDuty(-20,20); + + // HAL_Delay(300); // ??300?? /* USER CODE END WHILE */ /* USER CODE BEGIN 3 */ - } + } /* USER CODE END 3 */ } /** - * @brief System Clock Configuration - * @retval None - */ + * @brief System Clock Configuration + * @retval None + */ void SystemClock_Config(void) { RCC_OscInitTypeDef RCC_OscInitStruct = {0}; RCC_ClkInitTypeDef RCC_ClkInitStruct = {0}; /** Initializes the RCC Oscillators according to the specified parameters - * in the RCC_OscInitTypeDef structure. - */ + * in the RCC_OscInitTypeDef structure. + */ RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE; RCC_OscInitStruct.HSEState = RCC_HSE_ON; RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1; @@ -168,9 +172,8 @@ void SystemClock_Config(void) } /** Initializes the CPU, AHB and APB buses clocks - */ - RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK - |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2; + */ + RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2; RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK; RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1; RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2; @@ -187,33 +190,33 @@ void SystemClock_Config(void) /* USER CODE END 4 */ /** - * @brief This function is executed in case of error occurrence. - * @retval None - */ + * @brief This function is executed in case of error occurrence. + * @retval None + */ void Error_Handler(void) { /* USER CODE BEGIN Error_Handler_Debug */ - /* User can add his own implementation to report the HAL error return state */ - __disable_irq(); - while (1) - { - } + /* User can add his own implementation to report the HAL error return state */ + __disable_irq(); + while (1) + { + } /* USER CODE END Error_Handler_Debug */ } -#ifdef USE_FULL_ASSERT +#ifdef USE_FULL_ASSERT /** - * @brief Reports the name of the source file and the source line number - * where the assert_param error has occurred. - * @param file: pointer to the source file name - * @param line: assert_param error line source number - * @retval None - */ + * @brief Reports the name of the source file and the source line number + * where the assert_param error has occurred. + * @param file: pointer to the source file name + * @param line: assert_param error line source number + * @retval None + */ void assert_failed(uint8_t *file, uint32_t line) { /* USER CODE BEGIN 6 */ - /* User can add his own implementation to report the file name and line number, - ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */ + /* User can add his own implementation to report the file name and line number, + ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */ /* USER CODE END 6 */ } #endif /* USE_FULL_ASSERT */ diff --git a/MDK-ARM/CAR.uvoptx b/MDK-ARM/CAR.uvoptx index 90ae7ad..8b9c252 100644 --- a/MDK-ARM/CAR.uvoptx +++ b/MDK-ARM/CAR.uvoptx @@ -1,4 +1,4 @@ - + 1.0 @@ -45,7 +45,7 @@ 79 66 8 - + 1 @@ -104,16 +104,16 @@ 0 0 3 - - - - - - - - - - + + + + + + + + + + BIN\CMSIS_AGDI.dll @@ -130,7 +130,7 @@ 0 ARMDBGFLAGS - + 0 @@ -140,7 +140,7 @@ 0 DLGUARM - + 0 @@ -162,7 +162,7 @@ 0 1 ..\Peripheral\Src\bluetooth.c - + \\CAR\../Peripheral/Src/bluetooth.c\34 @@ -178,7 +178,7 @@ 0 1 ..\Peripheral\Src\control.c - + \\CAR\../Peripheral/Src/control.c\32 @@ -194,8 +194,8 @@ 0 0 ..\Peripheral\Src\bluetooth.c - - + + 3 @@ -210,8 +210,8 @@ 0 0 ../Core/Src/main.c - - + + 4 @@ -226,8 +226,8 @@ 0 0 ../Core/Src/main.c - - + + @@ -282,19 +282,19 @@ 0 0 - - + + 0 0 0 - - - - - - - - + + + + + + + + 1 1 @@ -418,18 +418,6 @@ 0 0 0 - ../Core/Src/i2c.c - i2c.c - 0 - 0 - - - 3 - 9 - 1 - 0 - 0 - 0 ../Core/Src/tim.c tim.c 0 @@ -437,7 +425,7 @@ 3 - 10 + 9 1 0 0 @@ -449,7 +437,7 @@ 3 - 11 + 10 1 0 0 @@ -461,7 +449,7 @@ 3 - 12 + 11 1 0 0 @@ -473,6 +461,38 @@ + + Application/User/Service + 1 + 0 + 0 + 0 + + 4 + 12 + 1 + 0 + 0 + 0 + ..\Service\Src\path_plan.c + path_plan.c + 0 + 0 + + + 4 + 13 + 1 + 0 + 0 + 0 + ..\Service\Src\timer.c + timer.c + 0 + 0 + + + Application/User/Peripheral 0 @@ -480,8 +500,8 @@ 0 0 - 4 - 13 + 5 + 14 1 0 0 @@ -492,8 +512,20 @@ 0 - 4 - 14 + 5 + 15 + 1 + 0 + 0 + 0 + ..\Peripheral\Src\buzzer.c + buzzer.c + 0 + 0 + + + 5 + 16 1 0 0 @@ -504,8 +536,8 @@ 0 - 4 - 15 + 5 + 17 1 0 0 @@ -516,8 +548,8 @@ 0 - 4 - 16 + 5 + 18 1 0 0 @@ -528,8 +560,8 @@ 0 - 4 - 17 + 5 + 19 1 0 0 @@ -540,8 +572,8 @@ 0 - 4 - 18 + 5 + 20 1 0 0 @@ -552,8 +584,8 @@ 0 - 4 - 19 + 5 + 21 1 0 0 @@ -564,31 +596,7 @@ 0 - 4 - 20 - 1 - 0 - 0 - 0 - ..\Peripheral\Src\path_plan.c - path_plan.c - 0 - 0 - - - 4 - 21 - 1 - 0 - 0 - 0 - ..\Peripheral\Src\servo.c - servo.c - 0 - 0 - - - 4 + 5 22 1 0 @@ -608,7 +616,7 @@ 0 0 - 5 + 6 23 1 0 @@ -620,152 +628,20 @@ 0 - 5 + 6 24 1 0 0 0 - ../Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_i2c.c - stm32f1xx_hal_i2c.c - 0 - 0 - - - 5 - 25 - 1 - 0 - 0 - 0 - ../Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal.c - stm32f1xx_hal.c - 0 - 0 - - - 5 - 26 - 1 - 0 - 0 - 0 - ../Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_rcc.c - stm32f1xx_hal_rcc.c - 0 - 0 - - - 5 - 27 - 1 - 0 - 0 - 0 - ../Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_rcc_ex.c - stm32f1xx_hal_rcc_ex.c - 0 - 0 - - - 5 - 28 - 1 - 0 - 0 - 0 - ../Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_gpio.c - stm32f1xx_hal_gpio.c - 0 - 0 - - - 5 - 29 - 1 - 0 - 0 - 0 - ../Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_dma.c - stm32f1xx_hal_dma.c - 0 - 0 - - - 5 - 30 - 1 - 0 - 0 - 0 - ../Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_cortex.c - stm32f1xx_hal_cortex.c - 0 - 0 - - - 5 - 31 - 1 - 0 - 0 - 0 - ../Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_pwr.c - stm32f1xx_hal_pwr.c - 0 - 0 - - - 5 - 32 - 1 - 0 - 0 - 0 - ../Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_flash.c - stm32f1xx_hal_flash.c - 0 - 0 - - - 5 - 33 - 1 - 0 - 0 - 0 - ../Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_flash_ex.c - stm32f1xx_hal_flash_ex.c - 0 - 0 - - - 5 - 34 - 1 - 0 - 0 - 0 - ../Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_exti.c - stm32f1xx_hal_exti.c - 0 - 0 - - - 5 - 35 - 1 - 0 - 0 - 0 ../Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_tim.c stm32f1xx_hal_tim.c 0 0 - 5 - 36 + 6 + 25 1 0 0 @@ -776,8 +652,128 @@ 0 - 5 - 37 + 6 + 26 + 1 + 0 + 0 + 0 + ../Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal.c + stm32f1xx_hal.c + 0 + 0 + + + 6 + 27 + 1 + 0 + 0 + 0 + ../Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_rcc.c + stm32f1xx_hal_rcc.c + 0 + 0 + + + 6 + 28 + 1 + 0 + 0 + 0 + ../Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_rcc_ex.c + stm32f1xx_hal_rcc_ex.c + 0 + 0 + + + 6 + 29 + 1 + 0 + 0 + 0 + ../Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_gpio.c + stm32f1xx_hal_gpio.c + 0 + 0 + + + 6 + 30 + 1 + 0 + 0 + 0 + ../Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_dma.c + stm32f1xx_hal_dma.c + 0 + 0 + + + 6 + 31 + 1 + 0 + 0 + 0 + ../Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_cortex.c + stm32f1xx_hal_cortex.c + 0 + 0 + + + 6 + 32 + 1 + 0 + 0 + 0 + ../Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_pwr.c + stm32f1xx_hal_pwr.c + 0 + 0 + + + 6 + 33 + 1 + 0 + 0 + 0 + ../Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_flash.c + stm32f1xx_hal_flash.c + 0 + 0 + + + 6 + 34 + 1 + 0 + 0 + 0 + ../Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_flash_ex.c + stm32f1xx_hal_flash_ex.c + 0 + 0 + + + 6 + 35 + 1 + 0 + 0 + 0 + ../Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_exti.c + stm32f1xx_hal_exti.c + 0 + 0 + + + 6 + 36 1 0 0 @@ -796,8 +792,8 @@ 0 0 - 6 - 38 + 7 + 37 1 0 0 diff --git a/MDK-ARM/CAR.uvprojx b/MDK-ARM/CAR.uvprojx index 75de188..3424e78 100644 --- a/MDK-ARM/CAR.uvprojx +++ b/MDK-ARM/CAR.uvprojx @@ -1,7 +1,10 @@ - - + + + 2.1 +
### uVision Project, (C) Keil Software
+ CAR @@ -16,28 +19,28 @@ Keil.STM32F1xx_DFP.2.4.1 https://www.keil.com/pack/ IRAM(0x20000000-0x20004FFF) IROM(0x8000000-0x800FFFF) CLOCK(8000000) CPUTYPE("Cortex-M3") TZ - - - + + + 0 - - - - - - - - - - + + + + + + + + + + $$Device:STM32F103C8$SVD\STM32F103xx.svd 0 0 - - - - - + + + + + 0 0 @@ -52,15 +55,15 @@ 1 1 1 - + 1 0 0 0 0 - - + + 0 0 0 @@ -69,8 +72,8 @@ 0 0 - - + + 0 0 0 @@ -79,15 +82,15 @@ 0 1 - - + + 0 0 0 0 1 - + 0 @@ -101,8 +104,8 @@ 0 0 3 - - + + 0 @@ -111,7 +114,7 @@ DCM.DLL -pCM3 SARMCM3.DLL - + TCM.DLL -pCM3 @@ -136,10 +139,10 @@ 1 BIN\UL2CM3.DLL "" () - - - - + + + + 0 @@ -172,7 +175,7 @@ 0 0 "Cortex-M3" - + 0 0 0 @@ -306,7 +309,7 @@ 0x0 - + 1 @@ -333,10 +336,10 @@ 0 0 - + USE_HAL_DRIVER,STM32F103xB - - ../App/Inc;../Core/Inc;../Peripheral/Inc;../Drivers/STM32F1xx_HAL_Driver/Inc;../Drivers/STM32F1xx_HAL_Driver/Inc/Legacy;../Drivers/CMSIS/Device/ST/STM32F1xx/Include;../Drivers/CMSIS/Include + + ../App/Inc;../Core/Inc;../Service/Inc;../Peripheral/Inc;../Drivers/STM32F1xx_HAL_Driver/Inc;../Drivers/STM32F1xx_HAL_Driver/Inc/Legacy;../Drivers/CMSIS/Device/ST/STM32F1xx/Include;../Drivers/CMSIS/Include @@ -351,10 +354,10 @@ 0 1 - - - - + + + + @@ -364,15 +367,15 @@ 0 1 0 - - - - - - - - - + + + + + + + + + @@ -447,6 +450,21 @@
+ + Application/User/Service + + + path_plan.c + 1 + ..\Service\Src\path_plan.c + + + timer.c + 1 + ..\Service\Src\timer.c + + + Application/User/Peripheral @@ -455,6 +473,11 @@ 1 ..\Peripheral\Src\bluetooth.c + + buzzer.c + 1 + ..\Peripheral\Src\buzzer.c + control.c 1 @@ -485,11 +508,6 @@ 1 ..\Peripheral\Src\motor.c - - path_plan.c - 1 - ..\Peripheral\Src\path_plan.c - servo.c 1 @@ -593,18 +611,20 @@ + - + - + - + - + + @@ -613,4 +633,5 @@ + diff --git a/Peripheral/Inc/buzzer.h b/Peripheral/Inc/buzzer.h new file mode 100644 index 0000000..1e5322e --- /dev/null +++ b/Peripheral/Inc/buzzer.h @@ -0,0 +1,13 @@ +#ifndef __BUZZER_H +#define __BUZZER_H + +#include "main.h" + +void BUZZER_Start(); +void BUZZER_Stop(); + +/// @brief 蜂鸣器定时鸣叫 +/// @param time 鸣叫时间,单位100毫秒 +void BUZZER_StartTimed(uint8_t time); + +#endif diff --git a/Peripheral/Inc/line_seek.h b/Peripheral/Inc/line_seek.h index 7954ed2..266cdad 100644 --- a/Peripheral/Inc/line_seek.h +++ b/Peripheral/Inc/line_seek.h @@ -7,4 +7,4 @@ void LineSeek_GetStatus(int *p_iL1, int *p_iL2, int *p_iR1, int *p_iR2); char *LineSeek_GetStatusStr(); int LineSeek_Equals(const char *expression); -#endif \ No newline at end of file +#endif diff --git a/Peripheral/Src/buzzer.c b/Peripheral/Src/buzzer.c new file mode 100644 index 0000000..99e80bc --- /dev/null +++ b/Peripheral/Src/buzzer.c @@ -0,0 +1,20 @@ +#include "buzzer.h" +#include "timer.h" + +static uint8_t state; + +inline void BUZZER_Start() +{ + HAL_GPIO_WritePin(FM_K2_POWERC_GPIO_Port, FM_K2_POWERC_Pin, GPIO_PIN_RESET); +} + +inline void BUZZER_Stop() +{ + HAL_GPIO_WritePin(FM_K2_POWERC_GPIO_Port, FM_K2_POWERC_Pin, GPIO_PIN_SET); +} + +void BUZZER_StartTimed(uint8_t time) +{ + BUZZER_Start(); + TIMER_AddLoopEvent(EVENT_BUZZER, BUZZER_Stop, time * 100); +} diff --git a/Peripheral/Src/line_seek.c b/Peripheral/Src/line_seek.c index 5779674..c23eb45 100644 --- a/Peripheral/Src/line_seek.c +++ b/Peripheral/Src/line_seek.c @@ -1,6 +1,7 @@ #include "line_seek.h" #include "main.h" +#include char LineSeek_Status[5]; diff --git a/Peripheral/Inc/path_plan.h b/Service/Inc/path_plan.h similarity index 100% rename from Peripheral/Inc/path_plan.h rename to Service/Inc/path_plan.h diff --git a/Service/Inc/timer.h b/Service/Inc/timer.h new file mode 100644 index 0000000..79c38af --- /dev/null +++ b/Service/Inc/timer.h @@ -0,0 +1,16 @@ +#ifndef __TIMER_H +#define __TIMER_H + +#include "main.h" + +#define EVENT_LED 0 +#define EVENT_BUZZER 1 +#define EVENT_HCSR04 2 +#define EVENT_SERVO 3 +#define EVENT_MAX 4 + +void TIMER_Init(void); +void TIMER_AddLoopEvent(uint8_t type, void (*func)(void), uint16_t loop_time); +void TIMER_DelLoopEvent(uint8_t type); + +#endif diff --git a/Peripheral/Src/path_plan.c b/Service/Src/path_plan.c similarity index 100% rename from Peripheral/Src/path_plan.c rename to Service/Src/path_plan.c diff --git a/Service/Src/timer.c b/Service/Src/timer.c new file mode 100644 index 0000000..1bd0e91 --- /dev/null +++ b/Service/Src/timer.c @@ -0,0 +1,63 @@ +#include "timer.h" +#include "tim.h" +#include + +typedef struct Event +{ + void (*func)(void); + uint16_t time; + uint8_t enabled; +} Event; + +static Event event[EVENT_MAX]; +static uint32_t time_cnt; +static uint32_t time_lcm = 1; + +uint32_t gcd(uint32_t a, uint32_t b); +uint32_t lcm(uint32_t a, uint32_t b); + +void TIMER_Count(void); + +void TIMER_Init(void) +{ + HAL_TIM_RegisterCallback(&htim4, HAL_TIM_PERIOD_ELAPSED_CB_ID, TIMER_Count); + HAL_TIM_Base_Start_IT(&htim4); +} + +/// @brief 注册定时回调事件 +/// @param func 回调函数 void func(void) +/// @param loop_time 定时时间,单位毫秒 +void TIMER_AddLoopEvent(uint8_t type, void (*func)(void), uint16_t loop_time) +{ + event[type].func = func; + event[type].time = loop_time; + event[type].enabled = 1; + time_lcm = lcm(time_lcm, loop_time); +} + +void TIMER_DelLoopEvent(uint8_t type) +{ + event[type].enabled = 0; +} + +void TIMER_Count(void) +{ + ++time_cnt; + for (uint8_t i = 0; i < EVENT_MAX; ++i) + if (event[i].enabled && time_cnt % event[i].time == 0) + event[i].func(); + if (time_cnt == time_lcm) + time_cnt = 0; +} + +uint32_t gcd(uint32_t a, uint32_t b) +{ + if (b == 0) + return a; + return gcd(b, a % b); +} + +uint32_t lcm(uint32_t a, uint32_t b) +{ + return a / gcd(a, b) * b; +}