Testing PIC C18 CAN Routines on PIC18F4680

Author
mariela
New Member
  • Total Posts : 13
  • Reward points : 0
  • Joined: 2008/03/26 12:33:36
  • Location: 0
  • Status: offline
2008/03/26 13:06:49 (permalink)
0

Testing PIC C18 CAN Routines on PIC18F4680

I´m trying to connect a PIC18F4680 + MCP2551 with a TS7260(SC-ARM)+TSCAN1 board (ISO-11898). I´m using C18 CAN routines, but I can´t get communication between boards. My C18 code on 18F4680 is very simple, but I´m not sure if it is working. How can I test if it is working alone?
 
Here is my code:
 
/* Compile options:  -ml (Large code model) */
#include "can18xx8.c"
#include <p18cxxx.h>
#include <stdio.h>
#include <delays.h>
#pragma config WDT = OFF

// Application specific variable declarations
 
/* CAN TRIS bits for Tx and Rx pins */
#define         CAN_TX_TRIS             TRISCbits.TRISC1
#define         CAN_RX_TRIS             TRISCbits.TRISC2
 
/* LED TRIS & LATCH bits (for testing) */
#define         LED_TRIS                 TRISCbits.TRISC0
#define         LED_LAT                  LATCbits.LATC0 /* green led*/
#define         LED_TRIS1                TRISCbits.TRISC1
#define         LED_LAT1                 LATCbits.LATC1  /* red led*/
#define         LED_TRIS4                TRISCbits.TRISC4
#define         LED_LAT4                 LATCbits.LATC4
#define         LED_TRIS3                TRISCbits.TRISC3
#define         LED_LAT3                 LATCbits.LATC3
#define         LED_TRIS2                TRISCbits.TRISC2
#define         LED_LAT2                LATCbits.LATC2
 
// CAN module related variables
 
unsigned long NewMessage;
BYTE NewMessageData[8];
BYTE MessageData[8];
BYTE NewMessageLen;
enum CAN_RX_MSG_FLAGS NewMessageFlags;
BYTE RxFilterMatch;

// Application specific initialization code follows

// Main
 
void main(void)
{
// Configure CAN I/O
       CAN_TX_TRIS =0 ;
       CAN_RX_TRIS =1 ;
       CIOCON =0x20;        /* CANTX pin will drive VDD when recessive  */
 
// Initialize CAN module with no message filtering
// CANInitialize(SJW, BRP, PHSEG1, PHSEG2, PROPSEG, config);
 
CANInitialize(1, 5, 7, 6, 2, CAN_CONFIG_VALID_STD_MSG);

// 125 KHz
//Set CAN module into normal mode
 
CANSetOperationMode(CAN_OP_MODE_NORMAL);
 
// Main application loop
 
while(1)
{
  if ( CANIsTxReady() )
  {
  MessageData[1] = 0x01;
  CANSendMessage( 0x02,
  MessageData,
  1,
  CAN_TX_PRIORITY_0 &
  CAN_TX_STD_FRAME &
  CAN_TX_NO_RTR_FRAME);
  LED_LAT = 1;
  Delay10KTCYx(50);
     LED_LAT = 0;
  }
  else
  {
  // 
  LED_LAT1 = 1;
  Delay10KTCYx(50);
  LED_LAT1 = 0;
  }
 
} // Do this forever
// End of program
}
 
Three times green LED blinks then RED LED turn on. I can´t see anything from my TS.
 
Many thanks
 
 
#1

