-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpololu_crc7.c
51 lines (40 loc) · 842 Bytes
/
pololu_crc7.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
#include <stdio.h>
const unsigned char CRC7_POLY = 0x91;
unsigned char CRCTable[256];
int result;
unsigned char GetCRC(unsigned char val)
{
unsigned char j;
for (j = 0; j < 8; j++)
{
if (val & 1)
val ^= CRC7_POLY;
val >>= 1;
}
return val;
}
void GenerateCRCTable()
{
int i, j;
// generate a table value for all 256 possible byte values
for (i = 0; i < 256; i++)
{
CRCTable[i] = GetCRC(i);
}
}
unsigned char CRC(unsigned char message[], unsigned char length)
{
unsigned char i, crc = 0;
for (i = 0; i < length; i++)
crc = CRCTable[crc ^ message[i]];
return crc;
}
int get_crc7(unsigned char *message, int param_num)
{
GenerateCRCTable();
result = CRC(message, param_num);
return result;
}
int main() {
return 0;
}