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