Teckm
Teckm

Reputation: 41

CAN communication not working between different PIC

I am working on project, and we need to establish a CAN communication between 4 nodes, 2 using a PIC 18F4580 and 2 using 18F25K80. In all those circuits, I'm using a Crystal oscillator 20MHz. The issue is when I test the communication between same PICs, it's working, but when I try with two different PICs it's not working.

The codes I used to test: For the emitting PIC 18F4580 : Emitting a CAN message every 1 second :

int i;

unsigned char Can_Init_Flags, Can_Send_Flags, Can_Rcv_Flags; // can flags
unsigned char Rx_Data_Len;                                   // received data length in bytes
char RxTx_Data[8];                                           // can rx/tx data buffer
char Msg_Rcvd;                                               // reception flag
const long ID_cmd = 3, ID_led1 = 2;                       // node IDs
long Rx_ID;

void main() {

ADCON1=0xF;
TRISA=0xFF;
TRISD=0;
PORTD=0;


for(i=0;i<10;i++) {
    PORTD=0xFF ^ PORTD;   //Blinking Leds
    Delay_ms(100);
}

Can_Init_Flags = 0;                                       //
Can_Send_Flags = 0;                                       // clear flags
Can_Rcv_Flags  = 0;                                       //

Can_Send_Flags = _CAN_TX_PRIORITY_0 &                     // form value to be used
                 _CAN_TX_XTD_FRAME &                      //     with CANWrite
                 _CAN_TX_NO_RTR_FRAME;

Can_Init_Flags = _CAN_CONFIG_SAMPLE_THRICE &              // form value to be used
                 _CAN_CONFIG_PHSEG2_PRG_ON &              // with CANInit
                 _CAN_CONFIG_XTD_MSG &
                 _CAN_CONFIG_DBL_BUFFER_ON &
                 _CAN_CONFIG_VALID_XTD_MSG;

CANInitialize(1,3,3,3,1,Can_Init_Flags);                  // Initialize CAN module

CANSetOperationMode(_CAN_MODE_NORMAL,0xFF);               // set NORMAL mode

for(i=0;i<10;i++) {
    PORTD=0xFF ^ PORTD;   //Blinking Leds
    Delay_ms(100);
}

while(1){
    PORTD.F7=PORTA.F0;
    PORTD.F6=PORTA.F1;
    PORTD.F5=PORTA.F2;
    PORTD.F4=PORTA.F3;    //LEDS := SWITCHS

    CANWrite(ID_cmd, RxTx_Data, 1, Can_Send_Flags);                      // send incremented data back
    Delay_ms(1000);


}
}

For the receiving Node PIC 18F25K80 : Blink after receiving any CAN message (Should blink every 1 second) :

unsigned char Can_Init_Flags, Can_Send_Flags, Can_Rcv_Flags; // can flags
unsigned char Rx_Data_Len;                                   // received data length in bytes
char RxTx_Data[8];                                           // can rx/tx data buffer
char Msg_Rcvd;                                               // reception flag
const long ID_led1 = 2, ID_cmd = 3;                       // node IDs
long Rx_ID;

void main() {

//OSCCON |= 0b01110010;
TRISC = 0;

Can_Init_Flags = 0;                                       //
Can_Send_Flags = 0;                                       // clear flags
Can_Rcv_Flags  = 0;                                       //

Can_Send_Flags = _CAN_TX_PRIORITY_0 &                     // form value to be used
                 _CAN_TX_XTD_FRAME &                      //     with CANWrite
                 _CAN_TX_NO_RTR_FRAME;

Can_Init_Flags = _CAN_CONFIG_SAMPLE_THRICE &              // form value to be used
                 _CAN_CONFIG_PHSEG2_PRG_ON &              // with CANInit
                 _CAN_CONFIG_XTD_MSG &
                 _CAN_CONFIG_DBL_BUFFER_ON &
                 _CAN_CONFIG_VALID_XTD_MSG;

CANInitialize(1,3,3,3,1,Can_Init_Flags);                  // Initialize CAN module
CANSetOperationMode(_CAN_MODE_CONFIG,0xFF);               // set CONFIGURATION mode
CANSetMask(_CAN_MASK_B1,-1,_CAN_CONFIG_XTD_MSG);          // set all mask1 bits to ones
CANSetMask(_CAN_MASK_B2,-1,_CAN_CONFIG_XTD_MSG);          // set all mask2 bits to ones
CANSetFilter(_CAN_FILTER_B2_F4,ID_cmd,_CAN_CONFIG_XTD_MSG);// set id of filter B2_F4 to 2nd node ID

CANSetOperationMode(_CAN_MODE_NORMAL,0xFF);               // set NORMAL mode


while(1) {                                                               // endless loop
    Msg_Rcvd = CANRead(&Rx_ID , RxTx_Data , &Rx_Data_Len, &Can_Rcv_Flags); // receive message
    if ((Rx_ID == ID_cmd) && Msg_Rcvd) {                                   // if message received check id
        PORTC.F3=!PORTC.F3;
    }
}



}

Any help would be greatly appreciated, thanks.

Upvotes: 1

Views: 200

Answers (1)

Teckm
Teckm

Reputation: 41

It's me again, it worked, the nodes must have the same oscillator value (in my case : 20MHz Crystal).

Upvotes: 2

Related Questions