From 0f8c6ac778f4a234c7413ba046c9e023769aa339 Mon Sep 17 00:00:00 2001 From: liuyuyan Date: Fri, 7 Jun 2024 20:38:46 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E5=AE=8C=E6=88=90=E5=91=BC=E5=90=B8?= =?UTF-8?q?=E7=81=AF=E6=B3=A8=E5=86=8C=E5=92=8C=E5=8D=B8=E8=BD=BD=E6=8E=A5?= =?UTF-8?q?=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Core/Inc/main.h | 9 ++++++++- Core/Src/main.c | 40 ++++++++++++++++++++++++++++++++-------- Core/Src/user/rgb.c | 25 +++++++++++++++++++++++-- Core/Src/user/rgb.h | 5 ++++- 4 files changed, 67 insertions(+), 12 deletions(-) diff --git a/Core/Inc/main.h b/Core/Inc/main.h index 2debe43..1b86845 100644 --- a/Core/Inc/main.h +++ b/Core/Inc/main.h @@ -61,9 +61,16 @@ void Error_Handler(void); #define JD_PIN_Pin GPIO_PIN_11 #define JD_PIN_GPIO_Port GPIOA /* USER CODE BEGIN Private defines */ +#define FUNC_NAME_LENGTH 50 typedef void (*LoopCallBack)(void); -void registerLoopCallBack(LoopCallBack loopc); +typedef struct st_LoopFunc{ + LoopCallBack func; + uint8_t funcName[FUNC_NAME_LENGTH]; +}st_LoopFunc_t; + +void unRegisterLoopCallBack(uint8_t * funcName); +void registerLoopCallBack(st_LoopFunc_t loopF); /** * 设置PWM脉宽 diff --git a/Core/Src/main.c b/Core/Src/main.c index b3de0fa..763715d 100644 --- a/Core/Src/main.c +++ b/Core/Src/main.c @@ -18,6 +18,7 @@ */ /* USER CODE END Header */ /* Includes ------------------------------------------------------------------*/ +#include #include "main.h" #include "tim.h" #include "usart.h" @@ -58,16 +59,39 @@ void SystemClock_Config(void); /* USER CODE BEGIN 0 */ #define LOOP_FUNC_SIZE 20 -LoopCallBack loopCallBack[LOOP_FUNC_SIZE]={NULL}; +st_LoopFunc_t loopFunc[LOOP_FUNC_SIZE]={NULL}; + +void mainInit(void) +{ + for (int i = 0; i < LOOP_FUNC_SIZE; ++i) { + loopFunc[i].func=NULL; + memset(loopFunc[i].funcName,0,sizeof(loopFunc[i].funcName)); + } +} /** * 注册循环函数 * @param func : 要循环调用的函数实体 */ -void registerLoopCallBack(LoopCallBack loopc){ - for (int i = 0; i < LOOP_FUNC_SIZE; ++i) { - if (loopCallBack[i] == NULL) { - loopCallBack[i] =loopc; +void registerLoopCallBack(st_LoopFunc_t loopF){ + for (int i = 0; i #include #include +#include #include "rgb.h" /** @@ -13,7 +14,7 @@ * @param gPWM : 绿色的配比 0-4999 * @param bPWM : 蓝色的配比 0-4999 */ -static void setRGBColor(uint32_t rPWM,uint32_t gPWM,uint32_t bPWM) +void setRGBColor(uint32_t rPWM,uint32_t gPWM,uint32_t bPWM) { setPWMPulse(&htim2,TIM_CHANNEL_2,rPWM); @@ -45,6 +46,24 @@ static void breathingLamp(void){ setRGBColor(count,count,count); } +/** + * 打开呼吸灯 + */ +void onBreathingLamp(void) +{ + st_LoopFunc_t loopFunc; + loopFunc.func=breathingLamp; + strcpy(loopFunc.funcName,"breathingLamp"); + registerLoopCallBack(loopFunc); +} + +/** + * 关闭呼吸灯 + */ +void offBreathingLamp(void) +{ + unRegisterLoopCallBack("breathingLamp"); +} /** * 初始化rgb @@ -54,5 +73,7 @@ void rgbInit(void) HAL_TIM_PWM_Start(&htim3,TIM_CHANNEL_1); HAL_TIM_PWM_Start(&htim3,TIM_CHANNEL_2); HAL_TIM_PWM_Start(&htim2,TIM_CHANNEL_2); - registerLoopCallBack(breathingLamp); + onBreathingLamp(); } + + diff --git a/Core/Src/user/rgb.h b/Core/Src/user/rgb.h index feb7a2b..360016b 100644 --- a/Core/Src/user/rgb.h +++ b/Core/Src/user/rgb.h @@ -4,7 +4,10 @@ #ifndef DEMO1_START_RGB_H #define DEMO1_START_RGB_H +#include "stdint.h" void rgbInit(void); - +void setRGBColor(uint32_t rPWM,uint32_t gPWM,uint32_t bPWM); +void onBreathingLamp(void); +void offBreathingLamp(void); #endif //DEMO1_START_RGB_H -- Gitee From eab170797782c81b925377a8afccf63276254449 Mon Sep 17 00:00:00 2001 From: liuyuyan Date: Fri, 7 Jun 2024 22:10:16 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E9=A9=AC=E8=BE=BE?= =?UTF-8?q?=E9=A9=B1=E5=8A=A8=E5=92=8C=E5=88=9D=E5=A7=8B=E6=B5=8B=E8=AF=95?= =?UTF-8?q?=E7=A8=8B=E5=BA=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Core/Inc/tim.h | 2 + Core/Src/gpio.c | 1 + Core/Src/main.c | 6 ++- Core/Src/tim.c | 117 ++++++++++++++++++++++++++++++++++++++++++ Core/Src/user/init.c | 30 +++++++++++ Core/Src/user/init.h | 1 + Core/Src/user/motor.c | 34 ++++++++++++ Core/Src/user/motor.h | 16 ++++++ Core/Src/user/rgb.c | 4 +- Core/Src/user/rgb.h | 1 + demo1_start.ioc | 40 ++++++++++----- 11 files changed, 236 insertions(+), 16 deletions(-) create mode 100644 Core/Src/user/motor.c create mode 100644 Core/Src/user/motor.h diff --git a/Core/Inc/tim.h b/Core/Inc/tim.h index fb62449..9993917 100644 --- a/Core/Inc/tim.h +++ b/Core/Inc/tim.h @@ -33,6 +33,7 @@ extern "C" { extern TIM_HandleTypeDef htim2; extern TIM_HandleTypeDef htim3; +extern TIM_HandleTypeDef htim8; /* USER CODE BEGIN Private defines */ @@ -40,6 +41,7 @@ extern TIM_HandleTypeDef htim3; void MX_TIM2_Init(void); void MX_TIM3_Init(void); +void MX_TIM8_Init(void); void HAL_TIM_MspPostInit(TIM_HandleTypeDef *htim); diff --git a/Core/Src/gpio.c b/Core/Src/gpio.c index 88d1ed7..ba4e227 100644 --- a/Core/Src/gpio.c +++ b/Core/Src/gpio.c @@ -46,6 +46,7 @@ void MX_GPIO_Init(void) /* GPIO Ports Clock Enable */ __HAL_RCC_GPIOD_CLK_ENABLE(); __HAL_RCC_GPIOA_CLK_ENABLE(); + __HAL_RCC_GPIOC_CLK_ENABLE(); /*Configure GPIO pin Output Level */ HAL_GPIO_WritePin(JD_PIN_GPIO_Port, JD_PIN_Pin, GPIO_PIN_RESET); diff --git a/Core/Src/main.c b/Core/Src/main.c index 763715d..2c1670a 100644 --- a/Core/Src/main.c +++ b/Core/Src/main.c @@ -18,7 +18,6 @@ */ /* USER CODE END Header */ /* Includes ------------------------------------------------------------------*/ -#include #include "main.h" #include "tim.h" #include "usart.h" @@ -27,6 +26,7 @@ /* Private includes ----------------------------------------------------------*/ /* USER CODE BEGIN Includes */ #include "user/init.h" +#include /* USER CODE END Includes */ /* Private typedef -----------------------------------------------------------*/ @@ -71,7 +71,7 @@ void mainInit(void) /** * 注册循环函数 - * @param func : 要循环调用的函数实体 + * @param func �? 要循环调用的函数实体 */ void registerLoopCallBack(st_LoopFunc_t loopF){ for (int i = 0; i Instance==TIM8) + { + /* USER CODE BEGIN TIM8_MspInit 0 */ + + /* USER CODE END TIM8_MspInit 0 */ + /* TIM8 clock enable */ + __HAL_RCC_TIM8_CLK_ENABLE(); + /* USER CODE BEGIN TIM8_MspInit 1 */ + + /* USER CODE END TIM8_MspInit 1 */ + } +} void HAL_TIM_MspPostInit(TIM_HandleTypeDef* timHandle) { @@ -209,6 +290,26 @@ void HAL_TIM_MspPostInit(TIM_HandleTypeDef* timHandle) /* USER CODE END TIM3_MspPostInit 1 */ } + else if(timHandle->Instance==TIM8) + { + /* USER CODE BEGIN TIM8_MspPostInit 0 */ + + /* USER CODE END TIM8_MspPostInit 0 */ + + __HAL_RCC_GPIOC_CLK_ENABLE(); + /**TIM8 GPIO Configuration + PC6 ------> TIM8_CH1 + PC7 ------> TIM8_CH2 + */ + GPIO_InitStruct.Pin = GPIO_PIN_6|GPIO_PIN_7; + GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; + GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; + HAL_GPIO_Init(GPIOC, &GPIO_InitStruct); + + /* USER CODE BEGIN TIM8_MspPostInit 1 */ + + /* USER CODE END TIM8_MspPostInit 1 */ + } } @@ -239,6 +340,22 @@ void HAL_TIM_Base_MspDeInit(TIM_HandleTypeDef* tim_baseHandle) } } +void HAL_TIM_PWM_MspDeInit(TIM_HandleTypeDef* tim_pwmHandle) +{ + + if(tim_pwmHandle->Instance==TIM8) + { + /* USER CODE BEGIN TIM8_MspDeInit 0 */ + + /* USER CODE END TIM8_MspDeInit 0 */ + /* Peripheral clock disable */ + __HAL_RCC_TIM8_CLK_DISABLE(); + /* USER CODE BEGIN TIM8_MspDeInit 1 */ + + /* USER CODE END TIM8_MspDeInit 1 */ + } +} + /* USER CODE BEGIN 1 */ /* USER CODE END 1 */ diff --git a/Core/Src/user/init.c b/Core/Src/user/init.c index 4ff5148..b198f30 100644 --- a/Core/Src/user/init.c +++ b/Core/Src/user/init.c @@ -4,9 +4,39 @@ #include "init.h" #include "rgb.h" +#include "motor.h" +#define TEST_CYCLE_NUM 5000 + +/** +* @brief 测试程序 +* @param test +* @param void +* @return void +* @retval +* @bug +*/ +void test(void) +{ + breathingLamp(); + motorSpeed(MOTOR_TIM8_CH1,5000); + for (int i = 0; i < TEST_CYCLE_NUM; ++i) { + for (int j = 0; j < TEST_CYCLE_NUM; ++j) { + + } + } + motorSpeed(MOTOR_TIM8_CH2,5000); + for (int i = 0; i < TEST_CYCLE_NUM; ++i) { + for (int j = 0; j < TEST_CYCLE_NUM; ++j) { + + } + } + setRGBColor(0,0,0); + motorSpeed(MOTOR_TIM8_CH1,0); +} void userInit(void) { rgbInit(); + motorInit(); } \ No newline at end of file diff --git a/Core/Src/user/init.h b/Core/Src/user/init.h index 09cb807..32b21d6 100644 --- a/Core/Src/user/init.h +++ b/Core/Src/user/init.h @@ -5,4 +5,5 @@ #ifndef DEMO1_START_INIT_H #define DEMO1_START_INIT_H void userInit(void); +void test(void); #endif //DEMO1_START_INIT_H diff --git a/Core/Src/user/motor.c b/Core/Src/user/motor.c new file mode 100644 index 0000000..ff24dde --- /dev/null +++ b/Core/Src/user/motor.c @@ -0,0 +1,34 @@ +// +// Created by liuyu on 2024/6/7. +// + +#include +#include +#include "motor.h" + + +/** + * 马达初始化 + */ +void motorInit(void) +{ + HAL_TIM_PWM_Start(&htim8,TIM_CHANNEL_1); + HAL_TIM_PWM_Start(&htim8,TIM_CHANNEL_2); + +} +/** + * 马达设置方向及速度 + * @param dir : 方向 ,true :顺时针 false : 逆时针 + * @param speed : 速度 0-49999 + */ +void motorSpeed(bool dir,uint32_t speed) +{ + if (dir){ + setPWMPulse(&htim8,TIM_CHANNEL_1,speed); + setPWMPulse(&htim8,TIM_CHANNEL_2,0); + } else{ + setPWMPulse(&htim8,TIM_CHANNEL_1,0); + setPWMPulse(&htim8,TIM_CHANNEL_2,speed); + } + +} \ No newline at end of file diff --git a/Core/Src/user/motor.h b/Core/Src/user/motor.h new file mode 100644 index 0000000..37028e9 --- /dev/null +++ b/Core/Src/user/motor.h @@ -0,0 +1,16 @@ +// +// Created by liuyu on 2024/6/7. +// + +#ifndef DEMO1_START_MOTOR_H +#define DEMO1_START_MOTOR_H + +#include +#include + +#define MOTOR_TIM8_CH1 1 +#define MOTOR_TIM8_CH2 0 + +void motorInit(void); +void motorSpeed(bool dir,uint32_t speed); +#endif //DEMO1_START_MOTOR_H diff --git a/Core/Src/user/rgb.c b/Core/Src/user/rgb.c index 80745b9..b53bf34 100644 --- a/Core/Src/user/rgb.c +++ b/Core/Src/user/rgb.c @@ -26,7 +26,8 @@ void setRGBColor(uint32_t rPWM,uint32_t gPWM,uint32_t bPWM) /** * 呼吸灯循环执行 */ -static void breathingLamp(void){ +void breathingLamp(void) +{ static uint32_t count=0; static bool flag=true; if (flag){ @@ -73,7 +74,6 @@ void rgbInit(void) HAL_TIM_PWM_Start(&htim3,TIM_CHANNEL_1); HAL_TIM_PWM_Start(&htim3,TIM_CHANNEL_2); HAL_TIM_PWM_Start(&htim2,TIM_CHANNEL_2); - onBreathingLamp(); } diff --git a/Core/Src/user/rgb.h b/Core/Src/user/rgb.h index 360016b..6ff7e0c 100644 --- a/Core/Src/user/rgb.h +++ b/Core/Src/user/rgb.h @@ -8,6 +8,7 @@ void rgbInit(void); void setRGBColor(uint32_t rPWM,uint32_t gPWM,uint32_t bPWM); +void breathingLamp(void); void onBreathingLamp(void); void offBreathingLamp(void); #endif //DEMO1_START_RGB_H diff --git a/demo1_start.ioc b/demo1_start.ioc index 8ac3772..75fbb9d 100644 --- a/demo1_start.ioc +++ b/demo1_start.ioc @@ -8,24 +8,27 @@ Mcu.IP1=RCC Mcu.IP2=SYS Mcu.IP3=TIM2 Mcu.IP4=TIM3 -Mcu.IP5=USART1 -Mcu.IPNb=6 +Mcu.IP5=TIM8 +Mcu.IP6=USART1 +Mcu.IPNb=7 Mcu.Name=STM32F103R(C-D-E)Tx Mcu.Package=LQFP64 Mcu.Pin0=PD0-OSC_IN Mcu.Pin1=PD1-OSC_OUT -Mcu.Pin10=VP_SYS_VS_Systick -Mcu.Pin11=VP_TIM2_VS_ClockSourceINT -Mcu.Pin12=VP_TIM3_VS_ClockSourceINT +Mcu.Pin10=PA13 +Mcu.Pin11=PA14 +Mcu.Pin12=VP_SYS_VS_Systick +Mcu.Pin13=VP_TIM2_VS_ClockSourceINT +Mcu.Pin14=VP_TIM3_VS_ClockSourceINT Mcu.Pin2=PA1 Mcu.Pin3=PA6 Mcu.Pin4=PA7 -Mcu.Pin5=PA9 -Mcu.Pin6=PA10 -Mcu.Pin7=PA11 -Mcu.Pin8=PA13 -Mcu.Pin9=PA14 -Mcu.PinsNb=13 +Mcu.Pin5=PC6 +Mcu.Pin6=PC7 +Mcu.Pin7=PA9 +Mcu.Pin8=PA10 +Mcu.Pin9=PA11 +Mcu.PinsNb=15 Mcu.ThirdPartyNb=0 Mcu.UserConstants= Mcu.UserName=STM32F103RCTx @@ -60,6 +63,10 @@ PA7.Locked=true PA7.Signal=S_TIM3_CH2 PA9.Mode=Asynchronous PA9.Signal=USART1_TX +PC6.Locked=true +PC6.Signal=S_TIM8_CH1 +PC7.Locked=true +PC7.Signal=S_TIM8_CH2 PD0-OSC_IN.Mode=HSE-External-Oscillator PD0-OSC_IN.Signal=RCC_OSC_IN PD1-OSC_OUT.Mode=HSE-External-Oscillator @@ -92,7 +99,7 @@ ProjectManager.StackSize=0x400 ProjectManager.TargetToolchain=SW4STM32 ProjectManager.ToolChainLocation= ProjectManager.UnderRoot=true -ProjectManager.functionlistsort=1-MX_GPIO_Init-GPIO-false-HAL-true,2-SystemClock_Config-RCC-false-HAL-false,3-MX_USART1_UART_Init-USART1-false-HAL-true +ProjectManager.functionlistsort=1-MX_GPIO_Init-GPIO-false-HAL-true,2-SystemClock_Config-RCC-false-HAL-false,3-MX_USART1_UART_Init-USART1-false-HAL-true,4-MX_TIM2_Init-TIM2-false-HAL-true,5-MX_TIM3_Init-TIM3-false-HAL-true RCC.ADCFreqValue=36000000 RCC.AHBFreq_Value=72000000 RCC.APB1CLKDivider=RCC_HCLK_DIV2 @@ -124,6 +131,10 @@ SH.S_TIM3_CH1.0=TIM3_CH1,PWM Generation1 CH1 SH.S_TIM3_CH1.ConfNb=1 SH.S_TIM3_CH2.0=TIM3_CH2,PWM Generation2 CH2 SH.S_TIM3_CH2.ConfNb=1 +SH.S_TIM8_CH1.0=TIM8_CH1,PWM Generation1 CH1 +SH.S_TIM8_CH1.ConfNb=1 +SH.S_TIM8_CH2.0=TIM8_CH2,PWM Generation2 CH2 +SH.S_TIM8_CH2.ConfNb=1 TIM2.Channel-PWM\ Generation2\ CH2=TIM_CHANNEL_2 TIM2.IPParameters=Channel-PWM Generation2 CH2,Prescaler,Period,Pulse-PWM Generation2 CH2 TIM2.Period=5000-1 @@ -136,6 +147,11 @@ TIM3.Period=5000-1 TIM3.Prescaler=72-1 TIM3.Pulse-PWM\ Generation1\ CH1=500-1 TIM3.Pulse-PWM\ Generation2\ CH2=100-1 +TIM8.Channel-PWM\ Generation1\ CH1=TIM_CHANNEL_1 +TIM8.Channel-PWM\ Generation2\ CH2=TIM_CHANNEL_2 +TIM8.IPParameters=Channel-PWM Generation1 CH1,Channel-PWM Generation2 CH2,Prescaler,Period +TIM8.Period=50000-1 +TIM8.Prescaler=72-1 USART1.IPParameters=VirtualMode USART1.VirtualMode=VM_ASYNC VP_SYS_VS_Systick.Mode=SysTick -- Gitee