TI中文支持网
TI专业的中文技术问题搜集分享网站

关于28335can口中断接收

你好,我计划采用can中断的方式来接收数据,但是在实验过程中能够进入can中断,接收消息等待寄存器却没有置位,邮箱里也没有数据。

下面是我的相关代码,麻烦看看有没有什么需要改正的地方

can配置;

void InitECan(void)
{
struct ECAN_REGS ECanbShadow;
EALLOW;

//配置GPIO引脚工作在eCAN功能
GpioCtrlRegs.GPAPUD.bit.GPIO16 = 0;
GpioCtrlRegs.GPAPUD.bit.GPIO17 = 0;
GpioCtrlRegs.GPAMUX2.bit.GPIO16= 2;
GpioCtrlRegs.GPAMUX2.bit.GPIO17 = 2;
GpioCtrlRegs.GPAQSEL2.bit.GPIO16=3;
GpioCtrlRegs.GPAQSEL2.bit.GPIO17=3;
/*GpioCtrlRegs.GPAPUD.bit.GPIO20 = 0;
GpioCtrlRegs.GPAPUD.bit.GPIO21 = 0;
GpioCtrlRegs.GPAMUX2.bit.GPIO20 = 3;
GpioCtrlRegs.GPAMUX2.bit.GPIO21 = 3;*/

//配置eCAN的RX和TX分别为eCAN的接收和发送引脚
ECanbShadow.CANTIOC.all = ECanbRegs.CANTIOC.all;
ECanbShadow.CANTIOC.bit.TXFUNC = 1;
ECanbRegs.CANTIOC.all = ECanbShadow.CANTIOC.all;

ECanbShadow.CANRIOC.all = ECanbRegs.CANRIOC.all;
ECanbShadow.CANRIOC.bit.RXFUNC = 1;
ECanbRegs.CANRIOC.all = ECanbShadow.CANRIOC.all;
EDIS;

EALLOW;
ECanbShadow.CANMC.all = ECanbRegs.CANMC.all;
//工作在正常模式
ECanbShadow.CANMC.bit.STM = 0;
//工作在eCAN模式
ECanbShadow.CANMC.bit.SCB = 1;
ECanbRegs.CANMC.all = ECanbShadow.CANMC.all;
EDIS;

//清除所有的TA位
ECanbRegs.CANTA.all = 0xFFFFFFFF;

//清除所有的RMP位
ECanbRegs.CANRMP.all = 0xFFFFFFFF;
ECanbRegs.CANGIF0.all = 0xFFFFFFFF; /* Clear all interrupt flag bits */
ECanbRegs.CANGIF1.all = 0xFFFFFFFF;

//配置时钟参数
EALLOW;
ECanbShadow.CANMC.all = ECanbRegs.CANMC.all;
ECanbShadow.CANMC.bit.CCR = 1;
ECanbRegs.CANMC.all = ECanbShadow.CANMC.all;
EDIS;

//CPU请求向CANBTC和CANGAM写配置信息,该位置1后必须等到CANED.CCE为1,才能
//对CANBTC进行操作。
ECanbShadow.CANES.all = ECanbRegs.CANES.all;
do
{
ECanbShadow.CANES.all = ECanbRegs.CANES.all;
}
while(ECanbShadow.CANES.bit.CCE != 1);

EALLOW;
//(BRPREG+1)=10 ,CAN时钟为15MHz
ECanbShadow.CANBTC.all = ECanbRegs.CANBTC.all ;
ECanbShadow.CANBTC.bit.BRPREG = 9;
ECanbShadow.CANBTC.bit.TSEG2REG = 2;
ECanbShadow.CANBTC.bit.TSEG1REG = 10;

ECanbShadow.CANBTC.bit.SAM = 1;
ECanbRegs.CANBTC.all = ECanbShadow.CANBTC.all;

ECanbShadow.CANMC.all = ECanbRegs.CANMC.all;
ECanbShadow.CANMC.bit.CCR = 0 ; // Set CCR = 0
//ECanbShadow.CANMC.all &= 0xE000;
ECanbRegs.CANMC.all = ECanbShadow.CANMC.all;

//初始化所有主设备控制区域为0,控制区域所有的位都初始化为0
ECanbMboxes.MBOX0.MSGCTRL.all = 0x00000000;
ECanbMboxes.MBOX1.MSGCTRL.all = 0x00000000;
ECanbMboxes.MBOX2.MSGCTRL.all = 0x00000000;
ECanbMboxes.MBOX3.MSGCTRL.all = 0x00000000;
ECanbMboxes.MBOX4.MSGCTRL.all = 0x00000000;
ECanbMboxes.MBOX5.MSGCTRL.all = 0x00000000;
ECanbMboxes.MBOX6.MSGCTRL.all = 0x00000000;
ECanbMboxes.MBOX7.MSGCTRL.all = 0x00000000;
ECanbMboxes.MBOX8.MSGCTRL.all = 0x00000000;
ECanbMboxes.MBOX9.MSGCTRL.all = 0x00000000;
ECanbMboxes.MBOX10.MSGCTRL.all = 0x00000000;
ECanbMboxes.MBOX11.MSGCTRL.all = 0x00000000;
ECanbMboxes.MBOX12.MSGCTRL.all = 0x00000000;
ECanbMboxes.MBOX13.MSGCTRL.all = 0x00000000;
ECanbMboxes.MBOX14.MSGCTRL.all = 0x00000000;
ECanbMboxes.MBOX15.MSGCTRL.all = 0x00000000;
ECanbMboxes.MBOX16.MSGCTRL.all = 0x00000000;
ECanbMboxes.MBOX17.MSGCTRL.all = 0x00000000;
ECanbMboxes.MBOX18.MSGCTRL.all = 0x00000000;
ECanbMboxes.MBOX19.MSGCTRL.all = 0x00000000;
ECanbMboxes.MBOX20.MSGCTRL.all = 0x00000000;
ECanbMboxes.MBOX21.MSGCTRL.all = 0x00000000;
ECanbMboxes.MBOX22.MSGCTRL.all = 0x00000000;
ECanbMboxes.MBOX23.MSGCTRL.all = 0x00000000;
ECanbMboxes.MBOX24.MSGCTRL.all = 0x00000000;
ECanbMboxes.MBOX25.MSGCTRL.all = 0x00000000;
ECanbMboxes.MBOX26.MSGCTRL.all = 0x00000000;
ECanbMboxes.MBOX27.MSGCTRL.all = 0x00000000;
ECanbMboxes.MBOX28.MSGCTRL.all = 0x00000000;
ECanbMboxes.MBOX29.MSGCTRL.all = 0x00000000;
ECanbMboxes.MBOX30.MSGCTRL.all = 0x00000000;
ECanbMboxes.MBOX31.MSGCTRL.all = 0x00000000;

ECanbShadow.CANES.all = ECanbRegs.CANES.all;
do
{
ECanbShadow.CANES.all = ECanbRegs.CANES.all;
}
while(ECanbShadow.CANES.bit.CCE != 0); //等待CCE位清零

//屏蔽所有邮箱,在写MSGID之前要完成该操作
ECanbRegs.CANME.all = 0;
EDIS;

//设置接收邮箱的ID,扩展帧
ECanbMboxes.MBOX17.MSGID.all = 0x9555AABA;

//设置发送邮箱的ID号,扩展帧
ECanbMboxes.MBOX0.MSGID.all = 0x9555AAA0;
ECanbMboxes.MBOX1.MSGID.all = 0x80C80000;
//ECanbMboxes.MBOX0.MID.all = 0x80C80000;

//邮箱0为TX
EALLOW;
ECanbShadow.CANMD.all = ECanbRegs.CANMD.all;
ECanbShadow.CANMD.bit.MD0 =0;
ECanbShadow.CANMD.bit.MD1 =0;
ECanbShadow.CANMD.bit.MD17 =1;
ECanbRegs.CANMD.all = ECanbShadow.CANMD.all;
EDIS;
//数据长度 8个BYTE
ECanbMboxes.MBOX0.MSGCTRL.bit.DLC = 8;
ECanbMboxes.MBOX1.MSGCTRL.bit.DLC = 8;
ECanbMboxes.MBOX17.MSGCTRL.bit.DLC = 8;

//邮箱中断线路
EALLOW;
ECanbShadow.CANMIL.all = ECanbRegs.CANMIL.all;
ECanbShadow.CANMIL.all = 0x0;//邮箱中断产生在线路0上
ECanbRegs.CANMIL.all = ECanbShadow.CANMIL.all;

//邮箱中断后启用
ECanbShadow.CANMIM.all = ECanbRegs.CANMIM.all;
ECanbShadow.CANMIM.all = 0xFFFFFFFF;//邮箱中断启用
ECanbRegs.CANMIM.all = ECanbShadow.CANMIM.all;

ECanbShadow.CANGIM.all = ECanbRegs.CANGIM.all;
/*ECanbRegs.CANGIM.bit.MTOM = 0x1; // mailbox time-out
ECanbRegs.CANGIM.bit.TCOM = 0x1; // time counter overflown
ECanbRegs.CANGIM.bit.AAIM = 0x1; // acknowledge failure
ECanbRegs.CANGIM.bit.WDIM = 0x1; // write refusal
ECanbRegs.CANGIM.bit.WUIM = 0x1; // wake-up
ECanbRegs.CANGIM.bit.RMLIM = 0x1; // messages lost
ECanbRegs.CANGIM.bit.BOIM = 0x1; // bus closed
ECanbRegs.CANGIM.bit.EPIM = 0x1; // Error-passive
ECanbRegs.CANGIM.bit.WLIM = 0x1; // warning
ECanbShadow.CANGIM.bit.GIL=0x0;*/
ECanbShadow.CANGIM.bit.I1EN=0x0;
ECanbShadow.CANGIM.bit.I0EN=0x1;//启用针对ECAN0INT线路的所有中断
ECanbRegs.CANGIM.all = ECanbShadow.CANGIM.all;
EDIS;
//设置发送优先级
ECanbMboxes.MBOX0.MSGCTRL.bit.TPL = 0;
ECanbMboxes.MBOX1.MSGCTRL.bit.TPL = 1;
//ECanbMboxes.MBOX17.MSGCTRL.bit.TPL = 0;

//没有远方应答帧被请求
ECanbMboxes.MBOX0.MSGCTRL.bit.RTR = 0;
ECanbMboxes.MBOX1.MSGCTRL.bit.RTR = 0;
ECanbMboxes.MBOX17.MSGCTRL.bit.RTR = 0;

ECanbMboxes.MBOX17.MDL.all =0;
ECanbMboxes.MBOX17.MDH.all =0;
//向邮箱RAM区写数据
//ECanbMboxes.MBOX0.MDL.all = 0x01234567;
//ECanbMboxes.MBOX0.MDH.all = 0x89ABCDEF;

//邮箱使能Mailbox0,1,17
EALLOW;
ECanbShadow.CANME.all = ECanbRegs.CANME.all;
ECanbShadow.CANME.bit.ME0 =1;
ECanbShadow.CANME.bit.ME1 =1;
ECanbShadow.CANME.bit.ME17 =1;
ECanbRegs.CANME.all = ECanbShadow.CANME.all;
EDIS;
ECanbMboxes.MBOX17.MDL.all = 0; //Write Rx_mailbox_RAM
ECanbMboxes.MBOX17.MDH.all = 0;
ECanbShadow.CANOPC.all = ECanbRegs.CANOPC.all;//写覆盖保护控制寄存器
ECanbShadow.CANOPC.bit.OPC17=0;
ECanbRegs.CANOPC.all = ECanbShadow.CANOPC.all;
}

