STM32高分辨率定時(shí)器(HRTIM)使用實(shí)例之PWM
STM32高分辨率功能非常強(qiáng)大,寄存器數(shù)量非常多,使用起來也比較麻煩,本文使用HRTIM1產(chǎn)生一組互補(bǔ)輸出的PWM
高分辨率定時(shí)器的初始化比普通定時(shí)器要復(fù)雜得多,
HRTIM_OutputCfgTypeDef HRTIM_TIM_OutputStructure;//配置輸出特性,高低電平等
HRTIM_BaseInitTypeDef HRTIM_BaseInitStructure;//配置基本參數(shù),這部分和普通定時(shí)器一樣
HRTIM_TimerInitTypeDef HRTIM_TimerInitStructure; //配置不同模式
HRTIM_TimerCfgTypeDef HRTIM_TimerWaveStructure;//配置波形參數(shù),錯(cuò)誤等
HRTIM_CompareCfgTypeDef HRTIM_CompareStructure;//配置比較寄存器
用以上幾個(gè)部分就能產(chǎn)生最簡(jiǎn)單的PWM波形
HRTIM_DeadTimeCfgTypeDef HRTIM_DeadtimeStructure;//配置死區(qū)時(shí)間和性質(zhì)
HRTIM_FaultCfgTypeDef HRTIM_FaultCfgStructure;//配置錯(cuò)誤輸入
HRTIM_ADCTriggerCfgTypeDef HRTIM_ADCTrigStructure; //配置ADC觸發(fā)信號(hào)
HRTIM_BurstModeCfgTypeDef HRTIM_BurstStructure;//配置Burst模式
等等,還有很多初始化參數(shù)和初始化函數(shù),基本分為簡(jiǎn)單基本功能相關(guān)函數(shù),波形相關(guān),輸出相關(guān),中斷、DMA等模塊;
下面的程序用HRTIM產(chǎn)生一組簡(jiǎn)單的PWM波形,50%占空比,無死區(qū)
#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