• AVR Freaks

Hot!CAN communication problem with PIC18F

Author
luisa123
New Member
  • Total Posts : 2
  • Reward points : 0
  • Joined: 2019/05/14 08:17:15
  • Location: 0
  • Status: offline
2019/05/14 23:02:01 (permalink)
0

CAN communication problem with PIC18F

Hi, I'm new using CAN protocol and i'm trying to send and receive messages using PIC18F2580 with PCA82C250 transceiver but the TXREQ bit never gets cleared, i'm using two nodes and I don't receive nothing even if a have the PIC in error recognition mode, i made my own code and tried to guide with the datasheet examples but the communication still doesn't work, I attach the code of the CAN header for the initiallization, the receive and transmitt c files.  Sorry for my bad english and thanks! 
void iniciar_can(){
    TRISBbits.RB2=1;
    TRISBbits.RB3=1;
    CANCON=0b10000000;
    while(!(CANSTAT & 0b10000000));
    ECANCON=0x00;
    BRGCON1=0b00000000;
    BRGCON2=0b10001010;
    BRGCON3=0b01000001;
    RXB0CON=0x20;
    RXF0SIDH = 0XFF;
    RXF0SIDL = 0XE0;
    RXM0SIDH=0;
    RXM0SIDL=0;
    CANCON=0b00000000;
     while(CANSTAT&0xE0);
    }
post edited by luisa123 - 2019/05/15 09:53:46
#1

1 Reply Related Threads

    tam07
    New Member
    • Total Posts : 29
    • Reward points : 0
    • Joined: 2016/04/04 18:39:11
    • Location: 0
    • Status: offline
    Re: CAN communication problem with PIC18F 2019/05/27 00:30:22 (permalink)
    0
    Hi Luisa,
     
    Based on your code, you are trying to run the CAN module with the following configuration:
     
    Baud rate: 250kbps
    System frequency: 4000000
    Time quanta: 8
    Sample point: 75%
     
    Are these correct?
     
    I suggest updating your code in three places:
     
    (1) In your initialization code, you may try the following modifications (please note of the comments):
    void iniciar_can(){
     TRISBbits.RB2=0; // CANTX pin as output
     TRISBbits.RB3=1; // CANRX pin as input
     LATBbits.LATB2 = 1 // Set CANTX pin to high
        
     CANCON=0b10000000;
        while (0x80 != (CANSTAT & 0xE0)); // wait until ECAN is in config mode
        
     ECANCON=0x00;
     
     BRGCON1=0b00000000;
        BRGCON2=0b10001010;
        BRGCON3=0b01000001;
     
        RXB0CON=0x20;
     
        RXF0SIDH = 0XFF;
        RXF0SIDL = 0XE0;
        RXM0SIDH=0;
        RXM0SIDL=0; 
     
        CANCON=0b00000000; 
        while (0x00 != (CANSTAT & 0xE0)); // wait until ECAN is in Normal mode
    }

     
    (2) In transmitcan.c, you should check first if TXREQ is cleared before writing to the CAN transmit registers to. I noticed that you are also repeatedly calling the ADC initialization routine in while(1), which is not correct.
     
    void main(void){
        OSCCONbits.IRCF2=1;
        OSCCONbits.IRCF1=1;
        OSCCONbits.IRCF0=0;
        
     initialize_ADC(); // ADC initialization code should be added here not in while(1)
     iniciar_can();

     while(1){
      if(TXB0CONbits.TXREQ==0){
       TXB0SIDH=0b11111111;
       TXB0SIDL=0b11100000;
       TXB0DLC=0b00000001;
       TXB0D0=dato;
       TXB0CONbits.TXREQ=1;
      }
    }

     
    (3) In recepcan.c, the code has the tendency to be stuck at
    while(!(RXB0SIDLbits.EXID));

     
    Try updating it using the following (I'm assuming here that the CAN is expecting to receive a Standard message):
    void main(void)
    {
        OSCCONbits.IRCF2=1;
        OSCCONbits.IRCF1=1;
        OSCCONbits.IRCF0=0;

        iniciar_can();
     while(1){

      if (RXB0CONbits.RXFUL != 0) //CheckRXB0
      {
       if ((RXB0SIDL.EXID == 0) //If Standard Message
       {
        dato=RXB0D0;
       } else{
        // If Extended Message
       }
       RXB0CONbits.RXFUL=0;
      }
     }
    }

     
    Newer PIC18F devices are now supported by the MPLABX Code Configurator (MCC) plugin. MCC will generate the CAN APIs (including initialization, transmit and receive) for you based on the configuration you set in the GUI. Since setting up the CAN registers manually is such a tedious task, you can try generating code for devices like the PIC18F25K80 or PIC18F25K83, and tweak it for the PIC18F2580.
     
    If you are not familiar with MCC, this application note can help you get started:
    MPLAB® Code Configurator CAN 2.0B Module for PIC18 Microcontrollers
     
    Or, I strongly recommend that you migrate to a newer device that is supported by MCC for your CAN project.
     
    Hope this helps. :)
     
    Regards,
    Tam
    #2
    Jump to:
    © 2019 APG vNext Commercial Version 4.5