【雅特力AT-START-F435】TIM1生成3路互补PWM
[复制链接]
本次测评中参考例程中TMR,生成三路互补的PWM,并利用时基单元中断实现软定时器。
参考文件目录如下:
AT32F435_437_Firmware_Library_V2.1.2\project\at_start_f435\examples\tmr\complementary_signalsIIC的基础原理
官方例程参考
①GPIO初始化
GPIOA.GPIO8->TMR1.channel1
GPIOA.GPIO9->TMR1.channel2
GPIOA.GPIO10->TMR1.channel3
GPIOB.GPIO13->TMR1.channel1N
GPIOB.GPIO14->TMR1.channel2N
GPIOB.GPIO15->TMR1.channel3N
GPIOB.GPIO12->TMR1.BRK
刹车为低电平时PWM正常输出,刹车输入为高电平时PWM输出被封锁。
/* timer1 output pin configuration */
gpio_init_struct.gpio_pins = GPIO_PINS_8 | GPIO_PINS_9 | GPIO_PINS_10;
gpio_init_struct.gpio_mode = GPIO_MODE_MUX;
gpio_init_struct.gpio_out_type = GPIO_OUTPUT_PUSH_PULL;
gpio_init_struct.gpio_pull = GPIO_PULL_NONE;
gpio_init_struct.gpio_drive_strength = GPIO_DRIVE_STRENGTH_STRONGER;
gpio_init(GPIOA, &gpio_init_struct);
gpio_init_struct.gpio_pins = GPIO_PINS_13 | GPIO_PINS_14 | GPIO_PINS_15;
gpio_init_struct.gpio_mode = GPIO_MODE_MUX;
gpio_init_struct.gpio_out_type = GPIO_OUTPUT_PUSH_PULL;
gpio_init_struct.gpio_pull = GPIO_PULL_NONE;
gpio_init_struct.gpio_drive_strength = GPIO_DRIVE_STRENGTH_STRONGER;
gpio_init(GPIOB, &gpio_init_struct);
gpio_init_struct.gpio_pins = GPIO_PINS_12;
gpio_init_struct.gpio_mode = GPIO_MODE_MUX;
gpio_init_struct.gpio_out_type = GPIO_OUTPUT_PUSH_PULL;
gpio_init_struct.gpio_pull = GPIO_PULL_NONE;
gpio_init_struct.gpio_drive_strength = GPIO_DRIVE_STRENGTH_STRONGER;
gpio_init(GPIOB, &gpio_init_struct);
gpio_pin_mux_config(GPIOA, GPIO_PINS_SOURCE8, GPIO_MUX_1);
gpio_pin_mux_config(GPIOA, GPIO_PINS_SOURCE9, GPIO_MUX_1);
gpio_pin_mux_config(GPIOA, GPIO_PINS_SOURCE10, GPIO_MUX_1);
gpio_pin_mux_config(GPIOB, GPIO_PINS_SOURCE13, GPIO_MUX_1);
gpio_pin_mux_config(GPIOB, GPIO_PINS_SOURCE14, GPIO_MUX_1);
gpio_pin_mux_config(GPIOB, GPIO_PINS_SOURCE15, GPIO_MUX_1);
gpio_pin_mux_config(GPIOB, GPIO_PINS_SOURCE12, GPIO_MUX_1);
②时基单元初始化
定时器时基单元初始化,定时器的输入主频为288MHz,二分频后定时器主频为144MHz,定时器的最大计数值为14400,则PWM的频率为10KHz。同时打开溢出中断,分配中断优先级。
tmr_base_init(TMR1, timerperiod, 1);
tmr_cnt_dir_set(TMR1, TMR_COUNT_UP);
tmr_interrupt_enable(TMR1, TMR_OVF_INT, TRUE);
/* tmr1 hall interrupt nvic init */
nvic_priority_group_config(NVIC_PRIORITY_GROUP_4);
nvic_irq_enable(TMR1_OVF_TMR10_IRQn, 1, 0);
③输出比较单元初始化
/* channel 1, 2, 3 and 4 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_HIGH;
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);
/* channel 2 */
tmr_output_channel_config(TMR1, TMR_SELECT_CHANNEL_2, &tmr_output_struct);
tmr_channel_value_set(TMR1, TMR_SELECT_CHANNEL_2, channel2pulse);
/* channel 3 */
tmr_output_channel_config(TMR1, TMR_SELECT_CHANNEL_3, &tmr_output_struct);
tmr_channel_value_set(TMR1, TMR_SELECT_CHANNEL_3, channel3pulse);
④刹车单元初始化
可以添加死区时间,使能PWM输出通道,使能PWM输出和计数器。
/* automatic output enable, stop, dead time and lock configuration*/
tmr_brkdt_default_para_init(&tmr_brkdt_config_struct);
tmr_brkdt_config_struct.brk_enable = TRUE;
tmr_brkdt_config_struct.auto_output_enable = TRUE;
tmr_brkdt_config_struct.deadtime = 200;
tmr_brkdt_config_struct.fcsodis_state = TRUE;
tmr_brkdt_config_struct.fcsoen_state = TRUE;
tmr_brkdt_config_struct.brk_polarity = TMR_BRK_INPUT_ACTIVE_HIGH;
tmr_brkdt_config_struct.wp_level = TMR_WP_LEVEL_3;
tmr_brkdt_config(TMR1, &tmr_brkdt_config_struct);
//
tmr_channel_enable(TMR1, TMR_SELECT_CHANNEL_1,TRUE);
tmr_channel_enable(TMR1, TMR_SELECT_CHANNEL_1C, TRUE);
tmr_channel_enable(TMR1, TMR_SELECT_CHANNEL_2, TRUE);
tmr_channel_enable(TMR1, TMR_SELECT_CHANNEL_2C, TRUE);
tmr_channel_enable(TMR1, TMR_SELECT_CHANNEL_3, TRUE);
tmr_channel_enable(TMR1, TMR_SELECT_CHANNEL_3C, TRUE);
/* tmr1 output enable */
tmr_output_enable(TMR1, TRUE);
/* enable tmr1 */
tmr_counter_enable(TMR1, TRUE);
⑤定时器溢出中断&软定时器
void TMR1_OVF_TMR10_IRQHandler(void)
{
if (tmr_flag_get(TMR1, TMR_OVF_FLAG) == SET)
{
I_Task_Exec(0, 0);
tmr_flag_clear(TMR1, TMR_OVF_FLAG);
}
}
实际测试
测试中的PWM的计数器最大值为14400,比较值固定为7200,死区时间为200。因此理论的PWM的正脉宽为(7200-200)/14400 = 48.61%,频率为10KHz,经过示波器测试可得:
周期为10KHz,正占空比为48.89%,和理论值相符。
|