;This assembly program implements the self-test mode of CAN bus communication sending buffer 0 to send data to receiving buffer 0. The microcontroller uses P18F458, in which reception uses interrupt mode and transmission uses query mode. For the c51 program of this example, please open http://www.51hei.com/mcu/555.html, the process is the same as this one.
LIST P=18F458
INCLUDE "P18F458.INC"
CAN_FLAG EQU 0X20 ; define flag register
ORG 0X00
GOTO MAIN
ORG 0X18
GOTO CAN_INTSERVE ;Go to interrupt service subroutine
ORG 0X30
;************Initialization subroutine****************
INITIAL
BCF TRISB, 2
BSF TRISB, 3 ;Set CANRX/RB3 as input
; CANTX/RB2 as output
; Set the baud rate of CAN to 125K, when Fosc=4M, Tbit=8us, assuming BRP=01h, then
; TQ=[2*(1+BRP)]/Fosc=2*(1+1)/4=1us.
;NOMINAL BIT RATE=8TQ,SJW=1,Sync_Seg=1TQ,Prog _Seg=1TQ,Phase_Seg1=3TQ,Phase_Seg2=3TQ
MOVLW 0X80
MOVWF CANCON ;Request to enter CAN configuration mode REQOP=100
WAIT
BTFSS CANSTAT,OPMODE2 ;Wait to enter CAN configuration mode OPMODE=100
GOTO WAIT
MOVLW 0X01
MOVWF BRGCON1 ;Set SJW and BRP, SJW=1TQ, BRP=01H
MOVLW 0X90
MOVWF BRGCON2 ;Set Phase_Seg1=3TQ and Prog _Seg=1TQ
MOVLW 0X42
MOVWF BRGCON3 ;Set Phase_Seg2=3TQ
;Set the identification number of the sending mailbox 0 and the data to be sent
MOVLW 0XFF
MOVWF BSR ;The address of TXB0D0 to TXB0D7 is within F60h, so you need
to specify BSR
MOVLW 0X08
MOVWF TXB0DLC ;Set the data length to 8 bytes
MOVLW 0X00
MOVWF TXB0D0
MOVLW 0X01
MOVWF TXB0D1
MOVLW 0X02 MOVWF TXB0D2 MOVLW 0X03
MOVWF
TXB0D3
MOVLW
0X04
MOVWF TXB0D4
MOVLW 0X05
MOVWF
TXB0D5
MOVLW 0X06
MOVWF TXB0D6
MOVLW 0X07
MOVWF TXB0D7 ;Write the data in the data area of the send buffer
MOVLW 0XFF
MOVWF TXB0SIDH
MOVLW 0XE0
MOVWF TXB0SIDL ;Set the standard identifier of send buffer 0, this program uses
; standard identifier
; Set the identifier and initialization data of receive mailbox 0
MOVLW 0XFF
MOVWF RXB0SIDH
MOVLW
0XE0
MOVWF RXB0SIDL ;Set the identifier of receive buffer 0
MOVLW 0XFF
MOVWF RXF0SIDH
MOVLW 0XE0
MOVWF RXF0SIDL ;Initialize receive filter 0
MOVLW 0X00
MOVWF RXM0SIDH
MOVLW 0X00
MOVWF RXM0SIDL ;Initialize receive mask
MOVLW 0X20
MOVWF RXB0CON ;Only receive valid information of standard identifier, FILHIT0=0
;Indicates that RXB0 uses filter0
MOVLW 0X08
MOVWF RXB0DLC ;Set the length of the data area of receive buffer 0
MOVLW 0X00
MOVWF RXB0D0
MOVWF RXB0D1
MOVWF RXB0D2
MOVWF RXB0D3
MOVWF RXB0D4
MOVWF RXB0D5
MOVWF RXB0D6
MOVWF RXB0D7 ;Initialize the data area of receive buffer 0
;Initialize the I/O control register of CAN module
MOVLW 0X00
MOVWF CIOCON
;Make CAN enter a certain working mode
MOVLW 0X00
MOVWF CANCON ;=0X40, enter self-test mode;
;=0x00, normal operation mode
WAIT1
MOVF CANSTAT
ANDLW 0XE0
SUBLW 0X00
BTFSS STATUS, Z ;Wait to enter CAN normal operation mode OPMODE=000
; or test mode OPMODE=010
GOTO WAIT1
;Initialize CAN interrupt
MOVLW 0X00
MOVWF PIR3 ;Clear all interrupt flags
BSF PIE3, RXB0IE ;Enable receive interrupt of receive buffer 0
MOVLW 0X01
MOVWF IPR3 ;Receive interrupt of receive buffer 0 is the highest priority
RETURN
;************Receive buffer 0 receive interrupt service routine************
CAN_INTSERVE
BTFSS PIR3, RXB0IF
GOTO ERR_EXIT
BSF CAN_FLAG, 0
BCF PIR3, RXB0IF ; Clear receive interrupt flag
BCF RXB0CON, RXFUL ; Open receive buffer to receive new information
ERR_EXIT
RETFIE
;****************PIC MCU CAN bus communication assembly program main program******************
;****************This program was first published on
http://www.51hei.com/
Single chip network reprint please keep
MAIN NOP
CLRF INTCON ;Disable all interrupts
CALL INITIAL ;Initialize
BSF INTCON, GIE
BSF INTCON, PEIE ;Enable interrupt
MOVLW 0X03
MOVWF TXB0CON ;The transmission priority is the highest priority, TXPRI=11
LOOP
BSF TXB0CON, TXREQ ;Request to send, TXREQ=1
WAITE2
BTFSS PIR3, TXB0IF ;Wait for sending to complete
GOTO WAITE2
BCF PIR3, TXB0IF
WAITE3
BTFSS CAN_FLAG, 0 ;Wait for receiving data
GOTO WAITE3
BCF CAN_FLAG, 0 ;Clear received flag
BCF TXB0CON, TXREQ ;Disable sending
INCF RXB0D0,0
MOVWF TXB0D0
INCF RXB0D1,0
MOVWF TXB0D1
INCF RXB0D2,0
MOVWF
TXB0D2
INCF RXB0D3,0
MOVWF TXB0D3
INCF RXB0D4,0
MOVWF TXB0D4
INCF RXB0D5,0
MOVWF TXB0D5
INCF RXB0D6,0
MOVWF TXB0D6
INCF RXB0D7,0
MOVWF TXB0D7 ; Update the transmit data with the receive data plus 1
GOTO LOOP
RETURN
END
Previous article:PIC single chip stopwatch program design
Next article:PIC Microcontroller Study Notes
- Popular Resources
- Popular amplifiers
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Innolux's intelligent steer-by-wire solution makes cars smarter and safer
- 8051 MCU - Parity Check
- How to efficiently balance the sensitivity of tactile sensing interfaces
- What should I do if the servo motor shakes? What causes the servo motor to shake quickly?
- 【Brushless Motor】Analysis of three-phase BLDC motor and sharing of two popular development boards
- Midea Industrial Technology's subsidiaries Clou Electronics and Hekang New Energy jointly appeared at the Munich Battery Energy Storage Exhibition and Solar Energy Exhibition
- Guoxin Sichen | Application of ferroelectric memory PB85RS2MC in power battery management, with a capacity of 2M
- Analysis of common faults of frequency converter
- In a head-on competition with Qualcomm, what kind of cockpit products has Intel come up with?
- Dalian Rongke's all-vanadium liquid flow battery energy storage equipment industrialization project has entered the sprint stage before production
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- New real-time microcontroller system from Texas Instruments enables smarter processing in automotive and industrial applications
- SPI settings for msp430f149
- [Image recognition classification & motion detection & analog signal processing system based on Raspberry Pi 400, first post] MJPEG
- MaixSense R329 development board plays Bad Apple demo
- Please recommend a board
- Streamlining mobile phone system design
- What? Intel is sued for infringing the Chinese Academy of Sciences' FinFET patent
- Question: How to paste the attachment
- EEWORLD University - High-Speed Transimpedence Amplifier Design Process
- PCB
- It seems that a problem with gcc compiling threadx has been discovered