📄 can.a51
字号:
$INCLUDE (t89c51cc01.INC)
num_channel DATA 10H
num_data DATA 11H
data0 DATA 12H
data1 DATA 13H
data2 DATA 14H
data3 DATA 15H
data4 DATA 16H
data5 DATA 17H
data6 DATA 18H
data7 DATA 19H
;/* baud rate and bit timing parameters at Fosc=12Mhz */
;/* refert to Xcalculator software on Atmel website for other bit timing */
#define BRP_100k 05
#define SJW_100k 00
#define PRS_100k 01
#define PHS1_100k 03
#define PHS2_100k 02
org 000h
ljmp begin
org 3Bh
ljmp can_it
;/**
; * FUNCTION_PURPOSE: This file set up Can at 100kbauds with channel 0 id 0x123 in reception
; * and channel 1 id 0x001 in emission.
; * FUNCTION_INPUTS: void
; * FUNCTION_OUTPUTS: void
; */
org 0100h
begin:
ORL CANGCON,#01h; ;/* reset CAN */
;/* reset all mailboxes */
MOV num_channel,#00h
reset_mailbox:
MOV A,num_channel ;/* load accumulator with channel value */
SWAP A
ANL A,#0F0h
MOV CANPAGE,A ;/* CHNB=num_channel; */
MOV CANCONCH,#00h ;/* channel disable */
MOV CANSTCH,#00h
MOV CANIDT1,#00h
MOV CANIDT2,#00h
MOV CANIDT3,#00h
MOV CANIDT4,#00h
MOV CANIDM1,#00h
MOV CANIDM2,#00h
MOV CANIDM3,#00h
MOV CANIDM4,#00h
MOV num_data,#00h
reset_data:
MOV CANMSG,#00h
INC num_data
MOV A,num_data
CJNE A,#08h,reset_data
INC num_channel
MOV A,num_channel
CJNE A,#0Fh,reset_mailbox
;/* setup bit timing */
MOV A,#BRP_100k
RL A
MOV CANBT1,A
ANL CANBT2,#9Fh ;/* reset SJW */
MOV A,#SJW_100k
SWAP A
RL A
ANL A,#0E0h
ORL CANBT2,A ;/* SJW=0x00; */
ANL CANBT2,#0F1h ;/* reset PRS */
MOV A,#PRS_100k
RL A
ORL CANBT2,A ;/* PRS=0x02; */
ANL CANBT3,#8Fh ;/* reset PHS2 */
MOV A,#PHS2_100k
SWAP A
ANL A,#0F0h
ORL CANBT3,A ;/* PHS2=0x03;*/
ANL CANBT3,#0F1h ;/* reset PHS1 */
MOV A,#PHS1_100k
RL A
ORL CANBT3,A ;/* PHS1=0x03 */
ORL CANGCON,#02h ;/* start CAN */
;/* Channel 0 init */
MOV A,#00h ;/* load accumulator with channel value */
SWAP A
ANL A,#0F0h
MOV CANPAGE,A ;/* CHNB=0x00; select channel 0 */
MOV CANSTCH,#00h ;/* reset channel staus */
MOV CANCONCH,#00h ;/* reset control and dlc register */
;/* Channel 0: identifier = 11bits. CANIDT=0x123 */
MOV CANIDT1,#24h
ANL CANIDT2,#07Fh
ORL CANIDT2,#060h
;/* Channel 0: mask = 11bits. 0x7F0 */
MOV CANIDM1,#0FEh
ANL CANIDM2,#1Fh
MOV CANIDM4,#00h
;/* Channel 0 configuration */
ANL CANIDT4,#0FBh ;/* clear bit rtr in CANIDT4. */
ORL CANCONCH,#08h ;/* Reception 8 bytes.*/
ORL CANCONCH,#80h ;/* Reception enabled without buffer.*/
;/* Channel 1 init */
MOV A,#01h ;/* load accumulator with channel value */
SWAP A
ANL A,#0F0h
MOV CANPAGE,A ;/* CHNB=0x01; select channel 1 */
MOV CANSTCH,#00h ;/* reset channel staus */
MOV CANCONCH,#00h ;/* reset control and dlc register */
;/* Channel 1: identifier = 11bits. CANIDT=0x001 */
MOV CANIDT1,#80h
ANL CANIDT2,#3Fh
ORL CANIDT2,#20h
;/* interrupt configuration */
ORL CANIE2,#01h ;/* IECH0=1 */
ORL CANGIE,#10h ;/* Can_Tx IT enable */
ORL CANGIE,#20h ;/* Can_Rx IT enable */
SETB ECAN ;/* CAN IT enable */
SETB EA ;/* all IT enable */
JMP $ ;/* endless */
;/**
; * FUNCTION_PURPOSE: can interrupt. echo receive data on channel 0 reception.
; * Reception id between 0x120 and 0x12F.
; * FUNCTION_INPUTS: P4.1(RxDC) can input
; * FUNCTION_OUTPUTS: P4.0(TxDC) can output
; */
can_it:
;/* CAUTION can interrupt function modify CANPAGE. Save CANPAGE at beginning
;and restore it at ending */
MOV R7,CANPAGE ;/* save current context */
;/* set channel */
MOV A,#00h ;/* load accumulator with channel value */
SWAP A
ANL A,#0F0h
MOV CANPAGE,A ;/* CHNB=0x00; select channel 0 */
;/* echo receive data on channel 0 reception */
MOV A,CANSTCH
CJNE A,#20h,end_if_rxok
;/* save receive data */
MOV data0,CANMSG
MOV data1,CANMSG
MOV data2,CANMSG
MOV data3,CANMSG
MOV data4,CANMSG
MOV data5,CANMSG
MOV data6,CANMSG
MOV data7,CANMSG
;/* set channel */
MOV A,#01h ;/* load accumulator with channel value */
SWAP A
ANL A,#0F0h
MOV CANPAGE,A ;/* CHNB=0x00; select channel 1 */
;/* Channel 1 configuration */
MOV CANCONCH,#00h; ;/* reset channel 1 configuration */
;/* load saved data */
MOV CANMSG,data0
MOV CANMSG,data1
MOV CANMSG,data2
MOV CANMSG,data3
MOV CANMSG,data4
MOV CANMSG,data5
MOV CANMSG,data6
MOV CANMSG,data7
ORL CANCONCH,#08h ;/* transmit 8 bytes */
ORL CANCONCH,#40h ;/* emission enabled */
ORL CANEN2,#02h ;/* channel 1 enable */
MOV CANSTCH,#00h ;/* reset channel 1 status */
end_if_rxok:
;/* set channel */
MOV A,#00h ;/* load accumulator with channel value */
SWAP A
ANL A,#0F0h
MOV CANPAGE,A ;/* CHNB=0x00; select channel 0 */
MOV CANCONCH,#00h ;/* reset channel 0 configuration */
ORL CANCONCH,#08h ;/* receive 8 bytes */
ORL CANCONCH,#80h ;/* reception enable */
ORL CANEN2,#01h ;/* channel 0 enable */
MOV CANSTCH,#00h ;/* reset channel 0 status */
MOV CANPAGE,R7 ;/* restore saved context */
MOV CANGIT,#00h ;/* reset all flags */
RETI
end
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -