#include "DSP28_Device.h"
#include "DSP28_Ev.h"
#include "stdlib.h" //库函数,求绝对值函数
#define P 3 //比例系数
#define I 0.35 //积分系数
unsigned int t1=0;
unsigned int pwm=18750;
unsigned int count=0;
int pwmout=0;
int expect_speed=2400; //期望速度
int current_speed=0; //当前速度
int CMPR1=0;
int current_position=0; //当前采样周期位置
int adjacent_T2CNT=0;
int adjacent=0;
int T1CNT=0;
int now_position=0;
int last_position=0; //上一次采样周期位置
int speed_error=0; //当前速度偏差
int last_speed_error=0; //上一次速度偏差
void InitSysCtrl()
{
Uint16 i;
EALLOW;
SysCtrlRegs.WDCR= 0x0068; // 禁止看门狗模块
SysCtrlRegs.PLLCR = 0xA; // 初始化PLL模块
for(i= 0; i< 5000; i++){};// 延时,使得PLL模块能够完成初始化操作
SysCtrlRegs.HISPCP.all = 0x0001; // HSPCLK=150/2=75MHz
SysCtrlRegs.LOSPCP.all = 0x0002; // LSPCLK=150/4=37.5MHz
SysCtrlRegs.PCLKCR.bit.EVAENCLK=1;
EDIS;
}
void InitGpio(void)
{
EALLOW;
// 将GPIO中和PWM相关的引脚设置为PWM功能
GpioMuxRegs.GPAMUX.bit.PWM1_GPIOA0=1; //设置PWM1引脚
GpioMuxRegs.GPAMUX.bit.PWM2_GPIOA1=1; //设置PWM2引脚
GpioMuxRegs.GPAMUX.bit.PWM3_GPIOA2=1; //设置PWM3引脚
GpioMuxRegs.GPAMUX.bit.PWM4_GPIOA3=1; //设置PWM4引脚
GpioMuxRegs.GPAMUX.bit.QEP1_GPIOA8=1; //设置光电编码QEP1引脚
GpioMuxRegs.GPAMUX.bit.QEP2_GPIOA9=1; //设置光电编码QEP2引脚
EDIS;
}
void InitEv(void)
{
//此段是用来设置引脚为复用功能
EvaRegs.T2CON.bit.TMODE=2; //连续增计数模式
EvaRegs.T2CON.bit.TPS=5; //进行64分频
EvaRegs.T2CON.bit.TENABLE=0; //暂时禁止T2的计数操作
EvaRegs.T2CON.bit.TCLKS10=0; //使用内部时钟
EvaRegs.T2PR=0xFFFF; //给T2定时器设终值
EvaRegs.T2CNT=0x0000; //设置计数初值为0
EvaRegs.CAPCONA.all=0x0000; //初始化捕获控制寄存器
EvaRegs.CAP1FIFO=0x0000; //CAP1FIFO为空
EvaRegs.CAPCONA.bit.CAP12EN=1; //使能捕获1、2功能
EvaRegs.CAPCONA.bit.CAP12TSEL=0; //设置捕获单元1的定时器
EvaRegs.CAPCONA.bit.CAP1EDGE=1; //检测上升沿
EvaRegs.CAPCONA.bit.CAP2EDGE=1;
EvaRegs.EVAIFRC.bit.CAP1INT=1; //清除捕获单元1的捕获中断标志位
EvaRegs.EVAIMRC.bit.CAP1INT=1; //使能捕获单元1的捕获中断
EvaRegs.EVAIFRC.bit.CAP2INT=1; //清除捕获单元2的捕获中断标志位
EvaRegs.EVAIMRC.bit.CAP2INT=1; //使能捕获单元2的捕获中断
EvaRegs.T2CON.bit.TENABLE=1; //enable timer1
EvaRegs.CAPCONA.bit.CAP12EN=1; //enable cap1 cap2
EvaRegs.CAPFIFOA.all=0x0200;
}
#include "DSP28_Device.h"
void main(void)
{
InitSysCtrl(); //初始化系统函数
DINT;
IER=0x0000; //禁止CPU中断
IFR=0x0000; //清除CPU中断标志
InitPieCtrl(); //初始化PIE控制寄存器
InitPieVectTable(); //初始化PIE中断向量表
InitGpio(); //初始化GPIO口
InitEv(); //初始化EV
PieCtrl.PIEIER2.bit.INTx4=1; //使能PIE中断,T1定时器中断位于INT2.4
PieCtrl.PIEIER3.bit.INTx1=1; //使能PIE中断,T2定时器中断位于INT3.1
IER|=M_INT2; //开CPU中断
EINT; //开全局中断
ERTM; //开实时中断
while(1)
{
}
}
void T1PINT_ISR(void) // 通用定时器T1的周期中断
{
T1CNT=0;
t1++;
if(t1==5 ) //采样周期=5ms
{
t1=0;
last_position=current_position;
current_position=EvaRegs.T2CNT;
adjacent_T2CNT=abs(now_position-last_position);
current_speed=(adjacent*60)/(0.003*4096); //求当前的电机转速
last_speed_error=speed_error;
speed_error = expect_speed- current_speed;
pwmout=pwmout+P*( speed_error -last_speed_error)+ speed_error*I; //电机转速位置PI调节
if(pwmout>18750)
pwmout=18750;
else if(pwmout<0)
pwmout=0;
else{};
CMPR1=pwm+pwmout; //电机正转
PieCtrl.PIEACK.bit.ACK2=1; //响应同组中断
EvaRegs.EVAIFRA.bit.T1PINT=1;//清除T1同期中断标志位
IER|=M_INT2; //开CPU中断
EINT; //开全局中断
ERTM; //开实时中断
}
}
void T2PINT_ISR(void) //通用定时器2的周期中断
{
count++;
if(count==200) //当电机正转10秒时
{
CMPR1=pwm; //PWM1与PWM2的占空比都为50%,此时电机停转
count=0;
}
PieCtrl.PIEACK.bit.ACK2=1; //响应同组中断
EvaRegs.EVAIFRB.bit.T2PINT=1;//清除T2周期中断标志位
EINT; //开全局中断
}
DSP2812编程调试时出现这个错误error: struct "EVA_REGS" has no field "CAPCONA",请问这是什么原因
|