sht11 crc calculation

General discussion on mikroC.
Post Reply
Author
Message
burtay
Posts: 2
Joined: 21 Aug 2008 10:08

sht11 crc calculation

#1 Post by burtay » 21 Aug 2008 10:46

hi everybody;
I'm trying to do a bytewise crc calculation for sht11. I got 1 byte read humidity command, 2 bytes of humidity data, and 1 byte checksum. I'm keeping them in an array (checksumarray[4]) so here how i call it: "crcResult=calculateChecksum(checksumarray, sizeof(checksumarray)-1);" It should return "reverse of the cheksum byte" that I got from sht11 but it does not. calculateChecksum function and look-up table is below. Help will be greatly appreciated.
Here is the sht11 crc calculation algorithm, in case I'm getting it wrong:
With this implementation the CRC data is stored in a 256 byte lookup table.
Perform the following operations:
1. Initialize the CRC register with the value of the lower nibble of the value of the status register
(reversed (s0s1s2s3‘0000)). (default ‘00000000’ = 0)
2. XOR each (transmitted and received) byte with the previous CRC value.
The result is the new byte that you need to calculate the CRC value from.
3. Use this value as the index to the table to obtain the new CRC value.
4. Repeat from 2.) until you have passed all bytes through the process.
5. The last byte retrieved from the table is the final CRC value.
6. The CRC value retrieved from the SHTxx must be reversed (bit 0 = bit 7, bit 1=bit 6 … bit 7 = bit 0) and can then be
compared to the final CRC value.



Code: Select all


unsigned char calculateChecksum(unsigned char * t, short size ){
       short index;
       unsigned char crcValue=0;
       for(index=0; index<size; index++)
       {
        crcValue= crcTab[ (crcValue ^ t[index]) ]; //xors the crcvalue with given array and uses it as an index for crcTab to gather a new crcValue 
       }
       return crcValue;

}

// crc lookup table

const unsigned char crcTab[]={
0,  49, 98, 83, 196,  245, 166, 151, 185, 136, 219, 234, 125,  76, 31, 46, 67, 114, 33, 16,
135, 182, 229, 212, 250, 203, 152, 169,  62, 15, 92, 109,  134, 183, 228, 213,  66, 115, 32, 17,
63, 14, 93, 108,  251, 202, 153, 168, 197, 244, 167, 150,  1,  48, 99, 82, 124, 77, 30, 47,
184, 137, 218, 235,  61, 12, 95, 110,  249, 200, 155, 170, 132, 181, 230, 215,  64, 113, 34, 19,
126,  79, 28, 45, 186,  139, 216, 233, 199, 246, 165, 148,  3,  50, 97, 80, 187,  138, 217, 232,
127,  78, 29, 44, 2,  51, 96, 81, 198,  247, 164, 149, 248, 201, 154, 171,  60, 13, 94, 111,
65, 112, 35, 18, 133,  180, 231, 214, 122,  75, 24, 41, 190,  143, 220, 237, 195, 242, 161, 144,
7, 54, 101,  84, 57, 8,  91, 106,  253, 204, 159, 174, 128, 177, 226,  211,  68, 117, 38, 23,
252, 205, 158, 175,  56, 9,  90, 107, 69, 116, 39, 22, 129,  176, 227, 210, 191, 142, 221, 236,
123,  74, 25, 40, 6,  55, 100, 85, 194,  243, 160, 145,  71, 118, 37, 20, 131,  178, 225, 208,
254, 207, 156, 173,  58, 11, 88, 105,  4, 53, 102,  87, 192,  241, 162, 147, 189, 140, 223, 238,
121,  72, 27, 42, 193,  240, 163, 146,  5, 52, 103,  86, 120, 73, 26, 43, 188,  141, 222, 239,
130, 179, 224, 209,  70, 119, 36, 21, 59, 10, 89, 104,  255, 206, 157, 172};
Last edited by burtay on 21 Aug 2008 15:29, edited 1 time in total.

burtay
Posts: 2
Joined: 21 Aug 2008 10:08

#2 Post by burtay » 21 Aug 2008 15:29

nevermind, it worked. i'm keepin the post in case somebody needs it.
here how i call it in main

Code: Select all


crcResult = calculateChecksum(chksmarray,sizeof(chksmarray)-1);
c=chksmarray[3]; // c holds checksum value          
 //---- reverse checksum 

c = (c & 0x0F) << 4 | (c & 0xF0) >> 4;
c = (c & 0x33) << 2 | (c & 0xCC) >> 2;
c = (c & 0x55) << 1 | (c & 0xAA) >> 1;

c = c^crcResult; //xor with crcResult to see if they are same


Drigo
Posts: 66
Joined: 23 Jun 2009 10:23

Re: sht11 crc calculation

#3 Post by Drigo » 24 Feb 2012 09:24

I am also interested in a CRC calculation of the SHTX1 sensors.
Can you post a full Code for this?

Post Reply

Return to “mikroC General”