>1、其它不用变化,只是改变MCP初始化程序中的发送缓冲器低位为扩展报文,并设定发送的标准与扩展报文ID. MCP2515_WriteByte(TXB0SIDH,0x00); MCP2515_WriteByte(TXB0SIDL,0x68); MCP2515_WriteByte(TXB0EID8,0x00); MCP2515_WriteByte(TXB0EID0,0x00);
2、清空接受缓冲器标准与扩展标识符 MCP2515_WriteByte(RXB0SIDH,0x00); MCP2515_WriteByte(RXB0SIDL,0x00); MCP2515_WriteByte(RXB0EID8,0x00); MCP2515_WriteByte(RXB0EID0,0x00);
3、设定接受的报文类别, 11 = 关闭屏蔽/滤波功能;接收所有报文 10 = 只接收符合滤波器条件的带有扩展标识符的有效报文 01 = 只接收符合滤波器条件的带有标准标识符的有效报文 00 = 接收符合滤波器条件的所有带扩展标识符或标准标识符的有效报文
MCP2515_WriteByte(RXB0CTRL,0x00);
4、配置滤波寄存器标准标识符与扩展标识符,配置为扩展报文滤波。 MCP2515_WriteByte(RXF0SIDH,0x00); MCP2515_WriteByte(RXF0SIDL,0x48); MCP2515_WriteByte(RXF0EID8,0x00); MCP2515_WriteByte(RXF0EID0,0x02);
5、配置屏蔽寄存器的标准报文与扩展报文位 MCP2515_WriteByte(RXM0SIDH,0xff); MCP2515_WriteByte(RXM0SIDL,0xE0); MCP2515_WriteByte(RXM0EID8,0xff); MCP2515_WriteByte(RXM0EID0,0xFC);
注意接受的报文滤波应该设置为发送方的发送报文ID,这样就可以选择接受对方的信号了。 A机的MCP初始化代码为: void MCP2515_Init(void) { unsigned char temp=0;
MCP2515_Reset(); Delay_Nms(1);
MCP2515_WriteByte(CNF1,CAN_125Kbps); MCP2515_WriteByte(CNF2,0x80|PHSEG1_3TQ|PRSEG_1TQ); MCP2515_WriteByte(CNF3,PHSEG2_3TQ); MCP2515_WriteByte(TXB0SIDH,0x00); MCP2515_WriteByte(TXB0SIDL,0x68); MCP2515_WriteByte(TXB0EID8,0x00); MCP2515_WriteByte(TXB0EID0,0x00); MCP2515_WriteByte(RXB0SIDH,0x00); MCP2515_WriteByte(RXB0SIDL,0x00); MCP2515_WriteByte(RXB0EID8,0x00); MCP2515_WriteByte(RXB0EID0,0x00); MCP2515_WriteByte(RXB0CTRL,0x00); MCP2515_WriteByte(RXB0DLC,DLC_1); MCP2515_WriteByte(RXF0SIDH,0x00); MCP2515_WriteByte(RXF0SIDL,0x48); MCP2515_WriteByte(RXF0EID8,0x00); MCP2515_WriteByte(RXF0EID0,0x02);
MCP2515_WriteByte(RXM0SIDH,0xff); MCP2515_WriteByte(RXM0SIDL,0xE0); MCP2515_WriteByte(RXM0EID8,0xff); MCP2515_WriteByte(RXM0EID0,0xFC); MCP2515_WriteByte(CANINTF,0x00); MCP2515_WriteByte(CANCTRL,REQOP_NORMAL|CLKOUT_ENABLED); temp=MCP2515_ReadByte(CANSTAT); if(OPMODE_NORMAL!=(temp&0xE0)) { MCP2515_WriteByte(CANCTRL,REQOP_NORMAL|CLKOUT_ENABLED); } }
B机的初始化代码为: void MCP2515_Init(void) { unsigned char temp=0;
MCP2515_Reset(); Delay_Nms(1);
MCP2515_WriteByte(CNF1,CAN_125Kbps); MCP2515_WriteByte(CNF2,0x80|PHSEG1_3TQ|PRSEG_1TQ); MCP2515_WriteByte(CNF3,PHSEG2_3TQ); MCP2515_WriteByte(TXB0SIDH,0x00); MCP2515_WriteByte(TXB0SIDL,0x48); MCP2515_WriteByte(TXB0EID8,0x00); MCP2515_WriteByte(TXB0EID0,0x00); MCP2515_WriteByte(RXB0SIDH,0x00); MCP2515_WriteByte(RXB0SIDL,0x00); MCP2515_WriteByte(RXB0EID8,0x00); MCP2515_WriteByte(RXB0EID0,0x00); MCP2515_WriteByte(RXB0CTRL,0x00); MCP2515_WriteByte(RXB0DLC,DLC_1); MCP2515_WriteByte(RXF0SIDH,0x00); MCP2515_WriteByte(RXF0SIDL,0x68); MCP2515_WriteByte(RXF0EID8,0x00); MCP2515_WriteByte(RXF0EID0,0x01);
MCP2515_WriteByte(RXM0SIDH,0xff); MCP2515_WriteByte(RXM0SIDL,0xE0); MCP2515_WriteByte(RXM0EID8,0xff); MCP2515_WriteByte(RXM0EID0,0xFC); MCP2515_WriteByte(CANINTF,0x00); MCP2515_WriteByte(CANCTRL,REQOP_NORMAL|CLKOUT_ENABLED); temp=MCP2515_ReadByte(CANSTAT); if(OPMODE_NORMAL!=(temp&0xE0)) { MCP2515_WriteByte(CANCTRL,REQOP_NORMAL|CLKOUT_ENABLED); } }
因为扩展屏蔽寄存器的低位是:1111 1100 实际上,扩展ID的低位0-3都可以接受到。
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作! |