added PWM_powerground_control()

moved pg2 to TIM15CH2
This commit is contained in:
Hamza
2024-05-25 18:41:12 +02:00
parent 7133516721
commit f6ba786e1b
3 changed files with 50 additions and 21 deletions

View File

@ -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);

View File

@ -1,28 +1,41 @@
#include "PWM_control.h"
#include "stm32f3xx_hal_tim.h"
#include <stdint.h>
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){