主程序:

void main(void)
{

unsigned int i;
unsigned int j;
unsigned int senddata[]={0,1,2,33,44,5};
InitSysCtrl();
DINT;
IER = 0x0000;
IFR = 0x0000;
/*初始化PIE中断*/
InitPieCtrl();
/*初始化PIE中断矢量表*/
InitPieVectTable();
/*初始CAN寄存器*/
InitECan();
PieVectTable.ECAN0INTB = &ECAN0INTB_ISR;
//PieVectTable.ECAN1INTB = &ECAN1INTB1_ISR;
IER|=M_INT9;
PieCtrlRegs.PIEIER9.bit.INTx7=1;
EINT; // Enable Global interrupt INTM
ERTM; // Enable Global realtime interrupt DBGM
MessageSendCount = 0;
MessageReceiveCount = 0;

for(;;)
{

ECanbRegs.CANTRS.bit.TRS1 = 1;
while(ECanbRegs.CANTA.all == 0);
ECanbRegs.CANTA.bit.TA1 = 1;

MessageSendCount++; //在这里设断点,观察

ECanbMboxes.MBOX1.MDL.byte.BYTE0=senddata[i];
ECanbMboxes.MBOX1.MDL.byte.BYTE1=senddata[i+1];
ECanbMboxes.MBOX1.MDL.byte.BYTE2=senddata[i+2];
ECanbMboxes.MBOX1.MDL.byte.BYTE3=senddata[i+3];

ECanbMboxes.MBOX1.MDH.byte.BYTE4=senddata[i];
ECanbMboxes.MBOX1.MDH.byte.BYTE5=senddata[i+1];
ECanbMboxes.MBOX1.MDH.byte.BYTE6=senddata[i+2];
ECanbMboxes.MBOX1.MDH.byte.BYTE7=senddata[i+3];

for(j=0;j<10;j++)
{
DELAY_US(100000);
}

i=i+2;

if(i>6)
i=0;
}}

