F28035 三相 SPWM程序
时间: 2024-05-13 14:16:10 浏览: 117
以下是基于F28035芯片的三相SPWM程序的示例代码:
```c
#include "DSP2833x_Device.h"
#include "DSP2833x_Examples.h"
#define PWM_FREQUENCY 10000
#define PI 3.14159265358979323846
volatile struct EPWM_REGS *ePWM[] = {&EPwm1Regs, &EPwm2Regs, &EPwm3Regs};
void InitEPwm()
{
int i;
for (i = 0; i < 3; i++)
{
(*ePWM[i]).TBCTL.bit.CTRMODE = TB_COUNT_UP;
(*ePWM[i]).TBCTL.bit.PHSEN = TB_DISABLE;
(*ePWM[i]).TBCTL.bit.SYNCOSEL = TB_CTR_ZERO;
(*ePWM[i]).TBCTL.bit.HSPCLKDIV = TB_DIV1;
(*ePWM[i]).TBCTL.bit.CLKDIV = TB_DIV1;
(*ePWM[i]).TBPRD = (int)((SYSCLKOUT / (PWM_FREQUENCY * 2)) - 1);
(*ePWM[i]).CMPA.bit.CMPA = (*ePWM[i]).TBPRD / 2;
(*ePWM[i]).CMPB.bit.CMPB = (*ePWM[i]).TBPRD / 2;
(*ePWM[i]).CMPCTL.bit.SHDWAMODE = CC_SHADOW;
(*ePWM[i]).CMPCTL.bit.SHDWBMODE = CC_SHADOW;
(*ePWM[i]).CMPCTL.bit.LOADAMODE = CC_CTR_ZERO;
(*ePWM[i]).CMPCTL.bit.LOADBMODE = CC_CTR_ZERO;
(*ePWM[i]).AQCTLA.bit.CAU = AQ_CLEAR;
(*ePWM[i]).AQCTLA.bit.CAD = AQ_SET;
(*ePWM[i]).AQCTLB.bit.CBU = AQ_CLEAR;
(*ePWM[i]).AQCTLB.bit.CBD = AQ_SET;
(*ePWM[i]).DBCTL.bit.OUT_MODE = DB_FULL_ENABLE;
(*ePWM[i]).DBCTL.bit.POLSEL = DB_ACTV_HIC;
(*ePWM[i]).DBCTL.bit.IN_MODE = DBA_ALL;
(*ePWM[i]).DBRED.bit.DBRED = 0;
(*ePWM[i]).DBFED.bit.DBFED = 0;
(*ePWM[i]).ETSEL.bit.SOCAEN = 0;
(*ePWM[i]).ETSEL.bit.SOCASEL = ET_CTR_ZERO;
(*ePWM[i]).ETPS.bit.SOCAPRD = ET_1ST;
}
}
void SPWM(float Vref, float freq, float t)
{
float theta, t_sample, V1, V2, V3;
int i;
for (i = 0; i < 3; i++)
{
theta = (float)i * 2.0 * PI / 3.0;
t_sample = t + theta / freq;
V1 = Vref * sin(2.0 * PI * freq * t_sample);
V2 = Vref * sin(2.0 * PI * freq * t_sample - 2.0 * PI / 3.0);
V3 = Vref * sin(2.0 * PI * freq * t_sample + 2.0 * PI / 3.0);
if (V1 >= 0)
{
(*ePWM[i]).AQCTLA.bit.ZRO = AQ_SET;
(*ePWM[i]).AQCTLA.bit.CAU = AQ_CLEAR;
}
else
{
(*ePWM[i]).AQCTLA.bit.ZRO = AQ_CLEAR;
(*ePWM[i]).AQCTLA.bit.CAU = AQ_SET;
}
if (V2 >= 0)
{
(*ePWM[i]).AQCTLB.bit.ZRO = AQ_SET;
(*ePWM[i]).AQCTLB.bit.CBU = AQ_CLEAR;
}
else
{
(*ePWM[i]).AQCTLB.bit.ZRO = AQ_CLEAR;
(*ePWM[i]).AQCTLB.bit.CBU = AQ_SET;
}
if (V3 >= 0)
{
(*ePWM[i]).CMPA.bit.CMPA = (int)((float)(*ePWM[i]).TBPRD / 2.0 * (1.0 - V3 / Vref));
(*ePWM[i]).AQCTLB.bit.CAD = AQ_SET;
}
else
{
(*ePWM[i]).CMPA.bit.CMPA = (int)((float)(*ePWM[i]).TBPRD / 2.0 * (1.0 + V3 / Vref));
(*ePWM[i]).AQCTLB.bit.CAD = AQ_CLEAR;
}
}
}
void main()
{
InitSysCtrl();
InitEPwm();
while (1)
{
float Vref = 220.0;
float freq = 50.0;
float t = 0.0;
while (t < 1.0 / freq)
{
SPWM(Vref, freq, t);
t += 1.0 / (PWM_FREQUENCY * 6.0);
DELAY_US(100);
}
}
}
```
该程序实现了一个基于三角波的SPWM,包括初始化和SPWM函数。在SPWM函数中,计算了每个电平的电压并根据它们的符号设置PWM占空比。此外,根据每个电平的幅值调整了相应的PWM占空比,以产生所需的电压波形。
需要注意的是,该代码是一个简单的示例,可能需要根据具体应用进行修改和优化。
阅读全文