• AVR Freaks

Hot!PIC32MX795F512L K-Line

Author
somebody
New Member
  • Total Posts : 10
  • Reward points : 0
  • Joined: 2016/06/16 07:25:32
  • Location: 0
  • Status: offline
2020/02/17 10:45:39 (permalink)
0

PIC32MX795F512L K-Line

Hi
I Need help,  K-Line CRC calculation, I found an example on the net but that’s not good.
 
char test_CRC [] = {0x80,0x6f,0xf1,0x05,0x00,0x21,0xfa,0xff,0x29,0xf6,0x42};
Logic analyzer reads this CRC = 0xBF
 
   //CRC calculation
      char iso_checksum = 0;
     UINT32 crc = 0;
     int len = sizeof(test_CRC);
     int  i =0;
 
    for (  i = 0; i < len; i++){
       crc = crc + test_CRC ;
    }
   iso_checksum = crc;
 
Please help solve the problem.
Thanks.
#1

12 Replies Related Threads

    jdeguire
    Super Member
    • Total Posts : 567
    • Reward points : 0
    • Joined: 2012/01/13 07:48:44
    • Location: United States
    • Status: offline
    Re: PIC32MX795F512L K-Line 2020/02/17 11:10:25 (permalink)
    2.5 (2)
    I'm not familiar with K-Line; it sounds like some automotive interface standard.  Anyway, that code is not going to calculate a CRC.  It's close to calculating a checksum, but you would want to change
     
    crc = crc + test_CRC;


     
    to
     
    crc = crc + test_CRC[i];

     
    instead.
     
    Are you trying to calculate a checksum or a CRC?  They are different things and involve different math.
    post edited by jdeguire - 2020/02/17 11:11:40
    #2
    somebody
    New Member
    • Total Posts : 10
    • Reward points : 0
    • Joined: 2016/06/16 07:25:32
    • Location: 0
    • Status: offline
    Re: PIC32MX795F512L K-Line 2020/02/17 11:23:18 (permalink)
    0
    Hi
    it doesn't work like that, yes automotive interface standard.
     
     CRC
    #3
    somebody
    New Member
    • Total Posts : 10
    • Reward points : 0
    • Joined: 2016/06/16 07:25:32
    • Location: 0
    • Status: offline
    Re: PIC32MX795F512L K-Line 2020/02/17 11:57:52 (permalink)
    0
    ISO 9141 (K-Line)
    • Data rates
    • 5 Bits/s to 50 Kilobits/s (K-Line)
    • Bi-Directional Half-Duplex bus communication
    • Manchester type of signal coding
    • Single-Ended Bus
    • Signal waveshaping
    • Dominant / Recessive Contention Handling
    • Collision Back-Off
    • Error detection
    • 8-bit CRC
    • Parity
    • Framing
    • Supports Multi-Masters (microcontrollers)
    #4
    simong123
    Lab Member No. 003
    • Total Posts : 1391
    • Reward points : 0
    • Joined: 2012/02/07 18:21:03
    • Location: Future Gadget Lab (UK Branch)
    • Status: offline
    Re: PIC32MX795F512L K-Line 2020/02/17 15:25:53 (permalink)
    0
    From what I've read it is a simple 8bit checksum not CRC.
    Correcting your code
    //CRC calculation
        char test_CRC [] = {0x80,0x6f,0xf1,0x05,0x00,0x21,0xfa,0xff,0x29,0xf6,0x42};
        char iso_checksum = 0;
        UINT8 crc = 0;
        int len = sizeof(test_CRC);
        int  i =0;

        for (  i = 0; i < len; i++)
        {
            crc = crc + test_CRC[i] ;
        }
        iso_checksum = crc;

    However that does not seem to work for the data you have supplied.
    #5
    ric
    Super Member
    • Total Posts : 27716
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: offline
    Re: PIC32MX795F512L K-Line 2020/02/17 15:57:49 (permalink)
    0
    jdeguire
    ...
    It's close to calculating a checksum, but you would want to change
     
    crc = crc + test_CRC;


     
    to
     
    crc = crc + test_CRC[i];

     
    instead.

    That is what the OP would have originally posted, but because they failed to use code tags around their code, the array index was parsed as a "start italics" command by the forum. Note how the code is all in italics from that point forward.
     

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #6
    somebody
    New Member
    • Total Posts : 10
    • Reward points : 0
    • Joined: 2016/06/16 07:25:32
    • Location: 0
    • Status: offline
    Re: PIC32MX795F512L K-Line 2020/02/18 03:29:02 (permalink)
    0
    Hi  simong123, ric
     
    I tried it in two ways, but this is not good either.
     
    char teszt_Cheksum [] = {0x80, 0x6F, 0xF1, 0x05, 0x00, 0x21, 0xFA, 0xFF, 0x11, 0xF6, 0x7E};
        UINT8 crc = 0;
        int len = sizeof (teszt_Cheksum);
        UINT16 i = 0;

        for (i = 0; i < len; i++) {
            crc = crc + teszt_Cheksum ;
        }
        crc;      // =  0x84  BAD
     
    another way
     
        UINT8 sum = 0;
      int len = sizeof (teszt_Cheksum);
        for (i = 0; i < len; i++) {
            sum += teszt_Cheksum;
            if (sum >= 256)
                sum -= 255;
        }
        sum = 0xFF & (~sum);  // = 0x7B BAD
     
     
     
     
    I've attached a chart which is a good checksum. I want to calculate the checksum in the picture, please help me solve this problem,
     
     
    Thanks.

    Attached Image(s)

    #7
    somebody
    New Member
    • Total Posts : 10
    • Reward points : 0
    • Joined: 2016/06/16 07:25:32
    • Location: 0
    • Status: offline
    Re: PIC32MX795F512L K-Line 2020/02/18 03:31:49 (permalink)
    0
    do not know why he left the i
    #8
    ric
    Super Member
    • Total Posts : 27716
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: offline
    Re: PIC32MX795F512L K-Line 2020/02/18 03:35:30 (permalink)
    0
    Don't use "char" for small numbers. Use "unsigned char" or "signed char" as appropriate.
     

    do not know why he left the i

    Because you did not place "code" tags around your code. The forum thinks that [i] is an instruction to start italic text.
     

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #9
    somebody
    New Member
    • Total Posts : 10
    • Reward points : 0
    • Joined: 2016/06/16 07:25:32
    • Location: 0
    • Status: offline
    Re: PIC32MX795F512L K-Line 2020/02/18 03:39:18 (permalink)
    0
     
    Thanks, ric
     
     
       
      unsigned char test_CRC [] = {0x80, 0x6F, 0xF1, 0x05, 0x00, 0x21, 0xFA, 0xFF, 0x11, 0xF6, 0x7E};
       UINT8 crc = 0;
        int len = sizeof (test_CRC);
        UINT16 i = 0;
        for (i = 0; i < len; i++) {
            crc = crc + test_CRC[i];
        }
        crc;

     
      
        signed char test_CRC [] = {0x80, 0x6F, 0xF1, 0x05, 0x00, 0x21, 0xFA, 0xFF, 0x11, 0xF6, 0x7E};
     
       int len = sizeof (test_CRC);
     
        UINT8 sum = 0;
        for (i = 0; i < len; i++) {
            sum += test_CRC[i];
            if (sum >= 256)
                sum -= 255;
        }
        sum = 0xFF & (~sum);

     
     but this is not good either.
     
    post edited by somebody - 2020/02/18 04:09:45
    #10
    andersm
    Super Member
    • Total Posts : 2823
    • Reward points : 0
    • Joined: 2012/10/07 14:57:44
    • Location: 0
    • Status: offline
    Re: PIC32MX795F512L K-Line 2020/02/18 04:16:19 (permalink)
    0
    As has already been posted above, adding the numbers is not CRC.
    #11
    JPortici
    Super Member
    • Total Posts : 1094
    • Reward points : 0
    • Joined: 2012/11/17 06:27:45
    • Location: Grappaland
    • Status: offline
    Re: PIC32MX795F512L K-Line 2020/02/18 06:02:50 (permalink)
    0
    ric
    Don't use "char" for small numbers. Use "unsigned char" or "signed char" as appropriate.



    better yet, he should use fixed size types as defined in <stdint.h> so the intention is clear.
    uint8_t for 8bit, unsigned.
    int8_t for 8bit, signed.
    and so on
     
    @OP: are you sure that the data you are looking at is correct? as per ISO 9141-2 the maximum message length is of 11 bytes (2x header + address + up to 7x data + checksum)
    And the checksum is indeed the sum of all the previous bytes.
    #12
    somebody
    New Member
    • Total Posts : 10
    • Reward points : 0
    • Joined: 2016/06/16 07:25:32
    • Location: 0
    • Status: offline
    Re: PIC32MX795F512L K-Line 2020/02/18 07:48:38 (permalink)
    0
    Hi  JPortici
    the serial port monitor reads this.
     
    [18/02/2020 15:25:53] - Written data

    80 6f f1 05 00 21 fa ff 58 f6 2a 84                      €oñ..!úÿXö*„

    [18/02/2020 13:25:53] - Read data

    80 6f f1 05 00 21 fa ff 58 f6 2a 84 80 f1 6f 0d     €oñ..!úÿXö*„€ño.
    00 61 fa ff 58 f6 c8 00 58 02 ff ff ff ff c0 56             .aúÿXöÈ.X.ÿÿÿÿÀV
    Protokol  K-Line (ISO 14230) ?
     
    Not help:
    uint8_t for 8bit, unsigned.
    int8_t for 8bit, signed
     
    Any suggestion?
     
    Thanks
    #13
    Jump to:
    © 2020 APG vNext Commercial Version 4.5