基于DSP28335写一个通过PID调节两对EPWM互补方波的占空比实现稳压程序代码
时间: 2024-05-10 18:18:55 浏览: 106
DSP28335_epwm.rar_EPWM_ccs3.3_dsp epwm程序_dsp28335_epwm dsp
以下是基于DSP28335编写的PID稳压程序代码,实现通过PID调节两对EPWM互补方波的占空比:
```c
#include "DSP2833x_Device.h"
#include "DSP2833x_Examples.h"
#define PWM_FREQ 20000 // PWM频率
#define PWM_PERIOD (150E6 / (PWM_FREQ * 2)) // PWM周期
#define VOLTAGE_REF 3.3 // 设定电压
#define VOLTAGE_ADC_SCALE (3.3 / 4096) // ADC电压分辨率
#define KP 0.1 // 比例系数
#define KI 0.01 // 积分系数
#define KD 0.001 // 微分系数
#define MAX_DUTY_CYCLE (PWM_PERIOD / 2) // 最大占空比
#define MIN_DUTY_CYCLE 0 // 最小占空比
float voltage_adc = 0; // ADC采样电压
float voltage_error = 0; // 电压误差
float voltage_integral = 0; // 电压积分误差
float voltage_derivative = 0; // 电压微分误差
float duty_cycle = 0; // 互补方波占空比
float duty_cycle_inverted = 0; // 反向互补方波占空比
float pid_output = 0; // PID输出
interrupt void epwm1_isr(void);
void main(void)
{
InitSysCtrl();
InitPieCtrl();
EALLOW;
PieVectTable.EPWM1_INT = &epwm1_isr;
EDIS;
InitEPwmGpio();
InitEPwm1();
IER |= M_INT3;
PieCtrlRegs.PIEIER3.bit.INTx1 = 1;
EINT;
EPwm1Regs.TBCTL.bit.CTRMODE = TB_FREEZE;
EPwm1Regs.TBPRD = PWM_PERIOD;
EPwm1Regs.CMPA.bit.CMPA = PWM_PERIOD / 2;
EPwm1Regs.TBCTL.bit.PHSEN = TB_ENABLE;
EPwm1Regs.TBCTL.bit.HSPCLKDIV = TB_DIV2;
EPwm1Regs.TBCTL.bit.CLKDIV = TB_DIV2;
EPwm1Regs.TBCTL.bit.CTRMODE = TB_COUNT_UPDOWN;
EPwm1Regs.AQCTLA.bit.ZRO = AQ_SET;
EPwm1Regs.AQCTLA.bit.CAU = AQ_CLEAR;
EPwm1Regs.DBCTL.bit.OUT_MODE = DB_FULL_ENABLE;
EPwm1Regs.DBCTL.bit.POLSEL = DB_ACTV_HIC;
EPwm1Regs.DBFED = 50;
EPwm1Regs.DBRED = 50;
EPwm2Regs.TBCTL.bit.CTRMODE = TB_FREEZE;
EPwm2Regs.TBPRD = PWM_PERIOD;
EPwm2Regs.CMPA.bit.CMPA = PWM_PERIOD / 2;
EPwm2Regs.TBCTL.bit.PHSEN = TB_ENABLE;
EPwm2Regs.TBCTL.bit.HSPCLKDIV = TB_DIV2;
EPwm2Regs.TBCTL.bit.CLKDIV = TB_DIV2;
EPwm2Regs.TBCTL.bit.CTRMODE = TB_COUNT_UPDOWN;
EPwm2Regs.AQCTLA.bit.ZRO = AQ_SET;
EPwm2Regs.AQCTLA.bit.CAU = AQ_CLEAR;
EPwm2Regs.DBCTL.bit.OUT_MODE = DB_FULL_ENABLE;
EPwm2Regs.DBCTL.bit.POLSEL = DB_ACTV_HIC;
EPwm2Regs.DBFED = 50;
EPwm2Regs.DBRED = 50;
AdcRegs.ADCCTL1.bit.ADCREFSEL = 0;
AdcRegs.ADCCTL1.bit.ADCBGPWD = 1;
AdcRegs.ADCCTL1.bit.ADCPWDN = 1;
DELAY_US(1000);
AdcRegs.ADCCTL1.bit.ADCENABLE = 1;
AdcRegs.ADCSOC0CTL.bit.CHSEL = 0;
AdcRegs.ADCSOC0CTL.bit.ACQPS = 14;
AdcRegs.INTSEL1N2.bit.INT1SEL = 0;
AdcRegs.INTSEL1N2.bit.INT1E = 1;
AdcRegs.ADCINTFLGCLR.bit.ADCINT1 = 1;
while (1)
{
voltage_adc = AdcResult.ADCRESULT0 * VOLTAGE_ADC_SCALE;
voltage_error = VOLTAGE_REF - voltage_adc;
voltage_integral += voltage_error;
voltage_derivative = voltage_error - pid_output;
pid_output = KP * voltage_error + KI * voltage_integral + KD * voltage_derivative;
duty_cycle += pid_output;
duty_cycle_inverted = PWM_PERIOD - duty_cycle;
if (duty_cycle > MAX_DUTY_CYCLE)
{
duty_cycle = MAX_DUTY_CYCLE;
duty_cycle_inverted = MIN_DUTY_CYCLE;
}
else if (duty_cycle < MIN_DUTY_CYCLE)
{
duty_cycle = MIN_DUTY_CYCLE;
duty_cycle_inverted = PWM_PERIOD - MIN_DUTY_CYCLE;
}
EPwm1Regs.CMPA.bit.CMPA = (Uint16)duty_cycle;
EPwm2Regs.CMPA.bit.CMPA = (Uint16)duty_cycle_inverted;
DELAY_US(1000);
}
}
interrupt void epwm1_isr(void)
{
EPwm1Regs.ETCLR.bit.INT = 1;
PieCtrlRegs.PIEACK.all = PIEACK_GROUP3;
}
```
该程序中,通过EPWM1和EPWM2两个模块实现互补方波的输出,PWM周期和频率可根据实际需求进行调整。程序中使用ADC采样电压值,通过PID算法计算出控制信号,并以此调节互补方波的占空比,使输出电压稳定在设定值。其中,KP、KI和KD分别为比例系数、积分系数和微分系数,可以根据实际需求进行调整。
阅读全文