中断函数:

interrupt void ECAN0INTB_ISR(void) // eCAN-B
{
// Insert ISR Code here
PieCtrlRegs.PIEACK.all=PIEACK_GROUP9;
configtestled();
LED4=0;
DELAY_US(100000);
LED4=1;
i=0;
while(ECanbRegs.CANRMP.bit.RMP17 != 1 ) ;
ECanbRegs.CANRMP.bit.RMP17= 1;
//收到的数据在接收邮箱Mbox16
Rec_l[i] = ECanbMboxes.MBOX17.MDL.byte.BYTE0;
Rec_l[i+1] = ECanbMboxes.MBOX17.MDL.byte.BYTE1;
Rec_l[i+2] = ECanbMboxes.MBOX17.MDL.byte.BYTE2;
Rec_l[i+3] = ECanbMboxes.MBOX17.MDL.byte.BYTE3;
Rec_h[i+4] = ECanbMboxes.MBOX17.MDH.byte.BYTE4;
Rec_h[i+5] = ECanbMboxes.MBOX17.MDH.byte.BYTE5;
Rec_h[i+6] = ECanbMboxes.MBOX17.MDH.byte.BYTE6;
Rec_h[i+7] = ECanbMboxes.MBOX17.MDH.byte.BYTE7;
i=i+1;
EINT;

}

程序能够进入中断,但是一直在上面中断函数的红色部分,也就是说canrmp对应的位没有置位,这个应该是什么原因呢?望解答

赞(0)
未经允许不得转载:TI中文支持网 » 关于28335can口中断接收
分享到: 更多 (0)