diff --git a/Core/Inc/PWM_control.h b/Core/Inc/PWM_control.h index 590b0ef..8e67096 100644 --- a/Core/Inc/PWM_control.h +++ b/Core/Inc/PWM_control.h @@ -5,6 +5,24 @@ #include "ADBMS_LL_Driver.h" #include "main.h" +/* The PWM period (1/FPWM) is defined by the following parameters: +ARR value, the Prescaler value, and the internal clock itself which drives the timer module FCLK. +F_PWM = (F_CLK)/((ARR + 1) * (PSC + 1)) + +F_CLK = 16 MHz + +POWERGROUND ESC Signal: pulse every 20 ms, 1 ms = 0%, 2 ms = 100% +FREQ = 50 Hz -> 16 MHz/50 Hz = 320000 = ((9999 + 1) * (31 + 1)) +DUTY CYCLE = 1/20 -> 0%, DUTY CYCLE = 2/20 -> 100% +CCR * DUTY_CYCLE +CCR: 1/20 -> 500, 2/20 -> 1000 +*/ +#define POWERGROUND_FREQ 50 +#define POWERGROUND_MAX_DUTY_CYCLE 0.05 +#define POWERGROUND_MIN_DUTY_CYCLE 0.1 + +//#define BATTERY_COOLING_FREQ 20000 + void PWM_control_init(TIM_HandleTypeDef powerground1, TIM_HandleTypeDef powerground2, TIM_HandleTypeDef battery_cooling); void PWM_powerground_control(uint8_t percent); diff --git a/Core/Src/PWM_control.c b/Core/Src/PWM_control.c index 74c8bc3..5df4da1 100644 --- a/Core/Src/PWM_control.c +++ b/Core/Src/PWM_control.c @@ -1,28 +1,41 @@ #include "PWM_control.h" -#include "stm32f3xx_hal_tim.h" #include uint8_t powerground_status; uint8_t battery_cooling_status; +uint8_t powerground_percent; +uint8_t battery_cooling_percent; +//uint32_t powerground1_CCR, powerground2_CCR, battery_cooling_CCR; TIM_HandleTypeDef powerground1, powerground2, battery_cooling; +/* + Pulse width modulation mode allows for generating a signal with a frequency determined by + the value of the TIMx_ARR register and a duty cycle determined by the value of the TIMx_CCRx register. +*/ + void PWM_control_init(TIM_HandleTypeDef powerground1, TIM_HandleTypeDef powerground2, TIM_HandleTypeDef battery_cooling){ powerground_status = 0; - battery_cooling = 0; + battery_cooling_status = 0; - HAL_TIM_PWM_Start(&powerground1, 1); - HAL_TIM_PWM_Start(&powerground2, 4); - HAL_TIM_PWM_Start(&battery_cooling, 3); + HAL_TIM_PWM_Start(&powerground1, 1); //TIM15CH1 + HAL_TIM_PWM_Start(&powerground2, 2); //TIM15CH2 + HAL_TIM_PWM_Start(&battery_cooling, 3); //TIM1CH3 - //__HAL_TIM_SET_COMPARE(&powerground1, 1, 0); - //__HAL_TIM_SET_COMPARE(&powerground2, 4, 0); - //__HAL_TIM_SET_COMPARE(&battery_cooling, 3, 0); + powerground_percent = 0; + battery_cooling_percent = 0; } +/* + controls the duty cycle of the fans by setting the CCR of the channel percent/100 = x/ARR +*/ void PWM_powerground_control(uint8_t percent){ - //uint8_t duty_cycle = (percent/100) * 65536; - //__HAL_TIM_SET_COMPARE(&powerground1, 1, 0); + if (percent > 100 || percent < 0) + return; + + powerground_percent = percent/100; + TIM15->CCR1 = (POWERGROUND_MAX_DUTY_CYCLE-POWERGROUND_MIN_DUTY_CYCLE) * (percent/100) + POWERGROUND_MIN_DUTY_CYCLE; + TIM15->CCR2 = TIM15->CCR1; /* *1.01 or *0.99 if the speeds of the fans are different*/ } void PWM_battery_cooling_control(uint8_t percent){ diff --git a/mvbms.ioc b/mvbms.ioc index 5c75f5e..4c362af 100644 --- a/mvbms.ioc +++ b/mvbms.ioc @@ -23,10 +23,9 @@ Mcu.IP3=RCC Mcu.IP4=SPI1 Mcu.IP5=SYS Mcu.IP6=TIM1 -Mcu.IP7=TIM2 -Mcu.IP8=TIM15 -Mcu.IP9=USART1 -Mcu.IPNb=10 +Mcu.IP7=TIM15 +Mcu.IP8=USART1 +Mcu.IPNb=9 Mcu.Name=STM32F302C(B-C)Tx Mcu.Package=LQFP48 Mcu.Pin0=PF0-OSC_IN @@ -110,7 +109,7 @@ PA2.Signal=S_TIM15_CH1 PA3.GPIOParameters=GPIO_Label PA3.GPIO_Label=PWM_PG_FAN2 PA3.Locked=true -PA3.Signal=S_TIM2_CH4 +PA3.Signal=S_TIM15_CH2 PA4.GPIOParameters=GPIO_Label PA4.GPIO_Label=CSB PA4.Locked=true @@ -190,7 +189,7 @@ ProjectManager.FreePins=true ProjectManager.HalAssertFull=false ProjectManager.HeapSize=0x200 ProjectManager.KeepUserCode=true -ProjectManager.LastFirmware=true +ProjectManager.LastFirmware=false ProjectManager.LibraryCopy=1 ProjectManager.MainLocation=Core/Src ProjectManager.NoMain=false @@ -243,8 +242,8 @@ RCC.USBFreq_Value=16000000 RCC.VCOOutput2Freq_Value=4000000 SH.S_TIM15_CH1.0=TIM15_CH1,PWM Generation1 CH1 SH.S_TIM15_CH1.ConfNb=1 -SH.S_TIM2_CH4.0=TIM2_CH4,PWM Generation4 CH4 -SH.S_TIM2_CH4.ConfNb=1 +SH.S_TIM15_CH2.0=TIM15_CH2,PWM Generation2 CH2 +SH.S_TIM15_CH2.ConfNb=1 SPI1.BaudRatePrescaler=SPI_BAUDRATEPRESCALER_32 SPI1.CalculateBaudRate=500.0 KBits/s SPI1.DataSize=SPI_DATASIZE_8BIT @@ -255,9 +254,8 @@ SPI1.VirtualType=VM_MASTER TIM1.Channel-PWM\ Generation3\ CH3N=TIM_CHANNEL_3 TIM1.IPParameters=Channel-PWM Generation3 CH3N TIM15.Channel-PWM\ Generation1\ CH1=TIM_CHANNEL_1 -TIM15.IPParameters=Channel-PWM Generation1 CH1 -TIM2.Channel-PWM\ Generation4\ CH4=TIM_CHANNEL_4 -TIM2.IPParameters=Channel-PWM Generation4 CH4 +TIM15.Channel-PWM\ Generation2\ CH2=TIM_CHANNEL_2 +TIM15.IPParameters=Channel-PWM Generation1 CH1,Channel-PWM Generation2 CH2 USART1.IPParameters=VirtualMode-Asynchronous USART1.VirtualMode-Asynchronous=VM_ASYNC VP_SYS_VS_Systick.Mode=SysTick