这里给出配置的代码和PI的代码。
/***************************************************************************
#define PWM_PERIOD = 144000000*32/switchfrequency
#define DT_RISING = risingtime*switchfrequency*PWM_PERIOD
#define DT_FALLING = fallingtime*switchfrequency*PWM_PERIOD
***************************************************************************/
/**
*
@brief 用于配置HRTIM_A的输出,关闭deadtime时,为单输出,开启deadtime时,为双输出。
* @param 死区使能,配套AD采样使能,错误使能,中断使能,初始频率,初始占空比(HO),中断频率,上升死区时间(单位纳秒),下降死区时间
* @retval None
*/
void MY_BSP_Init_HRTIM_A(BOOLEAN deadtime,BOOLEAN adenable,BOOLEAN faultenable,BOOLEAN interrupt,uint32_t Initial_Fre,uint8_t Initial_Duty,uint8_t n_ISR,uint8_t risingtime,uint8_t fallingtime)
{
HRTIM_TimeBaseCfgTypeDef timebase_config;
HRTIM_TimerCfgTypeDef timer_config;
HRTIM_OutputCfgTypeDef output_config_TA;
HRTIM_CompareCfgTypeDef compare_config;
/* ----------------------------*/
/* HRTIM Global initialization */
/* ----------------------------*/
/* Initialize the hrtim structure (minimal configuration) */
hhrtimA.Instance = HRTIM1;
hhrtimA.Init.HRTIMInterruptResquests = HRTIM_IT_NONE;
hhrtimA.Init.SyncOptions = HRTIM_SYNCOPTION_NONE;
/* Initialize HRTIM */
HAL_HRTIM_Init(&hhrtimA);
/* HRTIM DLL calibration: periodic calibration, set period to 14祍 */
HAL_HRTIM_DLLCalibrationStart(&hhrtimA, HRTIM_CALIBRATIONRATE_14);
/* Wait calibration completion*/
if (HAL_HRTIM_PollForDLLCalibration(&hhrtimA, 100) != HAL_OK)
{
Error_Handler(); // if DLL or clock is not correctly set
}
/* --------------------------------------------------- */
/* TIMERA initialization: timer mode and PWM frequency */
/* --------------------------------------------------- */
timebase_config.Period = 4608000000/Initial_Fre; /* 400kHz switching frequency */
timebase_config.RepetitionCounter = n_ISR - 1; /* n ISR every 128 PWM periods */
timebase_config.PrescalerRatio = HRTIM_PRESCALERRATIO_MUL32;
timebase_config.Mode = HRTIM_MODE_CONTINUOUS;
HAL_HRTIM_TimeBaseConfig(&hhrtimA, HRTIM_TIMERINDEX_TIMER_A, &timebase_config);
/* --------------------------------------------------------------------- */
/* TIMERA global configuration: cnt reset, sync, update, fault, burst... */
/* timer running in continuous mode, with deadtime enabled */
/* --------------------------------------------------------------------- */
timer_config.DMARequests = HRTIM_TIM_DMA_NONE;
timer_config.DMASrcAddress = 0x0;
timer_config.DMADstAddress = 0x0;
timer_config.DMASize = 0x0;
timer_config.HalfModeEnable = HRTIM_HALFMODE_DISABLED;
timer_config.StartOnSync = HRTIM_SYNCSTART_DISABLED;
timer_config.ResetOnSync = HRTIM_SYNCRESET_DISABLED;
timer_config.DACSynchro = HRTIM_DACSYNC_NONE;
timer_config.PreloadEnable = HRTIM_PRELOAD_ENABLED;
timer_config.UpdateGating = HRTIM_UPDATEGATING_INDEPENDENT;
timer_config.BurstMode = HRTIM_TIMERBURSTMODE_MAINTAINCLOCK;
timer_config.RepetitionUpdate = HRTIM_UPDATEONREPETITION_ENABLED;
timer_config.ResetUpdate = HRTIM_TIMUPDATEONRESET_DISABLED;
if(interrupt == TRUE)
{
timer_config.InterruptRequests = HRTIM_TIM_IT_REP;
}
else
timer_config.InterruptRequests = HRTIM_TIM_IT_NONE;
timer_config.PushPull = HRTIM_TIMPUSHPULLMODE_DISABLED;
if(faultenable == TRUE)
timer_config.FaultEnable = HRTIM_TIMFAULTENABLE_FAULT1;
else
timer_config.FaultEnable = HRTIM_TIMFAULTENABLE_NONE;
timer_config.FaultLock = HRTIM_TIMFAULTLOCK_READWRITE;
timer_config.DeadTimeInsertion = HRTIM_TIMDEADTIMEINSERTION_ENABLED;
timer_config.DelayedProtectionMode = HRTIM_TIMER_A_B_C_DELAYEDPROTECTION_DISABLED;
timer_config.UpdateTrigger= HRTIM_TIMUPDATETRIGGER_NONE;
timer_config.ResetTrigger = HRTIM_TIMRESETTRIGGER_NONE;
HAL_HRTIM_WaveformTimerConfig(&hhrtimA, HRTIM_TIMERINDEX_TIMER_A, &timer_config);
/* Set compare registers for duty cycle on TA1 */
compare_config.CompareValue = 46080000*Initial_Duty/Initial_Fre; /*duty cycle */
HAL_HRTIM_WaveformCompareConfig(&hhrtimA,
HRTIM_TIMERINDEX_TIMER_A,
HRTIM_COMPAREUNIT_1,
&compare_config);
/* --------------------------------- */
/* TA1 and TA2 waveforms description */
/* --------------------------------- */
output_config_TA.Polarity = HRTIM_OUTPUTPOLARITY_HIGH;
output_config_TA.SetSource = HRTIM_OUTPUTSET_TIMPER;
output_config_TA.ResetSource = HRTIM_OUTPUTRESET_TIMCMP1;
output_config_TA.IdleMode = HRTIM_OUTPUTIDLEMODE_NONE;
output_config_TA.IdleLevel = HRTIM_OUTPUTIDLELEVEL_INACTIVE;
output_config_TA.FaultLevel = HRTIM_OUTPUTFAULTLEVEL_INACTIVE;
output_config_TA.ChopperModeEnable = HRTIM_OUTPUTCHOPPERMODE_DISABLED;
output_config_TA.BurstModeEntryDelayed = HRTIM_OUTPUTBURSTMODEENTRY_REGULAR;
HAL_HRTIM_WaveformOutputConfig(&hhrtimA,
HRTIM_TIMERINDEX_TIMER_A,
HRTIM_OUTPUT_TA1,
&output_config_TA);
if(deadtime == TRUE)
{
HAL_HRTIM_WaveformOutputConfig(&hhrtimA,
HRTIM_TIMERINDEX_TIMER_A,
HRTIM_OUTPUT_TA2,
&output_config_TA);
}
if(deadtime == TRUE)
{
HRTIM_DeadTimeCfgTypeDef HRTIM_TIM_DeadTimeConfig;
/* Deadtime configuration for Timer A */
HRTIM_TIM_DeadTimeConfig.FallingLock = HRTIM_TIMDEADTIME_FALLINGLOCK_WRITE;
HRTIM_TIM_DeadTimeConfig.FallingSign = HRTIM_TIMDEADTIME_FALLINGSIGN_POSITIVE;
HRTIM_TIM_DeadTimeConfig.FallingSignLock = HRTIM_TIMDEADTIME_FALLINGSIGNLOCK_READONLY;
HRTIM_TIM_DeadTimeConfig.FallingValue = risingtime*4096/1000;
HRTIM_TIM_DeadTimeConfig.Prescaler = HRTIM_TIMDEADTIME_PRESCALERRATIO_MUL8;
HRTIM_TIM_DeadTimeConfig.RisingLock = HRTIM_TIMDEADTIME_RISINGLOCK_WRITE;
HRTIM_TIM_DeadTimeConfig.RisingSign = HRTIM_TIMDEADTIME_RISINGSIGN_POSITIVE;
HRTIM_TIM_DeadTimeConfig.RisingSignLock = HRTIM_TIMDEADTIME_RISINGSIGNLOCK_READONLY;
HRTIM_TIM_DeadTimeConfig.RisingValue = fallingtime*4096/1000;
HAL_HRTIM_DeadTimeConfig(&hhrtimA, HRTIM_TIMERINDEX_TIMER_A, &HRTIM_TIM_DeadTimeConfig);
}
if(adenable == TRUE)
{
HRTIM_ADCTriggerCfgTypeDef adc_trigger_config;
/* ------------------------------------------- */
/* ADC trigger intialization (with CMP4 event) */
/* ------------------------------------------- */
compare_config.AutoDelayedMode = HRTIM_AUTODELAYEDMODE_REGULAR;
compare_config.AutoDelayedTimeout = 0;
if(Initial_Duty >=50)
compare_config.CompareValue = 46080000*Initial_Duty/Initial_Fre; /* Samples in middle of ON time */
else
compare_config.CompareValue = 23040000*(100+Initial_Duty)/Initial_Fre;
HAL_HRTIM_WaveformCompareConfig(&hhrtimA,
HRTIM_TIMERINDEX_TIMER_A,
HRTIM_COMPAREUNIT_4,
&compare_config);
adc_trigger_config.Trigger = HRTIM_ADCTRIGGEREVENT24_TIMERA_CMP4;
adc_trigger_config.UpdateSource = HRTIM_ADCTRIGGERUPDATE_TIMER_A;
HAL_HRTIM_ADCTriggerConfig(&hhrtimA,
HRTIM_ADCTRIGGER_2,
&adc_trigger_config);
}
if(faultenable == TRUE)
{
HRTIM_FaultCfgTypeDef fault_config;
/* ---------------------*/
/* FAULT initialization */
/* ---------------------*/
fault_config.Filter = HRTIM_FAULTFILTER_NONE;
fault_config.Lock = HRTIM_FAULTLOCK_READWRITE;
fault_config.Polarity = HRTIM_FAULTPOLARITY_LOW;
fault_config.Source = HRTIM_FAULTSOURCE_DIGITALINPUT;
HAL_HRTIM_FaultConfig(&hhrtimA,
HRTIM_FAULT_1,
&fault_config);
HAL_HRTIM_FaultModeCtl(&hhrtimA,
HRTIM_FAULT_1,
HRTIM_FAULTMODECTL_ENABLED);
}
if(deadtime == TRUE)
{
/* ---------------*/
/* HRTIM start-up */
/* ---------------*/
/* Enable HRTIM's outputs TA1 and TA2 */
/* Note: it is necessary to enable also GPIOs to have outputs functional */
/* This must be done after HRTIM initialization */
HAL_HRTIM_WaveformOutputStart(&hhrtimA, HRTIM_OUTPUT_TA1 | HRTIM_OUTPUT_TA2);
}
else
HAL_HRTIM_WaveformOutputStart(&hhrtimA, HRTIM_OUTPUT_TA1);
/* Start both HRTIM TIMER A, B and D */
if(interrupt == TRUE)
HAL_HRTIM_WaveformCounterStart_IT(&hhrtimA, HRTIM_TIMERID_TIMER_A);
else
HAL_HRTIM_WaveformCounterStart(&hhrtimA, HRTIM_TIMERID_TIMER_A);
GPIO_InitTypeDef GPIO_InitStruct;
/* Enable GPIOA clock for timer A outputs */
__HAL_RCC_GPIOA_CLK_ENABLE();
/* Configure HRTIM output: TA1 (PA8) */
GPIO_InitStruct.Pin = GPIO_PIN_8;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;;
GPIO_InitStruct.Alternate = GPIO_AF13_HRTIM1;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
if(deadtime == TRUE)
{
/* Configure HRTIM output: TA2 (PA9) */
GPIO_InitStruct.Pin = GPIO_PIN_9;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
}
}
这里给出配置的代码和PI的代码。
/***************************************************************************
#define PWM_PERIOD = 144000000*32/switchfrequency
#define DT_RISING = risingtime*switchfrequency*PWM_PERIOD
#define DT_FALLING = fallingtime*switchfrequency*PWM_PERIOD
***************************************************************************/
/**
*
@brief 用于配置HRTIM_A的输出,关闭deadtime时,为单输出,开启deadtime时,为双输出。
* @param 死区使能,配套AD采样使能,错误使能,中断使能,初始频率,初始占空比(HO),中断频率,上升死区时间(单位纳秒),下降死区时间
* @retval None
*/
void MY_BSP_Init_HRTIM_A(BOOLEAN deadtime,BOOLEAN adenable,BOOLEAN faultenable,BOOLEAN interrupt,uint32_t Initial_Fre,uint8_t Initial_Duty,uint8_t n_ISR,uint8_t risingtime,uint8_t fallingtime)
{
HRTIM_TimeBaseCfgTypeDef timebase_config;
HRTIM_TimerCfgTypeDef timer_config;
HRTIM_OutputCfgTypeDef output_config_TA;
HRTIM_CompareCfgTypeDef compare_config;
/* ----------------------------*/
/* HRTIM Global initialization */
/* ----------------------------*/
/* Initialize the hrtim structure (minimal configuration) */
hhrtimA.Instance = HRTIM1;
hhrtimA.Init.HRTIMInterruptResquests = HRTIM_IT_NONE;
hhrtimA.Init.SyncOptions = HRTIM_SYNCOPTION_NONE;
/* Initialize HRTIM */
HAL_HRTIM_Init(&hhrtimA);
/* HRTIM DLL calibration: periodic calibration, set period to 14祍 */
HAL_HRTIM_DLLCalibrationStart(&hhrtimA, HRTIM_CALIBRATIONRATE_14);
/* Wait calibration completion*/
if (HAL_HRTIM_PollForDLLCalibration(&hhrtimA, 100) != HAL_OK)
{
Error_Handler(); // if DLL or clock is not correctly set
}
/* --------------------------------------------------- */
/* TIMERA initialization: timer mode and PWM frequency */
/* --------------------------------------------------- */
timebase_config.Period = 4608000000/Initial_Fre; /* 400kHz switching frequency */
timebase_config.RepetitionCounter = n_ISR - 1; /* n ISR every 128 PWM periods */
timebase_config.PrescalerRatio = HRTIM_PRESCALERRATIO_MUL32;
timebase_config.Mode = HRTIM_MODE_CONTINUOUS;
HAL_HRTIM_TimeBaseConfig(&hhrtimA, HRTIM_TIMERINDEX_TIMER_A, &timebase_config);
/* --------------------------------------------------------------------- */
/* TIMERA global configuration: cnt reset, sync, update, fault, burst... */
/* timer running in continuous mode, with deadtime enabled */
/* --------------------------------------------------------------------- */
timer_config.DMARequests = HRTIM_TIM_DMA_NONE;
timer_config.DMASrcAddress = 0x0;
timer_config.DMADstAddress = 0x0;
timer_config.DMASize = 0x0;
timer_config.HalfModeEnable = HRTIM_HALFMODE_DISABLED;
timer_config.StartOnSync = HRTIM_SYNCSTART_DISABLED;
timer_config.ResetOnSync = HRTIM_SYNCRESET_DISABLED;
timer_config.DACSynchro = HRTIM_DACSYNC_NONE;
timer_config.PreloadEnable = HRTIM_PRELOAD_ENABLED;
timer_config.UpdateGating = HRTIM_UPDATEGATING_INDEPENDENT;
timer_config.BurstMode = HRTIM_TIMERBURSTMODE_MAINTAINCLOCK;
timer_config.RepetitionUpdate = HRTIM_UPDATEONREPETITION_ENABLED;
timer_config.ResetUpdate = HRTIM_TIMUPDATEONRESET_DISABLED;
if(interrupt == TRUE)
{
timer_config.InterruptRequests = HRTIM_TIM_IT_REP;
}
else
timer_config.InterruptRequests = HRTIM_TIM_IT_NONE;
timer_config.PushPull = HRTIM_TIMPUSHPULLMODE_DISABLED;
if(faultenable == TRUE)
timer_config.FaultEnable = HRTIM_TIMFAULTENABLE_FAULT1;
else
timer_config.FaultEnable = HRTIM_TIMFAULTENABLE_NONE;
timer_config.FaultLock = HRTIM_TIMFAULTLOCK_READWRITE;
timer_config.DeadTimeInsertion = HRTIM_TIMDEADTIMEINSERTION_ENABLED;
timer_config.DelayedProtectionMode = HRTIM_TIMER_A_B_C_DELAYEDPROTECTION_DISABLED;
timer_config.UpdateTrigger= HRTIM_TIMUPDATETRIGGER_NONE;
timer_config.ResetTrigger = HRTIM_TIMRESETTRIGGER_NONE;
HAL_HRTIM_WaveformTimerConfig(&hhrtimA, HRTIM_TIMERINDEX_TIMER_A, &timer_config);
/* Set compare registers for duty cycle on TA1 */
compare_config.CompareValue = 46080000*Initial_Duty/Initial_Fre; /*duty cycle */
HAL_HRTIM_WaveformCompareConfig(&hhrtimA,
HRTIM_TIMERINDEX_TIMER_A,
HRTIM_COMPAREUNIT_1,
&compare_config);
/* --------------------------------- */
/* TA1 and TA2 waveforms description */
/* --------------------------------- */
output_config_TA.Polarity = HRTIM_OUTPUTPOLARITY_HIGH;
output_config_TA.SetSource = HRTIM_OUTPUTSET_TIMPER;
output_config_TA.ResetSource = HRTIM_OUTPUTRESET_TIMCMP1;
output_config_TA.IdleMode = HRTIM_OUTPUTIDLEMODE_NONE;
output_config_TA.IdleLevel = HRTIM_OUTPUTIDLELEVEL_INACTIVE;
output_config_TA.FaultLevel = HRTIM_OUTPUTFAULTLEVEL_INACTIVE;
output_config_TA.ChopperModeEnable = HRTIM_OUTPUTCHOPPERMODE_DISABLED;
output_config_TA.BurstModeEntryDelayed = HRTIM_OUTPUTBURSTMODEENTRY_REGULAR;
HAL_HRTIM_WaveformOutputConfig(&hhrtimA,
HRTIM_TIMERINDEX_TIMER_A,
HRTIM_OUTPUT_TA1,
&output_config_TA);
if(deadtime == TRUE)
{
HAL_HRTIM_WaveformOutputConfig(&hhrtimA,
HRTIM_TIMERINDEX_TIMER_A,
HRTIM_OUTPUT_TA2,
&output_config_TA);
}
if(deadtime == TRUE)
{
HRTIM_DeadTimeCfgTypeDef HRTIM_TIM_DeadTimeConfig;
/* Deadtime configuration for Timer A */
HRTIM_TIM_DeadTimeConfig.FallingLock = HRTIM_TIMDEADTIME_FALLINGLOCK_WRITE;
HRTIM_TIM_DeadTimeConfig.FallingSign = HRTIM_TIMDEADTIME_FALLINGSIGN_POSITIVE;
HRTIM_TIM_DeadTimeConfig.FallingSignLock = HRTIM_TIMDEADTIME_FALLINGSIGNLOCK_READONLY;
HRTIM_TIM_DeadTimeConfig.FallingValue = risingtime*4096/1000;
HRTIM_TIM_DeadTimeConfig.Prescaler = HRTIM_TIMDEADTIME_PRESCALERRATIO_MUL8;
HRTIM_TIM_DeadTimeConfig.RisingLock = HRTIM_TIMDEADTIME_RISINGLOCK_WRITE;
HRTIM_TIM_DeadTimeConfig.RisingSign = HRTIM_TIMDEADTIME_RISINGSIGN_POSITIVE;
HRTIM_TIM_DeadTimeConfig.RisingSignLock = HRTIM_TIMDEADTIME_RISINGSIGNLOCK_READONLY;
HRTIM_TIM_DeadTimeConfig.RisingValue = fallingtime*4096/1000;
HAL_HRTIM_DeadTimeConfig(&hhrtimA, HRTIM_TIMERINDEX_TIMER_A, &HRTIM_TIM_DeadTimeConfig);
}
if(adenable == TRUE)
{
HRTIM_ADCTriggerCfgTypeDef adc_trigger_config;
/* ------------------------------------------- */
/* ADC trigger intialization (with CMP4 event) */
/* ------------------------------------------- */
compare_config.AutoDelayedMode = HRTIM_AUTODELAYEDMODE_REGULAR;
compare_config.AutoDelayedTimeout = 0;
if(Initial_Duty >=50)
compare_config.CompareValue = 46080000*Initial_Duty/Initial_Fre; /* Samples in middle of ON time */
else
compare_config.CompareValue = 23040000*(100+Initial_Duty)/Initial_Fre;
HAL_HRTIM_WaveformCompareConfig(&hhrtimA,
HRTIM_TIMERINDEX_TIMER_A,
HRTIM_COMPAREUNIT_4,
&compare_config);
adc_trigger_config.Trigger = HRTIM_ADCTRIGGEREVENT24_TIMERA_CMP4;
adc_trigger_config.UpdateSource = HRTIM_ADCTRIGGERUPDATE_TIMER_A;
HAL_HRTIM_ADCTriggerConfig(&hhrtimA,
HRTIM_ADCTRIGGER_2,
&adc_trigger_config);
}
if(faultenable == TRUE)
{
HRTIM_FaultCfgTypeDef fault_config;
/* ---------------------*/
/* FAULT initialization */
/* ---------------------*/
fault_config.Filter = HRTIM_FAULTFILTER_NONE;
fault_config.Lock = HRTIM_FAULTLOCK_READWRITE;
fault_config.Polarity = HRTIM_FAULTPOLARITY_LOW;
fault_config.Source = HRTIM_FAULTSOURCE_DIGITALINPUT;
HAL_HRTIM_FaultConfig(&hhrtimA,
HRTIM_FAULT_1,
&fault_config);
HAL_HRTIM_FaultModeCtl(&hhrtimA,
HRTIM_FAULT_1,
HRTIM_FAULTMODECTL_ENABLED);
}
if(deadtime == TRUE)
{
/* ---------------*/
/* HRTIM start-up */
/* ---------------*/
/* Enable HRTIM's outputs TA1 and TA2 */
/* Note: it is necessary to enable also GPIOs to have outputs functional */
/* This must be done after HRTIM initialization */
HAL_HRTIM_WaveformOutputStart(&hhrtimA, HRTIM_OUTPUT_TA1 | HRTIM_OUTPUT_TA2);
}
else
HAL_HRTIM_WaveformOutputStart(&hhrtimA, HRTIM_OUTPUT_TA1);
/* Start both HRTIM TIMER A, B and D */
if(interrupt == TRUE)
HAL_HRTIM_WaveformCounterStart_IT(&hhrtimA, HRTIM_TIMERID_TIMER_A);
else
HAL_HRTIM_WaveformCounterStart(&hhrtimA, HRTIM_TIMERID_TIMER_A);
GPIO_InitTypeDef GPIO_InitStruct;
/* Enable GPIOA clock for timer A outputs */
__HAL_RCC_GPIOA_CLK_ENABLE();
/* Configure HRTIM output: TA1 (PA8) */
GPIO_InitStruct.Pin = GPIO_PIN_8;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;;
GPIO_InitStruct.Alternate = GPIO_AF13_HRTIM1;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
if(deadtime == TRUE)
{
/* Configure HRTIM output: TA2 (PA9) */
GPIO_InitStruct.Pin = GPIO_PIN_9;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
}
}
举报