Hot!CRC problem

Page: 12 > Showing page 1 of 2
Author
kishor009
Junior Member
  • Total Posts : 96
  • Reward points : 0
  • Joined: 2017/08/22 05:48:27
  • Location: india
  • Status: offline
2017/09/07 02:32:43 (permalink)
0

CRC problem

hiii,
 
i writed CRC code. how to check this code is correct or not???
i use mplab xc8
 
"
#include <pic16f1936.h>
#define poly 0xA001;
unsigned char buffer[8];
void main(unsigned char *b, unsigned char len)
{
unsigned int crc, crchi, crclo, byte;
crc = 0xFFFF;
while (len--)
{
byte = *b;
*b++;
crc = crc ^ byte;
for (unsigned int i = 0; i < 8; i++)
{
crc = crc >> 1;
if (crc & 0x0001)
{
crc = crc ^ poly;
}
}
}
crclo = crc & 0xFF;
crchi = (crc >> 8) & 0xFF;
crc = crclo;
crc = (crc << 8) | crchi;
}
  "
#1

34 Replies Related Threads

    Nikolay_Po
    Super Member
    • Total Posts : 1657
    • Reward points : 0
    • Joined: 2012/04/01 13:49:27
    • Location: Russia, Novorossiysk
    • Status: offline
    Re: CRC problem 2017/09/07 03:48:33 (permalink)
    +1 (1)
    Take a look here.
    #2
    CinziaG
    rome burns :D
    • Total Posts : 3136
    • Reward points : 0
    • Joined: 2016/12/07 14:20:36
    • Location: Wien
    • Status: offline
    Re: CRC problem 2017/09/07 03:57:00 (permalink)
    0
    Also, better keep variables to "byte" and not "int" wherever possible!
     
     
    #3
    kishor009
    Junior Member
    • Total Posts : 96
    • Reward points : 0
    • Joined: 2017/08/22 05:48:27
    • Location: india
    • Status: offline
    Re: CRC problem 2017/09/07 04:09:27 (permalink)
    0
    sir, tell me my above program is correct or not??
    #4
    CinziaG
    rome burns :D
    • Total Posts : 3136
    • Reward points : 0
    • Joined: 2016/12/07 14:20:36
    • Location: Wien
    • Status: offline
    Re: CRC problem 2017/09/07 04:15:47 (permalink)
    0
    It looks good enough to me, did you try it?
     
    This is mine I used for long:

    BYTE GetChkSum(BYTE *d,BYTE len) {
        BYTE i,t,c;        //

        t=0;            //

        do {
            ClrWdt();
            t ^= *d++;
            for(i=0; i<8; i++) {
                c=t;
                t <<= 1;
                if(c & 0x80)
                    t ^= 0x85;
                }
            }    while(--len);

        return t;
        }

    #5
    kishor009
    Junior Member
    • Total Posts : 96
    • Reward points : 0
    • Joined: 2017/08/22 05:48:27
    • Location: india
    • Status: offline
    Re: CRC problem 2017/09/07 04:18:28 (permalink)
    0
    but how to check?
    i don't know
    #6
    CinziaG
    rome burns :D
    • Total Posts : 3136
    • Reward points : 0
    • Joined: 2016/12/07 14:20:36
    • Location: Wien
    • Status: offline
    Re: CRC problem 2017/09/07 04:22:10 (permalink)
    0
    maybe comparing the result you obtain with an online reference as Nikolay suggested Smile
    #7
    CinziaG
    rome burns :D
    • Total Posts : 3136
    • Reward points : 0
    • Joined: 2016/12/07 14:20:36
    • Location: Wien
    • Status: offline
    Re: CRC problem 2017/09/07 04:24:13 (permalink)
    0
    Note the, if you're testing CRC for Modbus as per your other thread, this is the code I used time ago:
     

    WORD CEnergyMeter::getChksum16(BYTE *p,int len) {
        WORD c;
        int i,j;

    /*BEGIN
            ErrorWord = Hex (FFFF)
    FOR Each byte in message
           ErrorWord = ErrorWord XOR byte in message
    FOR Each bit in byte
                     LSB = ErrorWord AND Hex (0001)
             IF LSB = 1 THEN ErrorWord  = ErrorWord  1
           ErrorWord = ErrorWord / 2
                           IF LSB = 1 THEN ErrorWord  = ErrorWord  XOR Hex (A001)
    NEXT bit in byte
    NEXT Byte in message
    END*/

        c=0xffff;
        while(len--) {
            c ^= *p;
            for(i=0; i<8; i++) {
                j=c & 1;
                c >>= 1;
                if(j) {
                    c ^= 0xa001;
                    }
                }

            p++;
            }

        return c;
        }




    #8
    kishor009
    Junior Member
    • Total Posts : 96
    • Reward points : 0
    • Joined: 2017/08/22 05:48:27
    • Location: india
    • Status: offline
    Re: CRC problem 2017/09/07 04:26:42 (permalink)
    0
    but i don't know which value store in crc after end of program
    #9
    kishor009
    Junior Member
    • Total Posts : 96
    • Reward points : 0
    • Joined: 2017/08/22 05:48:27
    • Location: india
    • Status: offline
    Re: CRC problem 2017/09/07 04:39:36 (permalink)
    +1 (1)
    your code and my code are same
    #10
    kishor009
    Junior Member
    • Total Posts : 96
    • Reward points : 0
    • Joined: 2017/08/22 05:48:27
    • Location: india
    • Status: offline
    Re: CRC problem 2017/09/08 00:44:20 (permalink)
    0
    how to place CRC value in modbus frame ??
    #11
    Alpha Whisky
    Senior Member
    • Total Posts : 35
    • Reward points : 0
    • Joined: 2014/10/02 07:05:22
    • Location: 0
    • Status: offline
    Re: CRC problem 2017/09/08 01:01:01 (permalink)
    +1 (1)
    kishor009
    how to place CRC value in modbus frame ??


    http://www.modbus.org/docs/Modbus_over_serial_line_V1_02.pdf
     
    #12
    CinziaG
    rome burns :D
    • Total Posts : 3136
    • Reward points : 0
    • Joined: 2016/12/07 14:20:36
    • Location: Wien
    • Status: offline
    Re: CRC problem 2017/09/08 01:55:29 (permalink)
    0
    It's is at the end of the frame.
    #13
    kishor009
    Junior Member
    • Total Posts : 96
    • Reward points : 0
    • Joined: 2017/08/22 05:48:27
    • Location: india
    • Status: offline
    Re: CRC problem 2017/09/08 02:11:56 (permalink)
    0
    let my modbus fram is,
    #define slaveid 0x01
    unsinged char mod_frame[] = {slaveid, 0x03, 0x00, 0x83, ?, ?}
     
    which value place at '?'
    #14
    CinziaG
    rome burns :D
    • Total Posts : 3136
    • Reward points : 0
    • Joined: 2016/12/07 14:20:36
    • Location: Wien
    • Status: offline
    Re: CRC problem 2017/09/08 02:18:02 (permalink)
    0
    The CRC as I explained above.
    #15
    Alpha Whisky
    Senior Member
    • Total Posts : 35
    • Reward points : 0
    • Joined: 2014/10/02 07:05:22
    • Location: 0
    • Status: offline
    Re: CRC problem 2017/09/08 02:22:24 (permalink)
    +1 (1)
    From http://www.modbus.org/docs/Modbus_over_serial_line_V1_02.pdf
     
    The CRC field contains a 16–bit value implemented as two 8–bit bytes. The CRC field is appended to the message as the last field in the message. When this is done, the low–order byte of the field is appended first, followed by the high–order byte. The CRC high–order byte is the last byte to be sent in the message.

    #16
    kishor009
    Junior Member
    • Total Posts : 96
    • Reward points : 0
    • Joined: 2017/08/22 05:48:27
    • Location: india
    • Status: offline
    Re: CRC problem 2017/09/08 02:24:04 (permalink)
    0
    sir, i think CRC register value place in modbus frame
    but my question is that,
    after end of CRC program which value contain CRC register??
    #17
    kishor009
    Junior Member
    • Total Posts : 96
    • Reward points : 0
    • Joined: 2017/08/22 05:48:27
    • Location: india
    • Status: offline
    Re: CRC problem 2017/09/09 04:54:51 (permalink)
    0
    i writed two program. first program for master and second program for CRC calculation 
    how place my CRC program inside master program?
    and
    ?1 and ?2 for CRC value but i don't know.
     
    for master
    "
    #include <pic16f1936.h>
    void uart_tx_config();
    void baud_rate_set();
    unsigned char mod_frame[2] = {0x01, 0x0B, ?1 , ?2};
    void baud_rate_set()
    {
    BAUDCONbits.BRG16 = 0; // 8-bit Baud Rate Generator is used
    TXSTAbits.BRGH = 1; // high baud rate select
    SPBRG = 0x19; // for 9600 baud rate
    }
    void uart_tx_config()
    {
    TRISCbits.TRISC5 = 0; // set RC5 as a output
    PORTCbits.RC5 = 1; // enable driver output
    TRISCbits.TRISC6 = 0; // set RC6 as a output
    TXSTAbits.SYNC = 0; // asynchronous mode
    TXSTAbits.CSRC = 1; // set master mode
    RCSTAbits.SREN = 0; // disables single reception
    RCSTAbits.CREN = 0; // disables continuous reception
    RCSTAbits.SPEN = 1; // Serial port enabled (configures TX/CK pins as serial port pins)
    TXSTAbits.TXEN = 1; // Transmit enabled
    }
    void main()
    {
    TRISB = 0x00; // make port b as a output
    PORTB = 0x00; // clear port b
    OSCCONbits.SCS0 = 0; // select external clock
    OSCCONbits.SCS1 = 0; // select external clock
    baud_rate_set();
    uart_tx_config();
    TXREG = 0x00;
    while (TXSTAbits.TRMT == 0); // check TSR register is empty
    TXREG = 0xFF;
    for (unsigned int count = 0; count < 30000; count++); // delay
    while (1)
    {
    unsigned char j = 0;
    while (TXSTAbits.TRMT == 0); // check TSR register is empty
    TXREG = mod_frame[j];
    j++;
    for (unsigned int count = 0; count < 30000; count++); // delay
    while (TXSTAbits.TRMT == 0); // check TSR register is empty
    TXREG = mod_frame[j];
    for (unsigned int count = 0; count < 30000; count++); // delay
    }
    }
    "
     
    for CRC
     
    "
    #include <pic16f1936.h>
    #define poly 0xA001;
    unsigned char buffer[8];
    void main(unsigned char *b, unsigned char len)
    {
    unsigned int crc, crchi, crclo;
    crc = 0xFFFF;
    while (len--)
    {
    crc ^= *b;
    b++;
    for (unsigned int i = 0; i < 8; i++)
    {
    crc = crc >> 1;
    if (crc & 0x0001)
    {
    crc = crc ^ poly;
    }
    }
    }
    crclo = crc & 0xFF;
    crchi = (crc >> 8) & 0xFF;
    crc = crclo;
    crc = (crc << 8) | crchi;
    }
    "
     
     
     
     
     
     
    #18
    kishor009
    Junior Member
    • Total Posts : 96
    • Reward points : 0
    • Joined: 2017/08/22 05:48:27
    • Location: india
    • Status: offline
    Re: CRC problem 2017/09/12 03:32:51 (permalink)
    0
    I am sending this data,
     
    address = 0x01
    function = 0x03
    data =      0xFF
    data =      0xFF
    crclo =     ?
    crchi  =    ?
     
     then what is my crc ?
    #19
    Alpha Whisky
    Senior Member
    • Total Posts : 35
    • Reward points : 0
    • Joined: 2014/10/02 07:05:22
    • Location: 0
    • Status: offline
    Re: CRC problem 2017/09/12 03:35:32 (permalink)
    +1 (1)
    As you obviously can't be bothered to do even the simplest google search I'm not sure why I'm posting this, but:
     
    https://www.lammertbies.nl/comm/info/crc-calculation.html
     
    #20
    Page: 12 > Showing page 1 of 2
    Jump to:
    © 2017 APG vNext Commercial Version 4.5