|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?注册
x
本帖最后由 cjsb37 于 2013-4-29 09:06 编辑
// Step 0. Include required header files
// DSP28_Device.h: device specific definitions #include statements for
// all of the peripheral .h definition files.
// DSP28_Example.h is specific for the given example.
#include "DSP28_Device.h"
#include "math.h"
#include "stdlib.h"
#include "stdio.h"
#define PI 3.1416//π
#define fp 5000 //载波频率5kHZ
#define fc 50 //调制波频率50HZ
#define M 0.8 //调制比
interrupt void eva_timer1_isr(void);
interrupt void eva_timer2_isr(void);
double cosdata[51]; //storage cos values
double tion[51]; //storage CMPR values
double CMPR[51]; //CMPR values
Uint16 drag1;
Uint16 drag2;
Uint16 TC=0x7530; //TC=30000
int i;
int N=0;
double deltaT;
void main(void)
{
// Step 1. Initialize System Control registers, PLL, WatchDog, Clocks to default state:
// This function is found in the DSP28_SysCtrl.c file.
InitSysCtrl();
// Step 2. Select GPIO for the device or for the specific application:
// This function is found in the DSP28_Gpio.c file.
// InitGpio(); // Skip for this test
// Initalize GPIO for this test here
EALLOW;
// Enable PWM pins
GpioMuxRegs.GPAMUX.all = 0x00FF; // EVA PWM
EDIS;
// Step 3. Initialize PIE vector table:
// The PIE vector table is initialized with pointers to shell Interrupt
// Service Routines (ISR). The shell routines are found in DSP28_DefaultIsr.c.
// Insert user specific ISR code in the appropriate shell ISR routine in
// the DSP28_DefaultIsr.c file.
// Disable and clear all CPU interrupts:
DINT;
IER = 0x0000;
IFR = 0x0000;
// Initialize Pie Control Registers To Default State:
// This function is found in the DSP28_PieCtrl.c file.
InitPieCtrl();
// Initialize the PIE Vector Table To a Known State:
// This function is found in DSP28_PieVect.c.
// This function populates the PIE vector table with pointers
// to the shell ISR functions found in DSP28_DefaultIsr.c.
InitPieVectTable();
// Step 4. Initialize all the Device Peripherals to a known state:
// This function is found in DSP28_InitPeripherals.c
// InitPeripherals();
//initalize interrupts count values to 0
drag1=0;
drag2=0;
// EVA Configure T1PWM, T2PWM, PWM1-PWM6
// Step 1 Initalize the timers
// fc=5kHZ,fs=50HZ,fdsp=150MHZ,Tc=30000*Ttimer
// Initalize EVA Timer1
EvaRegs.GPTCONA.all=0;
EvaRegs.T1PR = (int)(TC/2); // Timer1 period
EvaRegs.T1CMPR = 0x0000; // Timer1 compare
EvaRegs.T1CNT = 0x0001; // Timer1 counter
// TMODE = continuous up/down
// Timer enable
// Timer compare enable
EvaRegs.T1CON.all = 0x0842; //Timer1 continuous up/down mode
// Initalize EVA Timer2
EvaRegs.T2PR = (int)(TC/2); // Timer2 period
EvaRegs.T2CMPR =0x0000; // Timer2 compare
EvaRegs.T2CNT = 0x0001; // Timer2 counter
// TMODE = continuous up/down
// Timer enable
// Timer compare enable
EvaRegs.T2CON.all = 0x0842; //Timer2 continuous up/down mode
// Step 2 Setup T1PWM and T2PWM
// Drive T1/T2 PWM by compare logic
EvaRegs.GPTCONA.bit.TCOMPOE = 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;
// Step 3 Enable compare for PWM1-PWM6
EvaRegs.CMPR1 = (int)(TC/2);
EvaRegs.CMPR2 = (int)(TC/2);
EvaRegs.CMPR3 = (int)(TC/2);
// 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 = 0x0000; // deadband=1.6us
EvaRegs.COMCONA.all = 0xA600;//使能比较,下溢重载
EvaRegs.EVAIMRA.bit.T1PINT=1;//使能通用定时器周期中断
EvaRegs.EVAIFRA.bit.T1PINT=1;//clear interrupt
EvaRegs.EVAIMRB.bit.T2PINT=0;//disable Timer2 周期中断
EvaRegs.EVAIFRB.bit.T2PINT=1;//clear interrupt
EALLOW;
PieVectTable.T1PINT=&eva_timer1_isr;
PieVectTable.T2PINT=&eva_timer2_isr;
EDIS;
PieCtrlRegs.PIEIFR2.bit.INTx4=1;
PieCtrlRegs.PIEIER3.bit.INTx1=1;
IER|=(M_INT2|M_INT3);
EINT;//enable globl interrupts
ERTM;
// storage CMPR values
N=fp/(2*fc);
deltaT=0.0002;
for(i=0;i<N+1;i++)
cosdata=cos(i*PI/N);
for(i=0;i<N;i++)
{
tion[i+1]=cosdata-cosdata[i+1];
tion[i+1]=fabs(tion[i+1]);
tion[i+1]=M*tion[i+1];
tion[i+1]=tion[i+1]/2;
tion[i+1]=tion[i+1]/PI;
tion[i+1]=tion[i+1]/fc;
tion[i+1]=-tion[i+1];
tion[i+1]=tion[i+1]+(deltaT);
tion[i+1]=0.5*tion[i+1];
CMPR[i+1]=tion[i+1]*150000000;
// tion=150000000000/2*[1/fp-M/(2*PI*fc)*(cosdata-cosdata[i+1])];
}
for(;;);
}
interrupt void eva_timer1_isr(void)
{
drag1++;
if(drag1<51)
{
EvaRegs.CMPR1=(int)CMPR[drag1];
EvaRegs.EVAIFRA.bit.T1PINT=1;
}
else
{
EvaRegs.CMPR1=(int)(TC/2);
drag1=0;
EvaRegs.EVAIFRA.bit.T1PINT=1;
EvaRegs.EVAIMRB.bit.T2PINT=1;
EvaRegs.EVAIMRA.bit.T1PINT=0;
}
}
interrupt void eva_timer2_isr(void)
{
drag2++;
if(drag2<51)
{
EvaRegs.CMPR2=(int)CMPR[drag2];
EvaRegs.EVAIFRB.bit.T2PINT=1;
}
else
{
EvaRegs.CMPR2=(int)(TC/2);
drag2=0;
EvaRegs.EVAIFRB.bit.T2PINT=1;
EvaRegs.EVAIMRB.bit.T2PINT=0;
EvaRegs.EVAIMRA.bit.T1PINT=1;
}
}
SPWM的程序,现在是进不了中断,应该还有其他错误,本人初学,希望各位大侠见教了!
|
|