//////can.h//////////////////
#include "C8051F040.h"
#include "canreg.h"
typedef unsigned char uchar;
typedef unsigned int uint;
typedef unsigned long ulong;
union intchar{
uint tempval;
struct {uchar hi;uchar low;}bytek;
};
bit isnewdata;
data uchar temppage;
data uchar status;
xdata uchar sdata[8]={0x32,0x33,0x34,0x35,0xb2,0xb3,0xb4,0xb5};//发送缓冲区
xdata union intchar rdata[4];
void init_msg_object_TX (uchar MsgNum,uchar id);
void init_msg_object_RX (uchar MsgNum,uchar id);
void clear_msg_objects (void);
void start_CAN (void);
void init_msg_object_TX (char MsgNum,uint id)
{
uint temp;
SFRPAGE = CAN0_PAGE;
CAN0ADR = IF1CMDMSK; //指向IF1 Command Mask Registers
CAN0DAT = 0x00b3;
/* IF1 Command Mask Registers =0x00b3
WR/RD=1,Mask=0,Arb=1,Control=1,ClrIntPnd=0,TxRqst=0,DataA=1,DataB=1,一次发送8字节数据*/
CAN0ADR = IF1ARB1;
CAN0DAT = 0x0000;
/*IF1 Arbitration Registers1 =0x0000,即ID15-0=0*/
temp=id temp&=0x1fff;
temp|=0xa000;
CAN0DAT = temp; //地址自增,指向IF1 Arbitration Registers2
/*IF1 Arbitration Registers2=101(id)00b
MsgVal=1,Xtd=0,为标准模式,扩展ID无效,Dir=1,为发送*/
CAN0DAT = 0x0088;
/*IF1 Message Control Registers=0x0088
NewDat=0,MsgLst=0,IntPnd=0,UMask=0,TxIE=0,RxIE=0,RmtEn=0,TxRqst=0
EoB=1,DLC3-0=1000,即数据长度为8*/
CAN0ADR = IF1CMDRQST;
CAN0DAT = MsgNum;
/*IF1 Command Request Registers=MsgNum,将以上配置写入MsgNum号消息*/
}
void init_msg_object_RX (char MsgNum,uchar id)
{
uint temp;
SFRPAGE = CAN0_PAGE;
CAN0ADR = IF2CMDMSK;
CAN0DAT = 0x00bb;
/* IF2 Command Mask Registers =0x00bb
WR/RD=1,Mask=0,Arb=1,Control=1,ClrIntPnd=1,TxRqst=0,DataA=1,DataB=1,一次发送8字节数据*/
CAN0ADR = IF2ARB1;
CAN0DAT = 0x0000;
/*IF2 Arbitration Registers1 =0x0000,即ID15-0=0*/
temp=id temp&=0x1fff;
temp|=0x8000;
CAN0DAT = temp;
/*IF2 Arbitration Registers2=100(id)00b
MsgVal=1,Xtd=0,为标准模式,扩展ID无效,Dir=0,为接收*/
CAN0DAT = 0x0488;
/*IF2 Message Control Registers=0x0488
NewDat=0,MsgLst=0,IntPnd=0,UMask=0,TxIE=0,RxIE=1,接收中断使能;RmtEn=0,TxRqst=0
EoB=1,DLC3-0=1000,即数据长度为8*/
CAN0ADR = IF2CMDRQST;
CAN0DATL = MsgNum;
/*IF2 Command Request Registers=MsgNum,将以上配置写入MsgNum号消息*/
}
void clear_msg_objects (void) //将所有消息清零
{
uchar i;
SFRPAGE = CAN0_PAGE;
CAN0ADR = IF1CMDMSK;
CAN0DATL = 0xFF;
for (i=1;i {
CAN0ADR = IF1CMDRQST;
CAN0DATL = i;
}
}
void start_CAN (void)
{
SFRPAGE = CAN0_PAGE;
CAN0CN= 0x02;
/*IE置1,使能CAN模块中断,Init清零,结束初始化操作,CAN控制器进入正常工作*/
}