??在使用PSIM软件仿真开关电源时,大多数都是模拟电路,纯数字电路的仿真很少。无意间发现了在PSIM 2021版本中有官方的数字控制BUCK电路仿真。电路使用简单C模块编写的代码来控制电路。  ??由于下载的2021版是演示版,不能直接仿真,为了能够彻底的学习,于是将电路图和程序移植到了9.1版本中。现在将电路和代码分享出来。
??2021版官方例程  ??由于软件是演示版,有限制,所以不能仿真。 ??于是将电路图和代码移植到 PSIM 9.1 版本上  ??硬件电路如下:  ??首先使用电路传感器读取电流值。
 ??将采样到的模拟电流值缩小0.2倍然后加上1.5送入到ADC模块中。
 ??ADC模块有三路输入信号,两路输出信号。ADC模块是用简化C模块编写代码实现。t它的主要作用是将模拟信号转换为数字信号输出。 
float data_in, Gadc;
static int sampled_data, counter;
static bool Flag =1, Fsamp_instant, ap_start_compensator;
int Bits;
Fsamp_instant = x1;
Bits = x2;
data_in = x3;
Gadc = (pow(2,Bits) -1)/3.3;
if (Fsamp_instant == 1 && Flag ==1){
if (data_in > 3.3){
sampled_data = (3.3*Gadc);
}
if (data_in < 0){
sampled_data = (0*Gadc);
}
if (data_in <= 3.3 && data_in>=0){
sampled_data = (data_in*Gadc);
}
Flag = 0;
}
if (Fsamp_instant == 0){
Flag = 1;
ap_start_compensator = 0;
counter = 0;
}
if (Flag == 0){
counter = counter +1;
}
if (counter > 2){
ap_start_compensator = 1;
}
y1 = sampled_data;
y2 = ap_start_compensator;
??ADC模块输出电流的数字量之后,然后在送入PI模块中通过PI公式计算输出占空比的值。
 ??PI模块有8路输入,2路输出。PI计算时需要的一些常量通过常量模块输入。 
int IL_ref, IL_sense;
float Kp, Ti, Fsamp;
float upper_limit_compensator, lower_limit_compensator;
static int output;
float A1, A0;
float e_k, proportional_k, integral_k;
static float e_k_1, integral_k_1, y_k;
static bool Flag;
bool ap_start;
Kp = x3;
Ti = x4;
Fsamp = x5;
upper_limit_compensator = x6;
lower_limit_compensator = x7;
ap_start = x8;
A0 = Kp;
A1 = Kp/(Ti*2*Fsamp);
if (ap_start == 1 && Flag == 0){
Flag =1;
IL_ref = x1;
IL_sense = x2;
e_k = IL_ref - IL_sense;
proportional_k = A0*e_k;
integral_k = A1*e_k + A1*e_k_1 + integral_k_1;
y_k = proportional_k + integral_k;
e_k_1 = e_k;
integral_k_1 = integral_k;
if (y_k >= upper_limit_compensator) {
y_k = upper_limit_compensator;
}
if (y_k <= lower_limit_compensator) {
y_k = lower_limit_compensator;
}
output =y_k;
}
if (ap_start == 0) {
Flag =0;
}
y1 = output;
y2 = integral_k;
??最后将PI模块输出的占空比控制量送入到DPWM模块中,DPWM模块根据占空比的值输出两路互补的PWM波。
 
float Fclk_psim, time_step, deadtime;
int Nr, Fsw, digital_deadtime, duty_1, duty_2;
bool samp_inst;
static int counter_trig=0, duty_cycle;
static bool Up=1, Down=0, HB, LB;
Fsw = x1;
time_step = x2;
deadtime = x4;
Fclk_psim = 1/time_step;
Nr = 0.5*Fclk_psim/Fsw;
if (counter_trig ==0) {
Up =1;
Down =0;
duty_cycle = x3;
}
if (counter_trig == Nr) {
Up = 0;
Down =1;
duty_cycle = x3;
}
if ((counter_trig >=0 && counter_trig <= 4 && Up ==1) || (counter_trig <= Nr && counter_trig >= Nr-4 && Down ==1 )){
samp_inst = 1;
} else {
samp_inst = 0;
}
if (Up ==1) {
counter_trig = counter_trig +1;
}
if (Down ==1) {
counter_trig = counter_trig -1;
}
digital_deadtime = deadtime/(2*time_step);
duty_1 = duty_cycle - digital_deadtime;
duty_2 = duty_cycle + digital_deadtime;
if (counter_trig >= duty_1){
HB =0;
} else {
HB =1;
}
if (duty_2 <= counter_trig){
LB =1;
} else {
LB =0;
}
y1 = HB;
y2 = LB;
y3 = samp_inst;
y4 = counter_trig;
y5 = duty_cycle;
??为了便于验证ADC模块转换的正确性,官方线路中还提供了一个硬件将模拟信号转换为数字信号的电路。  ??同时为了便于监控信号,将变量的标签和电压表连接起来,这样就可以直接在波形中观察变量的值。  ??电路中用到了一些参考值是通过电压源直接输入到模块中的。  ??移植到PSIM 9.1 版本中的完整电路如下。  ??仿真结果如下  ??如果想看其他变量的结果,可以直接在波形中添加。  ??这里观察输出电流的模拟值,经过ADC模块转换后的数字值,PI模块计算后的占空比值  ??官方的代码写的很详细,里面也有很多注释。通过官方的这个例程去理解学习数字电源的仿真,相信大家会有很多收获的。
??仿真文件下载链接:https://download.csdn.net/download/qq_20222919/82906330
|