Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Help me fix my code for communicating over CAN with 18F4580

Status
Not open for further replies.

viks

Member level 1
Joined
Oct 8, 2005
Messages
40
Helped
2
Reputation
4
Reaction score
0
Trophy points
1,286
Activity points
1,604
Guys

I am tired with this problem ,please help me out here.
I am trying to communicate over CAN network.

I have two 18F4580
one as Transmitter and another as Receiver.
I am not able to get any data from Trans to Receiver.

Code as follows

-------------------------------------------------------
Transmitter.

list p=18f4580 ; list directive to define processor
radix dec
#include <p18f4580.inc> ; processor specific variable definitions


;************ Identifier is 123H

BRGCON1_CONST EQU 0x03; SJW = 2 Tq, BRP = 3
BRGCON2_CONST EQU 0xA1; Seg2 freely programmable,
; 1 samples/bit, Ph1 = 5 Tq, Prop seg = 2 Tq
BRGCON3_CONST EQU 0x01; Seg2 = 2 Tq ---> Sample at 80%
START_CAN EQU 0x08; request normal mode, abort all xmissions, TXB0 in access bank
TXB0CON_CONST EQU 0x03; value to be loaded into TXB0CON to allow write access to TXB0
; requests message abort, set buffer priority to 3 (highest)
TXB0CON_SEND EQU 0x08; requests that TXB0 be sent on CAN bus
DLC_CONST EQU 0x08; value for DLC field of message
CIOCON_CONST EQU 0x20; value to load into CIOCON
; Drive TX pin to Vdd, no CAN Capture function on RC2

goto start



;main_code code 0x2A
org 0x002A
start

movlw b'11111011' ; RB2 is output (CANTX)
movwf TRISB

; set up PORT D for use with LED on RD0, pulled up to Vcc.
movlw 0x00 ; All Port D pins are outputs
movwf TRISD
movlw 0x0E ; initialize LED to ON (D0=0), rest to OFF (D1, D2, D3 = 1). LCD data = 0 (D7:D4)
movwf PORTD
movlw TMR0_OFF ; sets up timer 0, but does not start it counting
movwf T0CON


; initialize CAN for TX Buffer 0
movlw CIOCON_CONST
movwf CIOCON

movlw 0x98 ;Configuration mode with buffer 0 selected.
movwf CANCON
check1:
btfss CANSTAT, OPMODE2
bra check1

movlw BRGCON1_CONST
movwf BRGCON1
movlw BRGCON2_CONST
movwf BRGCON2
movlw BRGCON3_CONST
movwf BRGCON3
movlw START_CAN
movwf CANCON ; start CAN in Normal Mode

; set up TX Buffer 0
movlw TXB0CON_CONST
movff WREG, TXB0CON ; use movff as TXB0CON is in bank 15
movlw 24H
movff WREG, TXB0SIDH
movlw 60H
movff WREG, TXB0SIDL
movlw DLC_CONST
movff WREG, TXB0DLC

; Load up Constant Data Bytes
movlw 0
movff WREG, TXB0D0
movlw 1
movff WREG, TXB0D1
movlw 2
movff WREG, TXB0D2
movlw 3
movff WREG, TXB0D3
movlw 4
movff WREG, TXB0D4
movlw 5
movff WREG, TXB0D5
movlw 6
movff WREG, TXB0D6
movlw 7
movff WREG, TXB0D7


; set request to transmit message
movlw TXB0CON_SEND
movff WREG, TXB0CON ; request sending TXB0 on bus

end

------------------------------------------
Receiver

list p=18f4580 ; list directive to define processor
#include <p18f4580.inc> ; processor specific variable definitions
BRGCON1_CONST EQU 0x03; SJW = 2 Tq, BRP = 3
BRGCON2_CONST EQU 0xA1; Seg2 freely programmable,
; 1 samples/bit, Ph1 = 5 Tq, Prop seg = 2 Tq
BRGCON3_CONST EQU 0x01; Seg2 = 2 Tq ---> Sample at 80%
START_CAN EQU 0x00; request normal mode, abort all xmissions, TXB0 in access bank
TXB0CON_CONST EQU 0x03; value to be loaded into TXB0CON to allow write access to TXB0
; requests message abort, set buffer priority to 3 (highest)
TXB0CON_SEND EQU 0x08; requests that TXB0 be sent on CAN bus
DLC_CONST EQU 0x08; value for DLC field of message
CIOCON_CONST EQU 0x20; value to load into CIOCON


;************ initial *******************

goto start

;main_code code 0x2A
org 0x002A
start

CLRF PORTA
MOVLW 0xff
MOVWF TRISA
MOVLW 0xff
MOVWF TRISC
CLRF PORTB
MOVLW 0xFB
MOVWF TRISB


; initialize CAN for TX Buffer 0
movlw CIOCON_CONST
movwf CIOCON

movlw 0x90 ;Configuration mode with buffer 0 selected.
movwf CANCON
check1:
btfss CANSTAT, OPMODE2
bra check1

movlw BRGCON1_CONST
movwf BRGCON1
movlw BRGCON2_CONST
movwf BRGCON2
movlw BRGCON3_CONST
movwf BRGCON3
movlw START_CAN
movwf CANCON ; start CAN in Normal Mode

;setup Rx Buffer


movlw 0x60; // Set to ignore all filters, masks
movwf RXB0CON


Nomessage BTFSS RXB0CON ,RXFUL
BRA Nomessage

MOVFF RXB0D0,var0
MOVFF RXB0D1,var1
MOVFF RXB0D2,var2
MOVFF RXB0D3,var3
MOVFF RXB0D4,var4
MOVFF RXB0D5,var5
MOVFF RXB0D6,var6
MOVFF RXB0D7,var7

BCF RXB0CON ,RXFUL

MOVFF var5,PORTA
MOVFF var6,PORTC
goto Nomessage

end
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top