小弟用的STM8S208MB,正在做CAN的回环模式自测。发送没有问题,因为相应TXOK位被置1了。我本来的发送的是标准数据帧,帧ID = 0x01, DLC = 4,然后就是4个数据,但是在接收到的却变成扩展帧,帧ID = 0xb00f5b9, DLC = 1,接收到的完全是错误的数据。先小弟将代码贴出来,请大家帮忙看下。
void CAN_Init(void)
{
CAN->MCR = 0x02;
//进入初始化模式
CAN_EntryMode(CAN_WORKMODE_INIT);
//CAN->MCR |= 0x01 << 4;//bit4-1配置成禁止自动重传模式,bit2-0发送报文优先级由标识符决定,bit3-0表示当FIFO满时,下一个报文会覆盖先前报文
CAN->DGR |= (0x01 << 0 | 0x01 << 4);//环回,正常模式
CAN->IER = 0x02;//开启FIFO消息挂号中断
CAN->PSR = 0;
CAN->PSR = 0x06;//CAN页面选择是设置/诊断
CAN->Page.Config.ESR = 0x0;
CAN->Page.Config.EIER = 0x0;
CAN->Page.Config.BTR1 = 0x01;//配置tq = 1/8us,SJW = 0x01
CAN->Page.Config.BTR2 = 0x67;//配置CAN的时钟为fmaster,BS2=0x06,BS1=0x07,配置波特率为500KHZ,选择的fmaster时钟源
CAN->Page.Config.FMR1 = 0xff;
CAN->Page.Config.FMR2 = 0x0f;//模式均配置为标识符模式
CAN->Page.Config.FCR1 = 0x66;
CAN->Page.Config.FCR2 = 0x66;
CAN->Page.Config.FCR3 = 0x66;//配置成32位的位宽,没有激活过滤器
//进入正常模式
CAN_EntryMode(CAN_WORKMODE_NORMAL);
}
void CAN_ReceiveFrame(RevFrame_T *pstRevFrame)
{
CAN->PSR = 0x07;//打开接收FIFO页面
//接收ID
if((CAN->Page.RxFIFO.MIDR1) & (0x01 << 6)) //判断是否扩展标识符
{
pstRevFrame->nCanId = (u32)((u32)0x1fffffff & ((CAN->Page.RxFIFO.MIDR4) | ((u32)((u32)CAN->Page.RxFIFO.MIDR3) << 8)
| ((u32)((u32)CAN->Page.RxFIFO.MIDR2) << 16) | ((u32)((u32)CAN->Page.RxFIFO.MIDR1 & 0x1f) << 24)));
}
else
{
pstRevFrame->nCanId = (u32)((u16)0x07ff & ((u16)((u16)(CAN->Page.RxFIFO.MIDR1 & 0x1f) << 6) | ((CAN->Page.RxFIFO.MIDR2 >> 2)&0x3f)));
}
pstRevFrame->uvRtr = (u8)((0x20 & CAN->Page.RxFIFO.MIDR1) >> 5);//接收RTR,用来判断接收的帧是数据帧还是远程帧
pstRevFrame->uvLdc = (u8)(CAN->Page.RxFIFO.MDLCR & 0x0F); //发送数据的长度码
pstRevFrame->uvFmi = CAN->Page.RxFIFO.MFMI; //过滤器的匹配序号
//接收数据
pstRevFrame->acRevData[0] = CAN->Page.RxFIFO.MDAR1;
pstRevFrame->acRevData[1] = CAN->Page.RxFIFO.MDAR2;
pstRevFrame->acRevData[2] = CAN->Page.RxFIFO.MDAR3;
pstRevFrame->acRevData[3] = CAN->Page.RxFIFO.MDAR4;
pstRevFrame->acRevData[4] = CAN->Page.RxFIFO.MDAR5;
pstRevFrame->acRevData[5] = CAN->Page.RxFIFO.MDAR6;
pstRevFrame->acRevData[6] = CAN->Page.RxFIFO.MDAR7;
pstRevFrame->acRevData[7] = CAN->Page.RxFIFO.MDAR8;
CAN->RFR |= (u8)(0x01 << 5); //释放接收FIFO的输出邮箱
}
请大家帮帮忙,感谢了!! |