CAN模块初始化代码如下:
void MSCAN_Init(void)
{
CANCTL1 =0X80; //enable the MSCAN ,no loopback mode
CANCTL0 =0X01;
while(!CANCTL1_INITAK);
CANBTR0 =0X43; //波特率500K
CANBTR1 =0X14;
CANIDAC =0X00; //two 32bit filter for extended id
CANIDAR0 = 0xff;
CANIDAR1 = 0xfe|(SlaveID & 0x80)>>7;
CANIDAR2 = 0x01|(SlaveID & 0x7f)<<1;
CANIDAR3 = 0xff;
CANIDMR0 = 0xff;
CANIDMR1 = 0xfe;
CANIDMR2 = 0x01;
CANIDMR3 = 0xff;
CANIDAR4 = 0xff;
CANIDAR5 = 0xfe|(BroadCas
tiD & 0x80)>>7;
CANIDAR6 = 0x01|(BroadCastID & 0x7f)<<1;
CANIDAR7 = 0xff;
CANIDMR4 = 0xff;
CANIDMR5 = 0xfe; //debug
CANIDMR6 = 0x01; //debug
CANIDMR7 = 0xff;
CANCTL0 =0; //exit init mode
while(CANCTL1_INITAK);
while(!CANCTL0_SYNCH); //wait until the synchronous with the bus
CANRFLG =0XC3;//reset Rx flag
发送代码如下:
while(!CANTFLG);
CANTBSEL =CANTFLG;
txbuffer =CANTBSEL;
CANTXIDR0 =0xc0;
CANTXIDR1 =0X0A;
CANTXIDR2 =0X23;
CANTXIDR3 =0XFA;
CANTXDSR0 =0X56;
CANTXDLR =0X01;
CANTXTBPR =0X80;
CANTFLG =txbuffer;
while(!(CANTFLG & txbuffer));
TURN_ON_LED1;
一个很简单的CAN模块测试程序,通过CAN总线发送一个数0x56发送出去,但是程序运行时会死在
while(!(CANTFLG & txbuffer));这一句,经过硬件
仿真后发现是因为清零CANTFLG的相应位使能发送后寄存器CANRFLG里的TSTAT0,TSTAT1这两个标志位一直在10和11之间跳动,导致
单片机的CAN模块一直在bus-off和主动错误状态之间跳变,导致数据发不出去,波特率设置没有问题,因为能正常接收到CAN数据,通过请问各位大神有没有遇到过这种情况,怎么解决???
没有,不过还是建议你看英文资料,更权威
一周热门 更多>