本文基于野火例程进行解说
实验内容
本次实验输出一对互补的pwm波,且进行死区时间的计算说明。
代码
互补输出对应的定时器初始化代码:
bsp_advance_tim.c
#include "./tim/bsp_advance_tim.h"TIM_HandleTypeDef TIM_TimeBaseStructure;
TIM_OC_InitTypeDef TIM_OCInitStructure;__IO uint16_t ChannelPulse = 500;
static void TIMx_GPIO_Config(void)
{GPIO_InitTypeDef GPIO_InitStructure;ADVANCE_OCPWM_GPIO_CLK_ENABLE();ADVANCE_OCNPWM_GPIO_CLK_ENABLE();ADVANCE_BKIN_GPIO_CLK_ENABLE(); GPIO_InitStructure.Pin = ADVANCE_OCPWM_PIN; GPIO_InitStructure.Mode = GPIO_MODE_AF_PP; GPIO_InitStructure.Pull = GPIO_NOPULL;GPIO_InitStructure.Speed = GPIO_SPEED_HIGH; GPIO_InitStructure.Alternate = ADVANCE_OCPWM_AF;HAL_GPIO_Init(ADVANCE_OCPWM_GPIO_PORT, &GPIO_InitStructure); GPIO_InitStructure.Pin = ADVANCE_OCNPWM_PIN; GPIO_InitStructure.Alternate = ADVANCE_OCNPWM_AF; HAL_GPIO_Init(ADVANCE_OCNPWM_GPIO_PORT, &GPIO_InitStructure);GPIO_InitStructure.Pin = ADVANCE_BKIN_PIN; GPIO_InitStructure.Alternate = ADVANCE_BKIN_AF; HAL_GPIO_Init(ADVANCE_BKIN_GPIO_PORT, &GPIO_InitStructure);
}
static void TIM_Mode_Config(void)
{TIM_BreakDeadTimeConfigTypeDef TIM_BDTRInitStructure;ADVANCE_TIM_CLK_ENABLE(); TIM_TimeBaseStructure.Instance = ADVANCE_TIM; TIM_TimeBaseStructure.Init.Period = 1000-1;TIM_TimeBaseStructure.Init.Prescaler = 168-1; TIM_TimeBaseStructure.Init.ClockDivision=TIM_CLOCKDIVISION_DIV1;TIM_TimeBaseStructure.Init.CounterMode=TIM_COUNTERMODE_UP;TIM_TimeBaseStructure.Init.RepetitionCounter=0; HAL_TIM_PWM_Init(&TIM_TimeBaseStructure);TIM_OCInitStructure.OCMode = TIM_OCMODE_PWM1;TIM_OCInitStructure.Pulse = ChannelPulse;TIM_OCInitStructure.OCPolarity = TIM_OCPOLARITY_HIGH;TIM_OCInitStructure.OCNPolarity = TIM_OCNPOLARITY_HIGH;TIM_OCInitStructure.OCIdleState = TIM_OCIDLESTATE_SET;TIM_OCInitStructure.OCNIdleState = TIM_OCNIDLESTATE_RESET;HAL_TIM_PWM_ConfigChannel(&TIM_TimeBaseStructure,&TIM_OCInitStructure,TIM_CHANNEL_1);TIM_BDTRInitStructure.OffStateRunMode = TIM_OSSR_ENABLE;TIM_BDTRInitStructure.OffStateIDLEMode = TIM_OSSI_ENABLE;TIM_BDTRInitStructure.LockLevel = TIM_LOCKLEVEL_1;TIM_BDTRInitStructure.DeadTime = 0xff;TIM_BDTRInitStructure.BreakState = TIM_BREAK_ENABLE;TIM_BDTRInitStructure.BreakPolarity = TIM_BREAKPOLARITY_LOW;TIM_BDTRInitStructure.AutomaticOutput = TIM_AUTOMATICOUTPUT_ENABLE;HAL_TIMEx_ConfigBreakDeadTime(&TIM_TimeBaseStructure, &TIM_BDTRInitStructure);HAL_TIM_PWM_Start(&TIM_TimeBaseStructure,TIM_CHANNEL_1);HAL_TIMEx_PWMN_Start(&TIM_TimeBaseStructure,TIM_CHANNEL_1);
}
void TIMx_Configuration(void)
{TIMx_GPIO_Config(); TIM_Mode_Config();
}
头文件:
bsp_advance_tim.h
#ifndef __ADVANCE_TIM_H
#define __ADVANCE_TIM_H#include "stm32f4xx.h"
#define ADVANCE_TIM TIM8
#define ADVANCE_TIM_CLK_ENABLE() __TIM8_CLK_ENABLE()
#define ADVANCE_OCPWM_PIN GPIO_PIN_6
#define ADVANCE_OCPWM_GPIO_PORT GPIOC
#define ADVANCE_OCPWM_GPIO_CLK_ENABLE() __GPIOC_CLK_ENABLE()
#define ADVANCE_OCPWM_AF GPIO_AF3_TIM8
#define ADVANCE_OCNPWM_PIN GPIO_PIN_5
#define ADVANCE_OCNPWM_GPIO_PORT GPIOA
#define ADVANCE_OCNPWM_GPIO_CLK_ENABLE() __GPIOA_CLK_ENABLE()
#define ADVANCE_OCNPWM_AF GPIO_AF3_TIM8
#define ADVANCE_BKIN_PIN GPIO_PIN_6
#define ADVANCE_BKIN_GPIO_PORT GPIOA
#define ADVANCE_BKIN_GPIO_CLK_ENABLE() __GPIOA_CLK_ENABLE()
#define ADVANCE_BKIN_AF GPIO_AF3_TIM8extern TIM_HandleTypeDef TIM_TimeBaseStructure;void TIMx_Configuration(void);#endif
代码讲解
断路功能
TIM_OCInitStructure.OCIdleState = TIM_OCIDLESTATE_SET;
TIM_OCInitStructure.OCNIdleState = TIM_OCNIDLESTATE_RESET;
TIM_BDTRInitStructure.BreakState = TIM_BREAK_ENABLE;
TIM_BDTRInitStructure.BreakPolarity = TIM_BREAKPOLARITY_LOW;
TIM_BDTRInitStructure.AutomaticOutput=TIM_AUTOMATICOUTPUT_ENABLE;
以上语句表示当断路功能对应的引脚输入变为低电平时,互补输出的功能就被禁止,此时变为空闲状态,而pwm对应的通道输出高电平,互补对应的通道变为低电平,而当断路功能对应的引脚变为高电平时,互补输出又再次出现。
死区时间计算
通过上面两张图,就可以知道死区时间的计算公式了
下面列举出配置死区时间的代码
TIM_TimeBaseStructure.Init.ClockDivision=TIM_CLOCKDIVISION_DIV1;TIM_BDTRInitStructure.DeadTime = 0xff;
开始计算,假设定时器的频率为165Mhz,且配置语句TIM_BDTRInitStructure.DeadTime = 0xff,则DTG[7:0]被写入11111111,则对应第四个计算方式,TIM_TimeBaseStructure.Init.ClockDivision=TIM_CLOCKDIVISION_DIV1表示对定时器频率1分频,则此时tDTS=1*Tck_int = (1/165000000)s,此时代码计算公式
DT=(32+DTG[4:0])*Tdtg=(32+31)16(1/165000000)*1000000us=6us