F28035 电机控制SPWM程序
时间: 2023-12-02 11:04:22 浏览: 74
以下是一个基于F28035的三相电机控制SPWM程序的示例:
```c
#include "F2803x_Device.h"
#include "F2803x_Examples.h"
#define PWM_FREQ 20000
#define PWM_PERIOD (1.0 / PWM_FREQ)
#define PWM_TBCLK 150E6
#define MOTOR_POLES 4
#define MOTOR_RATED_SPEED 3000.0 // rpm
#define PWM_RESOLUTION 1000.0
float32 Vref = 0.0;
float32 Vdc = 24.0;
void InitEPwm1(void);
void InitEPwm2(void);
void InitEPwm3(void);
void main(void)
{
DisableDog();
InitSysCtrl();
InitEPwm1();
InitEPwm2();
InitEPwm3();
EINT; // Enable Global interrupt INTM
ERTM; // Enable Global realtime interrupt DBGM
while (1)
{
// TODO: Implement motor control algorithm here
}
}
void InitEPwm1(void)
{
EPwm1Regs.TBPRD = (Uint16)(PWM_TBCLK / PWM_FREQ);
EPwm1Regs.TBCTL.bit.CTRMODE = TB_COUNT_UPDOWN;
EPwm1Regs.TBCTL.bit.PHSEN = TB_DISABLE;
EPwm1Regs.TBCTL.bit.SYNCOSEL = TB_CTR_ZERO;
EPwm1Regs.TBCTL.bit.HSPCLKDIV = TB_DIV1;
EPwm1Regs.TBCTL.bit.CLKDIV = TB_DIV1;
EPwm1Regs.TBCTL.bit.FREE_SOFT = TB_STOP;
EPwm1Regs.AQCTLA.bit.ZRO = AQ_SET;
EPwm1Regs.AQCTLA.bit.CAU = AQ_CLEAR;
EPwm1Regs.CMPA.half.CMPA = 0;
EPwm1Regs.ETSEL.bit.INTSEL = ET_CTR_ZERO;
EPwm1Regs.ETSEL.bit.INTEN = 1;
EPwm1Regs.ETPS.bit.INTPRD = ET_1ST;
}
void InitEPwm2(void)
{
EPwm2Regs.TBPRD = (Uint16)(PWM_TBCLK / PWM_FREQ);
EPwm2Regs.TBCTL.bit.CTRMODE = TB_COUNT_UPDOWN;
EPwm2Regs.TBCTL.bit.PHSEN = TB_DISABLE;
EPwm2Regs.TBCTL.bit.SYNCOSEL = TB_CTR_ZERO;
EPwm2Regs.TBCTL.bit.HSPCLKDIV = TB_DIV1;
EPwm2Regs.TBCTL.bit.CLKDIV = TB_DIV1;
EPwm2Regs.TBCTL.bit.FREE_SOFT = TB_STOP;
EPwm2Regs.AQCTLA.bit.ZRO = AQ_SET;
EPwm2Regs.AQCTLA.bit.CAU = AQ_CLEAR;
EPwm2Regs.CMPA.half.CMPA = 0;
EPwm2Regs.ETSEL.bit.INTSEL = ET_CTR_ZERO;
EPwm2Regs.ETSEL.bit.INTEN = 1;
EPwm2Regs.ETPS.bit.INTPRD = ET_1ST;
}
void InitEPwm3(void)
{
EPwm3Regs.TBPRD = (Uint16)(PWM_TBCLK / PWM_FREQ);
EPwm3Regs.TBCTL.bit.CTRMODE = TB_COUNT_UPDOWN;
EPwm3Regs.TBCTL.bit.PHSEN = TB_DISABLE;
EPwm3Regs.TBCTL.bit.SYNCOSEL = TB_CTR_ZERO;
EPwm3Regs.TBCTL.bit.HSPCLKDIV = TB_DIV1;
EPwm3Regs.TBCTL.bit.CLKDIV = TB_DIV1;
EPwm3Regs.TBCTL.bit.FREE_SOFT = TB_STOP;
EPwm3Regs.AQCTLA.bit.ZRO = AQ_SET;
EPwm3Regs.AQCTLA.bit.CAU = AQ_CLEAR;
EPwm3Regs.CMPA.half.CMPA = 0;
EPwm3Regs.ETSEL.bit.INTSEL = ET_CTR_ZERO;
EPwm3Regs.ETSEL.bit.INTEN = 1;
EPwm3Regs.ETPS.bit.INTPRD = ET_1ST;
}
interrupt void epwm1_isr(void)
{
// Clear interrupt flag
EPwm1Regs.ETCLR.bit.INT = 1;
// Calculate duty cycle of PWM signals
float32 dutyA = 0.5 + 0.5 * Vref / Vdc;
float32 dutyB = 0.5 + 0.5 * Vref / Vdc * sin(2.0 * PI / 3.0);
float32 dutyC = 0.5 + 0.5 * Vref / Vdc * sin(4.0 * PI / 3.0);
// Convert duty cycle to compare value
Uint16 CMPA = (Uint16)(dutyA * PWM_RESOLUTION * EPwm1Regs.TBPRD);
Uint16 CMPB = (Uint16)(dutyB * PWM_RESOLUTION * EPwm1Regs.TBPRD);
Uint16 CMPC = (Uint16)(dutyC * PWM_RESOLUTION * EPwm1Regs.TBPRD);
// Update compare values
EPwm1Regs.CMPA.half.CMPA = CMPA;
EPwm2Regs.CMPA.half.CMPA = CMPB;
EPwm3Regs.CMPA.half.CMPA = CMPC;
}
```
在上面的代码中,我们初始化了三个PWM模块(EPwm1、EPwm2和EPwm3),并且实现了一个中断函数epwm1_isr(),用于执行SPWM算法,并更新PWM信号的占空比。在while(1)循环中,您可以实现自己的电机控制算法,例如PID控制、矢量控制等。
需要注意的是,上面的代码仅提供了一个基本的SPWM实现,您需要根据实际应用来适当地调整PWM频率、占空比和分辨率等参数。
阅读全文