PIC microcontroller CAN bus communication assembly program

Publisher:电子创意达人Latest update time:2012-12-11 Source: 51hei Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

;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




































Reference address:PIC microcontroller CAN bus communication assembly program

Previous article:PIC single chip stopwatch program design
Next article:PIC Microcontroller Study Notes

Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号