/*2812A板:
使用直连的串口通信电缆;
启动串口调试助手.exe;
PC机发送一个"."为结束标志
*/
#include"DSP281x_Device.h"
#include"DSP281x_Examples.h"
////////////////////////////////////////////////////////////
unsigned int MaxCmpare=0xF000,LeftIndex=7,RightIndex=7;//cKey,OldSpeed,nDIR,StepLength=0x5000,nDIR=1,,temp
unsigned int Maxstep=7,Mindistance=50,distance=100;//,GPF1,GPF3
unsigned int a[15]={0,0x1000,0x2000,0x3000,0x4000,0x5000,0x6000,0x0F000,0x0F000,0x0F000,0x0F000,0x0F000,0x0F000,0x0F000,0x0F000};//
unsigned int ajust_num=0x1200,zhuangtai=0,mov_key=0;
//Prototypestatementsforfunctionsfoundwithinthisfile.
void init_eva(void );
void init_evb(void );
void Delay(unsigned int );
void Swap(unsigned int *,unsigned int *);
unsigned int mean(unsigned int* );
void adjust_speed(void);
void Stop();
void LinearMotion(unsigned int );
void LeftMotor(unsigned int ,unsigned int );
void RightMotor(unsigned int ,unsigned int );
void TurnAround(unsigned int ,unsigned int );
void vehicle_self_control(void);
void SaMotor1(unsigned int ,unsigned int );//,unsigned int
void SaMotor2(unsigned int ,unsigned int );//,unsigned int
void ReadDistance(void );
//Prototypestatementsforfunctionsfoundwithinthisfile.
void scia_loopback_init(void );
void scib_loopback_init(void );
void scia_fifo_init(void );
void scib_fifo_init(void );
void scia_xmit(int a);
interrupt void scia_rx_isr(void );
interrupt void scia_tx_isr(void );
interrupt void CAPINT4_ISR(void );
interrupt void CAPINT5_ISR(void );
//Globalcountsusedinthisexample
//Uint16LoopCount;
//Uint16ErrorCount;
char cString[6]={"Hello!"},cBuffer[7],cAnswer[5]={"HUC:"};//,ReceivedChar="";//,cReceive
char ReceivedChar='0';
void main(void )
{
unsigned int ZIZHU=0;//自主控制位,全局变量,默认不自主
unsigned int Step,time1=0,time2=0;//Direction,
int i,k=0,bReceive=0;//,nLen
//Step1.InitializeSystemControlregisters,PLL,WatchDog,Clockstodefaultstate:
//ThisfunctionisfoundintheDSP281x_SysCtrl.cfile.
InitSysCtrl();
//Step2.SelectGPIOforthedeviceorforthespecificapplication:
//ThisfunctionisfoundintheDSP281x_Gpio.cfile.
//InitGpio();skipthisasthisisexampleselectstheI/OforSCIinthisfileitself
EALLOW;
GpioMuxRegs.GPAMUX.all=0x00FF;//EVAPWM1-6pinsCAP123
GpioMuxRegs.GPBMUX.all=0x00FF;//EVBPWM7-12pinsCAP456
GpioMuxRegs.GPDMUX.all=0x0000;//GPIODIOpin
GpioMuxRegs.GPEMUX.all=0x0000;//GPIODIOpin
GpioMuxRegs.GPFMUX.all=0x0030;//SelectGPIOstobeScipins
//PortFMUX-x000000000110000
GpioMuxRegs.GPGMUX.all=0x0030;//SCIB
EDIS;
//Step3.InitializePIEvectortable:
//ThePIEvectortableisinitializedwithpointerstoshellInterrupt
//ServiceRoutines(ISR).TheshellroutinesarefoundinDSP281x_DefaultIsr.c.
//InsertuserspecificISRcodeintheappropriateshellISRroutinein
//theDSP28_DefaultIsr.cfile.
//DisableandclearallCPUinterrupts:
DINT;
IER=0x0000;
IFR=0x0000;
//InitializePieControlRegistersToDefaultState:
//ThisfunctionisfoundintheDSP281x_PieCtrl.cfile.
InitPieCtrl();//PIEisnotusedforthisexample
//InitializethePIEVectorTableToaKnownState:
//ThisfunctionisfoundinDSP281x_PieVect.c.
//ThisfunctionpopulatesthePIEvectortablewithpointers
//totheshellISRfunctionsfoundinDSP281x_DefaultIsr.c.
InitPieVectTable();
//EnableCPUandPIEinterrupts
//ThisexamplefunctionisfoundintheDSP281x_PieCtrl.cfile.
EnableInterrupts();
//Step4.InitializealltheDevicePeripheralstoaknownstate:
//ThisfunctionisfoundinDSP281x_InitPeripherals.c
//InitPeripherals();skipthisforSCItests
//Step5.Userspecificfunctions,Reassignvectors(optional),EnableInterrupts:
//LoopCount=0;
//ErrorCount=0;
PieCtrlRegs.PIEIER5.bit.INTx5=1;
IER |= M_INT5;
EINT; // Enable Global interrupt INTM
ERTM; // Enable Global realtime interrupt DBGM
scia_fifo_init();//InitializetheSCIFIFO
scia_loopback_init();//InitalizeSCIfordigitalloopback
scib_fifo_init();//InitializetheSCIFIFO
scib_loopback_init();//InitalizeSCIfordigitalloopback
//InitializeonlyGPAMUXandGPBMUXforthistest
init_eva();
init_evb();
EALLOW;
//接近开关
GpioMuxRegs.GPADIR.bit.GPIOA15=0;//右
GpioMuxRegs.GPADIR.bit.GPIOA14=0;//左
GpioMuxRegs.GPADIR.bit.GPIOA13=0;//后
//SAMoter1底盘
GpioMuxRegs.GPFDIR.bit.GPIOF14=1;//output方向
GpioMuxRegs.GPFDIR.bit.GPIOF13=1;//output脉冲
GpioMuxRegs.GPFDIR.bit.GPIOF9=0;//input边界
//SAMoter2机械臂
GpioMuxRegs.GPFDIR.bit.GPIOF12=1;//output方向
GpioMuxRegs.GPFDIR.bit.GPIOF11=1;//output脉冲
GpioMuxRegs.GPFDIR.bit.GPIOF10=0;//input边界
//轮子霍尔开关
GpioMuxRegs.GPFDIR.bit.GPIOF3=0;//后左接近
GpioMuxRegs.GPFDIR.bit.GPIOF2=0;//后右接近
GpioMuxRegs.GPFDIR.bit.GPIOF1=1;
//GpioMuxRegs.GPFDIR.bit.GPIOF0=0;//左后轮
GpioMuxRegs.GPFDIR.bit.GPIOF0=1;//output枪
EDIS;
GpioDataRegs.GPFDAT.bit.GPIOF0=0;
GpioDataRegs.GPFDAT.bit.GPIOF1=0;
//Note:Autobaudlockisnotrequiredforthisexample
//Sendacharacterstartingwith0
//while(1)
//{
////Step=1;SaMotor2(1,Step);
////Step=20;SaMotor1(0,Step);
//Stop();
//LinearMotion(1);
////RightMotor(0,2);
////LeftMotor(0,2);
//}
//Step6.SendCharactersforeverstartingwith0x00andgoingthrough
//0xFF.Aftersendingeach,checktherecievebufferforthecorrectvalue
for(i=0;i<6;i++)
{
scia_xmit(cString[i]);
while(SciaRegs.SCIFFTX.bit.TXFFST!=0){}
//Delay(1024);
}
for(;;)
{
if(bReceive==1)
{
for(i=0;i<4;i++)
{
scia_xmit(cAnswer[i]);
while(SciaRegs.SCIFFTX.bit.TXFFST!=0){}
}
scia_xmit(cBuffer[1]);
while(SciaRegs.SCIFFTX.bit.TXFFST!=0){}
scia_xmit('.');
}
if((cBuffer[2]>0x30)||(cBuffer[2]<0x3A))
Step=cBuffer[2]-0x30;//3;
else
Step=1;
switch(cBuffer[1])
{
case'L':
zhuangtai=3;TurnAround(1,5*Step);
break;
case'R':
zhuangtai=4;TurnAround(0,5*Step);
break;
//(unsigned int Direction,unsigned int delaytime);
case'A':
zhuangtai=1;LinearMotion(0);
break;
case'D':
zhuangtai=2;LinearMotion(1);
break;
//(unsigned int cKey,unsigned int StepLength);
case'T':
zhuangtai=0;Stop();
break;
///////////////////////////
case'Z':
SaMotor1(1,Step);
break;
case'Y':
SaMotor1(0,Step);
break;
case'X':
SaMotor2(1,Step);
break;
case'S':
SaMotor2(0,Step);
break;
case'F':
GpioDataRegs.GPFDAT.bit.GPIOF0=1;
break;
case'f':
//Delay(1024);
GpioDataRegs.GPFDAT.bit.GPIOF0=0;
break;
case'V':
ZIZHU=1;
//scia_loopback_init();
break;
case'U':
ZIZHU=0;
//scia_loopback_init();
break;
case'r':
time1=0;
time2=0;
break;
default:break;
}
//测试
//LinearMotion(0);zhuangtai=1;
评论0
最新资源