I've two CAN network board with AT90CAN64 MCU and TJA101055 CAN transceiver. The frequency of mcu is 16 MHz.
I want to connect this two board through CAN with Mikroc 's CAN library and the sample code in the help.
I changed the sample as below but the USART (used for debug) only receives 0x00.
Code: Select all
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 = 0; // reception flag
const long ID_1st = 12111, ID_2nd = 3; // node IDs
long Rx_ID;
void main() {
//PORTC = 0x00; // clear PORTC
//DDRC = 0xFF; // set PORTC as output
DDC1_bit = 1; // Set PA3 pin as output
DDC3_bit = 1;
DDC4_bit = 1;
DDC5_bit = 1;
PORTC1_bit = 1;
PORTC3_bit = 1;
PORTC4_bit = 1;
PORTC5_bit = 1;
UART1_Init(9600); // Initialize UART module at 9600 bps
Delay_ms(100);
UART1_Write_Text("FCM Tester Init - EKS Co (R&D Dept)");
UART1_Write(13);UART1_Write(10);
Delay_ms(100);
Can_Init_Flags = 0; //
Can_Send_Flags = 0; // clear flags
Can_Rcv_Flags = 0; //
Can_Send_Flags = _CAN_IDE_XTD_FRAME // form value to be used
& _CAN_NO_RTR_FRAME; // with CANWrite
Can_Init_Flags = _CAN_CONFIG_SAMPLE_THRICE // form value to be used
& _CAN_CONFIG_XTD_MSG; // with CANInit
CANInitialize(1,6,3,3,1,Can_Init_Flags); // initialize external CAN module
CANSetOperationMode(_CAN_MODE_STANDBY,0xFF); // set STANDBY mode
//----------------------------------------------------------//
CANSetFilter(CAN_RX_FILTER_3, -1, _CAN_CONFIG_XTD_MSG); //
CANSetFilter(CAN_RX_FILTER_4, ID_2nd, _CAN_CONFIG_XTD_MSG); // set ID filter of 4th filter to 2nd node ID
CANSetFilter(CAN_RX_FILTER_5, -1, _CAN_CONFIG_XTD_MSG); // and ones to other filters
CANSetFilter(CAN_RX_FILTER_6, -1, _CAN_CONFIG_XTD_MSG);
CANSetFilter(CAN_RX_FILTER_7, -1, _CAN_CONFIG_XTD_MSG);
CANSetFilter(CAN_RX_FILTER_8, -1, _CAN_CONFIG_XTD_MSG);
CANSetFilter(CAN_RX_FILTER_9, -1, _CAN_CONFIG_XTD_MSG);
CANSetFilter(CAN_RX_FILTER_10, -1, _CAN_CONFIG_XTD_MSG);
CANSetFilter(CAN_RX_FILTER_11, -1, _CAN_CONFIG_XTD_MSG);
CANSetFilter(CAN_RX_FILTER_12, -1, _CAN_CONFIG_XTD_MSG);
CANSetFilter(CAN_RX_FILTER_13, -1, _CAN_CONFIG_XTD_MSG);
CANSetFilter(CAN_RX_FILTER_14, -1, _CAN_CONFIG_XTD_MSG);
//----------------------------------------------------------//
//----------------------------------------------------------//
CANSetMask(CAN_RX_MASK_3, -1, _CAN_CONFIG_XTD_MSG); // set all mask bits of masks[3..14] to all ones
CANSetMask(CAN_RX_MASK_4, -1, _CAN_CONFIG_XTD_MSG);
CANSetMask(CAN_RX_MASK_5, -1, _CAN_CONFIG_XTD_MSG);
CANSetMask(CAN_RX_MASK_6, -1, _CAN_CONFIG_XTD_MSG);
CANSetMask(CAN_RX_MASK_7, -1, _CAN_CONFIG_XTD_MSG);
CANSetMask(CAN_RX_MASK_8, -1, _CAN_CONFIG_XTD_MSG);
CANSetMask(CAN_RX_MASK_9, -1, _CAN_CONFIG_XTD_MSG);
CANSetMask(CAN_RX_MASK_10, -1, _CAN_CONFIG_XTD_MSG);
CANSetMask(CAN_RX_MASK_11, -1, _CAN_CONFIG_XTD_MSG);
CANSetMask(CAN_RX_MASK_12, -1, _CAN_CONFIG_XTD_MSG);
CANSetMask(CAN_RX_MASK_13, -1, _CAN_CONFIG_XTD_MSG);
CANSetMask(CAN_RX_MASK_14, -1, _CAN_CONFIG_XTD_MSG);
//----------------------------------------------------------//
CANSetOperationMode(_CAN_MODE_ENABLE,0xFF); // set ENABLE mode
RxTx_Data[0] = 9; // set initial data to be sent
CANWrite(ID_1st, RxTx_Data, 1, Can_Send_Flags); // send initial message
while(1) { // endless loop
Msg_Rcvd = CANRead(&Rx_ID, RxTx_Data, &Rx_Data_Len, &Can_Rcv_Flags); // receive message
UART1_Write(Msg_Rcvd);
UART1_Write(13);UART1_Write(10);
if ((Rx_ID == ID_2nd) && Msg_Rcvd) { // if message received check id
PORTC = RxTx_Data[0]; // id correct, output data at PORTC
RxTx_Data[0]++; // increment received data
Delay_ms(10);
CANWrite(ID_1st, RxTx_Data, 1, Can_Send_Flags); // send incremented data back
}
}
}
Please help me to solve the problem. Thanks.