Help about CAN network

Thread Starter

the kid

Joined Jan 4, 2015
81
hello everyone
I'm learning about the CAN network. I program for PIC18F2580 based on MikroC compiler. but I do not understand about the configuration flags such as:


const char
_CAN_CONFIG_DEFAULT = 0xFF, // 11111111

_CAN_CONFIG_PHSEG2_PRG_BIT = 0x01,
_CAN_CONFIG_PHSEG2_PRG_ON = 0xFF, // XXXXXXX1
_CAN_CONFIG_PHSEG2_PRG_OFF = 0xFE, // XXXXXXX0

_CAN_CONFIG_LINE_FILTER_BIT = 0x02,
_CAN_CONFIG_LINE_FILTER_ON = 0xFF, // XXXXXX1X
_CAN_CONFIG_LINE_FILTER_OFF = 0xFD, // XXXXXX0X

_CAN_CONFIG_SAMPLE_BIT = 0x04,
_CAN_CONFIG_SAMPLE_ONCE = 0xFF, // XXXXX1XX
_CAN_CONFIG_SAMPLE_THRICE = 0xFB, // XXXXX0XX

_CAN_CONFIG_MSG_TYPE_BIT = 0x08,
_CAN_CONFIG_STD_MSG = 0xFF, // XXXX1XXX
_CAN_CONFIG_XTD_MSG = 0xF7, // XXXX0XXX

_CAN_CONFIG_DBL_BUFFER_BIT = 0x10,
_CAN_CONFIG_DBL_BUFFER_ON = 0xFF, // XXX1XXXX
_CAN_CONFIG_DBL_BUFFER_OFF = 0xEF, // XXX0XXXX

_CAN_CONFIG_MSG_BITS = 0x60,
_CAN_CONFIG_ALL_MSG = 0xFF, // X11XXXXX
_CAN_CONFIG_VALID_XTD_MSG = 0xDF, // X10XXXXX
_CAN_CONFIG_VALID_STD_MSG = 0xBF, // X01XXXXX
_CAN_CONFIG_ALL_VALID_MSG = 0x9F; // X00XXXXX
.
can any one explain mean of the configuration flags,please ?
thank every one
 

Papabravo

Joined Feb 24, 2006
21,159
Yes, but the explanation starts with your understanding of CAN bit timing, in particular the location in the bit cell of the sample point. Do you understand how the CAN bit timing is determined in great graphic detail?
 

Thread Starter

the kid

Joined Jan 4, 2015
81
I HAVE A QUESTION
"_CAN_CONFIG_XTD_MSG'' fLAG IS CAN CONFIGUTION EXTENDED MESSAGE?
HOW TO THE FLAGS AFFECT TO THE CONFIGUTION REGISTER?
 

Papabravo

Joined Feb 24, 2006
21,159
One value of the bit selects a Standard Frame message with an 11-bit identifier. The other value of the bit selects an Extended Frame message with a 29-bit identifier. In a rational system you would select one message format or the other. Even if it were possible I would not recommend doing a system with mixed message formats. No more answers until you tell me that you understand CAN bit timing on a very deep level. It's OK if you don't we'll got through it.
 

Thread Starter

the kid

Joined Jan 4, 2015
81
thank you veryone.i have understood about CAN config flags as folow:
CAN_CONFIG_DEFAULT : Default flags
CAN_CONFIG_PHSEG2_PRG_ON :Use supplied PHSEG2 value
CAN_CONFIG_PHSEG2_PRG_OFF Use maximum of PHSEG1 or information processing time (IPT), whichever is greater.
CAN_CONFIG_LINE_FILTER_ON: Use CAN bus line filter for wake-up
CAN_CONFIG_FILTER_OFF: Do not use CAN bus line filter (using for wake up).
CAN_CONFIG_SAMPLE_ONCE: Sample bus once at sample point
CAN_CONFIG_SAMPLE_THRICE: Sample bus three times prior to sample point.
CAN_CONFIG_STD_MSG Accept only standard identifier messages
CAN_CONFIG_XTD_MSG Accept only extended identifier messages
CAN_CONFIG_DBL_BUFFER_ON: Use double buffering to receive data
CAN_CONFIG_DBL_BUFFER_OFF Do not use double buffering
CAN_CONFIG_ALL_MSG Accept all messages including invalid ones
CAN_CONFIG_VALID_XTD_MSG Accept only valid extended identifier messages
CAN_CONFIG_VALID_STD_MSG Accept only valid standard identifier messages
CAN_CONFIG_ALL_VALID_MSG Accept all valid messages



 
Last edited:

Thread Starter

the kid

Joined Jan 4, 2015
81
I have a question about MIKROC
I im learning CAN, but i see a proplem with MikroC as folow:

Code:
 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_1st = 12111, ID_2nd = 3;                       // node IDs
long Rx_ID;

void main() {

  PORTC = 0;                                                // clear PORTC
  TRISC = 0;                                                // set PORTC as output

  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_2nd,_CAN_CONFIG_XTD_MSG);// set id of filter B2_F4 to 2nd node ID

  CANSetOperationMode(_CAN_MODE_NORMAL,0xFF);               // set NORMAL 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
    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
    }
  }
}
i dont under stand why receiver function have & characters befor some variable below:
Msg_Rcvd = CANRead(&Rx_ID , RxTx_Data , &Rx_Data_Len, &Can_Rcv_Flags);
why is not
Msg_Rcvd= CANRead(Rx_ID , RxTx_Data , Rx_Data_Len, Can_Rcv_Flags);
can anynone explain for me. thank you
sory for my English
 

Papabravo

Joined Feb 24, 2006
21,159
Your confusion revolves around your misunderstanding of how arguments are passed to a function in C. They are passed on the stack by value. In your 2nd example, space would be created on the stack for each variable. The function CANRead would be called and new values of each variable would be written to temporary variables on the stack. Then the function CANRead would return and those temporary variables on the stack would would be reclaimed and therefore lost. Not a very useful behavior.

In the first example, what operation does the & character perform? This time try answering that specific question.
 

Papabravo

Joined Feb 24, 2006
21,159
Sory, but i see, my first example, variable 'RxTxdata' has not a '&' . i dont understaind this
Without the &, RxTxData cannot receive the contents of the CAN frame. Sorry, but you need to learn the C language better than you have done so far in order to understand how a function can return more than a single number. Show some industry and figure out what the purpose of the & operator is.
 

Thread Starter

the kid

Joined Jan 4, 2015
81
Thank u verymuch, but i have reading about http://download.mikroe.com/documents/compilers/mikroc/pic/help/can_library.htm

I dont understain this code of the side
while(1) { // endless loop
Msg_Rcvd = CANRead(&Rx_ID , RxTx_Data , &Rx_Data_Len, &Can_Rcv_Flags); // receive message
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
}
}
why in code
"Msg_Rcvd = CANRead(&Rx_ID , RxTx_Data , &Rx_Data_Len, &Can_Rcv_Flags);"
RxTx_Data has not "&" while other variable has got




 

Papabravo

Joined Feb 24, 2006
21,159
In a C Compiler the name of an array is often a synonym for the address of the first element of the array. For example if the following declaration was in your code:

Code:
unsigned char TxTxData[8] ;
then the expression RxTxData is equivalent to &RxTxData[0]
 

Thread Starter

the kid

Joined Jan 4, 2015
81
In a C Compiler the name of an array is often a synonym for the address of the first element of the array. For example if the following declaration was in your code:

Code:
unsigned char TxTxData[8] ;
then the expression RxTxData is equivalent to &RxTxData[0]
thank you very much
That is answer perfect.
once again, Thank
 
Top