R4 STM32高级定时器笔记之PWM互补输出

时间:2021-05-23 23:25:45

STM32高级定时器笔记之PWM互补输出

程序功能

通过两个GPIO,输出相反的PWM信号,带死区时间和刹车控制。
PWM为50%。要配置几个寄存器:
CNT计数器
CCR输出比较寄存器器(输入捕获寄存器)
ARR自动重装载寄存器(最大65535)
当CQT开始计数(从高电平开始),小于CCR为高电平,等于CCR时跳变低电平。再计数,计数到等于ARR,计数器清零,电平反转,变为高电平。
计数器的时钟(频率)为72Mhz/psc+1
计数器的周期psc+1/72Mhz
- psc分屏因子

R4 STM32高级定时器笔记之PWM互补输出

PWM互补输出—bsp_AdvanceTim.c

高级定时器时钟挂载在RCC_APB2上,如果改为通用定时器.h里的RCC_APB2要改为RCC_APB1

#define ADVANCE_TIM TIM1
#define ADVANCE_TIM_APBxClock_FUN RCC_APB2PeriphClockCmd
#define ADVANCE_TIM_CLK RCC_APB2Periph_TIM1

三个形参:

#define ADVANCE_TIM_PERIOD (8-1)//自动重装载寄存器,决定周期,ARR
#define ADVANCE_TIM_PSC (9-1)//计数器分频因子PSC
#define ADVANCE_TIM_PULSE 4//CCR

配置GPIO

仅用于STM32F103ZET6,STM32F103RCT6:
R4 STM32高级定时器笔记之PWM互补输出
用到三个引脚:CH1-PA8 CHIN-PB13 BKIN-PB12

static void ADVANCE_TIM_GPIO_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;

// 输出比较通道 GPIO 初始化
RCC_APB2PeriphClockCmd(ADVANCE_TIM_CH1_GPIO_CLK, ENABLE);
GPIO_InitStructure.GPIO_Pin = ADVANCE_TIM_CH1_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(ADVANCE_TIM_CH1_PORT, &GPIO_InitStructure);

// 输出比较通道互补通道 GPIO 初始化
RCC_APB2PeriphClockCmd(ADVANCE_TIM_CH1N_GPIO_CLK, ENABLE);
GPIO_InitStructure.GPIO_Pin = ADVANCE_TIM_CH1N_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(ADVANCE_TIM_CH1N_PORT, &GPIO_InitStructure);

// 输出比较通道刹车通道 GPIO 初始化
RCC_APB2PeriphClockCmd(ADVANCE_TIM_BKIN_GPIO_CLK, ENABLE);
GPIO_InitStructure.GPIO_Pin = ADVANCE_TIM_BKIN_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(ADVANCE_TIM_BKIN_PORT, &GPIO_InitStructure);
// BKIN引脚默认先输出低电平
GPIO_ResetBits(ADVANCE_TIM_BKIN_PORT,ADVANCE_TIM_BKIN_PIN);
}

配置高级定时器工作模式(初始化结构体)

static void ADVANCE_TIM_Mode_Config(void)
{
//开定时器时钟,即内部时钟CK_INT=72M
ADVANCE_TIM_APBxClock_FUN(ADVANCE_TIM_CLK,ENABLE);

/*--------------------时基结构体初始化(确定周期和计数器时钟)-----*/
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_TimeBaseStructure.TIM_Period=ADVANCE_TIM_PERIOD;//自动重装寄存器的值
TIM_TimeBaseStructure.TIM_Prescaler= ADVANCE_TIM_PSC;// 驱动CNT计数器的时钟= Fck_int/(psc+1)
TIM_TimeBaseStructure.TIM_ClockDivision=TIM_CKD_DIV1;//时钟分频因子,配置死区时用
TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up;//计数器计数模式,设置为向上计数
TIM_TimeBaseStructure.TIM_RepetitionCounter=0;//重复计数器的值
TIM_TimeBaseInit(ADVANCE_TIM, &TIM_TimeBaseStructure);//初始化定时器

/*--------------------输出比较结构体初始化(配置PWM)--------*/
TIM_OCInitTypeDef TIM_OCInitStructure;
// 配置为PWM模式1
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
// 输出使能
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
//互补输出使能
TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable;
// 设置占空比大小
TIM_OCInitStructure.TIM_Pulse = ADVANCE_TIM_PULSE;
// 输出通道电平极性配置
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
// 互补输出通道电平极性配置
TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High;
// 输出通道空闲电平极性配置
TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set;
// 互补输出通道空闲电平极性配置
TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCNIdleState_Reset;
TIM_OC1Init(ADVANCE_TIM, &TIM_OCInitStructure);
TIM_OC1PreloadConfig(ADVANCE_TIM, TIM_OCPreload_Enable);

/*-------------------刹车和死区结构体初始化-------------------*/
//可参看BDTR寄存器描述
TIM_BDTRInitTypeDef TIM_BDTRInitStructure;
TIM_BDTRInitStructure.TIM_OSSRState = TIM_OSSRState_Enable;
TIM_BDTRInitStructure.TIM_OSSIState = TIM_OSSIState_Enable;
TIM_BDTRInitStructure.TIM_LOCKLevel = TIM_LOCKLevel_1;
// 输出比较信号死区时间配置,可参考 BDTR:UTG[7:0]描述
// 配置的死区时间为152ns
TIM_BDTRInitStructure.TIM_DeadTime = 11;//刹车时间
TIM_BDTRInitStructure.TIM_Break = TIM_Break_Enable;//刹车是否使能
//当BKIN引脚检测到高电平,输出比较信号被禁止,如同刹车
TIM_BDTRInitStructure.TIM_BreakPolarity = TIM_BreakPolarity_High;
TIM_BDTRInitStructure.TIM_AutomaticOutput = TIM_AutomaticOutput_Enable;
TIM_BDTRConfig(ADVANCE_TIM, &TIM_BDTRInitStructure);

// 使能计数器
TIM_Cmd(ADVANCE_TIM, ENABLE);
// 主输出使能,当使用通用定时器时,这句不需要
TIM_CtrlPWMOutputs(ADVANCE_TIM, ENABLE);
}

调用上面两个函数

void ADVANCE_TIM_Init(void)
{
ADVANCE_TIM_GPIO_Config();
ADVANCE_TIM_Mode_Config();
}