JasonWon 发表于 2014-11-17 16:29
因为用的不同单片机,STM32只发,C8051F040只收,C8051F只要收到就点亮LED,但始终收不到,麻烦你帮我看 ...
void init_msg_object_RX (char MsgNum,uchar id,uchar len)
{
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<<2;//标准id为ID28-ID18,所以要左移2位
temp&=0x1fff;
temp|=0x8000;
CAN0DAT = temp;
/*IF2 Arbitration Registers2=100(id)00b
MsgVal=1,Xtd=0,为标准模式,扩展ID无效,Dir=0,为接收*/
CAN0DAT = 0x0480|len;
/*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 receive_data(uchar MsgNum,uchar *p,uchar len)
{
char SFRPAGE_PAGE=SFRPAGE;
int i=0;
SFRPAGE = CAN0_PAGE;
CAN0ADR = IF2CMDMSK;
CAN0DATL = 0x0f;
CAN0ADR = IF2CMDRQST;
CAN0DATL = MsgNum;
CAN0ADR = IF2DATA1;
//连续接收8个字节,存入接收缓冲器中
for(i=0;i<len;i++)
{
*p++=CAN0DATH;
i++;
*p++=CAN0DATL;
}
SFRPAGE=SFRPAGE_PAGE;
}
init_msg_object_RX (MSGR,ADR2,LEN);//MSGR是 单片机用来收发数据的消息对象,ADR2为ID=0x02,数据长度LEN=8
int main()
{
if(rdata[0]== 0x01)
{
LED=0;
}
else
{
LED=1;
}
}
|