KMA199 All information provided in this document is subject to legal disclaimers. © NXP B.V. 2011. All rights reserved.
Product data sheet Rev. 2 — 7 December 2011 22 of 36
NXP Semiconductors
KMA199
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 const int gpoly = 0x107; // generator polynomial
7 int i; //index variable
8 for (i = 15; i >= 0; i--)
9{
10 crc <<= 1; //shift left
11 crc = (int) ((data & (1u<<i))>>i);
12 // XOR of with generator polynomial when MSB(9) = HIGH
13 if (crc & 0x100) crc ^= gpoly;
14 }
15 return crc;
16 }
17 int main(void)
18 {
19 int crc, crc_res, i;
20 // 8 LSB are CRC field filled with 0
21 unsigned int data_seq[] = {0x1111, 0x2222, 0x3333, 0x4444,
22 0x5555, 0x6666, 0x7777, 0x8888,
23 0x9999, 0xAAAA, 0xBBBB, 0xCCCC,
24 0xDDDD, 0xEEEE, 0xFFFF, 0x4200};
25 // calculate checksum over all data
26 crc = 0xFF; // start value of crc register
27 printf(“Address\tValue\n”);
28 for (i = 0; i <= 15; i++)
29 {
30 printf(“0x%1X\t0x%04X\n”, i, data_seq[i]);
31 crc = calc_crc(crc, data_seq[i]);
32 }
33 crc_res = crc; // crc_res = 0x6F
34 printf(“\nChecksum\n0x%02X\n”, crc_res);
35 // check procedure for above data sequence
36 crc = 0xFF;
37 for (i = 0; i <= 14; i++)
38 crc = calc_crc(crc, data_seq[i]);
39 // last word gets crc inserted
40 crc = calc_crc(crc, data_seq[i] crc_res);
41 printf(“\nCheck procedure for data sequence: must be 0x00 is 0x%02X.\n”,crc);
42 return 1;
43 }
The checksum of this data sequence is 6Fh.