TIMER0 的 PWM 互补输出
该功能应用在电机控制中 可以配置三个通道对无刷电机驱动电路的三个半桥电路进行控制。
- CHx_O 和 CHx_ON 是一对互补输出通道,这两个信号不能同时有效(直白的说就是不能同时高电平)。 TIMER0 有四路通道,只有前三路有互补输出通道。
- 互补信号 CHx_O 和 CHx_ON 是由一组参数来决定:TIMERx_CHCTL2 寄存器中的 CHxEN 和 CHxNEN 位。
- TIMERx_CCHP 寄存器中和TIMERx_CTL1 寄 存 器 中 的 POEN , ROS , IOS , ISOx 和 ISOxN 位 。
- 输出极性由 TIMERx_CHCTL2 寄存器中的 CHxP 和 CHxNP 位来决定。
配置流程简述
基本参数初始化
-
初始化定时器和端口的时钟
-
端口的配置
-
定时器的初始化配置timer_init(TIMER0,&Timer_ParamType);
-
定时器刹车及死区的配置timer_break_config(TIMER0,&Timer_BreakParaType);
-
定时器输出参数的配置timer_channel_output_config(TIMER0,TIMER_CH_0,&Timer_OC_ParamType);
功能的使能
-
配置输出比较器的值timer_channel_output_pulse_value_config(TIMER0, TIMER_CH_0, 500-1);
-
配置通道输出的模式timer_channel_output_mode_config(TIMER0,TIMER_CH_0,TIMER_OC_MODE_PWM0);
-
使能端口的输出 timer_primary_output_config(TIMER0,ENABLE);
-
使能定时器 timer_enable(TIMER0);
开发环境&参考文档
- Keil-MDK 5.34
- Windows10
- 固件库-GD32F3x0_Firmware_Library_V2.2.0
- GD32F310xx-数据手册-Rev1.1
- GD32F3x0-用户手册-Rev2.5
- GD32F3x0-固件库使用指南-Rev1.2
代码部分
pwm.h 文件
#ifndef _PWM_H_
#define _PWM_H_
#include "gd32f3x0.h"
void HW_PWMInit(void);
#endif
pwm.c 文件
#include "pwm.h"
void HW_PWMInit(void)
{
timer_parameter_struct Timer_ParamType;
timer_oc_parameter_struct Timer_OC_ParamType;
timer_break_parameter_struct Timer_BreakParaType;
rcu_periph_clock_enable(RCU_GPIOA);
rcu_periph_clock_enable(RCU_TIMER0);
gpio_af_set(GPIOA,GPIO_AF_2,GPIO_PIN_6);
gpio_mode_set(GPIOA, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO_PIN_6);
gpio_output_options_set(GPIOA, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_6);
gpio_af_set(GPIOA,GPIO_AF_2,GPIO_PIN_7);
gpio_mode_set(GPIOA, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO_PIN_7);
gpio_output_options_set(GPIOA, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_7);
gpio_af_set(GPIOA,GPIO_AF_2,GPIO_PIN_8);
gpio_mode_set(GPIOA, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO_PIN_8);
gpio_output_options_set(GPIOA, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_8);
timer_deinit(TIMER0);
timer_struct_para_init(&Timer_ParamType);
Timer_ParamType.alignedmode = TIMER_COUNTER_EDGE;
Timer_ParamType.clockdivision = TIMER_CKDIV_DIV2;
Timer_ParamType.counterdirection = TIMER_COUNTER_UP;
Timer_ParamType.period = 1000-1;
Timer_ParamType.prescaler = 72-1;
Timer_ParamType.repetitioncounter = 1;
timer_init(TIMER0,&Timer_ParamType);
timer_break_struct_para_init(&Timer_BreakParaType);
Timer_BreakParaType.breakpolarity = TIMER_BREAK_POLARITY_HIGH;
Timer_BreakParaType.breakstate = TIMER_BREAK_ENABLE;
Timer_BreakParaType.deadtime = 10;
Timer_BreakParaType.ideloffstate = TIMER_IOS_STATE_ENABLE;
Timer_BreakParaType.protectmode = TIMER_CCHP_PROT_0;
Timer_BreakParaType.runoffstate = TIMER_ROS_STATE_ENABLE;
timer_break_config(TIMER0,&Timer_BreakParaType);
timer_break_enable(TIMER0);
timer_channel_output_struct_para_init(&Timer_OC_ParamType);
Timer_OC_ParamType.ocidlestate = TIMER_OC_IDLE_STATE_HIGH;
Timer_OC_ParamType.ocnidlestate = TIMER_OCN_IDLE_STATE_LOW;
Timer_OC_ParamType.ocnpolarity = TIMER_OCN_POLARITY_HIGH;
Timer_OC_ParamType.ocpolarity = TIMER_OC_POLARITY_HIGH;
Timer_OC_ParamType.outputnstate = TIMER_CCXN_ENABLE;
Timer_OC_ParamType.outputstate = TIMER_CCX_ENABLE;
timer_channel_output_config(TIMER0,TIMER_CH_0,&Timer_OC_ParamType);
timer_channel_output_pulse_value_config(TIMER0, TIMER_CH_0, 500-1);
timer_channel_output_mode_config(TIMER0,TIMER_CH_0,TIMER_OC_MODE_PWM0);
timer_primary_output_config(TIMER0,ENABLE);
timer_enable(TIMER0);
}
main.c 文件
#include "gd32f3x0.h"
#include "systick.h"
#include "gpio.h"
#include "pwm.h"
int main(void)
{
systick_config();
HW_GpioInit();
HW_PWMInit();
while(1)
{
/* insert 500 ms delay /
delay_1ms(100);
/ toggle the LED */
gpio_bit_toggle(GPIOA,GPIO_PIN_1);
}
}
原作者:Stark_GS