STM32高分辨率定时器(HRTIM)使用实例之PWM
扫描二维码
随时随地手机看文章
STM32高分辨率功能非常强大,寄存器数量非常多,使用起来也比较麻烦,本文使用HRTIM1产生一组互补输出的PWM
高分辨率定时器的初始化比普通定时器要复杂得多,
HRTIM_OutputCfgTypeDef HRTIM_TIM_OutputStructure;//配置输出特性,高低电平等
HRTIM_BaseInitTypeDef HRTIM_BaseInitStructure;//配置基本参数,这部分和普通定时器一样
HRTIM_TimerInitTypeDef HRTIM_TimerInitStructure; //配置不同模式
HRTIM_TimerCfgTypeDef HRTIM_TimerWaveStructure;//配置波形参数,错误等
HRTIM_CompareCfgTypeDef HRTIM_CompareStructure;//配置比较寄存器
用以上几个部分就能产生最简单的PWM波形
HRTIM_DeadTimeCfgTypeDef HRTIM_DeadtimeStructure;//配置死区时间和性质
HRTIM_FaultCfgTypeDef HRTIM_FaultCfgStructure;//配置错误输入
HRTIM_ADCTriggerCfgTypeDef HRTIM_ADCTrigStructure; //配置ADC触发信号
HRTIM_BurstModeCfgTypeDef HRTIM_BurstStructure;//配置Burst模式
等等,还有很多初始化参数和初始化函数,基本分为简单基本功能相关函数,波形相关,输出相关,中断、DMA等模块;
下面的程序用HRTIM产生一组简单的PWM波形,50%占空比,无死区
#definePERIOD46080//100k频率,PERIOD=4.608G/100k
#include"HRTIM_app.h"
voidHRTIM_Config(void)
{
HRTIM_OutputCfgTypeDefHRTIM_TIM_OutputStructure;
HRTIM_BaseInitTypeDefHRTIM_BaseInitStructure;
HRTIM_TimerInitTypeDefHRTIM_TimerInitStructure;
HRTIM_TimerCfgTypeDefHRTIM_TimerWaveStructure;
HRTIM_CompareCfgTypeDefHRTIM_CompareStructure;
HRTIM_DeadTimeCfgTypeDefHRTIM_DeadtimeStructure;
RCC_HRTIM1CLKConfig(RCC_HRTIM1CLK_PLLCLK);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_HRTIM1,ENABLE);
HRTIM_DLLCalibrationStart(HRTIM1,HRTIM_CALIBRATIONRATE_14);
HRTIM1_COMMON->DLLCR|=HRTIM_DLLCR_CALEN;
while(HRTIM_GetCommonFlagStatus(HRTIM1,HRTIM_ISR_DLLRDY)==RESET);
GPIO_HRTIM_outputs_Config();
HRTIM_BaseInitStructure.Mode=HRTIM_MODE_CONTINOUS;
HRTIM_BaseInitStructure.Period=PERIOD;
HRTIM_BaseInitStructure.PrescalerRatio=HRTIM_PRESCALERRATIO_MUL32;
HRTIM_BaseInitStructure.RepetitionCounter=0x00;
//HRTIM_SimpleBase_Init(HRTIM1,HRTIM_TIMERINDEX_TIMER_D,&HRTIM_BaseInitStructure);
HRTIM_TimerInitStructure.HalfModeEnable=HRTIM_HALFMODE_DISABLED;
HRTIM_TimerInitStructure.StartOnSync=HRTIM_SYNCSTART_DISABLED;
HRTIM_TimerInitStructure.ResetOnSync=HRTIM_SYNCRESET_DISABLED;
HRTIM_TimerInitStructure.DACSynchro=HRTIM_DACSYNC_NONE;
HRTIM_TimerInitStructure.PreloadEnable=HRTIM_PRELOAD_ENABLED;
HRTIM_TimerInitStructure.UpdateGating=HRTIM_UPDATEGATING_INDEPENDENT;
HRTIM_TimerInitStructure.BurstMode=HRTIM_TIMERBURSTMODE_MAINTAINCLOCK;
HRTIM_TimerInitStructure.RepetitionUpdate=HRTIM_UPDATEONREPETITION_ENABLED;
HRTIM_Waveform_Init(HRTIM1,HRTIM_TIMERINDEX_TIMER_D,&HRTIM_BaseInitStructure,&HRTIM_TimerInitStructure);
HRTIM_TimerWaveStructure.DeadTimeInsertion=HRTIM_TIMDEADTIMEINSERTION_ENABLED;
HRTIM_TimerWaveStructure.DelayedProtectionMode=HRTIM_TIMDELAYEDPROTECTION_DISABLED;
HRTIM_TimerWaveStructure.FaultEnable=HRTIM_TIMFAULTENABLE_NONE;
HRTIM_TimerWaveStructure.FaultLock=HRTIM_TIMFAULTLOCK_READWRITE;
HRTIM_TimerWaveStructure.PushPull=HRTIM_TIMPUSHPULLMODE_DISABLED;
HRTIM_TimerWaveStructure.ResetTrigger=HRTIM_TIMRESETTRIGGER_NONE;
HRTIM_TimerWaveStructure.ResetUpdate=HRTIM_TIMUPDATEONRESET_DISABLED;
HRTIM_TimerWaveStructure.UpdateTrigger=HRTIM_TIMUPDATETRIGGER_NONE;
HRTIM_WaveformTimerConfig(HRTIM1,HRTIM_TIMERINDEX_TIMER_D,&HRTIM_TimerWaveStructure);
HRTIM_TIM_OutputStructure.Polarity=HRTIM_OUTPUTPOLARITY_HIGH;
HRTIM_TIM_OutputStructure.SetSource=HRTIM_OUTPUTSET_TIMPER;
HRTIM_TIM_OutputStructure.ResetSource=HRTIM_OUTPUTRESET_TIMCMP1;
HRTIM_TIM_OutputStructure.IdleMode=HRTIM_OUTPUTIDLEMODE_NONE;
HRTIM_TIM_OutputStructure.IdleState=HRTIM_OUTPUTIDLESTATE_INACTIVE;
HRTIM_TIM_OutputStructure.FaultState=HRTIM_OUTPUTFAULTSTATE_INACTIVE;
HRTIM_TIM_OutputStructure.ChopperModeEnable=HRTIM_OUTPUTCHOPPERMODE_DISABLED;
HRTIM_TIM_OutputStructure.BurstModeEntryDelayed=HRTIM_OUTPUTBURSTMODEENTRY_REGULAR;
HRTIM_WaveformOutputConfig(HRTIM1,HRTIM_TIMERINDEX_TIMER_D,HRTIM_OUTPUT_TD1,&HRTIM_TIM_OutputStructure);
//HRTIM_WaveformOutputConfig(HRTIM1,HRTIM_TIMERINDEX_TIMER_D,HRTIM_OUTPUT_TD2,&HRTIM_TIM_OutputStructure);
HRTIM_CompareStructure.AutoDelayedMode=HRTIM_AUTODELAYEDMODE_REGULAR;
HRTIM_CompareStructure.AutoDelayedTimeout=0x00;
HRTIM_CompareStructure.CompareValue=PERIOD/2;
HRTIM_WaveformCompareConfig(HRTIM1,HRTIM_TIMERINDEX_TIMER_D,HRTIM_COMPAREUNIT_1,&HRTIM_CompareStructure);
HRTIM_DeadtimeStructure.FallingLock=HRTIM_TIMDEADTIME_FALLINGLOCK_READONLY;
HRTIM_DeadtimeStructure.FallingSign=HRTIM_TIMDEADTIME_FALLINGSIGN_POSITIVE;
HRTIM_DeadtimeStructure.FallingSignLock=HRTIM_TIMDEADTIME_FALLINGSIGNLOCK_READONLY;
HRTIM_DeadtimeStructure.FallingValue=0X00;
HRTIM_DeadtimeStructure.RisingLock=HRTIM_TIMDEADTIME_RISINGLOCK_READONLY;
HRTIM_DeadtimeStructure.RisingSign=HRTIM_TIMDEADTIME_RISINGSIGN_POSITIVE;
HRTIM_DeadtimeStructure.RisingSignLock=HRTIM_TIMDEADTIME_RISINGSIGNLOCK_READONLY;
HRTIM_DeadtimeStructure.RisingValue=0X00;
HRTIM_DeadTimeConfig(HRTIM1,HRTIM_TIMERINDEX_TIMER_D,&HRTIM_DeadtimeStructure);
HRTIM_WaveformOutputStart(HRTIM1,HRTIM_OUTPUT_TD1);
HRTIM_WaveformOutputStart(HRTIM1,HRTIM_OUTPUT_TD2);
HRTIM_WaveformCounterStart(HRTIM1,HRTIM_TIMERID_TIMER_D);
}
http://download.csdn.net/detail/wind4study/874