本人研究方向是高频开关电源,之前实验室所用的控制器都是DSP28335,其主频仅有150M,相对于1M的开关频率,分辨率太低了。后来发现专门为高频电源设计的STM32G474系列单片机,虽然主频仅有170M,但其高精度定时器(HRTIM)的时钟频率可倍频至5.44GHz,即184ps的分辨率,非常适合于高频开关电源应用。本文将分享STM32G474RxT系列的高精度定时器的寄存器配置与使用。
为了方便计算,宏定义了HRTIM时钟频率,单位为kHz。
#define HRTimerCloclk 5440000
?为了方便管理,新建一个HRTIM.c的文件。其中包含HRTIM的初始化函数和HRTIMA的中断服务函数。
HRTIM初始化函数实现了各模块的初始化和ADC触发的功能。
/*
******************************************************************************
*Filename :HRTIM.c
*Programmer(s) :SueRocket
*Description :Project of STM32g474
******************************************************************************
*/
/*
******************************************************************************
* Header File Declaration
*Description : 头文件声明
*Notes : none
******************************************************************************
*/
#include "HRTIM.h"
/*
******************************************************************************
* CONSTANTS & MACROS
*Description : 变量定义
*Notes : none
******************************************************************************
*/
uint16_t DeadTime_A_Up_ns = 100;//死区时间,单位ns
uint16_t DeadTime_A_Down_ns = 100;
uint16_t DeadTime_B_Up_ns = 140;//死区时间,单位ns
uint16_t DeadTime_B_Down_ns = 140;
uint16_t DeadTime_C_Up_ns = 140;//死区时间,单位ns
uint16_t DeadTime_C_Down_ns = 140;
uint16_t DeadTime_D_Up_ns = 140;//死区时间,单位ns
uint16_t DeadTime_D_Down_ns = 140;
uint16_t DeadTime_E_Up_ns = 100;//死区时间,单位ns
uint16_t DeadTime_E_Down_ns = 100;
uint16_t DeadTime_F_Up_ns = 140;//死区时间,单位ns
uint16_t DeadTime_F_Down_ns = 140;
HRTIM_HandleTypeDef hhrtim1;
/*
******************************************************************************
*Function : void HRTIM1_PWM_Init(uint32_t HRFreq)
*Programmer : SueRocket
*Param : uint16_t HRFreqkHz 开关频率, 单位kHz
*Description : 初始化HRTIM模块
* TA1------> PA8
* TA2------> PA9
* TB1------> PA10
* TB2------> PA11
* TC1------> PB12
* TC2------> PB13
* TD1------> PB14
* TD2------> PB15
* TE1------> PC8
* TE2------> PC9
* TF1------> PC6
* TF2------> PC7
*Returns : none
******************************************************************************
*/
void HRTIM1_PWM_Init(float HRTimer_Master_A_FreqkHz,
float HRTimer_B_FreqkHz,
float HRTimer_C_FreqkHz,
float HRTimer_D_FreqkHz,
float HRTimer_E_FreqkHz,
float HRTimer_F_FreqkHz)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
HRTIM_TimeBaseCfgTypeDef pTimeBaseCfg = {0};
HRTIM_TimerCfgTypeDef pTimerCfg = {0};
HRTIM_TimerCtlTypeDef pTimerCtl = {0};
HRTIM_CompareCfgTypeDef pCompareCfg = {0};
HRTIM_DeadTimeCfgTypeDef pDeadTimeCfg = {0};
HRTIM_OutputCfgTypeDef pOutputCfg = {0};
HRTIM_ADCTriggerCfgTypeDef pADCTriggerCfg = {0};
uint16_t HRTimer_Master_A_Period = 0;
uint16_t HRTimer_B_Period = 0;
uint16_t HRTimer_C_Period = 0;
uint16_t HRTimer_D_Period = 0;
uint16_t HRTimer_E_Period = 0;
uint16_t HRTimer_F_Period = 0;
uint16_t HRTimeCompA = 2720;
uint16_t HRTimeCompB = 2720;
uint16_t HRTimeCompC = 2720;
uint16_t HRTimeCompD = 2720;
uint16_t HRTimeCompE = 2720;
uint16_t HRTimeCompF = 2720;
uint16_t Phase_compreB = 0;
uint16_t Phase_compreC = 0;
uint16_t Phase_compreD = 0;
uint16_t Phase_compreE = 0;
uint16_t DeadTimePeriodUp;
uint16_t DeadTimePeriodDown;
/*
******************************************************************************
*Description : 开启GPIO和复用时钟
*/
__HAL_RCC_GPIOF_CLK_ENABLE();
__HAL_RCC_GPIOA_CLK_ENABLE();
__HAL_RCC_GPIOB_CLK_ENABLE();
__HAL_RCC_GPIOC_CLK_ENABLE();
/*
******************************************************************************/
/*
******************************************************************************
*Description : 配置GPIO
* TA1------> PA8
* TA2------> PA9
* TB1------> PA10
* TB2------> PA11
* TC1------> PB12
* TC2------> PB13
* TD1------> PB14
* TD2------> PB15
* TE1------> PC8
* TE2------> PC9
* TF1------> PC6
* TF2------> PC7
*/
GPIO_InitStruct.Pin = GPIO_PIN_8|GPIO_PIN_9|GPIO_PIN_10|GPIO_PIN_11;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
GPIO_InitStruct.Alternate = GPIO_AF13_HRTIM1;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
GPIO_InitStruct.Pin = GPIO_PIN_12|GPIO_PIN_13|GPIO_PIN_14|GPIO_PIN_15;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
GPIO_InitStruct.Alternate = GPIO_AF13_HRTIM1;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
GPIO_InitStruct.Pin = GPIO_PIN_6|GPIO_PIN_7|GPIO_PIN_8|GPIO_PIN_9;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
GPIO_InitStruct.Alternate = GPIO_AF13_HRTIM1;
HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
GPIO_InitStruct.Pin = GPIO_PIN_8|GPIO_PIN_9;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
GPIO_InitStruct.Alternate = GPIO_AF3_HRTIM1;
HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
/*
******************************************************************************/
/*
******************************************************************************
*Description : 根据频率计算各定时器周期寄存器值
*/
HRTimer_Master_A_Period = (uint16_t)(HRTimerCloclk/HRTimer_Master_A_FreqkHz);
HRTimer_B_Period = (uint16_t)(HRTimerCloclk/HRTimer_B_FreqkHz);
HRTimer_C_Period = (uint16_t)(HRTimerCloclk/HRTimer_C_FreqkHz);
HRTimer_D_Period = (uint16_t)(HRTimerCloclk/HRTimer_D_FreqkHz);
HRTimer_E_Period = (uint16_t)(HRTimerCloclk/HRTimer_E_FreqkHz);
HRTimer_F_Period = (uint16_t)(HRTimerCloclk/HRTimer_F_FreqkHz);
/*
******************************************************************************/
/*
******************************************************************************
*Description : 计算比较器的值,用于控制PWM占空比,默认为50%
*/
HRTimeCompA = HRTimer_Master_A_Period >>1;
HRTimeCompB = HRTimer_B_Period >>1;
HRTimeCompC = HRTimer_C_Period >>1;
HRTimeCompD = HRTimer_D_Period >>1;
HRTimeCompE = HRTimer_E_Period >>1;
HRTimeCompF = HRTimer_F_Period >>1;
/*
******************************************************************************/
/*
******************************************************************************
*Description : HRTIM外设结构体初始化
*/
hhrtim1.Instance = HRTIM1;
/* 无中断模式 */
/* 中断事件:错误、系统错误、延迟、突发情况 */
hhrtim1.Init.HRTIMInterruptResquests = HRTIM_IT_NONE;
/* 无同步信号 */
/* 同步信号:外部输入、主定时器产生 */
hhrtim1.Init.SyncOptions = HRTIM_SYNCOPTION_NONE;
/* 初始化 hhrtim1 结构体 */
HAL_HRTIM_Init(&hhrtim1);
/* 初始化 HRTIM 定期校准 */
/* 校准时间周期为 0.012ms */
HAL_HRTIM_DLLCalibrationStart(&hhrtim1, HRTIM_CALIBRATIONRATE_3);
/* 查看高分辨率是否可用 */
HAL_HRTIM_PollForDLLCalibration(&hhrtim1, 10);
/*
******************************************************************************/
/*
******************************************************************************
*Description : 设置TIME A的比较器3匹配事件用于触发ADC
*/
pADCTriggerCfg.UpdateSource = HRTIM_ADCTRIGGERUPDATE_TIMER_A;//TIMEA用于用于触发ADC
pADCTriggerCfg.Trigger = HRTIM_ADCTRIGGEREVENT13_TIMERA_CMP3;
HAL_HRTIM_ADCTriggerConfig(&hhrtim1, HRTIM_ADCTRIGGER_1, &pADCTriggerCfg);
/*当计数器等于比较器3 1次后,第1次才触发ADC*/
HAL_HRTIM_ADCPostScalerConfig(&hhrtim1, HRTIM_ADCTRIGGER_1, 0x0);
/*
******************************************************************************/
/*
******************************************************************************
*Description : 配置TIME Master,A,B,C,D,E,F的时基周期值
*/
/* 产生多少个波形 */
pTimeBaseCfg.RepetitionCounter = 0x00;
/* 倍频系数 */
/* HRTIMCLK = 170MHz * 32 = 5.44GHz */
pTimeBaseCfg.PrescalerRatio = HRTIM_PRESCALERRATIO_MUL32;
/* 连续模式 */
pTimeBaseCfg.Mode = HRTIM_MODE_CONTINUOUS;
/* HRTIM MASTER周期值 */
pTimeBaseCfg.Period = HRTimer_Master_A_Period;
HAL_HRTIM_TimeBaseConfig(&hhrtim1, HRTIM_TIMERINDEX_MASTER, &pTimeBaseCfg);
/*-----------HRTIM A周期值-----------*/
pTimeBaseCfg.Period = HRTimer_Master_A_Period;
HAL_HRTIM_TimeBaseConfig(&hhrtim1, HRTIM_TIMERINDEX_TIMER_A, &pTimeBaseCfg);
/*-----------HRTIM B周期值-----------*/
pTimeBaseCfg.Period = HRTimer_B_Period;
HAL_HRTIM_TimeBaseConfig(&hhrtim1, HRTIM_TIMERINDEX_TIMER_B, &pTimeBaseCfg);
/*-----------HRTIM C周期值-----------*/
pTimeBaseCfg.Period = HRTimer_C_Period;
HAL_HRTIM_TimeBaseConfig(&hhrtim1, HRTIM_TIMERINDEX_TIMER_C, &pTimeBaseCfg);
/*-----------HRTIM D周期值-----------*/
pTimeBaseCfg.Period = HRTimer_D_Period;
HAL_HRTIM_TimeBaseConfig(&hhrtim1, HRTIM_TIMERINDEX_TIMER_D, &pTimeBaseCfg);
/*-----------HRTIM E周期值-----------*/
pTimeBaseCfg.Period = HRTimer_E_Period;
HAL_HRTIM_TimeBaseConfig(&hhrtim1, HRTIM_TIMERINDEX_TIMER_E, &pTimeBaseCfg);
/*-----------HRTIM F周期值-----------*/
pTimeBaseCfg.Period = HRTimer_F_Period;
HAL_HRTIM_TimeBaseConfig(&hhrtim1, HRTIM_TIMERINDEX_TIMER_F, &pTimeBaseCfg);
/*
******************************************************************************/
/*
******************************************************************************
*Description : 配置TIME Master,A,B,C,D,E,F的配置结构体
*/
/* 主定时器中断请求选择 */
/* 主定时器中断:比较器1/2/3/4中断、重装载中断、同步输入中断、主定时器更新中断 */
pTimerCfg.InterruptRequests = HRTIM_MASTER_IT_NONE;
/* DMA请求配置 */
pTimerCfg.DMARequests = HRTIM_MASTER_DMA_NONE;
pTimerCfg.DMASrcAddress = 0x0000;
pTimerCfg.DMADstAddress = 0x0000;
pTimerCfg.DMASize = 0x1;
/* 半波模式使能 */
/* 半波模式 = 占空比等于50% */
pTimerCfg.HalfModeEnable = HRTIM_HALFMODE_DISABLED;
/* 交错模式 */
pTimerCfg.InterleavedMode = HRTIM_INTERLEAVED_MODE_DISABLED;
/* 启动同步 */
pTimerCfg.StartOnSync = HRTIM_SYNCSTART_DISABLED;
/* 同步信号输入的上升沿是否定时器被复位 */
pTimerCfg.ResetOnSync = HRTIM_SYNCRESET_DISABLED;
/* DAC同步信号 */
pTimerCfg.DACSynchro = HRTIM_DACSYNC_NONE;
/* 周期寄存器预装载使能 */
pTimerCfg.PreloadEnable = HRTIM_PRELOAD_ENABLED;
pTimerCfg.UpdateGating = HRTIM_UPDATEGATING_INDEPENDENT;
pTimerCfg.BurstMode = HRTIM_TIMERBURSTMODE_MAINTAINCLOCK;
/* 重复更新使能 */
pTimerCfg.RepetitionUpdate = HRTIM_UPDATEONREPETITION_ENABLED;
pTimerCfg.ReSyncUpdate = HRTIM_TIMERESYNC_UPDATE_UNCONDITIONAL;
/* 主定时器计数器配置初始化 */
HAL_HRTIM_WaveformTimerConfig(&hhrtim1, HRTIM_TIMERINDEX_MASTER, &pTimerCfg);
/*-----------TIMER_A波形配置结构体初始化--------------*/
/* 无中断 */
pTimerCfg.InterruptRequests = HRTIM_TIM_IT_NONE;
/* DMA请求 */
pTimerCfg.DMARequests = HRTIM_TIM_DMA_NONE;
pTimerCfg.DMASrcAddress = 0x0000;
pTimerCfg.DMADstAddress = 0x0000;
pTimerCfg.DMASize = 0x1;
pTimerCfg.PushPull = HRTIM_TIMPUSHPULLMODE_DISABLED;
pTimerCfg.FaultEnable = HRTIM_TIMFAULTENABLE_NONE;
pTimerCfg.FaultLock = HRTIM_TIMFAULTLOCK_READWRITE;
/* 使能死区功能 */
pTimerCfg.DeadTimeInsertion = HRTIM_TIMDEADTIMEINSERTION_ENABLED;
pTimerCfg.DelayedProtectionMode = HRTIM_TIMER_A_B_C_DELAYEDPROTECTION_DISABLED;
pTimerCfg.UpdateTrigger = HRTIM_TIMUPDATETRIGGER_NONE;
pTimerCfg.ResetTrigger = HRTIM_TIMRESETTRIGGER_MASTER_PER;
pTimerCfg.ResetUpdate = HRTIM_TIMUPDATEONRESET_ENABLED;
/* 初始化TIMEA波形输出配置 */
HAL_HRTIM_WaveformTimerConfig(&hhrtim1, HRTIM_TIMERINDEX_TIMER_A, &pTimerCfg);
/*-----------TIMER_B波形配置结构体初始化--------------*/
pTimerCfg.DMASrcAddress = 0x0000;
pTimerCfg.DMADstAddress = 0x0000;
pTimerCfg.DMASize = 0x1;
/*主定时器的比较器1值用于复位定时器B*/
pTimerCfg.ResetTrigger = HRTIM_TIMRESETTRIGGER_MASTER_CMP1;
/* 初始化TIMEB波形输出配置 */
HAL_HRTIM_WaveformTimerConfig(&hhrtim1, HRTIM_TIMERINDEX_TIMER_B, &pTimerCfg);
/*-----------TIMER_C波形配置结构体初始化--------------*/
pTimerCfg.DMASrcAddress = 0x0000;
pTimerCfg.DMADstAddress = 0x0000;
pTimerCfg.DMASize = 0x1;
/*主定时器的比较器2值用于复位定时器C*/
pTimerCfg.ResetTrigger = HRTIM_TIMRESETTRIGGER_MASTER_CMP2;
/* 初始化TIMEC波形输出配置 */
HAL_HRTIM_WaveformTimerConfig(&hhrtim1, HRTIM_TIMERINDEX_TIMER_C, &pTimerCfg);
/*-----------TIMER_D波形配置结构体初始化--------------*/
pTimerCfg.DMASrcAddress = 0x0000;
pTimerCfg.DMADstAddress = 0x0000;
pTimerCfg.DMASize = 0x1;
pTimerCfg.DelayedProtectionMode = HRTIM_TIMER_D_E_DELAYEDPROTECTION_DISABLED;
/*主定时器的比较器3值用于复位定时器D*/
pTimerCfg.ResetTrigger = HRTIM_TIMRESETTRIGGER_MASTER_CMP3;
/* 初始化TIMED波形输出配置 */
HAL_HRTIM_WaveformTimerConfig(&hhrtim1, HRTIM_TIMERINDEX_TIMER_D, &pTimerCfg);
/*-----------TIMER_E波形配置结构体初始化--------------*/
pTimerCfg.DMASrcAddress = 0x0000;
pTimerCfg.DMADstAddress = 0x0000;
pTimerCfg.DMASize = 0x1;
pTimerCfg.DelayedProtectionMode = HRTIM_TIMER_D_E_DELAYEDPROTECTION_DISABLED;
/*主定时器的比较器3值用于复位定时器E*/
pTimerCfg.ResetTrigger = HRTIM_TIMRESETTRIGGER_MASTER_CMP4;
/* 初始化TIMEE波形输出配置 */
HAL_HRTIM_WaveformTimerConfig(&hhrtim1, HRTIM_TIMERINDEX_TIMER_E, &pTimerCfg);
/*-----------TIMER_F波形配置结构体初始化--------------*/
pTimerCfg.DMASrcAddress = 0x0000;
pTimerCfg.DMADstAddress = 0x0000;
pTimerCfg.DMASize = 0x1;
pTimerCfg.DelayedProtectionMode = HRTIM_TIMER_F_DELAYEDPROTECTION_DISABLED;
/*无复位*/
pTimerCfg.ResetTrigger = HRTIM_TIMRESETTRIGGER_NONE;
/* 初始化TIMEF波形输出配置 */
HAL_HRTIM_WaveformTimerConfig(&hhrtim1, HRTIM_TIMERINDEX_TIMER_F, &pTimerCfg);
/*
******************************************************************************/
/*
******************************************************************************
*Description : 配置TIME Master比较器初始化,用于复位TIME A,B,C,D,E产生移相
*/
//比较器1用于复位HRTIMB
pCompareCfg.CompareValue = Phase_compreB;
HAL_HRTIM_WaveformCompareConfig(&hhrtim1, HRTIM_TIMERINDEX_MASTER, HRTIM_COMPAREUNIT_1, &pCompareCfg);
//比较器2用于复位HRTIMC
pCompareCfg.CompareValue = Phase_compreC;
HAL_HRTIM_WaveformCompareConfig(&hhrtim1, HRTIM_TIMERINDEX_MASTER, HRTIM_COMPAREUNIT_2, &pCompareCfg);
//比较器3用于复位HRTIMD
pCompareCfg.CompareValue = Phase_compreD;
HAL_HRTIM_WaveformCompareConfig(&hhrtim1, HRTIM_TIMERINDEX_MASTER, HRTIM_COMPAREUNIT_3, &pCompareCfg);
//比较器4用于复位HRTIME
pCompareCfg.CompareValue = Phase_compreE;
HAL_HRTIM_WaveformCompareConfig(&hhrtim1, HRTIM_TIMERINDEX_MASTER, HRTIM_COMPAREUNIT_4, &pCompareCfg);
/*
******************************************************************************/
/*
******************************************************************************
*Description : 配置TIME A,B,C,D,E,F计数方式
*/
/* 计数模式为向上计数模式 */
pTimerCtl.UpDownMode = HRTIM_TIMERUPDOWNMODE_UP;
/* 启用双通道DAC触发机制 */
pTimerCtl.DualChannelDacEnable = HRTIM_TIMER_DCDE_DISABLED;
/* 比较器值与计数器值相等动作 */
pTimerCtl.GreaterCMP1 = HRTIM_TIMERGTCMP1_EQUAL;
/* 初始化TIMEA波形输出控制 */
HAL_HRTIM_WaveformTimerControl(&hhrtim1, HRTIM_TIMERINDEX_TIMER_A, &pTimerCtl);
HAL_HRTIM_WaveformTimerControl(&hhrtim1, HRTIM_TIMERINDEX_TIMER_B, &pTimerCtl);
HAL_HRTIM_WaveformTimerControl(&hhrtim1, HRTIM_TIMERINDEX_TIMER_C, &pTimerCtl);
HAL_HRTIM_WaveformTimerControl(&hhrtim1, HRTIM_TIMERINDEX_TIMER_D, &pTimerCtl);
HAL_HRTIM_WaveformTimerControl(&hhrtim1, HRTIM_TIMERINDEX_TIMER_E, &pTimerCtl);
HAL_HRTIM_WaveformTimerControl(&hhrtim1, HRTIM_TIMERINDEX_TIMER_F, &pTimerCtl);
/*
******************************************************************************/
/*
******************************************************************************
*Description : 配置TIME A,B,C,D,E,F比较器1用于控制占空比
*/
pCompareCfg.CompareValue = HRTimeCompA;
/* TIMEA比较器初始化 */
HAL_HRTIM_WaveformCompareConfig(&hhrtim1, HRTIM_TIMERINDEX_TIMER_A, HRTIM_COMPAREUNIT_1, &pCompareCfg);
pCompareCfg.CompareValue = HRTimeCompB;
HAL_HRTIM_WaveformCompareConfig(&hhrtim1, HRTIM_TIMERINDEX_TIMER_B, HRTIM_COMPAREUNIT_1, &pCompareCfg);
pCompareCfg.CompareValue = HRTimeCompC;
HAL_HRTIM_WaveformCompareConfig(&hhrtim1, HRTIM_TIMERINDEX_TIMER_C, HRTIM_COMPAREUNIT_1, &pCompareCfg);
pCompareCfg.CompareValue = HRTimeCompD;
HAL_HRTIM_WaveformCompareConfig(&hhrtim1, HRTIM_TIMERINDEX_TIMER_D, HRTIM_COMPAREUNIT_1, &pCompareCfg);
pCompareCfg.CompareValue = HRTimeCompE;
HAL_HRTIM_WaveformCompareConfig(&hhrtim1, HRTIM_TIMERINDEX_TIMER_E, HRTIM_COMPAREUNIT_1, &pCompareCfg);
pCompareCfg.CompareValue = HRTimeCompF;
HAL_HRTIM_WaveformCompareConfig(&hhrtim1, HRTIM_TIMERINDEX_TIMER_F, HRTIM_COMPAREUNIT_1, &pCompareCfg);
/*
******************************************************************************/
/*
******************************************************************************
*Description : 配置TIME A,B,C,D,E,F死区结构体
*/
pDeadTimeCfg.Prescaler = HRTIM_TIMDEADTIME_PRESCALERRATIO_MUL8;
pDeadTimeCfg.RisingSign = HRTIM_TIMDEADTIME_RISINGSIGN_POSITIVE;
pDeadTimeCfg.RisingLock = HRTIM_TIMDEADTIME_RISINGLOCK_WRITE;
pDeadTimeCfg.RisingSignLock = HRTIM_TIMDEADTIME_RISINGSIGNLOCK_WRITE;
/* 在下降沿时加入死区 */
pDeadTimeCfg.FallingSign = HRTIM_TIMDEADTIME_FALLINGSIGN_POSITIVE;
pDeadTimeCfg.FallingLock = HRTIM_TIMDEADTIME_FALLINGLOCK_WRITE;
pDeadTimeCfg.FallingSignLock = HRTIM_TIMDEADTIME_FALLINGSIGNLOCK_WRITE;
/*初始化TIMEA的死区配置 */
DeadTimePeriodUp = (uint16_t)(DeadTime_A_Up_ns/0.735);
DeadTimePeriodDown = (uint16_t)(DeadTime_A_Down_ns/0.735);
pDeadTimeCfg.RisingValue = DeadTimePeriodUp;
pDeadTimeCfg.FallingValue = DeadTimePeriodDown;
HAL_HRTIM_DeadTimeConfig(&hhrtim1, HRTIM_TIMERINDEX_TIMER_A, &pDeadTimeCfg);
/*-----------TIMER_B死区结构体初始化--------------*/
DeadTimePeriodUp = (uint16_t)(DeadTime_B_Up_ns/0.735);
DeadTimePeriodDown = (uint16_t)(DeadTime_B_Down_ns/0.735);
pDeadTimeCfg.RisingValue = DeadTimePeriodUp;
pDeadTimeCfg.FallingValue = DeadTimePeriodDown;
/*初始化TIMEB的死区配置 */
HAL_HRTIM_DeadTimeConfig(&hhrtim1, HRTIM_TIMERINDEX_TIMER_B, &pDeadTimeCfg);
/*-----------TIMER_C死区结构体初始化--------------*/
DeadTimePeriodUp = (uint16_t)(DeadTime_C_Up_ns/0.735);
DeadTimePeriodDown = (uint16_t)(DeadTime_C_Down_ns/0.735);
pDeadTimeCfg.RisingValue = DeadTimePeriodUp;
pDeadTimeCfg.FallingValue = DeadTimePeriodDown;
/*初始化TIMEC的死区配置 */
HAL_HRTIM_DeadTimeConfig(&hhrtim1, HRTIM_TIMERINDEX_TIMER_C, &pDeadTimeCfg);
/*-----------TIMER_D死区结构体初始化--------------*/
DeadTimePeriodUp = (uint16_t)(DeadTime_D_Up_ns/0.735);
DeadTimePeriodDown = (uint16_t)(DeadTime_D_Down_ns/0.735);
pDeadTimeCfg.RisingValue = DeadTimePeriodUp;
pDeadTimeCfg.FallingValue = DeadTimePeriodDown;
/*初始化TIMED的死区配置 */
HAL_HRTIM_DeadTimeConfig(&hhrtim1, HRTIM_TIMERINDEX_TIMER_D, &pDeadTimeCfg);
/*-----------TIMER_E死区结构体初始化--------------*/
DeadTimePeriodUp = (uint16_t)(DeadTime_E_Up_ns/0.735);
DeadTimePeriodDown = (uint16_t)(DeadTime_E_Down_ns/0.735);
pDeadTimeCfg.RisingValue = DeadTimePeriodUp;
pDeadTimeCfg.FallingValue = DeadTimePeriodDown;
/*初始化TIMEE的死区配置 */
HAL_HRTIM_DeadTimeConfig(&hhrtim1, HRTIM_TIMERINDEX_TIMER_E, &pDeadTimeCfg);
/*-----------TIMER_F死区结构体初始化--------------*/
DeadTimePeriodUp = (uint16_t)(DeadTime_F_Up_ns/0.735);
DeadTimePeriodDown = (uint16_t)(DeadTime_F_Down_ns/0.735);
pDeadTimeCfg.RisingValue = DeadTimePeriodUp;
pDeadTimeCfg.FallingValue = DeadTimePeriodDown;
/*初始化TIMEF的死区配置 */
HAL_HRTIM_DeadTimeConfig(&hhrtim1, HRTIM_TIMERINDEX_TIMER_F, &pDeadTimeCfg);
/*
******************************************************************************/
/*
******************************************************************************
*Description : 配置TIME A,B,C,D,E,F输出波形极性
*/
/*-----------TIMER_A输出配置结构体初始化--------------*/
/* 输出极性高有效 */
pOutputCfg.Polarity = HRTIM_OUTPUTPOLARITY_HIGH;
pOutputCfg.SetSource = HRTIM_OUTPUTRESET_TIMCMP1;
pOutputCfg.ResetSource = HRTIM_OUTPUTSET_TIMPER;
pOutputCfg.IdleMode = HRTIM_OUTPUTIDLEMODE_NONE;
pOutputCfg.IdleLevel = HRTIM_OUTPUTIDLELEVEL_INACTIVE;
pOutputCfg.FaultLevel = HRTIM_OUTPUTFAULTLEVEL_NONE;
pOutputCfg.ChopperModeEnable = HRTIM_OUTPUTCHOPPERMODE_DISABLED;
pOutputCfg.BurstModeEntryDelayed = HRTIM_OUTPUTBURSTMODEENTRY_REGULAR;
HAL_HRTIM_WaveformOutputConfig(&hhrtim1, HRTIM_TIMERINDEX_TIMER_A, HRTIM_OUTPUT_TA1, &pOutputCfg);
/*-----------TIMER_B输出配置结构体初始化--------------*/
pOutputCfg.SetSource = HRTIM_OUTPUTRESET_TIMCMP1;
pOutputCfg.ResetSource = HRTIM_OUTPUTSET_TIMPER;
HAL_HRTIM_WaveformOutputConfig(&hhrtim1, HRTIM_TIMERINDEX_TIMER_B, HRTIM_OUTPUT_TB1, &pOutputCfg);
/*-----------TIMER_C输出配置结构体初始化--------------*/
pOutputCfg.SetSource = HRTIM_OUTPUTSET_TIMPER;
pOutputCfg.ResetSource = HRTIM_OUTPUTRESET_TIMCMP1;
HAL_HRTIM_WaveformOutputConfig(&hhrtim1, HRTIM_TIMERINDEX_TIMER_C, HRTIM_OUTPUT_TC1, &pOutputCfg);
/*-----------TIMER_D输出配置结构体初始化--------------*/
pOutputCfg.SetSource = HRTIM_OUTPUTRESET_TIMCMP1;
pOutputCfg.ResetSource = HRTIM_OUTPUTSET_TIMPER;
HAL_HRTIM_WaveformOutputConfig(&hhrtim1, HRTIM_TIMERINDEX_TIMER_D, HRTIM_OUTPUT_TD1, &pOutputCfg);
/*-----------TIMER_E输出配置结构体初始化--------------*/
pOutputCfg.SetSource = HRTIM_OUTPUTSET_TIMPER;
pOutputCfg.ResetSource = HRTIM_OUTPUTRESET_TIMCMP1;
HAL_HRTIM_WaveformOutputConfig(&hhrtim1, HRTIM_TIMERINDEX_TIMER_E, HRTIM_OUTPUT_TE1, &pOutputCfg);
/*-----------TIMER_F输出配置结构体初始化--------------*/
pOutputCfg.SetSource = HRTIM_OUTPUTSET_TIMPER;
pOutputCfg.ResetSource = HRTIM_OUTPUTRESET_TIMCMP1;
HAL_HRTIM_WaveformOutputConfig(&hhrtim1, HRTIM_TIMERINDEX_TIMER_F, HRTIM_OUTPUT_TF1, &pOutputCfg);
/*-----------互补输出通道2初始化--------------*/
HAL_HRTIM_WaveformOutputConfig(&hhrtim1, HRTIM_TIMERINDEX_TIMER_A, HRTIM_OUTPUT_TA2, &pOutputCfg);
HAL_HRTIM_WaveformOutputConfig(&hhrtim1, HRTIM_TIMERINDEX_TIMER_B, HRTIM_OUTPUT_TB2, &pOutputCfg);
HAL_HRTIM_WaveformOutputConfig(&hhrtim1, HRTIM_TIMERINDEX_TIMER_C, HRTIM_OUTPUT_TC2, &pOutputCfg);
HAL_HRTIM_WaveformOutputConfig(&hhrtim1, HRTIM_TIMERINDEX_TIMER_D, HRTIM_OUTPUT_TD2, &pOutputCfg);
HAL_HRTIM_WaveformOutputConfig(&hhrtim1, HRTIM_TIMERINDEX_TIMER_E, HRTIM_OUTPUT_TE2, &pOutputCfg);
HAL_HRTIM_WaveformOutputConfig(&hhrtim1, HRTIM_TIMERINDEX_TIMER_F, HRTIM_OUTPUT_TF2, &pOutputCfg);
/*
******************************************************************************/
/*
******************************************************************************
*Description : 开启TIME A重复中断,并设置优先级
*/
__HAL_HRTIM_TIMER_ENABLE_IT(&hhrtim1,HRTIM_TIMERINDEX_TIMER_A,HRTIM_TIM_IT_REP);
HAL_NVIC_SetPriority(HRTIM1_TIMA_IRQn,2,0);
HAL_NVIC_EnableIRQ(HRTIM1_TIMA_IRQn);
/*
******************************************************************************/
}
/*
******************************************************************************
*Function : void HRTIM1_TIMA_IRQHandler(void)
*Programmer : SueRocket
*Param : none
*Description : TIME A的中断服务函数
*Returns : none
******************************************************************************
*/
void HRTIM1_TIMA_IRQHandler(void)
{
if((HRTIM1->sTimerxRegs[HRTIM_TIMERINDEX_TIMER_A].TIMxDIER & HRTIM_TIM_IT_REP) == HRTIM_TIM_IT_REP)
{
HRTIM1->sTimerxRegs[HRTIM_TIMERINDEX_TIMER_A].TIMxICR = HRTIM_TIM_IT_REP;
Control();
}
}
?可操作相应的寄存器实现改变频率变化与占空比,以及触发ADC的时刻。
/*-----------更新周期寄存器-----------*/
HRTIM1->sMasterRegs.MPER = HRTimer_Period;
HRTIM1->sTimerxRegs[HRTIM_TIMERINDEX_TIMER_A].PERxR = HRTimer_Period;
/*-----------更改比较寄存器值,改变占空比-----------*/
HRTIM1->sTimerxRegs[HRTIM_TIMERINDEX_TIMER_A].CMP1xR = HRTimeCompA;
/*-----------用于触发ADC采样-----------*/
HRTIM1->sTimerxRegs[HRTIM_TIMERINDEX_TIMER_A].CMP3xR = HRTimeCompA>>1;
最后,希望大家多多点赞收藏!
转载须经过本人同意哈!
|