diff --git a/Core/Inc/main.h b/Core/Inc/main.h index 2debe43769397b289b431c824534d120d3bb4d8a..1b868455a7e80d1abd3338adab4fc157a04345e2 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/Inc/tim.h b/Core/Inc/tim.h index fb62449bcb2c7a470fcb231942bb093306c504fa..9993917f66019a79010bb3fcf0e8f476ec0f3604 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 88d1ed710d952e73571bed3006cd97620a1ce706..ba4e227b8dcc008a530f80a34dd234445015e69d 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 b3de0fab1a22b8d9cae25afb54addd66349238a0..2c1670a70aa2ac95b72b9a59ebffd0b8866923df 100644 --- a/Core/Src/main.c +++ b/Core/Src/main.c @@ -26,6 +26,7 @@ /* Private includes ----------------------------------------------------------*/ /* USER CODE BEGIN Includes */ #include "user/init.h" +#include /* USER CODE END Includes */ /* Private typedef -----------------------------------------------------------*/ @@ -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 : 要循环调用的函数实体 + * @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 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 4ff51481f720a7c5e7e07b7037ab8c2312b2164d..b198f30250009a966f2fbba2ac2cf5be45002938 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 09cb807e1d13dc7cfb39cab0a09e7ce161446f38..32b21d6b45f469cc891f84a316a6a5acb89904f5 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 0000000000000000000000000000000000000000..ff24ddecb22e87b8b1876353d0253542a2730ef9 --- /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 0000000000000000000000000000000000000000..37028e9f45f696c034b3929e689cb08df5e1efe4 --- /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 f17016993ba9376c0ad86d608794f09b7df495a9..b53bf34bb9f167cf157815b5efd82f6378b45a94 100644 --- a/Core/Src/user/rgb.c +++ b/Core/Src/user/rgb.c @@ -5,6 +5,7 @@ #include #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); @@ -25,7 +26,8 @@ static 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){ @@ -45,6 +47,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 +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); - registerLoopCallBack(breathingLamp); } + + diff --git a/Core/Src/user/rgb.h b/Core/Src/user/rgb.h index feb7a2b1752fa292f38a7b07c39fac3d8469cd53..6ff7e0c1617c3bbbf3c7575ae56988d263515d7e 100644 --- a/Core/Src/user/rgb.h +++ b/Core/Src/user/rgb.h @@ -4,7 +4,11 @@ #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 breathingLamp(void); +void onBreathingLamp(void); +void offBreathingLamp(void); #endif //DEMO1_START_RGB_H diff --git a/demo1_start.ioc b/demo1_start.ioc index 8ac37720a600a6566e57a919f5215649c966215d..75fbb9d78dd56d3c7f726ca2e474d41192c90ca2 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