STM32G474之TIM1输出PWM互补信号,无死区时间,无BKIN输入。定时器1是16向上计数器,16向下计数器,16向上/向下计数器,输入时钟分频值:“1至65536”中的任意整数;捕获输入通道4个,比较输出通道4个,互补输出4个;
1、PWM输出互补信号引脚
PWM第1通道输出引脚TIM1_CH1映射到PA8,TIM1_CH1N映射到PA7
PWM第2通道输出引脚TIM1_CH2映射到PA9,TIM1_CH2N映射到PA12
PWM第3通道输出引脚TIM1_CH3映射到PA10,TIM1_CH3N映射到PB1
PWM第4通道输出引脚TIM1_CH4映射到PA11,TIM1_CH4N映射到PC5
2、定时器功能比较
1)、定时器1是16向上计数器,16向下计数器,16向上/向下计数器,输入时钟分频值:“1至65536”中的任意整数;
捕获输入通道4个,比较输出通道4个,互补输出4个;
2)、定时器2是32向上计数器,32向下计数器,32向上/向下计数器,输入时钟分频值:“1至65536”中的任意整数;
捕获输入通道4个,比较输出通道4个,不支持互补输出;
3)、定时器3是16向上计数器,16向下计数器,16向上/向下计数器,输入时钟分频值:“1至65536”中的任意整数;
捕获输入通道4个,比较输出通道4个,不支持互补输出;
4)、定时器4是16向上计数器,16向下计数器,16向上/向下计数器,输入时钟分频值:“1至65536”中的任意整数;
捕获输入通道4个,比较输出通道4个,不支持互补输出;
5)、定时器5是32向上计数器,32向下计数器,32向上/向下计数器,输入时钟分频值:“1至65536”中的任意整数;
捕获输入通道4个,比较输出通道4个,不支持互补输出;
6)、定时器6是16向上计数器,输入时钟分频值:“1至65536”中的任意整数;
捕获输入通道0个,比较输出通道0个,不支持互补输出;
7)、定时器7是16向上计数器,输入时钟分频值:“1至65536”中的任意整数;
捕获输入通道0个,比较输出通道0个,不支持互补输出;
8)、定时器8是16向上计数器,16向下计数器,16向上/向下计数器,输入时钟分频值:“1至65536”中的任意整数;
捕获输入通道4个,比较输出通道4个,互补输出4个;
9)、定时器15是16向上计数器,输入时钟分频值:“1至65536”中的任意整数;
捕获输入通道2个,比较输出通道2个,互补输出通道只有1个;
10)、定时器16是16向上计数器,输入时钟分频值:“1至65536”中的任意整数;
捕获输入通道1个,比较输出通道1个,互补输出通道1个;
11)、定时器17是16向上计数器,输入时钟分频值:“1至65536”中的任意整数;
捕获输入通道1个,比较输出通道1个,互补输出通道1个;
12)、定时器20是16向上计数器,16向下计数器,16向上/向下计数器,输入时钟分频值:“1至65536”中的任意整数;
捕获输入通道4个,比较输出通道4个,互补输出4个;
13)、HRTIM是16位向上计数器,输入时钟分频值:1分频,2分频,4分频,2倍频,4倍频,8倍频,16倍频,32倍频;
捕获输入通道12个,比较输出通道12个,可互补输出;
STM32G474无定时器9,定时器10,定时器11,定时器12,定时器13,定时器14,定时器18,定时器19
3、TIM1输出PWM互补信号测试程序
void Timer1_Init(void)
{
TIM_HandleTypeDef htim1;
GPIO_InitTypeDef GPIO_InitStruct = {0};
TIM_MasterConfigTypeDef sMasterConfig = {0};
TIM_OC_InitTypeDef sConfigOC = {0};
TIM_BreakDeadTimeConfigTypeDef sBreakDeadTimeConfig = {0};
__HAL_RCC_TIM1_CLK_ENABLE(); //使能“定时器1”的时钟,Enable TIM1 clock
__HAL_RCC_GPIOA_CLK_ENABLE(); //GPIOA时钟使能
__HAL_RCC_GPIOB_CLK_ENABLE(); //GPIOB时钟使能
__HAL_RCC_GPIOC_CLK_ENABLE(); //GPIOC时钟使能
GPIO_InitStruct.Pin = GPIO_PIN_8|GPIO_PIN_9|GPIO_PIN_10;
//选择引脚编号8,9,10
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; //复用功能推挽模式
GPIO_InitStruct.Pull = GPIO_PULLUP; //设置上拉
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;//引脚的输出速度为120MHz
GPIO_InitStruct.Alternate = GPIO_AF6_TIM1;
//将PA8引脚复用为TIM1_CH1
//将PA9引脚复用为TIM1_CH2
//将PA10引脚复用为TIM1_CH3
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
//根据GPIO_InitStruct结构变量指定的参数初始化GPIOA的外设寄存器
GPIO_InitStruct.Pin = GPIO_PIN_11;
//选择引脚编号11
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;//复用功能推挽模式
GPIO_InitStruct.Pull = GPIO_PULLUP; //设置上拉
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
//引脚的输出速度为120MHz
GPIO_InitStruct.Alternate = GPIO_AF11_TIM1;
//将PA11引脚复用为TIM1_CH4
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
//根据GPIO_InitStruct结构变量指定的参数初始化GPIOA的外设寄存器
GPIO_InitStruct.Pin = GPIO_PIN_7;
//选择引脚编号11
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;//复用功能推挽模式
GPIO_InitStruct.Pull = GPIO_PULLUP; //设置上拉
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
//引脚的输出速度为120MHz
GPIO_InitStruct.Alternate = GPIO_AF6_TIM1;
//将PA7引脚复用为TIM1_CH1N
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
//根据GPIO_InitStruct结构变量指定的参数初始化GPIOA的外设寄存器
GPIO_InitStruct.Pin = GPIO_PIN_12;
//选择引脚编号11
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;//复用功能推挽模式
GPIO_InitStruct.Pull = GPIO_PULLUP; //设置上拉
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
//引脚的输出速度为120MHz
GPIO_InitStruct.Alternate = GPIO_AF6_TIM1;
//将PA12引脚复用为TIM1_CH2N
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
//根据GPIO_InitStruct结构变量指定的参数初始化GPIOA的外设寄存器
GPIO_InitStruct.Pin = GPIO_PIN_1;
//选择引脚编号11
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;//复用功能推挽模式
GPIO_InitStruct.Pull = GPIO_PULLUP; //设置上拉
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
//引脚的输出速度为120MHz
GPIO_InitStruct.Alternate = GPIO_AF6_TIM1;
//将PB1引脚复用为TIM1_CH3N
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
//根据GPIO_InitStruct结构变量指定的参数初始化GPIOB的外设寄存器
GPIO_InitStruct.Pin = GPIO_PIN_5;
//选择引脚编号11
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;//复用功能推挽模式
GPIO_InitStruct.Pull = GPIO_PULLUP; //设置上拉
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
//引脚的输出速度为120MHz
GPIO_InitStruct.Alternate = GPIO_AF6_TIM1;
//将PC5引脚复用为TIM1_CH4N
HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
//根据GPIO_InitStruct结构变量指定的参数初始化GPIOC的外设寄存器
htim1.Instance = TIM1;
htim1.Init.Period = PERIOD_VALUE; //周期值999
htim1.Init.Prescaler = 0; //0表示设置TIM1预分频器为1
htim1.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
//设置时钟分频系数,TIM1_CR1中的CKD[9:8]=00b,tDTS=ttim_ker_ck;
//溢出时间为(999+1)*1*1/170000000/1=5.882us
//最大误差为1/170000000=5.882ns
htim1.Init.CounterMode = TIM_COUNTERMODE_UP;
htim1.Init.RepetitionCounter = 0;//重复计数(1-0)次,产生1次中断,比较重要
htim1.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
HAL_TIM_PWM_Init(&htim1);//将TIM1初始化为PWM
sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;//TIMx_EGR.UG位用作产生TIM1_TRGO触发信号
sMasterConfig.MasterOutputTrigger2 = TIM_TRGO2_RESET;//TIMx_EGR.UG位用作产生TIM1_TRGO2触发信号
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;//Master/slave mode is selected
HAL_TIMEx_MasterConfigSynchronization(&htim1, &sMasterConfig);
//Configures the TIM in master mode.
sConfigOC.OCMode = TIM_OCMODE_PWM1; //设置为PWM1模式
sConfigOC.Pulse = PULSE_WIDTH_VALUE1; //比较值1,用来设置输出脉宽
sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH; //“OC输出极性”为高电平有效
sConfigOC.OCNPolarity = TIM_OCNPOLARITY_HIGH; //“OCN输出极性”为高电平有效
sConfigOC.OCFastMode = TIM_OCFAST_DISABLE; //定时器输出快速状态:“输出比较快速”不使能
sConfigOC.OCIdleState = TIM_OCIDLESTATE_RESET; //“OC的空闲状态”为低电平
sConfigOC.OCNIdleState = TIM_OCNIDLESTATE_RESET; //“OCN的空闲状态”为低电平
HAL_TIM_PWM_ConfigChannel(&htim1, &sConfigOC, TIM_CHANNEL_1);
//初始化“比较输出1通道”
sConfigOC.Pulse = PULSE_WIDTH_VALUE2;//比较值2,用来设置输出脉宽
HAL_TIM_PWM_ConfigChannel(&htim1, &sConfigOC, TIM_CHANNEL_2);
//初始化“比较输出2通道”
sConfigOC.Pulse = PULSE_WIDTH_VALUE3;
HAL_TIM_PWM_ConfigChannel(&htim1, &sConfigOC, TIM_CHANNEL_3);
//初始化“比较输出3通道”
sConfigOC.Pulse = PULSE_WIDTH_VALUE4;
sConfigOC.OCNPolarity = TIM_OCNPOLARITY_HIGH;
HAL_TIM_PWM_ConfigChannel(&htim1, &sConfigOC, TIM_CHANNEL_4);
//初始化“比较输出4通道”
sBreakDeadTimeConfig.OffStateRunMode = TIM_OSSR_DISABLE; //运行模式下的OFF状态:当不活动时,“OC/OCN的输出”将被禁用(意思是不再由定时器1控制了)
sBreakDeadTimeConfig.OffStateIDLEMode = TIM_OSSI_DISABLE; //空闲模式下的OFF状态:当不活动时,“OC/OCN的输出”将被禁用(意思是不再由定时器1控制了)
sBreakDeadTimeConfig.LockLevel = TIM_LOCKLEVEL_OFF; //上电只能写一次,需要更新“死区时间”时只能用此值
sBreakDeadTimeConfig.DeadTime = 0; //死区时间
sBreakDeadTimeConfig.BreakState = TIM_BREAK_DISABLE; //关闭TIM1_BKIN引脚检测
sBreakDeadTimeConfig.BreakPolarity = TIM_BREAKPOLARITY_HIGH;//TIM1_BKIN引脚高电平有效
sBreakDeadTimeConfig.BreakFilter = 0; //指定TIM1_BKIN引脚的“输入过滤器”
sBreakDeadTimeConfig.BreakAFMode = TIM_BREAK_AFMODE_INPUT; //指定TIM1_BKIN引脚的“复用功能”
sBreakDeadTimeConfig.Break2State = TIM_BREAK2_DISABLE; //关闭TIM1_BKIN2引脚检测
sBreakDeadTimeConfig.Break2Polarity = TIM_BREAK2POLARITY_HIGH; //TIM1_BKIN2引脚高电平有效
sBreakDeadTimeConfig.Break2Filter = 0; //指定TIM1_BKIN2引脚的“输入过滤器”
sBreakDeadTimeConfig.Break2AFMode = TIM_BREAK_AFMODE_INPUT; //指定TIM1_BKIN2引脚的“复用功能”
sBreakDeadTimeConfig.AutomaticOutput = TIM_AUTOMATICOUTPUT_DISABLE;//允许刹车后自动恢复输出
HAL_TIMEx_ConfigBreakDeadTime(&htim1, &sBreakDeadTimeConfig);
//配置BKIN刹车引脚和死区时间
HAL_TIM_PWM_Start(&htim1, TIM_CHANNEL_1);
//启动PWM信号发生器:启动OC通道1
//Start PWM signals generation:Start channel 1
HAL_TIMEx_PWMN_Start(&htim1, TIM_CHANNEL_1);//开启互补通道1输出到TIM1_CH1N
HAL_TIM_PWM_Start(&htim1, TIM_CHANNEL_2);
//启动PWM信号发生器:启动通道2
//Start PWM signals generation:Start channel 2
HAL_TIMEx_PWMN_Start(&htim1, TIM_CHANNEL_2);//开启互补通道2输出到TIM1_CH2N
HAL_TIM_PWM_Start(&htim1, TIM_CHANNEL_3);
//启动PWM信号发生器:启动通道3
//Start PWM signals generation:Start channel 3
HAL_TIMEx_PWMN_Start(&htim1, TIM_CHANNEL_3);//开启互补通道3输出到TIM1_CH3N
HAL_TIM_PWM_Start(&htim1, TIM_CHANNEL_4);
//启动PWM信号发生器:启动通道4
//Start PWM signals generation:Start channel 4
HAL_TIMEx_PWMN_Start(&htim1, TIM_CHANNEL_4);//开启互补通道4输出到TIM1_CH4N
}
Timer1.h程序如下:
#ifndef __Timer1_H__
#define __Timer1_H__
#include "stm32g4xx_hal.h"
#include "main.h"
#define PERIOD_VALUE (uint32_t)(1000 - 1) //周期值
#define PULSE_WIDTH_VALUE1 (uint32_t)(1000*0.5) //比较值1,用来设置输出脉宽
#define PULSE_WIDTH_VALUE2 (uint32_t)(1000*0.375) //比较值2,用来设置输出脉宽
#define PULSE_WIDTH_VALUE3 (uint32_t)(1000*0.25) //比较值3,用来设置输出脉宽
#define PULSE_WIDTH_VALUE4 (uint32_t)(1000*0.125) //比较值4,用来设置输出脉宽
extern void Timer1_Init(void);
extern void Print_TIM1_input_frequency(void);
#endif
4、测试结果
1)、通道1互补输出波形
2)、通道2互补输出波形
3)、通道3互补输出波形
4)、通道4互补输出波形
5)、通道1和通道正脉宽比较
5、 总结:
定时器1是16向上计数器,16向下计数器,16向上/向下计数器,输入时钟分频值:“1至65536”中的任意整数;捕获输入通道4个,比较输出通道4个,互补输出4个;