PWM输出实验
1、使用Timer2输出四路pwm.管脚对应关系如下图。
2、在timer.c 中增加pwm_config函数。
timer.c
#if ENABLE_PWM_TEST == 1
static void pwm_channel_gpio_config(void)
{
rcu_periph_clock_enable(RCU_GPIOA);
rcu_periph_clock_enable(RCU_GPIOB);
rcu_periph_clock_enable(RCU_AF);
/*Configure PA6 PA7 PB0 PB1(TIMER2 CH0 CH1 CH2) as alternate function*/
gpio_init(GPIOA,GPIO_MODE_AF_PP,GPIO_OSPEED_50MHZ,GPIO_PIN_6);
gpio_init(GPIOA,GPIO_MODE_AF_PP,GPIO_OSPEED_50MHZ,GPIO_PIN_7);
gpio_init(GPIOB,GPIO_MODE_AF_PP,GPIO_OSPEED_50MHZ,GPIO_PIN_0);
gpio_init(GPIOB,GPIO_MODE_AF_PP,GPIO_OSPEED_50MHZ,GPIO_PIN_1);
}
static void timer2_config(void)
{
/* -----------------------------------------------------------------------
TIMER2 configuration: generate 3 PWM signals with 3 different duty cycles:
TIMER2CLK = SystemCoreClock / 108 = 1MHz
108M/(107+1)/(999+1) = 1000HZ
TIMER2 channel0 duty cycle = (250/ 1000)* 100 = 25%
TIMER2 channel1 duty cycle = (500/ 1000)* 100 = 50%
TIMER2 channel2 duty cycle = (750/ 1000)* 100 = 75%
----------------------------------------------------------------------- */
timer_oc_parameter_struct timer_ocintpara;
timer_parameter_struct timer_initpara;
rcu_periph_clock_enable(RCU_TIMER2);
timer_deinit(TIMER2);
/* TIMER2 configuration */
timer_initpara.prescaler = 107;
timer_initpara.alignedmode = TIMER_COUNTER_EDGE;
timer_initpara.counterdirection = TIMER_COUNTER_UP;
timer_initpara.period = 999;
timer_initpara.clockdivision = TIMER_CKDIV_DIV1;
timer_initpara.repetitioncounter = 0;
timer_init(TIMER2,&timer_initpara);
/* CH0,CH1 and CH2 configuration in PWM mode */
timer_ocintpara.outputstate = TIMER_CCX_ENABLE;
timer_ocintpara.outputnstate = TIMER_CCXN_DISABLE;
timer_ocintpara.ocpolarity = TIMER_OC_POLARITY_HIGH;
timer_ocintpara.ocnpolarity = TIMER_OCN_POLARITY_HIGH;
timer_ocintpara.ocidlestate = TIMER_OC_IDLE_STATE_LOW;
timer_ocintpara.ocnidlestate = TIMER_OCN_IDLE_STATE_LOW;
timer_channel_output_config(TIMER2,TIMER_CH_0,&timer_ocintpara);
timer_channel_output_config(TIMER2,TIMER_CH_1,&timer_ocintpara);
timer_channel_output_config(TIMER2,TIMER_CH_2,&timer_ocintpara);
/* CH0 configuration in PWM mode0,duty cycle 25% */
timer_channel_output_pulse_value_config(TIMER2,TIMER_CH_0,249);
timer_channel_output_mode_config(TIMER2,TIMER_CH_0,TIMER_OC_MODE_PWM0);
timer_channel_output_shadow_config(TIMER2,TIMER_CH_0,TIMER_OC_SHADOW_DISABLE);
/* CH1 configuration in PWM mode0,duty cycle 50% */
timer_channel_output_pulse_value_config(TIMER2,TIMER_CH_1,499);
timer_channel_output_mode_config(TIMER2,TIMER_CH_1,TIMER_OC_MODE_PWM0);
timer_channel_output_shadow_config(TIMER2,TIMER_CH_1,TIMER_OC_SHADOW_DISABLE);
/* CH2 configuration in PWM mode0,duty cycle 75% */
timer_channel_output_pulse_value_config(TIMER2,TIMER_CH_2,749);
timer_channel_output_mode_config(TIMER2,TIMER_CH_2,TIMER_OC_MODE_PWM0);
timer_channel_output_shadow_config(TIMER2,TIMER_CH_2,TIMER_OC_SHADOW_DISABLE);
/* auto-reload preload enable */
timer_auto_reload_shadow_enable(TIMER2);
/* auto-reload preload enable */
timer_enable(TIMER2);
}
// 使用通用定时器TIM2 产生PWM
void pwm_config(void)
{
pwm_channel_gpio_config();
timer2_config();
}
#endif //if ENABLE_PWM_TEST == 1
timer.h
#define ENABLE_PWM_TEST 1
#if ENABLE_PWM_TEST == 1
void pwm_config(void);
#endif
main.c 增加
#if ENABLE_PWM_TEST == 1
pwm_config();
#endif
3、验证
PA6
PA7
PB0
4、关于timer2的时钟
Tim1、2、3、4、5、6、11、12、13 都是挂在APB1的外设,上图得知如果prescale!=1时,TIMER时钟2,所以这几个TIM频率应该的是APB12.
/* APB1 = AHB/2 */
RCU_CFG0 |= RCU_APB1_CKAHB_DIV2;