【雅特力AT-START-F435】PWM可调占空比
一、时钟配置
所有定时器连接到APB2总线上,在 system_clock_config 函数中对APB2二分频,所以在计算TMR1时钟周期的时候需要x2操作。
设置PWM的频率为:timerperiod = ((crm_clocks_freq_struct.apb2_freq * 2) / 10000 ) - 1;
计算TMR1的分频系数,设置为不分频。
计算PWM占空比:
/* tmr1 configuration ---------------------------------------------------
generate 7 pwm signals with 4 different duty cycles:
prescaler = 0, tmr1 counter clock = apb2_freq *2
the objective is to generate 7 pwm signal at 17.57 khz:
- tim1_period = (apb2_freq * 2 / 17570) - 1
the channel 1 and channel 1n duty cycle is set to 50%
the channel 2 and channel 2n duty cycle is set to 37.5%
the channel 3 and channel 3n duty cycle is set to 25%
the channel 4 duty cycle is set to 12.5%
the timer pulse is calculated as follows:
- channelxpulse = dutycycle * (tim1_period - 1) / 100
----------------------------------------------------------------------- */
二、PWM输出配置
设置为PWMB模式、使能输出、不使能互补输出、极性选择LOW、空闲状态为高电位。
设置占空比函数为 tmr_channel_value_set(TMR1, TMR_SELECT_CHANNEL_1, channel1pulse);
/* channel 1 configuration in output mode */
tmr_output_default_para_init(&tmr_output_struct);
tmr_output_struct.oc_mode = TMR_OUTPUT_CONTROL_PWM_MODE_B;
tmr_output_struct.oc_output_state = TRUE;
tmr_output_struct.oc_polarity = TMR_OUTPUT_ACTIVE_LOW;
tmr_output_struct.oc_idle_state = TRUE;
tmr_output_struct.occ_output_state = TRUE;
tmr_output_struct.occ_polarity = TMR_OUTPUT_ACTIVE_HIGH;
tmr_output_struct.occ_idle_state = FALSE;
/* channel 1 */
tmr_output_channel_config(TMR1, TMR_SELECT_CHANNEL_1, &tmr_output_struct);
tmr_channel_value_set(TMR1, TMR_SELECT_CHANNEL_1, channel1pulse);
在主函数中,while循环中更改占空比:
PWM周期为100us,每100us增加1%占空比,如下
二、PWM信号抓取
抓取的波形如下
在临界点发生突变的波形如下