从最基本的说起吧,DC-DC的变换电路有很多种,线性电源、开关电源、电荷泵,线性电源大家比较熟悉的应该就是78XX系列的芯片了,电荷泵主要用在小电流的应用中,我们也不加讨论。主要讲讲开关电源,我呢也是一个先学先卖的人,就对照资料啥的随便介绍下拉,权当是开源本设计前的一点准备工作。
开关稳压器的工作原理,就是通过控制电路来控制开关器件的通断,配合负反馈完成稳压,跟线性稳压比起来,具有效率高体积小的特点,但是输出没有线性电源稳定。开关电源的基本结构有很多种,包括BUCK、BOOST、BUCK-BOOST、CUK等非隔离式的DCDC变换器,也有Flyback、LLC等隔离式的DCDC变换器。开源的这个设计,是以buck拓扑为核心,配合F334的高级定时器的PWM、PI算法,实现的一个很简单的闭环控制,设计输入电压60V时,输出电压可调,输出电流最大5A,输出最大功率在200W左右。
这是基本的原理框图
系统框图如上,首先说明我这款电压是从HP电源的基础上增加人机界面和改善栅极驱动做的,借这个机会分享下自己的心得。
采用同步BUCK,就是采用导通电阻特别低的mosfet来代替续流二极管,以此来提高整个拓扑的工作效率。基本图如下:
图中采用了无电解电容设计,这样虽然纹波可能会大一点,但是响应的体积却小了很多,实际测试中,纹波在100MV以下。电感和电容的取值有响应公式可以推到,这里不多赘述,直接给大家提供一个小工具,输入参数就可以计算出结果的小工具:在文章最后配套资料
下面谈一谈程序的设计思路,因为这款设计为了尽可能减少体积,因此使用了较大频率的PWM波,取值为250k,所采用的主控stm32f334是意法半导体专为数控电源所设计的一款MCU。STM32F334xx微控制器具有高分辨率定时器(HRTIM)外设,可产生多达10个信号,能够处理用于控制、同步或保护的各种不同输入信号。其模块化架构允许对大部分转换拓扑和多并联转换器进行处理,并可在运行中重新配置它们。
在如上所示的拓扑当中,包括输出电压读数和过流保护(利用FAULT输入),使在电流超出可编程阈值时关闭转换器。为简单起见,此处不讨论电流传感器和调整电路;预期的FAULT反馈(在FLT1输入上)为数字信号(在PA12输入上)。
HRTIM工作于连续模式,PWM信号定义如下:
• TA1:在 TA Period 置位,在 TA CMP2 复位
• TA2:利用死区时间发生器,与 TA1 互补 (相同的上升沿和下降沿死区时间)
需要注意的是,有关AD采样的触发时机选择是一个很关键的点,如下图所示,对于特定占空比的PWM波,在其中央触发AD工作,这样可以避免纹波的影响。
由此,通过AD采样的输出电压与设定的电压一起,配合PI调节占空比,即完成了闭环反馈过程,通过对输出电压电流的编程,即完成电池充电程序的编写。
这里给出配置的代码和PI的代码。
/***************************************************************************
#define PWM_PERIOD = 144000000*32/switchfrequency
#define DT_RISING = risingtime*switchfrequency*PWM_PERIOD
#define DT_FALLING = fallingtime*switchfrequency*PWM_PERIOD
***************************************************************************/
/**
* [url=home.php?mod=space&uid=159083]@brief[/url] 用于配置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);
}
}
/**
* @brief This function calculates new duty order with PI.
* @param None
* @retval New duty order
*/
int32_t PI_Buck(uint32_t RealVol,uint32_t SetVol,int32_t dec2hex(int32_t temp))
{
/* Compute PI for Buck Mode */
/* Every time the PI order sets extreme values then CTMax or CTMin are managed */
int32_t seterr, pid_out;
int32_t error;
error = ((int32_t ) RealVol - (int32_t) SetVol);
error = dec2hex(error);
seterr = (-Kp * error) / 200;
Int_term_Buck = Int_term_Buck + ((-Ki * error) / 200);
if (Int_term_Buck > SAT_LIMIT)
{
Int_term_Buck = SAT_LIMIT;
}
if (Int_term_Buck < -(SAT_LIMIT))
{
Int_term_Buck = -(SAT_LIMIT);
}
pid_out = seterr + Int_term_Buck;
pid_out += BUCK_PWM_PERIOD / 2;
if (pid_out >= MAX_DUTY_A)
{
pid_out = MAX_DUTY_A;
CTMax++;
}
else
{
if (CTMax != 0)
{
CTMax--;
}
}
if (pid_out <= MIN_DUTY_A)
{
pid_out = MIN_DUTY_A;
CTMin++;
}
else
{
if (CTMin != 0)
{
CTMin--;
}
}
return pid_out;
}