CAN通信在不同的PIC之间不起作用

时间:2018-01-21 17:04:31

标签: pic microchip can-bus xc8

我正在研究项目,我们需要在4个节点之间建立CAN通信,2个使用PIC 18F4580,2个使用18F25K80。在所有这些电路中,我使用的是20MHz的晶体振荡器。问题是当我测试相同PIC之间的通信时,它正在工作,但是当我尝试使用两个不同的PIC时,它不起作用。

我用来测试的代码: 对于发射PIC 18F4580:每1秒发送一条CAN消息:

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);


}
}

对于接收节点PIC 18F25K80:收到任何CAN消息后闪烁(应每1秒闪烁一次):

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;
    }
}



}

非常感谢任何帮助,谢谢。

1 个答案:

答案 0 :(得分:2)

再次,它是有效的,节点必须具有相同的振荡器值(在我的情况下:20MHz晶体)。