// file name: svpwm.c
//generate svpwm waveforms
#include "math.h"
#include "DSP281x_Device.h" // DSP281x Headerfile Include File
#include "DSP281x_Examples.h"
#include <stdio.h>
// DSP281x Examples Include File
//global variables
float Tz=2e-4;
int Vdc=600;
//float PI=3.1415;
//int f=50;
// Prototype statements for functions found in this file.
void svpwm(float *ptr,float uapha,float ubeta,float Tswitch,int vdc_link);
void init_eva(void);
void main(void)
{
float cmpr[3]={0,0,0};//cmpr[3]={CMPR1,CMPR2,CMPR3)
float *ptrc=&cmpr[0];//point to the addrs of cmpr
float v_aphar=100;//reference value of vaphar
float v_betar=-20;//reference value of vbetar
//float tstep=0.0000001;
//float tend=5
//float w=2*PI*f;
//float y=sin(w*t);
// Step 1. Initialize System Control:
// PLL--30MHz*10/2, disable WatchDog, enable Peripheral Clocks
// This example function is found in the DSP281x_SysCtrl.c file.
InitSysCtrl();
// Step 2. Initalize GPIO:
// This example function is found in the DSP281x_Gpio.c file and
// illustrates how to set the GPIO to it's default state.
InitGpio();
// Step 3. Clear all interrupts and initialize PIE vector table:
// Disable CPU interrupts
DINT; //asm(" setc INTM")
// Initialize the PIE control registers to their default state.
// The default state is all PIE interrupts disabled and flags
// are cleared.
// This function is found in the DSP281x_PieCtrl.c file.
//InitPieCtrl();
// Disable CPU interrupts and clear all CPU interrupt flags:
IER = 0x0000;
IFR = 0x0000;
// Initialize the PIE vector table with pointers to the shell Interrupt
// Service Routines (ISR).
// This will populate the entire table, even if the interrupt
// is not used in this example. This is useful for debug purposes.
// The shell ISR routines are found in DSP281x_DefaultIsr.c.
// This function is found in DSP281x_PieVect.c.
// InitPieVectTable();
init_eva();
svpwm(ptrc,v_aphar,v_betar,Tz,Vdc);
EvaRegs.CMPR1 = cmpr[0]*75e6;
EvaRegs.CMPR2 = cmpr[1]*75e6;
EvaRegs.CMPR3 = cmpr[2]*75e6;
}
//********************************************
//function: svpwm
//discription:
//********************************************
void svpwm(float *ptr,float uapha,float ubeta,float Tswitch,int vdc_link)
{
int A,B,C,N;
double X,Y,Z,Tx,Ty,T0,Tl,Tm,Th;
if (ubeta>0) A = 1;
else A=0;
if ((1.732051*uapha-ubeta)>0) B = 1;
else B=0;
if ((-1.732051*uapha-ubeta)>0) C = 1;
else C=0;
N=A+2*B+4*C;
X=1.732051*ubeta*Tswitch/vdc_link;
Y=(0.8660*ubeta+1.5*uapha)*Tswitch/vdc_link;
Z=(-0.8660*ubeta+1.5*uapha)*Tswitch/vdc_link;
switch (N)
{
case 1: Tx= Y;Ty=-Z;break;
case 2: Tx=-X;Ty= Y;break;
case 3: Tx= Z;Ty= X;break;
case 4: Tx=-Z;Ty=-X;break;
case 5: Tx= X;Ty=-Y;break;
default: Tx=-Y;Ty= Z;
}
if ((Tx+Ty)>Tswitch)
{
Tx=Tx*Tswitch/(Tx+Ty);
Ty=Ty*Tswitch/(Tx+Ty);
}
T0=(Tswitch-(Tx+Ty))/4;
Tl=(Tswitch+Tx-Ty)/4;/*Tl=T0/4+Tx/2*/
Tm=(Tswitch-Tx+Ty)/4;/*Tm=T0/4+Ty/2*/
Th=(Tswitch+Tx+Ty)/4;/*Th=T0/4+Ty/2+Ty/2*/
switch (N)
{
case 1 :*ptr=Tm; *(ptr+1)=T0; *(ptr+2)=Th;break;
case 2 :*ptr=T0; *(ptr+1)=Th; *(ptr+2)=Tm;break;
case 3 :*ptr=T0; *(ptr+1)=Tl; *(ptr+2)=Th;break;
case 4 :*ptr=Th; *(ptr+1)=Tm; *(ptr+2)=T0;break;
case 5 :*ptr=Th; *(ptr+1)=T0; *(ptr+2)=Tl;break;
default :*ptr=Tl; *(ptr+1)=Th; *(ptr+2)=T0;
}
}
void init_eva()
{
// EVA Configure T1PWM, T2PWM, PWM1-PWM6
// Initalize the timers
// Initalize EVA Timer1
EvaRegs.T1PR = 0x1D4C; // Timer1 period HSPCLK/fz=75E6/5E3=15000,T1PR=15000/2=0X1D4C
//EvaRegs.T1CMPR = 0x3C00; // Timer1 compare
EvaRegs.T1CNT = 0x0000; // Timer1 counter
// TMODE = continuous up/down
// Timer enable
// Timer compare enable
//EVA_INPUT_CLK=HSPCLK/1=75MHZ
EvaRegs.T1CON.all = 0x0842;
// Initalize EVA Timer2
//EvaRegs.T2PR = 0x0FFF; // Timer2 period
// EvaRegs.T2CMPR = 0x03C0; // Timer2 compare
//EvaRegs.T2CNT = 0x0000; // Timer2 counter
// TMODE = continuous up/down
// Timer enable
// Timer compare enable
//EvaRegs.T2CON.all = 0x1042;
// Setup T1PWM and T2PWM
// Drive T1/T2 PWM by compare logic
EvaRegs.GPTCONA.bit.TCMPOE = 1;
// Polarity of GP Timer 1 Compare = Active low
EvaRegs.GPTCONA.bit.T1PIN = 1;
// Polarity of GP Timer 2 Compare = Active high
EvaRegs.GPTCONA.bit.T2PIN = 2;
// Enable compare for PWM1-PWM6
EvaRegs.CMPR1 = 0x0C00;
EvaRegs.CMPR2 = 0x3C00;
EvaRegs.CMPR3 = 0xFC00;
// Compare action control. Action that takes place
// on a cmpare event
// output pin 1 CMPR1 - active high
// output pin 2 CMPR1 - active low
// output pin 3 CMPR2 - active high
// output pin 4 CMPR2 - active low
// output pin 5 CMPR3 - active high
// output pin 6 CMPR3 - active low
EvaRegs.ACTRA.all = 0x0666;
EvaRegs.DBTCONA.all = 0x0FF8; // deadband time is 3.2us
//compare enable
//reload when T1CNT=0
//hardware svpwm mode disable
//FCMPOE=1 ENABLE
EvaRegs.COMCONA.all = 0x8200;
}
联系客服