• 1 - Talking to Itself

    The first thing to test is that the Node can communicate with itself, if this does not work there is no point in going any farther as it WILL NOT work.
    The code below uses "Loopback" mode where as long as there is a CAN transceiver attached correctly and everything else is working it will work.
    Code:
    '****************************************************************
    '*  Name    : CAN_LOOPBACK.BAS                                  *
    '*  Author  : Bobby Garrett                                     *
    '*  Notice  : Copyright (c) 2013 Bobby Garrett                  *
    '*          : All Rights Reserved                               *
    '*  Date    : 27/01/2013                                        *
    '*  Version : 1.0                                               *
    '*  Notes   : Tested on 18F4680 and compiler 3.5.5.4            *
    '*          :                                                   *
    '****************************************************************
    
       Device = 18F4680  
    
    Config_Start
       OSC = HS ; HS oscillator
       FCMEN = On ; Fail-Safe Clock Monitor enabled
       IESO = OFF ; Oscillator Switchover mode disabled
       PWRT = On ; PWRT enabled
       BOREN = BOHW ; Brown-out Reset enabled in hardware only (SBOREN is disabled)
       BORV = 0 ; Maximum Setting
       WDT = OFF ; HW Disabled - SW Controlled
       MCLRE = On ; MCLR pin enabled; RE3 input pin disabled
       LPT1OSC = OFF ; Timer1 configured for higher power operation
       PBADEN = OFF ; PORTB<4> and PORTB<1:0> Configured as Digital I/O Pins on Reset
       Debug = OFF ; Background debugger disabled, RB6 and RB7 configured as general purpose I/O pins
       XINST = OFF ; Instruction set extension and Indexed Addressing mode disabled (Legacy mode)
       LVP = OFF ; Single-Supply ICSP disabled
       STVREN = On ; Stack full/underflow will cause Reset
       CP0 = OFF ; Block 0 (000800-003FFFh) not code-protected
       CP1 = OFF ; Block 1 (004000-007FFFh) not code-protected
       CP2 = OFF ; Block 2 (008000-00BFFFh) not code-protected
       CP3 = OFF ; Block 3 (00C000-00FFFFh) not code-protected
       CPB = OFF ; Boot block (000000-0007FFh) not code-protected
       CPD = OFF ; Data EEPROM not code-protected
       WRT0 = OFF ; Block 0 (000800-003FFFh) not write-protected
       WRT1 = OFF ; Block 1 (004000-007FFFh) not write-protected
       WRT2 = OFF ; Block 2 (008000-00BFFFh) not write-protected
       WRT3 = OFF ; Block 3 (00C000-00FFFFh) not write-protected
       WRTB = OFF ; Boot block (000000-0007FFh) not write-protected
       WRTC = OFF ; Configuration registers (300000-3000FFh) not write-protected
       WRTD = OFF ; Data EEPROM not write-protected
    Config_End
    
            Xtal = 20                   'Do not change unless you know what you are doing as the CAN 
                                        'settings are based on this.
            All_Digital = True          'Set all I/O to digital   
            
            Clear                       'Clear all memory locations
            
    'Set port direction On power up Or after reset        
            TRISA.0 = 0                 'Set port direction - LED for diagnostics
    
    'Set state of port on power up or after reset
            PORTA.0 = 0                 'Initialise the output as off
    
    'Set Diplay up
            Declare LCD_Type 0 
            Declare LCD_Interface 4
            Declare LCD_Lines 2
            Declare LCD_DTPin = PORTD.4
            Declare LCD_ENPin = PORTD.2
            Declare LCD_RSPin = PORTD.3        
    
    'Set port direction for CAN I/O        
            TRISB.2 = 0                 'Set port direction - CANTx pin as output
            TRISB.3 = 1                 'Set port direction - CANRx pin as input 
    
    'Set up the CAN module   
            CANCON =  000000      'Select config mode
            TXB0CON = 0              'Clear transmit buffer 0 control register
            RXB0CON = 100000      'Receive only valid messages with standard identifier            
            TXB0DLC = 001000      'RTR bit cleared, 8 byte data length
            RXB0DLC = 001000      '8 byte data length
                
          
    'Set up the CAN module acceptance masks   
            RXM0SIDL = 000000
            RXM0SIDH = 000000
            
    'set up CAN hardware
            BRGCON1 = 010011      'SJW = 1 Tq, Tq = 40/Fosc
            BRGCON2 = 111111      'Phase seg 1 = 16 Tq, Sample 3 times
            BRGCON3 = 000111      'Phase seg 2 = 8 Tq
            
            Set CIOCON.5             'ENDRHI  'recessive drives Tx to Vdd (required if using MCP 2551)
            
            CANCON = 001000       'Select loopback mode For testing only !!!!
    
            PIR3 = 0                    'Clear interrupt register
            
            Dim LoopCtr As Byte         'Loop counter 
            Dim TotalCount As Dword     'Total number of TX 
            Dim Module1 As Byte
            Dim Module2 As Byte
            Dim Module3 As Byte
            Dim Module4 As Byte
            Dim Module5 As Byte
    
            Cls                         'Clear screen
            DelayMS 100                 'Wait for PIC & LCD to start
            Print At 1,3,"Bobby Garrett's" 
            Print At 2,4,"CAN BUS Test"
            DelayMS 1000                'Show message for 1 seconds  
    
    send: 
            Cls                         'Clear screen  
            For LoopCtr = 0 To 50       'For testing
            Inc TotalCount
            Toggle PORTA.0
                   
            TXB0D0 = LoopCtr            'For testing
            
    'Every count of 10 change the Tx identifier to simulate different unit
            If LoopCtr <= 10 Then TXB0SIDL = 100000 : TXB0SIDH = 000000
            If LoopCtr > 10 And LoopCtr <= 20 Then TXB0SIDL = 000000 : TXB0SIDH = 000000
            If LoopCtr > 20 And LoopCtr <= 30 Then TXB0SIDL = 100000 : TXB0SIDH = 000000
            If LoopCtr > 30 And LoopCtr <= 40 Then TXB0SIDL = 000000 : TXB0SIDH = 000000
            If LoopCtr > 40 And LoopCtr <= 50 Then TXB0SIDL = 100000 : TXB0SIDH = 000000
                                   
            TXB0CON = 001001         'Start transmition, priority level 1 
    'Not sure but it appears there should be nothing between setting the bits and checking them?
    'I think this was a hardware issue but do not think that there should be anything between them anyway.
            While TXB0CON.3 = 1 : Wend                               
            
    'Every count of 10 change the Rx identifier to recieve message from "correct module"
            If LoopCtr <= 10 Then RXB0SIDL = 100000 : RXB0SIDH = 000000 : Module1 = RXB0D0
            If LoopCtr > 10 And LoopCtr <= 20 Then RXB0SIDL = 000000 : RXB0SIDH = 000000 : Module2 = RXB0D0
            If LoopCtr > 20 And LoopCtr <= 30 Then RXB0SIDL = 100000 : RXB0SIDH = 000000 : Module3 = RXB0D0
            If LoopCtr > 30 And LoopCtr <= 40 Then RXB0SIDL = 000000 : RXB0SIDH = 000000 : Module4 = RXB0D0
            If LoopCtr > 40 And LoopCtr <= 50 Then RXB0SIDL = 100000 : RXB0SIDH = 000000 : Module5 = RXB0D0
        
            DelayMS 500                 
           
            Print At 1,1, Dec LoopCtr   
            Print At 1,5, Dec TotalCount, "    "
            Print At 2,1, Dec Module1   
            Print At 2,5, Dec Module2   
            Print At 2,9, Dec Module3   
            Print At 2,13, Dec Module4  
            Print At 2,17, Dec Module5  
            
            Print At 2,21, Dec TXERRCNT, " TX ERR" 
            Print At 2,31, Dec RXERRCNT, " RX ERR" 
                  
            Next LoopCtr                
            GoTo send
    Connection diagram to follow.
  • Tag Cloud