最简单的CAN测试程序,一个节点发,一个节点收
本帖最后由 winnie 于 2009-3-20 14:11 编辑#INCLUDE <P18F248.INC>
;发送数据是08H
;*************LOCAL MACROS***************
CONFIGURE_MODE MACRO
BSF CANCON,REQOP2
ENDM
NORMAL_MODEMACRO
BCF CANCON,REQOP2
BCF CANCON,REQOP1
BCF CANCON,REQOP0
BCF CANCON,ABAT
ENDM
;******************复位矢量中断向量*************
ORG 000h
NOP
GOTO MAIN
;*****************主程序*************************
MAIN
CALL CLR_BANK;CLEAR ALL BANK 0,1,2,15
;**********开始进入传输初始化的设定***************
SETUP
MOVLB 0FH ;BANK 15
MOVLW B'11000000';ENABLE ALL
MOVWF INTCON ;enable gie
MOVLW B'00000000';MOVWF PIE3
CALL SET_PORT;setport
CALL TXB0_IDENTIFIER;TXB0 FILTER
CALL RXB0_FILTER0;RXB0 FILTER0
CALL BAUD_RATE;50KB/S
NORMAL_MODE ;NORMAL MODE
MOVLW 08h ;发数据,现在是08H
MOVWF TXB0D0
BSF TXB0CON,TXREQ
BTFSS PIR3,TXB0IF;判断是否发出去
GOTO $-2
BCF PIR3,TXB0IF
STOP
NOP
GOTO STOP
;******************RXB0 FILTER0****************
;RXB0_FILTER0
;STANDARD FRAME
PORTA 低五位作为ID的高五位
;*********************************************
RXB0_FILTER0
CONFIGURE_MODE
BCFRXB0CON,RXM1
BCFRXB0CON,RXM0
;0,0 FILTER 有效
;0,1 ONLY STANDARD FRAME
;1,1 RECEIVE ALL
;1,0 ONLY EXTERNFRAME
MOVLW B'10101111'
MOVWF RXF0SIDH
MOVLW b'11100000'
MOVWF RXF0SIDL
MOVLW B'11111111';识别位XXXXX000111
MOVWF RXM0SIDH
MOVLW B'11111111';全部起作用
MOVWF RXM0SIDL;进行屏蔽位设定,全部为11
RETURN
;*********设置TXB0IDENTIFIER***********************
;TXB0_IDENTIFIER
;***************************************************
TXB0_IDENTIFIER
BCFTXB0CON,TXPRI1
BCFTXB0CON,TXPRI0;high interrupt
MOVLW B'10101111'
MOVWF TXB0SIDH
MOVLW B'11100000' ;标志位是XXXXX000111
MOVWF TXB0SIDL
MOVLW B'00000001'
MOVWF TXB0DLC ;数据帧 6,txrtr是零
RETURN
;***********SET_PORT******************************
SET_PORT
;********PORTB********
;RB0 OUT
;RB1 OUT
;RB2 OUT TXCAN
;RB3 IN RXCAN
;RB4 IN
;RB5 IN
;RB6ICD2
;RB7ICD2
CLRF LATB
MOVLW B'00001000'
MOVWF TRISB
;*******PORTC*********
;RC0 OUTRC0-RC7 ALL OUT
;RC1 OUT
;RC7 OUT
CLRF LATC
MOVLW B'00000000'
MOVWF TRISC
RETURN
;**************BAUD RATE SET******************
;FOSC =4MHZ
; NOMINAL BIT TIME = 8 TQ
;TQ=2*(BAUD RATE +1)/FOSC
;SYN=1 TQ,PRO=1TQ,PHE1=3TQ,PHE2=3TQ
;BAUD RATE= 50KB/S
;BAUD RATE PRESCALER=00100
; fosc =4mhz MAX_baud rate=250kb/s
;If fosc=16Mhz ,the max_band=1Mkb/s
;************************************************
BAUD_RATE ;50KB/S
CONFIGURE_MODE
MOVLW B'00000100';BAUD RATE SET
MOVWF BRGCON1
MOVLW B'10010000'
MOVWF BRGCON2
MOVLW B'00000010'
MOVWF BRGCON3
RETURN
;**********CLEAR BANK************************
CLR_BANK
LFSR FSR0,00H;BANK 0
NEXT CLRF POSTINC0
BTFSS FSR0H,0
GOTO NEXT
LFSR FSR0,100H
NEXT1 CLRF POSTINC0;BANK1
BTFSS FSR0H,1
GOTO NEXT1
LFSR FSR0,200H
NEXT2 CLRF POSTINC0;BANK 2
BTFSS FSR0H,0
GOTO NEXT2
LFSR FSR0,0F00H
NEXT3 CLRF POSTINC0;BANK 15
BTFSS FSR0L,7
GOTO NEXT3
RETURN
END
#INCLUDE <P18F248.INC>
; 接收数据
;*************LOCAL MACROS***************
CONFIGURE_MODE MACRO
BSF CANCON,REQOP2
ENDM
NORMAL_MODEMACRO
BCF CANCON,REQOP2
BCF CANCON,REQOP1
BCF CANCON,REQOP0
BCF CANCON,ABAT
ENDM
;******************复位矢量中断向量*************
ORG000h
NOP
GOTOMAIN
ORG0008h
RXB0_INTERRUPT
BCFRXB0CON,RXFUL;必须清零,否则会溢出到RXB1D0
BCFPIR3,RXB0IF
MOVFFRXB0D0,PORTC ;接收到放到PORTC显示
RETFIE
;*****************主程序*************************
MAIN
CALL CLR_BANK;CLEAR ALL BANK 0,1,2,15
;**********开始进入传输初始化的设定***************
SETUP
MOVLB 0FH ;BANK 15
MOVLW B'11000000';ENABLE ALL
MOVWF INTCON ;enable gie
MOVLW B'00000001';ONLY ENABLE RXB0IE
MOVWF PIE3
CALL SET_PORT;setport
CALL RXB0_FILTER0;RXB0 FILTER0
CALL BAUD_RATE;250KB/S
NORMAL_MODE ;NORMAL MODE
STOP
NOP
GOTO STOP
;******************RXB0 FILTER0****************
;RXB0_FILTER0
;STANDARD FRAME
;*********************************************
RXB0_FILTER0
CONFIGURE_MODE
BCFRXB0CON,RXM1
BCFRXB0CON,RXM0
;0,0 FILTER 有效
;0,1 ONLY STANDARD FRAME
;1,1 RECEIVE ALL
;1,0 ONLY EXTERNFRAME
MOVLW B'10101111'
MOVWF RXF0SIDH;
MOVLW b'11100000'
MOVWF RXF0SIDL
MOVLW B'11111111';识别位XXXXX000111
MOVWF RXM0SIDH
MOVLW B'11111111';全部起作用
MOVWF RXM0SIDL;进行屏蔽位设定,全部为11
RETURN
;***********SET_PORT******************************
SET_PORT
;********PORTB********
;RB0 OUT
;RB1 OUT
;RB2 OUT TXCAN
;RB3 IN RXCAN
;RB4 IN
;RB5 IN
;RB6ICD2
;RB7ICD2
CLRF LATB
MOVLW B'00001000'
MOVWF TRISB
;*******PORTC*********
;RC0 OUTRC0-RC7 ALL OUT
;RC1 OUT
;RC7 OUT
CLRF LATC
MOVLW B'00000000'
MOVWF TRISC
RETURN
;**************BAUD RATE SET******************
;FOSC =4MHZ
; NOMINAL BIT TIME = 8 TQ
;TQ=2*(BAUD RATE +1)/FOSC
;SYN=1 TQ,PRO=1TQ,PHE1=3TQ,PHE2=3TQ
;BAUD RATE= 50KB/S
;BAUD RATE PRESCALER=00100
; fosc =4mhz MAX_baud rate=250kb/s
;If fosc=16Mhz ,the max_band=1Mkb/s
;************************************************
BAUD_RATE ;50KB/S
CONFIGURE_MODE
MOVLW B'00000100';BAUD RATE SET
MOVWF BRGCON1
MOVLW B'10010000'
MOVWF BRGCON2
MOVLW B'00000010'
MOVWF BRGCON3
RETURN
;**********CLEAR BANK************************
CLR_BANK
LFSR FSR0,00H;BANK 0
NEXT CLRF POSTINC0
BTFSS FSR0H,0
GOTO NEXT
LFSR FSR0,100H
NEXT1 CLRF POSTINC0;BANK1
BTFSS FSR0H,1
GOTO NEXT1
LFSR FSR0,200H
NEXT2 CLRF POSTINC0;BANK 2
BTFSS FSR0H,0
GOTO NEXT2
LFSR FSR0,0F00H
NEXT3 CLRF POSTINC0;BANK 15
BTFSS FSR0L,7
GOTO NEXT3
RETURN
END
页:
[1]