我买了一本书,《电动机的DSP控制技术与实践》,任志斌写的。
我参考书里的例子,写了个SPWM波控制电机的程序,可是电机不转。
请高手看看,哪里错了?谢谢!
//规则采样法SPWM的输出
#include "DSP281x_Device.h"
#include "system.h"
#include "stdio.h"
#include "math.h"
#include "float.h"
#define NUM 500
#define PI 3.1415926
interrupt void eva_timer1_isr(void);
float tt;
float t;
float ft;
float M;
unsigned int direction;
float cmpr=0;
float fr;
unsigned int N;
unsigned int j;
unsigned int i=0;
double r1[NUM];
double r2[NUM];
double r3[NUM];
void init_eva()
{
//Initalize EVA Timer1&Configure PWM1-PWM6
//EvaRegs.T1PR=tt; //period=2775*2/25M=222us(连续增减)
EvaRegs.T1PR=2775;
EvaRegs.T1CNT=0; //Timer1 counter
EvaRegs.GPTCONA.bit.T1CMPOE=1; //enable timer compare
EvaRegs.GPTCONA.bit.T1PIN=1; //timer compare output
EvaRegs.EVAIMRA.bit.T1PINT=1; //enable Timer1 period interrupt
EvaRegs.EVAIFRA.bit.T1PINT=1; //clear Timer1 period interrupt flag
/*********************************************************************
TMODE=continuous up/down count
Input clock prescaler=X/1 (X=HSPCLK)=25MHz
Timer enable; Timer compare enable
*********************************************************************/
EvaRegs.T1CON.all=0x0842;
//EvaRegs.T1CON.all=0x1040;
//EvaRegs.T2CON.bit.TENABLE=1;
//Enable compare for PWM1-PWM6
EvaRegs.CMPR1=500;
EvaRegs.CMPR2=500;
EvaRegs.CMPR3=500;
EvaRegs.ACTRA.all=0X666; //PWM1,2,3,4,5,6
//EvaRegs.DBTCONA.all=0X05FC; //死区1us (1/(150M/32))*5
EvaRegs.DBTCONA.all=0X0000;
//EvaRegs.COMCONA.all=0X8E00; //Enable full compare 1010 0110 0000 0000
EvaRegs.COMCONA.all=0Xa600;
}
void main(void)
{
InitSysCtrl(); //系统初始化
EALLOW;
SysCtrlRegs.HISPCP.all=0x3; //HSPCLK=SYSCLKOUT/2*3=150/(2*3)=25MHz(AD clock)
EDIS;
//通用IO初始化
EALLOW;
GpioMuxRegs.GPAMUX.all=0x00FF; //EVA PWM 1-6 pins and set Cap1-3 to IO pins.
GpioMuxRegs.GPBMUX.bit.CAP4Q1_GPIOB8=0; //A
GpioMuxRegs.GPBMUX.bit.CAP5Q2_GPIOB9=0; //B
GpioMuxRegs.GPADIR.all=0X00FF; //Set IO pins to be input pins
GpioMuxRegs.GPBDIR.all=0x0;
EDIS;
//禁止中断
DINT;
IER=0X0000;
IFR=0X0000;
//初始化PIE控制器及PIE向量表
//InitEv();
InitPieCtrl();
InitPieVectTable();
EALLOW; //设置中断向量表
//PieVectTable.T1UFINT=&eva_T1UFINT_ISR;
//PieVectTable.CAPINT1=&capint1_ISR;
PieVectTable.T1PINT=&eva_timer1_isr;
EDIS; //This is needed to disable write to EALLOW protected registers
init_eva();
//开中断
//Enable PIE group 2 interrupt 4 for T1PINT
PieCtrlRegs.PIEIER2.all=M_INT4;
//Enable CPU INT2 for T1PINT
IER|=M_INT2; //开中断2
//Enable global Interrupts and higher priority real-time debug events:
//////////////////////////////
//程序变量赋值
tt=2775; //载波对应的时间周期计算的T1PR值
t=tt/2;
ft=4500; //4.5kHz载波频率
M=0.9; //调制度
direction=1; //正转
cmpr=50;
if(cmpr<5) //停掉电动机
{
cmpr=1;
EvaRegs.CMPR1=0;
EvaRegs.CMPR2=0;
EvaRegs.CMPR3=0;
}
else
{
fr=cmpr; //基波频率(0-51Hz)
N=ft/fr; //在一个调制波正弦波周期里的载波个数
if(direction==1)
{
for(j=0;j<N;j++)
{
r1[j]=t*(1+M*sin(PI/N+2*PI/3+j*tt)); //计算各个载波周期里脉冲宽度
r2[j]=t*(1+M*sin(PI/N+j*tt));
r3[j]=t*(1+M*sin(PI/N-2*PI/3+j*tt));
}
}
else
{
for(j=0;j<N;j++)
{
r2[j]=t*(1+M*sin(PI/N+2*PI/3+j*tt)); //计算各个载波周期里脉冲宽度
r1[j]=t*(1+M*sin(PI/N+j*tt));
r3[j]=t*(1+M*sin(PI/N-2*PI/3+j*tt));
}
}
}
EINT; //Enable Global interrupt INTM
ERTM; //Enable Global realtime interrupt DBGM
for(;;)
{
}
}
//下溢中断服务程序
interrupt void eva_timer1_isr(void)
{
i++;
if(i>N)
i=0;
EvaRegs.CMPR1=(tt-r1[i])/2;
EvaRegs.CMPR2=(tt-r2[i])/2;
EvaRegs.CMPR3=(tt-r3[i])/2;
//Enable more interrupt from this timer
EvaRegs.EVAIMRA.bit.T1PINT=1;
//Note: To be safe,use a mask value to write to the entire
//EVAIFRA register.Writing to one bit will cause a read-modify-write
//operation that may have the result of writing 1's to clear
//bit other then those intended.
EvaRegs.EVAIFRA.all=BIT7;
//Acknowledeg interrupt to receive more interrupts from PIE group 2
PieCtrlRegs.PIEACK.all=PIEACK_GROUP2;
}
|