PWM_F450.cpp
#include "PWM_F450.hpp"
#include "main.h"
bool flag = 0;
//PE9
void PWM_T0CH0_F450::Init(uint16_t _pwmFq)
{
pwmfq = 20000;
fullPwm = 8400;
rcu_periph_clock_enable(RCU_GPIOE);
gpio_mode_set(GPIOE, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO_PIN_9);
gpio_output_options_set(GPIOE, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ,GPIO_PIN_9);
gpio_af_set(GPIOE, GPIO_AF_1, GPIO_PIN_9);
/* -----------------------------------------------------------------------
TIMER1 configuration: generate 3 PWM signals with 3 different duty cycles:
TIMER1CLK = SystemCoreClock / 200 = 1MHz
TIMER1 channel1 duty cycle = (4000/ 16000)* 100 = 25%
TIMER1 channel2 duty cycle = (8000/ 16000)* 100 = 50%
TIMER1 channel3 duty cycle = (12000/ 16000)* 100 = 75%
----------------------------------------------------------------------- */
timer_oc_parameter_struct timer_ocintpara;
timer_parameter_struct timer_initpara;
rcu_periph_clock_enable(RCU_TIMER0);
rcu_timer_clock_prescaler_config(RCU_TIMER_PSC_MUL4);
if (flag == 0)
{
timer_deinit(TIMER0);
flag = 1;
}
timer_primary_output_config(TIMER0,ENABLE);
/* TIMER1 configuration */
timer_initpara.prescaler = 99;
timer_initpara.alignedmode = TIMER_COUNTER_EDGE;
timer_initpara.counterdirection = TIMER_COUNTER_UP;
timer_initpara.period = 99;
timer_initpara.clockdivision = TIMER_CKDIV_DIV1;
timer_initpara.repetitioncounter = 0;
timer_init(TIMER0,&timer_initpara);
/* CH1,CH2 and CH3 configuration in PWM mode */
timer_ocintpara.ocpolarity = TIMER_OC_POLARITY_HIGH;
timer_ocintpara.outputstate = TIMER_CCX_ENABLE;
timer_ocintpara.ocnpolarity = TIMER_OCN_POLARITY_HIGH;
timer_ocintpara.outputnstate = TIMER_CCXN_DISABLE;
timer_ocintpara.ocidlestate = TIMER_OC_IDLE_STATE_LOW;
timer_ocintpara.ocnidlestate = TIMER_OCN_IDLE_STATE_LOW;
timer_channel_output_config(TIMER0,TIMER_CH_0,&timer_ocintpara);
/* CH2 configuration in PWM mode1,duty cycle 50% */
timer_channel_output_pulse_value_config(TIMER0,TIMER_CH_0,0);
timer_channel_output_mode_config(TIMER0,TIMER_CH_0,TIMER_OC_MODE_PWM0);
timer_channel_output_shadow_config(TIMER0,TIMER_CH_0,TIMER_OC_SHADOW_DISABLE);
/* auto-reload preload enable */
timer_auto_reload_shadow_enable(TIMER0);
/* auto-reload preload enable */
timer_enable(TIMER0);
}
void PWM_T0CH0_F450::StartPwM(void)
{
}
void PWM_T0CH0_F450::SetPWM(uint16_t _pwm)
{
/* CH0 configuration in PWM mode1,duty cycle 25% */
timer_channel_output_pulse_value_config(TIMER0,TIMER_CH_0,_pwm);
timer_channel_output_mode_config(TIMER0,TIMER_CH_0,TIMER_OC_MODE_PWM0);
timer_channel_output_shadow_config(TIMER0,TIMER_CH_0,TIMER_OC_SHADOW_DISABLE);
}
//PE11
void PWM_T0CH1_F450::Init(uint16_t _pwmFq)
{
pwmfq = 20000;
fullPwm = 8400;
rcu_periph_clock_enable(RCU_GPIOE);
gpio_mode_set(GPIOE, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO_PIN_11);
gpio_output_options_set(GPIOE, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ,GPIO_PIN_11);
gpio_af_set(GPIOE, GPIO_AF_1, GPIO_PIN_11);
/* -----------------------------------------------------------------------
TIMER1 configuration: generate 3 PWM signals with 3 different duty cycles:
TIMER1CLK = SystemCoreClock / 200 = 1MHz
TIMER1 channel1 duty cycle = (4000/ 16000)* 100 = 25%
TIMER1 channel2 duty cycle = (8000/ 16000)* 100 = 50%
TIMER1 channel3 duty cycle = (12000/ 16000)* 100 = 75%
----------------------------------------------------------------------- */
timer_oc_parameter_struct timer_ocintpara;
timer_parameter_struct timer_initpara;
rcu_periph_clock_enable(RCU_TIMER0);
rcu_timer_clock_prescaler_config(RCU_TIMER_PSC_MUL4);
if (flag == 0)
{
timer_deinit(TIMER0);
flag = 1;
}
timer_primary_output_config(TIMER0,ENABLE);
/* TIMER1 configuration */
timer_initpara.prescaler = 99;
timer_initpara.alignedmode = TIMER_COUNTER_EDGE;
timer_initpara.counterdirection = TIMER_COUNTER_UP;
timer_initpara.period = 99;
timer_initpara.clockdivision = TIMER_CKDIV_DIV1;
timer_initpara.repetitioncounter = 0;
timer_init(TIMER0,&timer_initpara);
/* CH1,CH2 and CH3 configuration in PWM mode */
timer_ocintpara.ocpolarity = TIMER_OC_POLARITY_HIGH;
timer_ocintpara.outputstate = TIMER_CCX_ENABLE;
timer_ocintpara.ocnpolarity = TIMER_OCN_POLARITY_HIGH;
timer_ocintpara.outputnstate = TIMER_CCXN_DISABLE;
timer_ocintpara.ocidlestate = TIMER_OC_IDLE_STATE_LOW;
timer_ocintpara.ocnidlestate = TIMER_OCN_IDLE_STATE_LOW;
timer_channel_output_config(TIMER0,TIMER_CH_1,&timer_ocintpara);
/* CH1 configuration in PWM mode1,duty cycle 25% */
timer_channel_output_pulse_value_config(TIMER0,TIMER_CH_1,0);
timer_channel_output_mode_config(TIMER0,TIMER_CH_1,TIMER_OC_MODE_PWM0);
timer_channel_output_shadow_config(TIMER0,TIMER_CH_1,TIMER_OC_SHADOW_DISABLE);
/* auto-reload preload enable */
timer_auto_reload_shadow_enable(TIMER0);
/* auto-reload preload enable */
timer_enable(TIMER0);
}
void PWM_T0CH1_F450::StartPwM(void)
{
}
void PWM_T0CH1_F450::SetPWM(uint16_t _pwm)
{
/* CH1 configuration in PWM mode1,duty cycle 25% */
timer_channel_output_pulse_value_config(TIMER0,TIMER_CH_1,_pwm);
timer_channel_output_mode_config(TIMER0,TIMER_CH_1,TIMER_OC_MODE_PWM0);
timer_channel_output_shadow_config(TIMER0,TIMER_CH_1,TIMER_OC_SHADOW_DISABLE);
}
//PE13
void PWM_T0CH2_F450::Init(uint16_t _pwmFq)
{
pwmfq = 20000;
fullPwm = 8400;
rcu_periph_clock_enable(RCU_GPIOE);
gpio_mode_set(GPIOE, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO_PIN_13);
gpio_output_options_set(GPIOE, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ,GPIO_PIN_13);
gpio_af_set(GPIOE, GPIO_AF_1, GPIO_PIN_13);
/* -----------------------------------------------------------------------
TIMER1 configuration: generate 3 PWM signals with 3 different duty cycles:
TIMER1CLK = SystemCoreClock / 200 = 1MHz
TIMER1 channel1 duty cycle = (4000/ 16000)* 100 = 25%
TIMER1 channel2 duty cycle = (8000/ 16000)* 100 = 50%
TIMER1 channel3 duty cycle = (12000/ 16000)* 100 = 75%
----------------------------------------------------------------------- */
timer_oc_parameter_struct timer_ocintpara;
timer_parameter_struct timer_initpara;
rcu_periph_clock_enable(RCU_TIMER0);
rcu_timer_clock_prescaler_config(RCU_TIMER_PSC_MUL4);
if (flag == 0)
{
timer_deinit(TIMER0);
flag = 1;
}
timer_primary_output_config(TIMER0,ENABLE);
/* TIMER1 configuration */
timer_initpara.prescaler = 99;
timer_initpara.alignedmode = TIMER_COUNTER_EDGE;
timer_initpara.counterdirection = TIMER_COUNTER_UP;
timer_initpara.period = 99;
timer_initpara.clockdivision = TIMER_CKDIV_DIV1;
timer_initpara.repetitioncounter = 0;
timer_init(TIMER0,&timer_initpara);
/* CH1,CH2 and CH3 configuration in PWM mode */
timer_ocintpara.ocpolarity = TIMER_OC_POLARITY_HIGH;
timer_ocintpara.outputstate = TIMER_CCX_ENABLE;
timer_ocintpara.ocnpolarity = TIMER_OCN_POLARITY_HIGH;
timer_ocintpara.outputnstate = TIMER_CCXN_DISABLE;
timer_ocintpara.ocidlestate = TIMER_OC_IDLE_STATE_LOW;
timer_ocintpara.ocnidlestate = TIMER_OCN_IDLE_STATE_LOW;
timer_channel_output_config(TIMER0,TIMER_CH_2,&timer_ocintpara);
/* CH2 configuration in PWM mode1,duty cycle 50% */
timer_channel_output_pulse_value_config(TIMER0,TIMER_CH_2,0);
timer_channel_output_mode_config(TIMER0,TIMER_CH_2,TIMER_OC_MODE_PWM0);
timer_channel_output_shadow_config(TIMER0,TIMER_CH_2,TIMER_OC_SHADOW_DISABLE);
/* auto-reload preload enable */
timer_auto_reload_shadow_enable(TIMER0);
/* auto-reload preload enable */
timer_enable(TIMER0);
}
void PWM_T0CH2_F450::StartPwM(void)
{
}
void PWM_T0CH2_F450::SetPWM(uint16_t _pwm)
{
timer_channel_output_pulse_value_config(TIMER0,TIMER_CH_2,_pwm);
timer_channel_output_mode_config(TIMER0,TIMER_CH_2,TIMER_OC_MODE_PWM0);
timer_channel_output_shadow_config(TIMER0,TIMER_CH_2,TIMER_OC_SHADOW_DISABLE);
}
//PE14
void PWM_T0CH3_F450::Init(uint16_t _pwmFq)
{
pwmfq = 20000;
fullPwm = 8400;
rcu_periph_clock_enable(RCU_GPIOE);
gpio_mode_set(GPIOE, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO_PIN_14);
gpio_output_options_set(GPIOE, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ,GPIO_PIN_14);
gpio_af_set(GPIOE, GPIO_AF_1, GPIO_PIN_14);
/* -----------------------------------------------------------------------
TIMER1 configuration: generate 3 PWM signals with 3 different duty cycles:
TIMER1CLK = SystemCoreClock / 200 = 1MHz
TIMER1 channel1 duty cycle = (4000/ 16000)* 100 = 25%
TIMER1 channel2 duty cycle = (8000/ 16000)* 100 = 50%
TIMER1 channel3 duty cycle = (12000/ 16000)* 100 = 75%
----------------------------------------------------------------------- */
timer_oc_parameter_struct timer_ocintpara;
timer_parameter_struct timer_initpara;
rcu_periph_clock_enable(RCU_TIMER0);
rcu_timer_clock_prescaler_config(RCU_TIMER_PSC_MUL4);
if (flag == 0)
{
timer_deinit(TIMER0);
flag = 1;
}
timer_primary_output_config(TIMER0,ENABLE);
/* TIMER1 configuration */
timer_initpara.prescaler = 99;
timer_initpara.alignedmode = TIMER_COUNTER_EDGE;
timer_initpara.counterdirection = TIMER_COUNTER_UP;
timer_initpara.period = 99;
timer_initpara.clockdivision = TIMER_CKDIV_DIV1;
timer_initpara.repetitioncounter = 0;
timer_init(TIMER0,&timer_initpara);
/* CH1,CH2 and CH3 configuration in PWM mode */
timer_ocintpara.ocpolarity = TIMER_OC_POLARITY_HIGH;
timer_ocintpara.outputstate = TIMER_CCX_ENABLE;
timer_ocintpara.ocnpolarity = TIMER_OCN_POLARITY_HIGH;
timer_ocintpara.outputnstate = TIMER_CCXN_DISABLE;
timer_ocintpara.ocidlestate = TIMER_OC_IDLE_STATE_LOW;
timer_ocintpara.ocnidlestate = TIMER_OCN_IDLE_STATE_LOW;
timer_channel_output_config(TIMER0,TIMER_CH_3,&timer_ocintpara);
/* CH3 configuration in PWM mode1,duty cycle 75% */
timer_channel_output_pulse_value_config(TIMER0,TIMER_CH_3,0);
timer_channel_output_mode_config(TIMER0,TIMER_CH_3,TIMER_OC_MODE_PWM0);
timer_channel_output_shadow_config(TIMER0,TIMER_CH_3,TIMER_OC_SHADOW_DISABLE);
/* auto-reload preload enable */
timer_auto_reload_shadow_enable(TIMER0);
// /* auto-reload preload enable */
timer_enable(TIMER0);
}
void PWM_T0CH3_F450::StartPwM(void)
{
}
void PWM_T0CH3_F450::SetPWM(uint16_t _pwm)
{
timer_channel_output_pulse_value_config(TIMER0,TIMER_CH_3,_pwm);
timer_channel_output_mode_config(TIMER0,TIMER_CH_3,TIMER_OC_MODE_PWM0);
timer_channel_output_shadow_config(TIMER0,TIMER_CH_3,TIMER_OC_SHADOW_DISABLE);
}
PWM_F450.hpp
#pragma once
#include "bsp/bsp.hpp"
class PWM_T0CH0_F450 : public PWM
{
public:
PWM_T0CH0_F450() {}
void Init(uint16_t _pwmFq);
void StartPwM(void);
void SetPWM(uint16_t _pwm);
};
class PWM_T0CH1_F450 : public PWM
{
public:
PWM_T0CH1_F450() {}
void Init(uint16_t _pwmFq);
void StartPwM(void);
void SetPWM(uint16_t _pwm);
};
class PWM_T0CH2_F450 : public PWM
{
public:
PWM_T0CH2_F450() {}
void Init(uint16_t _pwmFq);
void StartPwM(void);
void SetPWM(uint16_t _pwm);
};
class PWM_T0CH3_F450 : public PWM
{
public:
PWM_T0CH3_F450() {}
void Init(uint16_t _pwmFq);
void StartPwM(void);
void SetPWM(uint16_t _pwm);
};
PWM
#pragma once
#include <stdint.h>
class PWM
{
public:
PWM(){}
uint16_t fullPwm;
uint16_t pwmfq;
virtual void Init(uint16_t _pwmFq){}
virtual void StartPwM(void){}
virtual void SetPWM(uint16_t _pwm){}
};