大家好,是这样的,用两个c8051f500进行CAN通信时,一个为A节点,一个为B节点,A发送数据给B。主程序和CAN配置程序如下:
问题是,AB不能通信,STAT寄存器值为0X62,也就是提示Error Passive=1,也就是CAN处于 error passive state状态,Warning Status=1,也就是错误计数器计满,Form Error=1,也就是收到的数据帧中的固定的格式错误。
着实不知道该怎么弄了,目的就是实现两个51的CAN通信。各位路过的大神帮忙看一眼,拜谢拜谢!!!都快崩溃了啊…………
A发送的程序和B接收的程序在下面的帖子,拜谢各位大神!!
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
A发送程序:
这是主程序:
ORG 0000H
LJMP INITIAL ;初始化
MAIN: NOP
CLR A
MAIN01: LCALL CAN0Send
JNB CANTX,MAIN01
CLR CANTX
MOV A,#0FFH
MAIN02: DEC A
JNZ MAIN02
LJMP MAIN
这是发送函数:
CAN0Send:
MOV SDATA1,#01H
MOV SDATA2,#02H
MOV SDATA3,#03H
MOV SDATA4,#04H
MOV SDATA5,#05H
MOV SDATA6,#06H
MOV SDATA7,#07H
MOV SDATA8,#08H
MOV SFRPAGE,#0CH
MOV CAN0IF1DA1L,SDATA1
MOV CAN0IF1DA1H,SDATA2
MOV CAN0IF1DA2L,SDATA3
MOV CAN0IF1DA2H,SDATA4
MOV CAN0IF1DB1L,SDATA5
MOV CAN0IF1DB1H,SDATA6
MOV CAN0IF1DB2L,SDATA7
MOV CAN0IF1DB2H,SDATA8
SETB CANTX
MOV CAN0IF1CML,#87H ;Write TxRqst, all 8 data bytes
MOV CAN0IF1CRL,#02H ;Start command request
CAN0Send1:NOP
NOP
MOV A,CAN0IF1CRH
JB ACC.7,CAN0Send1
MOV A,#00H;
CAN0Send5:
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
DEC A;
JNZ CAN0Send5;
CAN0Send2: MOV A,CAN0STAT;
ANL A,#0E7H;
JNZ CAN0Send4;
MOV A,CAN0STAT;
ANL A,#08H;
JZ CAN0Send2;
CAN0Send3: XRL CAN0STAT,#08H;
MOV SFRPAGE,#00H
RET
CAN0Send4: LCALL iniCAN;
MOV SFRPAGE,#00H
RET;
这是CAN的配置:
iniCAN: MOV SFRPAGE,#0CH
MOV CAN0CN,#41H ;Start Intialization mode
;Enable access to bit timing register
MOV CAN0CFG,#00H; ;配置时钟等于系统时钟
NOP
NOP
NOP
NOP
NOP
NOP
NOP
MOV CAN0BTL,#0C8H;;CAN0BTL,#47H ;CAN bit rate to 125Kbps
MOV CAN0BTH,#06FH;;CAN0BTH,#5FH
;Message Object 1用于接收
MOV CAN0IF1CML,#0F0H ;Write Mask,Arbitration,Control to Message Object
;IF1 Mask1
MOV CAN0IF1M1L,#00H ;Mask Bits 15-0 not used for filtering
MOV CAN0IF1M1H,#00H
;IF1 Mask2
MOV CAN0IF1M2L,#0FCH ;Ignore Extended Identifier for filtering
MOV CAN0IF1M2H,#5FH ;Used Direction bit for filtering Use ID bits 28-18 for filtering(11个ID位都要仲裁)
;IF1 Arbitration1
MOV CAN0IF1A1L,#00H ;11-bit ID, so lower 16-bits not used
MOV CAN0IF1A1H,#00H
;IF1 Message Control Register
MOV CAN0IF1MCL,#88H ;Message Size=8 Message Object is a Single Message
MOV CAN0IF1MCH,#14H ;Enable Receive Interrupt Use Acceptance Mask
;IF1 Arbitration2
;A_RECEIVE_ID
MOV A,#IDCONST2
MOV B,#64
DIV AB
ORL A,#80H
MOV CAN0IF1A2H,A
MOV A,#4
MUL AB
MOV CAN0IF1A2L,A
MOV CAN0IF1CRL,#01H ;Start command request
INITCPU1:NOP
NOP
MOV A,CAN0IF1CRH
JB ACC.7,INITCPU1
;Message Object 2用于发送
;IF1 Message Control Register
MOV CAN0IF1MCL,#88H ;Message Size=8 Message Object is a Single Message
MOV CAN0IF1MCH,#08H ;Enable Transmit Interrupt
;IF1 Arbitration2
;A_SEND_ID
MOV A,#IDCONST1
MOV B,#64
DIV AB
ORL A,#0A0H
MOV CAN0IF1A2H,A
MOV A,#4
MUL AB
MOV CAN0IF1A2L,A
MOV CAN0IF1CRL,#02H ;Start command request
INITCPU2:NOP
NOP
MOV A,CAN0IF1CRH
JB ACC.7,INITCPU2
;Message Object 3—32不使用
MOV CAN0IF1A2L,#20H ;Set 11-bit Identifier to 08H
MOV CAN0IF1A2H,#20H ;Set MsgVal to unvalid Message Direction = transmit
MOV R0,#03H
INITCPU3:MOV CAN0IF1CRL,R0
INITCPU4:NOP
NOP
MOV A,CAN0IF1CRH
JB ACC.7,INITCPU4
INC R0
CJNE R0,#33,INITCPU3
MOV CAN0CN,#0EH ;Return to Normal Mode and disable access to bit timing register
RET;
一周热门 更多>