CAN Connection with AT90CAN64 and TJA101055

General discussion on mikroC PRO for AVR.
Post Reply
Author
Message
reza
Posts: 69
Joined: 09 Jul 2008 13:00

CAN Connection with AT90CAN64 and TJA101055

#1 Post by reza » 06 Feb 2019 11:55

Dear friends,
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
    }
  }
}
The c1, c3, c4 pins are for EN, STB and ERR of can transceiver.
Please help me to solve the problem. Thanks.

reza
Posts: 69
Joined: 09 Jul 2008 13:00

Re: CAN Connection with AT90CAN64 and TJA101055

#2 Post by reza » 10 Feb 2019 08:29

Forgive me for this duplicate post. this happened because of my unstable internet connection.

User avatar
petar.suknjaja
mikroElektronika team
Posts: 683
Joined: 05 Mar 2018 09:44
Location: Belgrade

Re: CAN Connection with AT90CAN64 and TJA101055

#3 Post by petar.suknjaja » 11 Feb 2019 12:21

Hi,
Usually we got two different projects - one for sending and another one for receiving the CAN data over the interface.
Have you tried to add a resistor between the CAN wires?
Kind regards,
Petar

reza
Posts: 69
Joined: 09 Jul 2008 13:00

Re: CAN Connection with AT90CAN64 and TJA101055

#4 Post by reza » 12 Feb 2019 05:11

petar.suknjaja wrote:Hi,
Usually we got two different projects - one for sending and another one for receiving the CAN data over the interface.
Have you tried to add a resistor between the CAN wires?
Kind regards,
Petar
Hi,
I used the two projects in my 2 nodes on the CAN interface but I copied one of them here because of many similarities.
Yes I used termination resistors. I checked my hardware with another hex code I downloaded and everything is good but with my code anything works and even the MCU don't output required signals.
I suspect to the CANInitialize(1,6,3,3,1,Can_Init_Flags); number's and changed them to 1,8,3,4,8 because my crystal oscillator is 16MHz but the problem not solved.
Thanks and best regards.

User avatar
petar.suknjaja
mikroElektronika team
Posts: 683
Joined: 05 Mar 2018 09:44
Location: Belgrade

Re: CAN Connection with AT90CAN64 and TJA101055

#5 Post by petar.suknjaja » 14 Feb 2019 11:32

Hi,
I'll look into this and let you know if I find something new.
Kind regards,
Petar

Post Reply

Return to “mikroC PRO for AVR General”