IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 嵌入式 -> 适用于高频开关电源的单片机STM32G474系列高精度定时器相关配置 -> 正文阅读

[嵌入式]适用于高频开关电源的单片机STM32G474系列高精度定时器相关配置

本人研究方向是高频开关电源,之前实验室所用的控制器都是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;

最后,希望大家多多点赞收藏!

转载须经过本人同意哈!

  嵌入式 最新文章
基于高精度单片机开发红外测温仪方案
89C51单片机与DAC0832
基于51单片机宠物自动投料喂食器控制系统仿
《痞子衡嵌入式半月刊》 第 68 期
多思计组实验实验七 简单模型机实验
CSC7720
启明智显分享| ESP32学习笔记参考--PWM(脉冲
STM32初探
STM32 总结
【STM32】CubeMX例程四---定时器中断(附工
上一篇文章      下一篇文章      查看所有文章
加:2022-04-22 18:52:42  更:2022-04-22 18:56:45 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年11日历 -2024/11/26 4:38:14-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码