From 900f6d7d85946a934743ba5ba1e3c06b771c114f Mon Sep 17 00:00:00 2001 From: Anton Mukhin Date: Fri, 21 Jun 2024 15:22:50 +0300 Subject: [PATCH] v11.2 changes: - moved 400ms boot delay to a point AFTER peripherals init - fixed TIM3 and TIM14 periods - mvoed light updates to separate function and fixed cuty cylce clamp during dimming process --- Core/Inc/board_logic.h | 2 +- Core/Src/board_logic.c | 42 ++++++++++++++++++++----------------- Core/Src/main.c | 8 ++++--- Relay_RS485_V2 Debug.launch | 1 + Relay_RS485_V2.ioc | 13 ++++++------ 5 files changed, 37 insertions(+), 29 deletions(-) diff --git a/Core/Inc/board_logic.h b/Core/Inc/board_logic.h index 2beda42..588c74b 100644 --- a/Core/Inc/board_logic.h +++ b/Core/Inc/board_logic.h @@ -6,7 +6,7 @@ #ifndef BOARD_LOGIC_H_ #define BOARD_LOGIC_H_ -#define MODBUS_FIRMWARE_VERSION ( /*major*/ 11 + /*minor*/ 1 * 0x100) +#define MODBUS_FIRMWARE_VERSION ( /*major*/ 11 + /*minor*/ 2 * 0x100) #define MODBUS_BOARD_TYPE (8) //Relay Module board ID #define REL_MAIN_BIT (1u<<0) diff --git a/Core/Src/board_logic.c b/Core/Src/board_logic.c index f0084ac..5bf25b9 100644 --- a/Core/Src/board_logic.c +++ b/Core/Src/board_logic.c @@ -36,8 +36,8 @@ uint16_t status = 0; uint16_t motor1_pwm = 0; uint16_t motor2_pwm = 0; uint16_t lights_pwm = 0; -uint16_t lights_pwm_target = 0; -int16_t lights_pwm_delta = 0; +volatile uint16_t lights_pwm_target = 0; +volatile int16_t lights_pwm_delta = 0; uint16_t led_time_act = 0; void estop_reset(void) { @@ -88,11 +88,11 @@ void set_pwm(uint8_t unit, uint16_t duty) { duty = clamp_duty(duty); if (duty == 0) - HAL_TIM_PWM_Stop(tim, channel); + HAL_TIM_PWM_Stop_IT(tim, channel); else { - HAL_TIM_PWM_Start(tim, channel); + HAL_TIM_PWM_Start_IT(tim, channel); __HAL_TIM_SetCompare(tim, channel, duty); - HAL_Delay(5); + //HAL_Delay(1); } } @@ -107,6 +107,23 @@ void set_light(uint16_t duty) { } } +void update_light(void) { + static uint32_t count_ms = 0; + // Check if we should change lights pwm for smooth transition + if (lights_pwm != lights_pwm_target) { + // Change pwm only every 20ms (PWM_LIGHTS_STEP) + if(++count_ms == PWM_LIGHTS_STEP) { + count_ms = 0; + + lights_pwm = clamp_duty(lights_pwm + lights_pwm_delta); + // Set lights pwm to target if the difference is less than the delta + if (abs(lights_pwm_target - lights_pwm) < abs(lights_pwm_delta)) + lights_pwm = lights_pwm_target; + set_pwm(3, lights_pwm); + } + } +} + void loop_iterate() { @@ -259,22 +276,9 @@ void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin) void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim) { // check if the interrupt comes from TIM1 if(htim->Instance == TIM1) { - static uint32_t count_ms = 0; update_service_indication(); - // Check if we should change lights pwm for smooth transition - if (lights_pwm != lights_pwm_target) { - // Change pwm only every 20ms (PWM_LIGHTS_STEP) - if(++count_ms == PWM_LIGHTS_STEP) { - count_ms = 0; - - lights_pwm += lights_pwm_delta; - // Set lights pwm to target if the difference is less than the delta - if (abs(lights_pwm_target - lights_pwm) < abs(lights_pwm_delta)) - lights_pwm = lights_pwm_target; - set_pwm(3, lights_pwm); - } - } + update_light(); } } diff --git a/Core/Src/main.c b/Core/Src/main.c index eeee9cc..56e46b0 100644 --- a/Core/Src/main.c +++ b/Core/Src/main.c @@ -78,6 +78,7 @@ static void MX_TIM14_Init(void); */ int main(void) { + /* USER CODE BEGIN 1 */ /* USER CODE END 1 */ @@ -95,7 +96,7 @@ int main(void) SystemClock_Config(); /* USER CODE BEGIN SysInit */ - HAL_Delay(400); +// HAL_Delay(400); /* USER CODE END SysInit */ /* Initialize all configured peripherals */ @@ -106,6 +107,7 @@ int main(void) MX_TIM1_Init(); MX_TIM14_Init(); /* USER CODE BEGIN 2 */ + HAL_Delay(400); board_init(); /* USER CODE END 2 */ @@ -234,7 +236,7 @@ static void MX_TIM3_Init(void) htim3.Instance = TIM3; htim3.Init.Prescaler = 240-1; htim3.Init.CounterMode = TIM_COUNTERMODE_UP; - htim3.Init.Period = 255; + htim3.Init.Period = 255-1; htim3.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1; htim3.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE; if (HAL_TIM_PWM_Init(&htim3) != HAL_OK) @@ -286,7 +288,7 @@ static void MX_TIM14_Init(void) htim14.Instance = TIM14; htim14.Init.Prescaler = 6-1; htim14.Init.CounterMode = TIM_COUNTERMODE_UP; - htim14.Init.Period = 255; + htim14.Init.Period = 255-1; htim14.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1; htim14.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE; if (HAL_TIM_Base_Init(&htim14) != HAL_OK) diff --git a/Relay_RS485_V2 Debug.launch b/Relay_RS485_V2 Debug.launch index 88b614d..4ff2aa5 100644 --- a/Relay_RS485_V2 Debug.launch +++ b/Relay_RS485_V2 Debug.launch @@ -78,5 +78,6 @@ + diff --git a/Relay_RS485_V2.ioc b/Relay_RS485_V2.ioc index 437ff36..b2eb728 100644 --- a/Relay_RS485_V2.ioc +++ b/Relay_RS485_V2.ioc @@ -66,8 +66,8 @@ Mcu.PinsNb=23 Mcu.ThirdPartyNb=0 Mcu.UserConstants= Mcu.UserName=STM32F030K6Tx -MxCube.Version=6.10.0 -MxDb.Version=DB.6.0.100 +MxCube.Version=6.11.1 +MxDb.Version=DB.6.0.111 NVIC.DMA1_Channel2_3_IRQn=true\:2\:0\:true\:false\:true\:false\:true\:true NVIC.EXTI0_1_IRQn=true\:0\:0\:false\:false\:true\:true\:true\:true NVIC.EXTI4_15_IRQn=true\:0\:0\:false\:false\:true\:true\:true\:true @@ -137,8 +137,9 @@ PB1.GPIOParameters=GPIO_Label PB1.GPIO_Label=LED_ACT PB1.Locked=true PB1.Signal=GPIO_Output -PB3.GPIOParameters=GPIO_Label +PB3.GPIOParameters=GPIO_PuPd,GPIO_Label PB3.GPIO_Label=RL_MAIN +PB3.GPIO_PuPd=GPIO_NOPULL PB3.Locked=true PB3.Signal=GPIO_Output PB4.Locked=true @@ -158,7 +159,7 @@ ProjectManager.CustomerFirmwarePackage= ProjectManager.DefaultFWLocation=true ProjectManager.DeletePrevious=true ProjectManager.DeviceId=STM32F030K6Tx -ProjectManager.FirmwarePackage=STM32Cube FW_F0 V1.11.4 +ProjectManager.FirmwarePackage=STM32Cube FW_F0 V1.11.5 ProjectManager.FreePins=false ProjectManager.HalAssertFull=false ProjectManager.HeapSize=0x200 @@ -215,12 +216,12 @@ TIM1.Prescaler=480-1 TIM14.Channel=TIM_CHANNEL_1 TIM14.IPParameters=Channel,Prescaler,Period,OCMode_PWM TIM14.OCMode_PWM=TIM_OCMODE_PWM1 -TIM14.Period=255 +TIM14.Period=255-1 TIM14.Prescaler=6-1 TIM3.Channel-PWM\ Generation1\ CH1=TIM_CHANNEL_1 TIM3.Channel-PWM\ Generation2\ CH2=TIM_CHANNEL_2 TIM3.IPParameters=Channel-PWM Generation1 CH1,Prescaler,Period,Channel-PWM Generation2 CH2 -TIM3.Period=255 +TIM3.Period=255-1 TIM3.Prescaler=240-1 USART1.BaudRate=9600 USART1.IPParameters=VirtualMode-Asynchronous,BaudRate