KMA220
NXP Semiconductors
Dual channel programmable angle sensor
13.4.1 Software example in C
1
#include <stdio.h.>
2
3
// calc_crc accepts unsigned 16-bit data in data
4
int calc_crc(int crc, unsigned int data)
5
{
6
7
const int gpoly
int i;
=
0x107; // generator polynomial
//index variable
8
for (i = 15; i >= 0; i--)
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
crc <<= 1;
crc = (int) ((data
//shift left
(1u<<i))>>i);
&
// XOR of with generator polynomial when MSB(9)
if (crc 0x100) crc ^= gpoly;
= HIGH
&
}
return crc;
}
int main(void)
{
int crc, crc_res, i;
// LSB are CRC field filled with
unsigned int data_seq[]
8
0
=
{0x0000, 0xFFC1, 0x0400, 0x0100,
0x1300, 0x0000, 0x0000, 0x0000};
// calculate checksum over all data
crc 0xFF; // start value of crc register
printf(“Address\tValue\n”);
for (i 0; <= 7; i++)
=
=
i
{
printf(“0x%1X\t0x%04X\n”, i, data_seq[i]);
crc
}
= calc_crc(crc, data_seq[i]);
crc_res
printf(“\nChecksum\n0x%02X\n”, crc_res);
// check procedure for preceding data sequence
crc 0xFF;
for (i
crc
=
crc;
// crc_res = 0xA9
=
=
0;
=
i
<= 6; i++)
calc_crc(crc, data_seq[i]);
// last word gets crc inserted
crc calc_crc(crc, data_seq[i]
printf(“\nCheck procedure for data sequence: must be 0x00 is 0x%02X.\n”, crc);
=
crc_res);
return 1;
}
The checksum of this data sequence is A9h.
KMA220
All information provided in this document is subject to legal disclaimers.
© NXP B.V. 2012. All rights reserved.
Product data sheet
Rev. 1 — 24 May 2012
23 of 36