#include "DSP2833x_Device.h"
#include "DSP2833x_Examples.h"
interrupt void ECanR_ISR(void);
void Initcan();
Uint32 Counts1 = 0;
int j =0;
int ii;
int jj;
Uint32 MSGL = 0;
Uint32 MSGH = 0;
struct ECAN_REGS ECanbShadow;
void main(void)
{
InitSysCtrl(); //系统初始化
InitXintf16Gpio(); //初始化配置总线
InitECanbGpio(); //初始化ECanb模块
DINT; //总中断关闭
IER=0x0000;
IFR=0x0000;
InitPieCtrl(); //PIE模块初始化
InitPieVectTable(); //初始化中断向量表
InitECanb();
Initcan();
/////开始传输数据/////
for(;;)
{
//if(ECanbRegs.CANTA.all == 0x00000000)
ECanbMboxes.MBOX0.MDL.byte.BYTE2++; //向指定邮箱写入数据
ECanbMboxes.MBOX0.MDH.byte.BYTE6++;
ECanbMboxes.MBOX1.MDL.byte.BYTE3++;
ECanbMboxes.MBOX1.MDH.byte.BYTE7++;
EALLOW;
ECanbShadow.CANTRS.all=ECanbRegs.CANTRS.all;
//ECanbShadow.CANTRS.all=0x00000000;
ECanbShadow.CANTRS.bit.TRS0=1; //发送请求置位寄存器置位为1。启动发送
ECanbRegs.CANTRS.all=ECanbShadow.CANTRS.all;
EDIS;
for(ii=0; ii<100; ii++)//延时
{
for(jj=0; jj<1000; jj++){}
}
ECanbRegs.CANTRS.all = 0x00000002;//发送请求box1
for(ii=0; ii<100; ii++)//延时
{
for(jj=0; jj<1000; jj++){}
}
j++;
if(j > 2000)
{
j = 0;
}
if(ECanbRegs.CANTA.all == 0x00000001 || ECanbRegs.CANTA.all == 0x00000002) //
{ ECanbRegs.CANTA.all=0x00000003; //写1清零,为了下一个发送确认
}
}
}
interrupt void ECanR_ISR(void)
{
if(ECanbRegs.CANRMP.all == 0x00010000)
{
ECanbRegs.CANRMP.all = 0xFFFF0000;
MSGL = ECanbMboxes.MBOX16.MDL.all;
MSGH = ECanbMboxes.MBOX16.MDH.all;
Counts1++;
}
PieCtrlRegs.PIEACK.bit.ACK9=1;
ECanbShadow.CANOPC.all = 0x00000000;
}
void Initcan()
{
EALLOW;
PieVectTable.ECAN0INTB = &ECanR_ISR;
PieCtrlRegs.PIECTRL.bit.ENPIE=1; //使能PIE模块
PieCtrlRegs.PIEIER9.bit.INTx7=1; //使能INT9.7和INT9.8,即使能ECAN0INTB和ECAN1INTB
EDIS;
IER|=M_INT9; //启用CAN模块对应的CPU中断线路
EINT; //使能全局中断
ERTM; //使能全局实时中断
EALLOW; ECanbShadow.CANTIOC.all=ECanbRegs.CANTIOC.all; ECanbShadow.CANTIOC.bit.TXFUNC=1; //CANTX引脚用于CAN传输功能
ECanbRegs.CANTIOC.all=ECanbShadow.CANTIOC.all;
ECanbShadow.CANRIOC.all=ECanbRegs.CANRIOC.all;
ECanbShadow.CANRIOC.bit.RXFUNC=1; //CANRX引脚用于CAN接受功能
ECanbRegs.CANRIOC.all=ECanbShadow.CANRIOC.all;
EDIS; //
EALLOW;
ECanbShadow.CANMC.all=ECanbRegs.CANMC.all;
ECanbShadow.CANMC.bit.CCR=1; ECanbRegs.CANMC.all=ECanbShadow.CANMC.all;
EDIS;
do
{
ECanbShadow.CANES.all = ECanbRegs.CANES.all;
} while(ECanbShadow.CANES.bit.CCE != 1 );//等待CPU禁止对配置寄存器写操作,当CCE=1时,执行以下程序
//CPU统一对CANBTC进行更改;
EALLOW;
ECanbShadow.CANBTC.all=ECanbRegs.CANBTC.all;
ECanbShadow.CANBTC.bit.BRPREG=9; //BRP=BRPREG+1,相当于提供一个15MHz的CAN模块时钟
//因为 SYSCLKOUT=150MHz 150/10=15
ECanbShadow.CANBTC.bit.TSEG1REG=10;
ECanbShadow.CANBTC.bit.TSEG2REG=2;
ECanbShadow.CANBTC.bit.SAM = 1; //采样三次取多数
ECanbRegs.CANBTC.all=ECanbShadow.CANBTC.all; //Bit Tine=15 , 采样点SP=80% , 波特率Bit rat=500k EDIS;
EALLOW;
ECanbShadow.CANMC.all=ECanbRegs.CANMC.all;
ECanbShadow.CANMC.bit.CCR=0; //CPU进入正常运行,不再有权配置位时序寄存器
ECanbShadow.CANMC.bit.SCB=1; //选择eCAN模式,而不是SCC模式
ECanbShadow.CANMC.bit.SUSP = 1; //挂起模式下继续运行
ECanbShadow.CANMC.bit.PDR=0;
ECanbShadow.CANMC.bit.DBO=0;
ECanbShadow.CANMC.bit.WUBA=0;
ECanbShadow.CANMC.bit.CDR=0;
ECanbShadow.CANMC.bit.ABO=1; //识别到总线自动恢复
ECanbShadow.CANMC.bit.STM=0; //CAN模块处于正常模式
ECanbShadow.CANMC.bit.SRES=0;
ECanbShadow.CANMC.bit.MBNR=0;
ECanbRegs.CANMC.all=ECanbShadow.CANMC.all;
EDIS; //对主控制寄存器(CANMC)及进行编程
do
{
ECanbShadow.CANES.all = ECanbRegs.CANES.all;
} while(ECanbShadow.CANES.bit.CCE != 0 ); //等待CCE位清零
ECanbShadow.CANGIM.all = 0x00000000;
ECanbShadow.CANMIM.all = 0x00000000;
ECanbShadow.CANMIL.all = 0xFFFFFFFF;
ECanbMboxes.MBOX0.MSGCTRL.all = 0x00000000;
ECanbMboxes.MBOX1.MSGCTRL.all = 0x00000000;
ECanbMboxes.MBOX16.MSGCTRL.all = 0x00000000; //将MSGCTRL寄存器所有位清零进行初始化
ECanbRegs.CANME.all=0; //清除邮箱使能寄存器CANME中相应的位,屏蔽邮箱
ECanbMboxes.MBOX0.MSGID.bit.IDE = 0; //要被接受的消息为标准标识符
ECanbMboxes.MBOX0.MSGID.bit.STDMSGID=0x0226; //邮箱ID
ECanbMboxes.MBOX0.MSGID.bit.AAM = 0;
ECanbMboxes.MBOX1.MSGID.bit.IDE = 0; //要被接受的消息为标准标识符
ECanbMboxes.MBOX1.MSGID.bit.STDMSGID = 0x0314; //邮箱ID ECanbMboxes.MBOX1.MSGID.bit.AAM = 0; ECanbMboxes.MBOX16.MSGID.bit.IDE = 0; //要被接收的消息为标准标识符
ECanbMboxes.MBOX16.MSGID.bit.STDMSGID=0x0565; //邮箱ID
ECanbMboxes.MBOX16.MSGID.bit.AAM = 0;
ECanbMboxes.MBOX16.MSGID.bit.AME = 1; ECanbLAMRegs.LAM16.all = 0xFFFFFFFF;
ECanbLAMRegs.LAM16.bit.LAMI = 1; ECanbRegs.CANMD.all=0xFFFF0000; // 设置邮箱方向,将邮箱0-15配置为Tx,将16-31配置为Rx
ECanbShadow.CANOPC.all = ECanbRegs.CANOPC.all; ECanbShadow.CANOPC.bit.OPC16 = 0; //设置为没有写覆盖保护
ECanbRegs.CANOPC.all = ECanbShadow.CANOPC.all; ECanbShadow.CANME.all=ECanbRegs.CANME.all;
ECanbShadow.CANME.bit.ME0 = 1;
ECanbShadow.CANME.bit.ME1 = 1;
ECanbShadow.CANME.bit.ME16 = 1; //使能邮箱
ECanbRegs.CANME.all = ECanbShadow.CANME.all;
ECanbShadow.CANOPC.all = ECanbRegs.CANOPC.all;//写覆盖保护控制寄存器
ECanbShadow.CANOPC.bit.OPC16=0; //没有写覆盖保护
ECanbRegs.CANOPC.all = ECanbShadow.CANOPC.all;
ECanbMboxes.MBOX0.MSGCTRL.bit.DLC=8; //设置接受或发送的数据长度,此时设置为8字节
ECanbMboxes.MBOX1.MSGCTRL.bit.DLC=8;
ECanbMboxes.MBOX16.MSGCTRL.bit.DLC=8;
ECanbMboxes.MBOX0.MSGCTRL.bit.RTR=0; //设置为没有远程帧传输请求
ECanbMboxes.MBOX1.MSGCTRL.bit.RTR=0;
ECanbMboxes.MBOX16.MSGCTRL.bit.RTR=0; //设置为没有远程帧传输请求
ECanbMboxes.MBOX0.MDL.byte.BYTE2; //向指定邮箱写入数据
ECanbMboxes.MBOX0.MDH.byte.BYTE6;
ECanbMboxes.MBOX1.MDL.byte.BYTE3;
ECanbMboxes.MBOX1.MDH.byte.BYTE7;
ECanbRegs.CANMIL.all=0x00000000; //发送邮箱中断产生在中断线路0上
//接受邮箱中断产生在中断线路1上
ECanbRegs.CANMIM.all=0xFFFFFFFF; //启用所有的邮箱中断
EALLOW;
ECanbShadow.CANGIM.all = ECanbRegs.CANGIM.all;
ECanbShadow.CANGIM.bit.AAIM = 1;
ECanbShadow.CANGIM.bit.WUIM = 1;
ECanbShadow.CANGIM.bit.WDIM = 1;
ECanbShadow.CANGIM.bit.BOIM = 1;
ECanbShadow.CANGIM.bit.EPIM = 1;
ECanbShadow.CANGIM.bit.WLIM = 1;
ECanbShadow.CANGIM.bit.I0EN = 1; //使能中断线路0
ECanbShadow.CANGIM.bit.I1EN = 1; //使能中断线路1
ECanbShadow.CANGIM.bit.GIL = 0;
ECanbRegs.CANGIM.all = ECanbShadow.CANGIM.all;
EDIS; //配置全局中断屏蔽寄存器
ECanbRegs.CANRMP.all = 0xFFFFFFFF; //复位接收挂起标志寄存器
ECanbRegs.CANTA.all = 0xFFFFFFFF; //复位发送挂起标志寄存器
ECanbRegs.CANGIF0.all=0xFFFFFFFF; //全局中断标志寄存器置位
ECanbRegs.CANGIF1.all=0xFFFFFFFF;
}