UART-CAN using 2 DVK90CAN1 board(AT90CAN128) read problem

General discussion on mikroC PRO for AVR.
Post Reply
Author
Message
nkfsl
Posts: 5
Joined: 20 Apr 2015 04:32

UART-CAN using 2 DVK90CAN1 board(AT90CAN128) read problem

#1 Post by nkfsl » 20 Apr 2015 04:43

Hi,
I'm new here and I have a problem with CanRead command.
I connected two DVK90CAN1 board and it works well when I try using CanWrite(the CAN led blinks)
However when I try to read the transmitted data, CanRead command seems doesn't respond. In CAN 2 code I placed led blink code to know whether CanRead operate or not. And I noticed led doesn't blink if I use CanRead. I am not sure whether the syntax itself is a problem. I might say that my CAN configuration has a problem.

CAN 1

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


  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                                                             // increment received data
      Delay_ms(10);
      CANWrite(ID_1st, RxTx_Data, 1, Can_Send_Flags);                           // send incremented data back
    }
  }
}
Can 2

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() {

  PORTA = 0x00;                                                  // clear PORTC
  DDRA  = 0xFF;                                                  // set PORTC as output

  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,  -1, _CAN_CONFIG_XTD_MSG);    //
  CANSetFilter(CAN_RX_FILTER_5,  -1, _CAN_CONFIG_XTD_MSG);    //
  CANSetFilter(CAN_RX_FILTER_6,  -1, _CAN_CONFIG_XTD_MSG);    //
  CANSetFilter(CAN_RX_FILTER_7, ID_1st, _CAN_CONFIG_XTD_MSG); // set ID filter of 7th filter to 2nd node ID
  CANSetFilter(CAN_RX_FILTER_8,  -1, _CAN_CONFIG_XTD_MSG);    // and ones to other filters
  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

  while (1) {                                                              // endless loop
   Msg_Rcvd = CANRead(&Rx_ID, &RxTx_Data, &Rx_Data_Len, &Can_Rcv_Flags);   // receive message

    PORTA = 0xFF;
    Delay_ms(100);
    PORTA = 0x00;
    Delay_ms(100);
  }

}

User avatar
darko.minic
Posts: 747
Joined: 01 Dec 2014 11:10

Re: UART-CAN using 2 DVK90CAN1 board(AT90CAN128) read proble

#2 Post by darko.minic » 20 Apr 2015 12:32

Hi,

As I can see you tried our examples on your boards.
Example is tested on our Hardware and should work as well.

Is there any hardware configuration on your Boards related to CAN?

Best regards,

nkfsl
Posts: 5
Joined: 20 Apr 2015 04:32

Re: UART-CAN using 2 DVK90CAN1 board(AT90CAN128) read proble

#3 Post by nkfsl » 20 Apr 2015 15:18

Hi,

Thanks for the reply. I tried to stick as much as examples given because it is known to work. The only configuration is in the picture attached. I use 8Mhz oscillator and not sure whether to baud rate is correct. I read somewhere in the internet that configuration given should work well with standard developement board. As I said before, the transmit and receive led blinks whenever I send data but nothing happen when I try to use Canread function.
Attachments
Capture.JPG
Capture.JPG (79.49 KiB) Viewed 4448 times

User avatar
darko.minic
Posts: 747
Joined: 01 Dec 2014 11:10

Re: UART-CAN using 2 DVK90CAN1 board(AT90CAN128) read proble

#4 Post by darko.minic » 21 Apr 2015 09:44

Hi,

Did you tried to add termination resistor (120 Ohms) between CAN-H and CAN-L?
As I can see from picture that you have provide, you can do it with J10 switch.

Regards,
Darko

nkfsl
Posts: 5
Joined: 20 Apr 2015 04:32

Re: UART-CAN using 2 DVK90CAN1 board(AT90CAN128) read proble

#5 Post by nkfsl » 21 Apr 2015 14:20

Hi Darko,

I don't think the jumper would be the main reason because I try both ways ended up the same problem. CanRead does not work. It is okay for CAN transmit.
I use development board and I don't think its a hardware problem. Maybe the code itself works well. Maybe because of the wrong baud rate. But I tried calculate the baudrate setting using CANCULATOR and use the given code replacing the examples code. And still got the same problem, CanRead seems stuck

Thank you

User avatar
darko.minic
Posts: 747
Joined: 01 Dec 2014 11:10

Re: UART-CAN using 2 DVK90CAN1 board(AT90CAN128) read proble

#6 Post by darko.minic » 22 Apr 2015 13:39

Hi,

Which CAN Baud rate did you used and which bus length?

Regards,
Darko

nkfsl
Posts: 5
Joined: 20 Apr 2015 04:32

Re: UART-CAN using 2 DVK90CAN1 board(AT90CAN128) read proble

#7 Post by nkfsl » 25 Apr 2015 00:07

Hi,

Im planning to 500kb of 3M serial cable. I use this code from CANCULATOR for both node Up until now I still don't know what the problem is

Code: Select all

 /*Place/Copy this part in declaration section*/
const unsigned int SJW = 1;
const unsigned int BRP = 2;
const unsigned int PHSEG1 = 1;
const unsigned int PHSEG2 = 2;
const unsigned int PROPSEG = 4;
const unsigned int CAN_CONFIG_FLAGS = 0;
Thank you

User avatar
darko.minic
Posts: 747
Joined: 01 Dec 2014 11:10

Re: UART-CAN using 2 DVK90CAN1 board(AT90CAN128) read proble

#8 Post by darko.minic » 27 Apr 2015 09:57

Hi,

As i can see from generated caluclator code you should try with:

Code: Select all

const unsigned int CAN_CONFIG_FLAGS =
		                       _CAN_CONFIG_SAMPLE_ONCE &
		                       _CAN_CONFIG_XTD_MSG;
Instead of:

Code: Select all

const unsigned int CAN_CONFIG_FLAGS = 0
Regards,
Darko

nkfsl
Posts: 5
Joined: 20 Apr 2015 04:32

Re: UART-CAN using 2 DVK90CAN1 board(AT90CAN128) read proble

#9 Post by nkfsl » 01 May 2015 19:30

Hi Marko,

It seems that I found the problem. The code couldn't run because Msg_Rcvd was set as char. But when I set it to long. it gives value '4356' but the code seems to run normally. With Msg_Rcvd set to char the code wouldn't even run. I tried to use jtagice mkii debugger and it could not pass the Msg_Rcvd as char and gives error before debug process. I tried declare the variable with long and the debugger run very well. I don't even know the can bus should work or not after this. My question is what type of data CanRead process return value?

Thank you

Nkfsl

User avatar
darko.minic
Posts: 747
Joined: 01 Dec 2014 11:10

Re: UART-CAN using 2 DVK90CAN1 board(AT90CAN128) read proble

#10 Post by darko.minic » 04 May 2015 09:36

Hi,

CanRead should return message from receive buffer or zero if no message found.
Message should be upto 8 bytes in length .
Can you please try to set Msg_Rcvd as char array of 8 elements instead of long and see if you have the same problem?

Regards,
Darko

Post Reply

Return to “mikroC PRO for AVR General”