📄 can_bus.c
字号:
data5 DATA 17H
data6 DATA 18H
data7 DATA 19H
;/* baud rate and bit timing parameters */
#define BRP_500k 00
#define SJW_500k 00
#define PRS_500k 02
#define PHS1_500k 03
#define PHS2_500k 03
org 000h
ljmp begin
org 3Bh
ljmp can_it
;/**
; * FUNCTION_PURPOSE: This file set up Can at 500Kbauds 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 */
13
4347A–CAN–06/04
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_500k
RL A
MOV CANBT1,A
ANL CANBT2,#9Fh ;/* reset SJW */
MOV A,#SJW_500k
SWAP A
RL A
ANL A,#0E0h
ORL CANBT2,A ;/* SJW=0x00; */
ANL CANBT2,#0F1h ;/* reset PRS */
MOV A,#PRS_500k
RL A
ORL CANBT2,A ;/* PRS=0x02; */
ANL CANBT3,#8Fh ;/* reset PHS2 */
MOV A,#PHS2_500k
SWAP A
ANL A,#0F0h
ORL CANBT3,A ;/* PHS2=0x03;*/
ANL CANBT3,#0F1h ;/* reset PHS1 */
MOV A,#PHS1_500k
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 */
14
4347A–CAN–06/04
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:
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
15
4347A–CAN–06/04
;/* 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
16
4347A–CAN–06/04
3.2 SFR Register Definition
;*INC***********************************************************************
**
; NAME: 89C51CC01.inc
;---------------------------------------------------------------------------
-
; PURPOSE: for Keil
;***************************************************************************
**
;----------------------------------------
; Include file for 8051 SFR Definitions
;----------------------------------------
; BYTE Register
P0 DATA 80H
P1 DATA 90H
P2 DATA 0A0H
P3 DATA 0B0H
RD BIT 0B7H
WR BIT 0B6H
T1 BIT 0B5H
T0 BIT 0B4H
INT1 BIT 0B3H
INT0 BIT 0B2H
TXD BIT 0B1H
RXD BIT 0B0H
P4 DATA 0C0H
PSW DATA 0D0H
CY BIT 0D7H
AC BIT 0D6H
F0 BIT 0D5H
RS1 BIT 0D4H
RS0 BIT 0D3H
OV BIT 0D2H
P BIT 0D0H
ACC DATA 0E0H
B DATA 0F0H
SP DATA 81H
DPL DATA 82H
DPH DATA 83H
PCON DATA 87H
CKCON DATA 8FH
;------------------ TIMERS registers ---------------------
TCON DATA 88H
17
4347A–CAN–06/04
TF1 BIT 8FH
TR1 BIT 8EH
TF0 BIT 8DH
TR0 BIT 8CH
IE1 BIT 8BH
IT1 BIT 8AH
IE0 BIT 89H
IT0 BIT 88H
TMOD DATA 89H
T2CON DATA 0C8H
TF2 BIT 0CFH
EXF2 BIT 0CEH
RCLK BIT 0CDH
TCLK BIT 0CCH
EXEN2 BIT 0CBH
TR2 BIT 0CAH
C_T2 BIT 0C9H
CP_RL2 BIT 0C8H
T2MOD DATA0C9H
TL0 DATA 8AH
TL1 DATA 8BH
TL2 DATA0CCH
TH0 DATA 8CH
TH1 DATA 8DH
TH2 DATA0CDH
RCAP2L DATA0CAH
RCAP2H DATA0CBH
WDTRST DATA0A6H
WDTPRG DATA0A7H
;------------------- UART registers ------------------------
SCON DATA 98H
SM0 BIT 9FH
FE BIT9FH
SM1 BIT9EH
SM2 BIT9DH
REN BIT9CH
TB8 BIT9BH
RB8 BIT9AH
TI BIT99H
RI BIT98H
SBUF DATA 99H
SADEN DATA0B9H
SADDR DATA0A9H
;-------------------- ADC registers ----------------------
18
4347A–CAN–06/04
ADCLK DATA0F2H
ADCON DATA0F3H
ADDL DATA0F4H
ADDH DATA0F5H
ADCF DATA0F6H
;-------------------- FLASH EEPROM registers ------------
FPGACON DATA0F1H
FCON DATA0D1H
EECON DATA0D2H
AUXR DATA8EH
AUXR1 DATA0A2H
;-------------------- IT registers -----------------------
IPL1 DATA0F8H
IPH1 DATA0F7H
IEN0 DATA0A8H
IPL0 DATA0B8H
IPH0 DATA0B7H
IEN1 DATA0E8H
; IEN0
EA BIT 0AFH
EC BIT 0AEH
ET2 BIT 0ADH
ES BIT 0ACH
ET1 BIT 0ABH
EX1 BIT 0AAH
ET0 BIT 0A9H
EX0 BIT 0A8H
; IEN1
ETIM BIT 0EAH
EADC BIT 0E9H
ECAN BIT 0E8H
;--------------------- PCA registers --------------------
CCON DATA0D8H
CF BIT 0DFH
CR BIT 0DEH
CCF4BIT0D4H
CCF3BIT0D3H
CCF2BIT0D2H
CCF1BIT0D1H
CCF0BIT0D0H
CMOD DATA0D9H
CH DATA0F9H
CL DATA0E9H
CCAP0H DATA0FAH
19
4347A–CAN–06/04
CCAP0L DATA0EAH
CCAPM0 DATA0DAH
CCAP1H DATA0FBH
CCAP1L DATA0EBH
CCAPM1 DATA0DBH
CCAP2H DATA0FCH
CCAP2L DATA0ECH
CCAPM2 DATA0DCH
CCAP3H DATA0FDH
CCAP3L DATA0EDH
CCAPM3 DATA0DDH
CCAP4H DATA0FEH
CCAP4L DATA0EEH
CCAPM4 DATA0DEH
;------------------- CAN registers --------------------------
CANGIT DATA 09BH
CANTEC DATA 09CH
CANREC DATA 09DH
CANTCON DATA 0A1H
CANMSG DATA 0A3H
CANTTCL DATA 0A4H
CANTTCH DATA 0A5H
CANGSTA DATA 0AAH
CANGCON DATA 0ABH
CANTIML DATA 0ACH
CANTIMH DATA 0ADH
CANSTMPL DATA 0AEH
CANSTMPH DATA 0AFH
CANPAGE DATA 0B1H
CANSTCH DATA 0B2H
CANCONCH DATA 0B3H
CANBT1 DATA 0B4H
CANBT2 DATA 0B5H
CANBT3 DATA 0B6H
CANSIT1 DATA 0BAH
CANSIT2 DATA 0BBH
CANIDT1 DATA 0BCH
CANIDT2 DATA 0BDH
CANIDT3 DATA 0BEH
CANIDT4 DATA 0BFH
CANGIE DATA 0C1H
CANIE1 DATA 0C2H
CANIE2 DATA 0C3H
CANIDM1 DATA 0C4H
CANIDM2 DATA 0C5H
CANIDM3 DATA 0C6H
CANIDM4 DATA 0C7H
CANEN1 DATA 0CEH
CANEN2 DATA 0CFH
Printed on recycled paper.
Disclaimer: Atmel Corporation makes no warranty for the use of its products, other than those expressly contained in the Company’s standard
warranty which is detailed in Atmel’s Terms and Conditions located on the Company’s web site. The Company assumes no responsibility for any
errors which may appear in this document, reserves the right to change devices or specifications detailed herein at any time without notice, and
does not make any commitment to update the information contained herein. No licenses to patents or other intellectual property of Atmel are
granted by the Company in connection with the sale of Atmel products, expressly or by implication. Atmel’s products are not authorized for use
as critical components in life support devices or systems.
Atmel Corporation Atmel Operations
2325 Orchard Parkway
San Jose, CA 95131
Tel: 1(408) 441-0311
Fax: 1(408) 487-2600
Regional Headquarters
Europe
Atmel Sarl
Route des Arsenaux 41
Case Postale 80
CH-1705 Fribourg
Switzerland
Tel: (41) 26-426-5555
Fax: (41) 26-426-5500
Asia
Room 1219
Chinachem Golden Plaza
77 Mody Road Tsimshatsui
East Kowloon
Hong Kong
Tel: (852) 2721-9778
Fax: (852) 2722-1369
Japan
9F, Tonetsu Shinkawa Bldg.
1-24-8 Shinkawa
Chuo-ku, Tokyo 104-0033
Japan
Tel: (81) 3-3523-3551
Fax: (81) 3-3523-7581
Memory
2325 Orchard Parkway
San Jose, CA 95131
Tel: 1(408) 441-0311
Fax: 1(408) 436-4314
Microcontrollers
2325 Orchard Parkway
San Jose, CA 95131
Tel: 1(408) 441-0311
Fax: 1(408) 436-4314
La Chantrerie
BP 70602
44306 Nantes Cedex 3, France
Tel: (33) 2-40-18-18-18
Fax: (33) 2-40-18-19-60
ASIC/ASSP/Smart Cards
Zone Industrielle
13106 Rousset Cedex, France
Tel: (33) 4-42-53-60-00
Fax: (33) 4-42-53-60-01
1150 East Cheyenne Mtn. Blvd.
Colorado Springs, CO 80906
Tel: 1(719) 576-3300
Fax: 1(719) 540-1759
Scottish Enterprise Technology Park
Maxwell Building
East Kilbride G75 0QR, Scotland
Tel: (44) 1355-803-000
Fax: (44) 1355-242-743
RF/Automotive
Theresienstrasse 2
Postfach 3535
74025 Heilbronn, Germany
Tel: (49) 71-31-67-0
Fax: (49) 71-31-67-2340
1150 East Cheyenne Mtn. Blvd.
Colorado Springs, CO 80906
Tel: 1(719) 576-3300
Fax: 1(719) 540-1759
Biometrics/Imaging/Hi-Rel MPU/
High Speed Converters/RF Datacom
Avenue de Rochepleine
BP 123
38521 Saint-Egreve Cedex, France
Tel: (33) 4-76-58-30-00
Fax: (33) 4-76-58-34-80
e-mail
literature@atmel.com
Web Site
http://www.atmel.com
4347A–CAN–06/04 /xM
?Atmel Corporation 2004. All rights reserved. Atmel, the Atmel logo, andcombinations thereof are registered trademarks of Atmel Corporation
or its subsidiaries. Windows? Windows 98?, Windows XP?, and Windows 2000? are trademarks and/ore registered trademark of Microsoft
Corporation. Other terms and product names in this document may be the trademarks of others.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -