#include "DSP28_Device.h"
//RampGen
RMPCNTL rc1 = RMPCNTL_DEFAULTS;
RAMPGEN rg1 = RAMPGEN_DEFAULTS;
//AD
ILEG2DCBUSMEAS ilg2_vdc1 = ILEG2DCBUSMEAS_DEFAULTS;
//clarke
CLARKE clarke = CLARKE_DEFAULTS;
//park
PARK park = PARK_DEFAULTS;
//ipark
IPARK ipark1 = IPARK_DEFAULTS;
//svpwm
SVGENDQ svgendq = SVGENDQ_DEFAULTS;
//eva
EVA eva = EVA_DEFAULTS;
//qep
QEP qep1 = QEP_DEFAULTS;
//speed
SPEED_MEAS_QEP speed1 = SPEED_MEAS_QEP_DEFAULTS;
//sci
SCI sci= SCI_DEFAULTS;
//system
SYSTEM system ;
interrupt void T1UFINT_ISR(void);
interrupt void QepISR(void);
interrupt void SCIRXINTB_ISR(void) ; // SCI-B
void SystemParameterInit(void);
void ShowDisp(void) ; //更新显示
int *ExRamStart=(int *)0x100000;
//hall code
unsigned short hall_angel[7]={0,5461,27306,0,16384,10922,21845};
unsigned short right_inc=0;
//show disp
unsigned short disp_inc=0;
// These are defined by the linker (see F2812.cmd)
extern Uint16 RamfuncsLoadStart;
extern Uint16 RamfuncsLoadEnd;
extern Uint16 RamfuncsRunStart;
void main(void)
{
InitSysCtrl(); /*初始化系统时钟*/
DINT; //关中断与清除中断标志
IER = 0x0000;
IFR = 0x0000;
InitPieCtrl(); /*初始化PIE控制寄存器*/
InitPieVectTable(); /*初始化PIE矢量表*/
//将需要再RAM中运行的程序复制到RAM
MemCopy(&RamfuncsLoadStart, &RamfuncsLoadEnd, &RamfuncsRunStart);
//初始化flash存储器
InitFlash();
InitGpio(); /*初始化IO口*/
eva.Init(&eva); /*初始化EV*/
ilg2_vdc1.init(); /*初始化ad*/
InitSci(); /*初始化SCIb寄存器*/
EALLOW; /*设置中断服务程序入口地址*/
PieVectTable.T1UFINT = &T1UFINT_ISR;
PieVectTable.RXBINT = &SCIRXINTB_ISR; //设置串口B接受中断的中断向量
EDIS;
/*使能位于PIE中组2的第6个中断 定时器1下溢中断*/
PieCtrl.PIEIER2.bit.INTx6=1;
PieCtrl.PIEIER3.all = M_INT7;
// Enable CAP3 interrupt bits for GP timer 2
EvaRegs.EVAIMRC.bit.CAP3INT = 1;
EvaRegs.EVAIFRC.bit.CAP3INT = 1;
/*开中断*/
IER |= (M_INT2 | M_INT3);
IER |= M_INT9; //SCI //允许串口中断
EINT;
ERTM;
SystemParameterInit();
for(;;)
{
if(disp_inc>200)
{
disp_inc=0;
ShowDisp(); //0.01s
}
if(system.run_switch==0)
eva.Close(&eva); //关闭EVA输出
else
eva.Open(&eva); //打开EVA输出
}
}
//===========================================================================
// 定时器1下溢中断服务程序.
//===========================================================================
interrupt void T1UFINT_ISR(void) // EV-A
{
static int time_inc=0;
static int speed_inc=0;
PieCtrl.PIEACK.bit.ACK2=1;
EvaRegs.EVAIFRA.bit.T1UFINT=1; //清中断标志位
disp_inc++;
time_inc++;
speed_inc++;
//hall_read
system.hall_code= (GpioDataRegs.GPBDAT.all>>8)&0x7 ;
//run statue
switch(system.run_statue)
{
case 0:
break;
case 1: //测试状态 需要参数:OpenLoop_f Vq
break;
case 2: //hall 开环
rc1.TargetValue = system.Vq;
rc1.calc(&rc1);
ipark1.Qs = rc1.SetpointValue;
ipark1.Ds = 0;
ipark1.Angle = hall_angel[system.hall_code] ;
ipark1.calc(&ipark1);
break;
case 3: //编码器开环
break;
case 4: //速度闭环
break;
case 5: //位置闭环
break;
case 6: //VVVF开环
break;
default:
system.run_statue = 0;
}
//svpwm
//svgendq模块 根据Ualpha Ubeta 产生比较器需要的TA TB TC
svgendq.Ualpha = ipark1.Alpha;
svgendq.Ubeta = ipark1.Beta ;
svgendq.calc(&svgendq) ;
//ev模块
eva.Ta = _IQtoIQ15(svgendq.Ta);
eva.Tb = _IQtoIQ15(svgendq.Tb);
eva.Tc = _IQtoIQ15(svgendq.Tc);
eva.SetPwm(&eva);
}
void SystemParameterInit(void)
{
rg1.StepAngleMax = _IQ((float)128/20000);
// 初始化测速模块参数
speed1.K1 = _IQ21(20000/(20*50));
speed1.K2 = _IQ(1/(1+(2*3.14*20)/1000)); // Low-pass cut-off frequency
speed1.K3 = _IQ(1)-speed1.K2;
speed1.BaseRpm = 3000;
//初始化系统参数
system.coder_LineEncoder = 2500;
system.coder_PolePairs = 4;
system.run_switch=1;
system.run_statue=2;
system.Vq=0;
}
void ShowDisp(void) //更新显示
{
static unsigned int i=0;
static unsigned short zero_flag=0;
static unsigned short disp_flag_inc=0;
short tmp;
if(system.disp_flag) // 显示输出占空比
{
// disp_flag_inc++; //0.01s
if(disp_flag_inc>150) // 1.5S
system.disp_flag=0;
if(system.Vq<0)
tmp = -system.Vq;
else
tmp = system.Vq;
switch(i)
{
case 0:
i++;
ScibRegs.SCITXBUF =0xb+(3<<5); // 显示 S
break;
case 1:
if(system.Vq>=0) ScibRegs.SCITXBUF =16+(2<<5); // 显示 灭
else ScibRegs.SCITXBUF =24+(2<<5); //显示 -
i++;
break;
case 2:
tmp = tmp /(_IQ(0.1));
ScibRegs.SCITXBUF =tmp+(1<<5);
i++;
break;
case 3:
tmp = tmp /(_IQ(0.01));
tmp = tmp%10;
ScibRegs.SCITXBUF =tmp;
i=0;
break;
default:
i=0;
break;
}
}
else // 显示速度
{
disp_flag_inc=0;
if(speed1.SpeedRpm<0)
tmp =- speed1.SpeedRpm;
else
tmp = speed1.SpeedRpm;
switch(i)
{
case 0:
i++;
tmp = tmp/1000;
if(tmp>0)
{
ScibRegs.SCITXBUF =tmp+(3<<5);
zero_flag=0;
}
else
{
ScibRegs.SCITXBUF =16+(3<<5); // 显示 灭
zero_flag=1;
}
break;
case 1:
tmp = tmp/100;
tmp = tmp%10;
if(tmp>0)
{
ScibRegs.SCITXBUF =tmp+(2<<5);
zero_flag=0;
}
else
{
if(zero_flag)
ScibRegs.SCITXBUF =16+(2<<5); // 显示 灭
else
ScibRegs.SCITXBUF =tmp+(2<<5);
}
i++;
break;
case 2:
tmp = tmp/10;
tmp = tmp%10;
if(tmp>0)
{
ScibRegs.SCITXBUF =tmp+(1<<5);
zero_flag=0;
}
else
{
if(zero_flag)
ScibRegs.SCITXBUF =16+(1<<5); // 显示 灭
else
ScibRegs.SCITXBUF =tmp+(1<<5);
}
i++;
break;
case 3:
tmp = tmp%10;
if(tmp>0)
ScibRegs.SCITXBUF =tmp;
i=0;
break;
default:
i=0;
break;
}
}
}
interrupt void SCIRXINTB_ISR(void) // SCI-B
{
static unsigned int RxBuf;
PieCtrl.PIEACK.bit.ACK9 = 1;
RxBuf=ScibRegs.SCIRXBUF.all;
system.disp_flag=1;
switch(RxBuf)
{
case 0:
break;
case 1:
break;
case 2: //提高输出占空比
if(system.Vq < _IQ(0.9))
system.Vq +=_IQ(0.1);
break;
case 3: //提高输出占空比
if(system.Vq < _IQ(0.98))
system.Vq +=_IQ(0.01);
break;
case 4:
break;
case 5: //停止
system.Vq =0;
break;
case 6: //减小输出占空比
if(system.Vq > -_IQ(0.9))
system.Vq -=_IQ(0.1);
break;
case 7: //减小输出占空比
if(system.Vq > -_IQ(0.99))
system.Vq -=_IQ(0.01);
break;
}
EINT;
}
//===========================================================================
// No more.
//===========================================================================
|