Help about CAN network

Discussion in 'Embedded Systems and Microcontrollers' started by the kid, Jul 9, 2016.

  1. the kid

    Thread Starter Member

    Jan 4, 2015
    59
    1
    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
     
  2. Papabravo

    Expert

    Feb 24, 2006
    10,163
    1,796
    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?
     
    the kid likes this.
  3. MaxHeadRoom

    Expert

    Jul 18, 2013
    10,565
    2,379
    I believe Picmicro have an App note on Canbus if you haven't seen it already.
    Max.
     
  4. Papabravo

    Expert

    Feb 24, 2006
    10,163
    1,796
    App Notes are great but I would start with Section 24 of the datasheet. There is an excellent explanation of CAN bit timing there in § 24.9 on p. 339
     
    the kid likes this.
  5. the kid

    Thread Starter Member

    Jan 4, 2015
    59
    1
    I HAVE A QUESTION
    "_CAN_CONFIG_XTD_MSG'' fLAG IS CAN CONFIGUTION EXTENDED MESSAGE?
    HOW TO THE FLAGS AFFECT TO THE CONFIGUTION REGISTER?
     
  6. Papabravo

    Expert

    Feb 24, 2006
    10,163
    1,796
    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.
     
    the kid and JohnInTX like this.
  7. JohnInTX

    Moderator

    Jun 26, 2012
    2,348
    1,029
  8. the kid

    Thread Starter Member

    Jan 4, 2015
    59
    1
    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: Jul 10, 2016
  9. the kid

    Thread Starter Member

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

    Code (Text):
    1.  unsigned char Can_Init_Flags, Can_Send_Flags, Can_Rcv_Flags; // can flags
    2. unsigned char Rx_Data_Len;                                   // received data length in bytes
    3. char RxTx_Data[8];                                           // can rx/tx data buffer
    4. char Msg_Rcvd;                                               // reception flag
    5. const long ID_1st = 12111, ID_2nd = 3;                       // node IDs
    6. long Rx_ID;
    7.  
    8. void main() {
    9.  
    10.   PORTC = 0;                                                // clear PORTC
    11.   TRISC = 0;                                                // set PORTC as output
    12.  
    13.   Can_Init_Flags = 0;                                       //
    14.   Can_Send_Flags = 0;                                       // clear flags
    15.   Can_Rcv_Flags  = 0;                                       //
    16.  
    17.   Can_Send_Flags = _CAN_TX_PRIORITY_0 &                     // form value to be used
    18.                    _CAN_TX_XTD_FRAME &                      //     with CANWrite
    19.                    _CAN_TX_NO_RTR_FRAME;
    20.  
    21.   Can_Init_Flags = _CAN_CONFIG_SAMPLE_THRICE &              // form value to be used
    22.                    _CAN_CONFIG_PHSEG2_PRG_ON &              // with CANInit
    23.                    _CAN_CONFIG_XTD_MSG &
    24.                    _CAN_CONFIG_DBL_BUFFER_ON &
    25.                    _CAN_CONFIG_VALID_XTD_MSG;
    26.  
    27.   CANInitialize(1,3,3,3,1,Can_Init_Flags);                  // Initialize CAN module
    28.   CANSetOperationMode(_CAN_MODE_CONFIG,0xFF);               // set CONFIGURATION mode
    29.   CANSetMask(_CAN_MASK_B1,-1,_CAN_CONFIG_XTD_MSG);          // set all mask1 bits to ones
    30.   CANSetMask(_CAN_MASK_B2,-1,_CAN_CONFIG_XTD_MSG);          // set all mask2 bits to ones
    31.   CANSetFilter(_CAN_FILTER_B2_F4,ID_2nd,_CAN_CONFIG_XTD_MSG);// set id of filter B2_F4 to 2nd node ID
    32.  
    33.   CANSetOperationMode(_CAN_MODE_NORMAL,0xFF);               // set NORMAL mode
    34.  
    35.   RxTx_Data[0] = 9;                                         // set initial data to be sent
    36.  
    37.   CANWrite(ID_1st, RxTx_Data, 1, Can_Send_Flags);           // send initial message
    38.  
    39.   while(1) {                                                               // endless loop
    40.     Msg_Rcvd = CANRead(&Rx_ID , RxTx_Data , &Rx_Data_Len, &Can_Rcv_Flags); // receive message
    41.     if ((Rx_ID == ID_2nd) && Msg_Rcvd) {                                   // if message received check id
    42.       PORTC = RxTx_Data[0];                                                // id correct, output data at PORTC
    43.       RxTx_Data[0]++ ;                                                     // increment received data
    44.       Delay_ms(10);
    45.       CANWrite(ID_1st, RxTx_Data, 1, Can_Send_Flags);                      // send incremented data back
    46.     }
    47.   }
    48. }
    49.  
    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
     
  10. Papabravo

    Expert

    Feb 24, 2006
    10,163
    1,796
    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.
     
    the kid likes this.
  11. the kid

    Thread Starter Member

    Jan 4, 2015
    59
    1
    Sory, but i see, my first example, variable 'RxTxdata' has not a '&' . i dont understaind this
     
  12. Papabravo

    Expert

    Feb 24, 2006
    10,163
    1,796
    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.
     
    the kid likes this.
  13. the kid

    Thread Starter Member

    Jan 4, 2015
    59
    1
    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




     
  14. Papabravo

    Expert

    Feb 24, 2006
    10,163
    1,796
    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 (Text):
    1.  
    2. unsigned char TxTxData[8] ;
    3.  
    then the expression RxTxData is equivalent to &RxTxData[0]
     
    the kid likes this.
  15. the kid

    Thread Starter Member

    Jan 4, 2015
    59
    1
    thank you very much
    That is answer perfect.
    once again, Thank
     
Loading...