10 Replies Related Threads

    p.erasmus
    Super Member
    • Total Posts : 1799
    • Reward points : 0
    • Joined: 2004/11/25 03:18:34
    • Location: Saratov Russia
    • Status: offline
    RE: Testing PIC C18 CAN Routines on PIC18F4680 2008/03/26 14:00:02 (permalink)
    0
    For a start you are using the wrong Lib and supporting.h.C files your device has an ECAN module therefore if you want to use the Lib you need to Down load the application  note AN878 which contains the .H .C and .Def file you need to include in your application and the AP note give you a detailed explenation about the Functions for the use of the ECAN module 
     
    you can doanload the AP from here
    http://www.microchip.com/stellent/idcplg?IdcService=SS_GET_PAGE&nodeId=1490&filterID=397

    Automotive Engineering

    #2
    john.dammeyer
    Super Member
    • Total Posts : 415
    • Reward points : 0
    • Joined: 2004/01/02 20:07:06
    • Status: offline
    RE: Testing PIC C18 CAN Routines on PIC18F4680 2008/03/26 14:04:47 (permalink)
    0
    Hi,
     
    I'll ask a couple of basic questions first.
     
    1. You want 125kbps but what frequency are you running the processor at?  Make sure that the values you've given to the CANInitialize actually work out to the bit rate desired.
     
    2. Have you terminated the bus?  120 ohm resistors at either end.
     
    3. Does the ARM board work or is it also under development?  If it does work,  start there.  Get it to send a message.  Look at  the bus with a scope.  If the message stays on the bus forever,  then youir PIC isn't sending an ACK to tell the sender to not retry.
     
    Once the message is gone work on the PIC side to test for the message available and write a small dump routine to display the register values and flags.  Very useful for testing.
     
    Then rewrite your receive code to do it under interrupts and you're half way there.
     
    Now write your code to send only one message.  Again,  does it stay on the bus forever according to the status flags?  Once it's gone does it show up at the other end correctly formatted.
     
    John Dammeyer
     
    #3
    mariela
    New Member
    • Total Posts : 13
    • Reward points : 0
    • Joined: 2008/03/26 12:33:36
    • Location: 0
    • Status: offline
    RE: Testing PIC C18 CAN Routines on PIC18F4680 2008/03/27 14:33:20 (permalink)
    0
    ORIGINAL: p.erasmus

    For a start you are using the wrong Lib and supporting.h.C files your device has an ECAN module therefore if you want to use the Lib you need to Down load the application  note AN878 which contains the .H .C and .Def file you need to include in your application and the AP note give you a detailed explenation about the Functions for the use of the ECAN module 

     
    OK, now I´m using ECAN Routines, I thought  CAN routines were compatible with ECAN in mode 0. I had to make a few modification on registers definition to compiled it. Using ECAN was easier. thanks!
     
    ORIGINAL: john.dammeyer

    Hi,

    I'll ask a couple of basic questions first.

    1. You want 125kbps but what frequency are you running the processor at?  Make sure that the values you've given to the CANInitialize actually work out to the bit rate desired.
     

    Hi!
    My processor is running at 20 MHz. I´m using default values from AP note, now a def file as follow:
     
    // SJW value - Value must be between 1-4 inclusive.
    #define ECAN_SJW_VAL 1
    //
    // BRP value - Value must be between 1-64 inclusive.
    #define ECAN_BRP_VAL 5
    //
    // PHSEG1 value - Value must be between 1-8 inclusive.
    #define ECAN_PHSEG1_VAL 7
    //
    // PHSEG2 value - Value must be between 1-8 inclusive.
    #define ECAN_PHSEG2_VAL 6
    //
    // PROPSEG value - Value must be between 1-8 inclusive.
    #define ECAN_PROPSEG_VAL 2

    ORIGINAL: john.dammeyer
    2. Have you terminated the bus?  120 ohm resistors at either end.

     
    Yes, I'm using standard termination (120) in both sides
     
    ORIGINAL: john.dammeyer

    3. Does the ARM board work or is it also under development?  If it does work,  start there.  Get it to send a message.  Look at  the bus with a scope.  If the message stays on the bus forever,  then youir PIC isn't sending an ACK to tell the sender to not retry.

     
    It is not under development, but I don´t have a lot of tools to test can bus. I can send and receive messages between two arms with no problem
     
    I looked at the bus with my scope, I could see bus activity. I think PIC isn´t sending an ACK. My Next question. When and how is PIC sending back an acknowledge to the bus? I really need to learn more about CAN protocol in data sheet I couldn´t find enough data.
      

    ORIGINAL: john.dammeyer
    Once the message is gone work on the PIC side to test for the message available and write a small dump routine to display the register values and flags.  Very useful for testing.

     
    Can you explain me how to do that or where can find an example?

    I appreciate very much your help
     
    Very begginer Mariela.
    #4
    Stefan Uhlemayr
    Super Member
    • Total Posts : 4292
    • Reward points : 0
    • Joined: 2005/05/12 12:25:46
    • Location: Germany
    • Status: offline
    RE: Testing PIC C18 CAN Routines on PIC18F4680 2008/03/28 13:04:54 (permalink)
    0
    ORIGINAL: mariela

    Hi!
    My processor is running at 20 MHz. I´m using default values from AP note, now a def file as follow:

    // SJW value - Value must be between 1-4 inclusive.
    #define ECAN_SJW_VAL 1
    //
    // BRP value - Value must be between 1-64 inclusive.
    #define ECAN_BRP_VAL 5
    //
    // PHSEG1 value - Value must be between 1-8 inclusive.
    #define ECAN_PHSEG1_VAL 7
    //
    // PHSEG2 value - Value must be between 1-8 inclusive.
    #define ECAN_PHSEG2_VAL 6
    //
    // PROPSEG value - Value must be between 1-8 inclusive.
    #define ECAN_PROPSEG_VAL 2

    Let's see your setup:

    TQ = (2*5)/Fosc = 0,5 usec
    nominal Bit-length = 1 (sync = fix) + 2 (prop) + 7 (phase1) + 6 (phase2) = 16 * TQ = 8 usec => 125 kBps => perfect!!!

    ORIGINAL: mariela

    I looked at the bus with my scope, I could see bus activity. I think PIC isn´t sending an ACK. My Next question. When and how is PIC sending back an acknowledge to the bus?

    ORIGINAL: MCP2515-datasheet

    During the ACK Slot bit, the transmitting node sends out a recessive bit. Any node that has received an error-free frame acknowledges the correct reception of the frame by sending back a dominant bit (regardless of whether the node is configured to accept that specific message or not). The recessive acknowledge delimiter completes the acknowledge field and may not be overwritten by a dominant bit.

    This means, that even if you get an acknowledge on the bus for your sended data, you can´t be sure, that the specific node (for which the data was thought) has got it. Never forget this in your considerations...[8|]

    ORIGINAL: mariela

    I appreciate very much your help

    Very begginer Mariela.

    Everyone was starting at point zero... Smile

    Greetings,
    Stefan
    #5
    mariela
    New Member
    • Total Posts : 13
    • Reward points : 0
    • Joined: 2008/03/26 12:33:36
    • Location: 0
    • Status: offline
    RE: Testing PIC C18 CAN Routines on PIC18F4680 2008/04/01 13:06:19 (permalink)
    0
     
    grin I  built two nodes and I successfully connected both nodes with a simple code. Now I´m sure uC is sending and receiving data, problems seems to be in TS7260 arm side
     
    Many thanks for your help
    Mariela
    #6
    p.erasmus
    Super Member
    • Total Posts : 1799
    • Reward points : 0
    • Joined: 2004/11/25 03:18:34
    • Location: Saratov Russia
    • Status: offline
    RE: Testing PIC C18 CAN Routines on PIC18F4680 2008/04/01 14:01:22 (permalink)
    0
    Question are you using the ECAN lib ;CAN lib or your own code.
    however nice to see you are up and running on the Microchip side

    Automotive Engineering

    #7
    mariela
    New Member
    • Total Posts : 13
    • Reward points : 0
    • Joined: 2008/03/26 12:33:36
    • Location: 0
    • Status: offline
    RE: Testing PIC C18 CAN Routines on PIC18F4680 2008/04/01 14:11:45 (permalink)
    0
    ORIGINAL: p.erasmus

    Question are you using the ECAN lib ;CAN lib or your own code.
    however nice to see you are up and running on the Microchip side

     
    I´m using ECAN lib. wink
    #8
    Buddace
    New Member
    • Total Posts : 2
    • Reward points : 0
    • Joined: 2008/04/02 03:29:20
    • Location: 0
    • Status: offline
    RE: Testing PIC C18 CAN Routines on PIC18F4680 2008/04/02 03:30:57 (permalink)
    0
    Hi all.
    i tried example of AN878 but I have stupid problem with compiler: "Skipping link step.  The project contains no linker script".
    Linker script file is correct in projetc.
    #9
    p.erasmus
    Super Member
    • Total Posts : 1799
    • Reward points : 0
    • Joined: 2004/11/25 03:18:34
    • Location: Saratov Russia
    • Status: offline
    RE: Testing PIC C18 CAN Routines on PIC18F4680 2008/04/02 03:38:08 (permalink)
    0
    What files do you have in your project tree show your include directive of you project what controller are you using

    Automotive Engineering

    #10
    Buddace
    New Member
    • Total Posts : 2
    • Reward points : 0
    • Joined: 2008/04/02 03:29:20
    • Location: 0
    • Status: offline
    RE: Testing PIC C18 CAN Routines on PIC18F4680 2008/04/02 03:52:52 (permalink)
    0
    ORIGINAL: p.erasmus

    What files do you have in your project tree show your include directive of you project what controller are you using

    Solved, thanks :D
    I would like change thios source for pic 18F4680. Enough is change only device ?
    #11
    Jump to:
    © 2017 APG vNext Commercial Version 4.5