Problems getting ECAN nodes to transmit.

Discussion in 'Embedded Systems and Microcontrollers' started by Simy, Sep 10, 2013.

  1. Simy

    Thread Starter New Member

    Aug 8, 2013
    7
    0
    I'm having a great deal of trouble getting these two nodes to communicate with each other. They work in loopback mode but not in normal mode. I've included the two most likely problem functions below, as well as a quick schematic, and pictures of the nodes. I'm not sure if it is software or hardware. I have tried many different things, but I can't seem to get them to work.

    A zipfile containing the J1939 library I have used once before and had to modify, as well as my current hobbled together code. I used the XC8 compiler with MPLABX and the PICKIT3. The files are located at http://tomt.me/halp/CANTest.zip

    Thank you for your time, and any insights you can help me with.


    Code ( (Unknown Language)):
    1. //Send a test message
    2. void testmsg(unsigned char msg){
    3.     TXB0EIDH = 0x0000;          //Clear extended IDs
    4.     TXB0SIDH = msg;
    5.     TXB0D0 = 1;
    6.     TXB0DLC = 0x00 | 1;   //changed from count to 1  //Set the RTR flag to false, and set the DLC register.
    7.     TXB0CON = TXB0CON | 0x08 ; //Set the TXREQ flag.
    8.     LED3 = 1;
    9.     while (TXB0CONbits.TXREQ);
    10.     while (!(LED3=0)); //This SHOULDN'T BE REQUIRED!!!
    11.     LED1 ^= 1;
    12.  
    13.  
    14. }
    15.  
    Code ( (Unknown Language)):
    1. /*
    2.  * Initilize the chip, and CAN module.
    3.  */
    4. void Init(){
    5.     // Set the internal oscillator to 64MHz
    6.     OSCCONbits.IRCF = 7;
    7.     OSCTUNEbits.PLLEN = 1;
    8.  
    9.     //Initialize I/O to be digital
    10.     ANCON0 = ANCON1 = 0x00;
    11.  
    12.     //Set I/O
    13.     TRISC = 0x0F; // LED's then Inputs
    14.  
    15.     //Set all LED's off.
    16.     LATC = 0x00;
    17.  
    18.     //Set io for tx/rx pins
    19.     TRISB3 = 1; //RX pin
    20.     TRISB2 = 0; //TX Pin
    21.  
    22.  
    23.  
    24.  
    25.     // Initialize CAN module
    26.  
    27.     // Enter CAN module into config mode
    28.     CANCON = 0x80;    //REQOP<2:0>=100
    29.     while(!(CANSTATbits.OPMODE ==0x04));
    30.  
    31.     //Select ECAN Mode 2
    32.     ECANCON = 0xA0; //enhanced fifo, with interrupt when one buffer remains,  Acceptance Filters 0, 1, 2 and BRGCON2, 3
    33.  
    34.     // Initialize CAN Timing to 1 Mbps @ 64MHz
    35.     //BRGCON1 = 0x81; //0000 0011     //SJW=3TQ     BRP  1
    36.     //BRGCON2 = 0xB8; //1011 1000     //SEG2PHTS 1    sampled once  PS1=8TQ  PropagationT 1TQ
    37.     //BRGCON3 = 0x05; //0000 0101     //PS2  6TQ
    38.  
    39.     //  100 Kbps @ 64MHz
    40.     BRGCON1 = 0x93; //0001 1111     //SJW=3TQ     BRP  31
    41.     BRGCON2 = 0xB8; //1010 0000     //SEG2PHTS 1    sampled once  PS1=8TQ  PropagationT 1TQ
    42.     BRGCON3 = 0x05; //0000 0010     //PS2  6TQ
    43.  
    44.  
    45.     // Initialize Receive Masks
    46.     //Recieve ALL messages
    47.     RXM0EIDH = 0x00;    //Extended ID Mask
    48.     RXM0EIDL = 0x00;
    49.     RXM0SIDH = 0x00;    // Standard ID Mask
    50.     RXM0SIDL = 0x00;
    51.  
    52.  
    53.     //Disable Filters
    54.     RXFCON0 = 0x00;     //Disable all
    55.     RXFCON1 = 0x00;     //Disable all
    56.  
    57.  
    58.     // Enter CAN module into normal mode
    59.     //CANCON = 0x40; //Loopback Mode
    60.     CANCON = 0x00; //Normal Mode
    61.     while(CANSTATbits.OPMODE==0x00);
    62.  
    63.     // Set Receive Mode for buffers
    64.     RXB0CON = 0x00;
    65.     RXB1CON = 0x00;
    66.  
    67. }
    [​IMG]
    [​IMG]
    [​IMG]
     
  2. Papabravo

    Expert

    Feb 24, 2006
    10,136
    1,786
    Having a terminating resistor between CANH and CANL on each board is a really bad idea. As you increase the number of nodes it becomes harder to drive the cable with all those terminators in parallel.

    First things first. If you have a lonely node you can send a packet and it will try to send that packet forever looking for an acknowledgement. This should allow you to determine if the transmitters are working and what badurate you have selected. When you attach the second node you will either get acknowledgement and all is well or the receiver will throw ERROR FRAMES. At first they will be ERROR ACTIVE frames, then ERROR PASSIVE frames and finally the nodes will go BUS OFF. You should monitor the status register to see if these condition occur. Getting CAN to work is generally easier than rolling out of bed on a Saturday morning, but you have to pay attention to small details.
     
  3. Simy

    Thread Starter New Member

    Aug 8, 2013
    7
    0
    I had seen previously that the node goes into busoff state.

    After reading your post I went through over and over again, and I found out I was trying to use the banked method of transmitting, while setting the flags for the interrupt method. It seems to be working now, I'll have to play with it some more.

    Thanks for the nudge!


    For those who are interested, the changed code was to testmsg:
    Code ( (Unknown Language)):
    1. void testmsg(unsigned int msg){
    2.  
    3.     ECANCONbits.EWIN = 0x003; //Select Transmit buffer one.
    4.     RXB0EIDH = 0x00;          //Clear extended IDs
    5.     RXB0SIDL = 0x00;
    6.     RXB0SIDH = msg >> 8;
    7.     RXB0SIDL = msg;
    8.     RXB0D0 = 0xF8;
    9.     RXB0DLC = 0x00 | 1;   //changed from count to 1  //Set the RTR flag to false, and set the DLC register.
    10.     RXB0CON |= 0x08 ; //Set the TXREQ flag.
    11.     while (RXB0CON & 0x80); //Waiting to xmit.
    12.     LED1 ^= 1;
    13.  
    14.  
    15. }
     
Loading...