-
Notifications
You must be signed in to change notification settings - Fork 11
/
Sensirion_SCD30_CRC31.c
67 lines (44 loc) · 1.43 KB
/
Sensirion_SCD30_CRC31.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
// CRC8-31 for SCD30
// If this saves you several man-hours/days consider https://paypal.me/cliveone
// 24-Apr-2024 [email protected]
// Now accepting coffee https://buymeacoffee.com/cliveone
// https://sensirion.com/media/documents/D7CEEF4A/6165372F/Sensirion_CO2_Sensors_SCD30_Interface_Description.pdf
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
typedef unsigned char uint8_t;
typedef unsigned short uint16_t;
typedef unsigned int uint32_t;
uint8_t test_crc8_31(uint8_t crc, uint8_t data)
{
int i;
crc = crc ^ data;
for(i=0; i<8; i++)
if (crc & 0x80)
crc = (crc << 1) ^ 0x31;
else
crc <<= 1;
return(crc);
}
uint8_t test_crc8_31_quick(uint8_t crc, uint8_t data)
{
static const uint8_t crctbl[] = { // 0x31 nibble table - [email protected]
0x00,0x31,0x62,0x53,0xC4,0xF5,0xA6,0x97,0xB9,0x88,0xDB,0xEA,0x7D,0x4C,0x1F,0x2E };
crc = crc ^ data;
crc = (crc << 4) ^ crctbl[crc >> 4]; // Process byte 4-bits at a time
crc = (crc << 4) ^ crctbl[crc >> 4];
return(crc);
}
int main(int argc, char **argv)
{
uint8_t crc;
crc = 0xFF; // pattern BE EF [92]
crc = test_crc8_31(crc, 0xBE);
crc = test_crc8_31(crc, 0xEF);
printf("%02X == 92 ??\n", crc);
crc = 0xFF; // pattern BE EF [92]
crc = test_crc8_31_quick(crc, 0xBE);
crc = test_crc8_31_quick(crc, 0xEF);
printf("%02X == 92 ??\n", crc);
return(1);
}