基于DSP的pwm整流器滑膜控制C代码
时间: 2024-01-24 11:02:29 浏览: 94
以下是一个基于TMS320F28335 DSP的PWM整流器滑模控制的C语言代码,你可以根据需要进行修改和适配:
```c
#include "DSP2833x_Device.h"
#define PWM_DUTY_CYCLE 1000 // PWM占空比
#define PWM_PERIOD 1999 // PWM周期
#define KP 0.5 // 比例系数
#define KI 0.01 // 积分系数
#define KD 0.1 // 微分系数
float error = 0, last_error = 0, integral = 0;
void GPIO_Configuration(void);
void PWM_Configuration(void);
void SMC_Configuration(void);
void main(void)
{
InitSysCtrl();
InitPieCtrl();
IER = 0x0000;
IFR = 0x0000;
InitPieVectTable();
GPIO_Configuration();
PWM_Configuration();
SMC_Configuration();
while (1)
{
// 你的代码逻辑
float reference_voltage = 10; // 参考电压
float feedback_voltage = 5; // 反馈电压
error = reference_voltage - feedback_voltage;
integral += error;
float derivative = error - last_error;
float control_signal = KP * error + KI * integral + KD * derivative;
last_error = error;
if (control_signal > PWM_PERIOD)
{
control_signal = PWM_PERIOD;
}
else if (control_signal < 0)
{
control_signal = 0;
}
EPwm1Regs.CMPA.bit.CMPA = (Uint16)control_signal;
}
}
void GPIO_Configuration(void)
{
EALLOW;
GpioCtrlRegs.GPAMUX1.bit.GPIO0 = 0;
GpioCtrlRegs.GPADIR.bit.GPIO0 = 0;
EDIS;
}
void PWM_Configuration(void)
{
EPwm1Regs.TBPRD = PWM_PERIOD;
EPwm1Regs.TBPHS.bit.TBPHS = 0;
EPwm1Regs.TBCTL.bit.CTRMODE = 0;
EPwm1Regs.TBCTL.bit.PHSEN = 0;
EPwm1Regs.TBCTL.bit.PRDLD = 0;
EPwm1Regs.TBCTL.bit.SYNCOSEL = 3;
EPwm1Regs.CMPCTL.bit.SHDWAMODE = 1;
EPwm1Regs.CMPCTL.bit.SHDWBMODE = 1;
EPwm1Regs.CMPCTL.bit.LOADAMODE = 0;
EPwm1Regs.CMPCTL.bit.LOADBMODE = 0;
EPwm1Regs.CMPA.bit.CMPA = PWM_DUTY_CYCLE - 1;
EPwm1Regs.CMPB.bit.CMPB = 0;
EPwm1Regs.AQCTLA.bit.CAU = 2;
EPwm1Regs.AQCTLA.bit.CAD = 1;
EPwm1Regs.AQCTLB.bit.CBU = 2;
EPwm1Regs.AQCTLB.bit.CBD = 1;
EPwm1Regs.DBCTL.bit.IN_MODE = 0;
EPwm1Regs.DBCTL.bit.OUT_MODE = 3;
EPwm1Regs.DBCTL.bit.POLSEL = 2;
EPwm1Regs.DBCTL.bit.HALFCYCLE = 1;
EPwm1Regs.DBRED = 0;
EPwm1Regs.DBFED = 0;
EPwm1Regs.TZSEL.bit.OSHT1 = 0;
EPwm1Regs.TZSEL.bit.OSHT2 = 0;
EPwm1Regs.TZSEL.bit.OSHT3 = 0;
EPwm1Regs.TZCTL.bit.TZA = 3;
EPwm1Regs.TZCTL.bit.TZB = 3;
EPwm1Regs.TZCLR.bit.OST = 1;
EPwm1Regs.TZFRC.bit.OST = 1;
EPwm1Regs.TBCTL.bit.CTRMODE = 0;
EPwm1Regs.TBCTL.bit.PHSEN = 0;
EPwm1Regs.TBCTL.bit.PRDLD = 0;
EPwm1Regs.TBCTL.bit.SYNCOSEL = 3;
}
void SMC_Configuration(void)
{
EPwm1Regs.DBCTL.bit.OUT_MODE = 3;
EPwm1Regs.DBCTL.bit.POLSEL = 2;
EPwm1Regs.DBCTL.bit.IN_MODE = 0;
EPwm1Regs.DBCTL.bit.HALFCYCLE = 0;
EPwm1Regs.DBRED = 100;
EPwm1Regs.DBFED = 100;
}
```
这个代码实现了一个基于滑模控制的PWM整流器,使用了TMS320F28335 DSP的EPwm1模块来生成PWM波形,并且使用了EPwm1的A和B通道来控制输出。在每个循环迭代中,代码计算出控制信号并将其设定为PWM的占空比。
阅读全文