Hallo Zusammen,
ich möchte gerne einen Maxon Motor via UART Ansteuern
(EPOS2 Motion Controller), alle Befehle bekommen hier
eine Prüfsumme (siehe Seite 23-24 im
Anhang, falls es jemanden interessiert).
Diese Beispiel müßte ich jedenfalls zum laufen kriegen.
1 | WORD CalcFieldCRC(WORD* pDataArray, WORD numberOfWords) {
|
2 |
|
3 | WORD shifter, c;
|
4 | WORD carry;
|
5 | WORD CRC = 0;
|
6 |
|
7 | //Calculate pDataArray Word by Word
|
8 | while(numberOfWords--) {
|
9 |
|
10 | shifter = 0x8000; //Initialize BitX to Bit15
|
11 | c = *pDataArray++; //Copy next DataWord to c
|
12 |
|
13 | do {
|
14 | carry = CRC & 0x8000; //Check if Bit15 of CRC is set
|
15 | CRC <<= 1; //CRC = CRC * 2
|
16 | if(c & shifter) CRC++; //CRC = CRC + 1, if BitX is set in c
|
17 | if(carry) CRC ^= 0x1021; //CRC = CRC XOR G(x), if carry is true
|
18 | shifter >>= 1; //Set BitX to next lower Bit, shifter = shifter/2
|
19 | } while(shifter);
|
20 | }
|
21 | return CRC
|
22 | }
|
habe ich soweit für GCC "übersetzt" :)
1 | uint16_t CalcFieldCRC(volatile uint16_t *pDataArray, uint16_t numberOfWords) {
|
2 |
|
3 | uint16_t shifter;
|
4 | uint16_t c;
|
5 | uint16_t carry;
|
6 | uint16_t _CRC = 0;
|
7 |
|
8 | //Calculate pDataArray Word by Word
|
9 | while(numberOfWords--) {
|
10 |
|
11 | shifter = 0x8000; //Initialize BitX to Bit15
|
12 | c = *pDataArray++; //Copy next DataWord to c
|
13 |
|
14 | do {
|
15 | carry = _CRC & 0x8000; //Check if Bit15 of _CRC is set
|
16 | _CRC <<= 1; //_CRC = _CRC * 2
|
17 | if(c & shifter) _CRC++; //_CRC = _CRC + 1, if BitX is set in c
|
18 | if(carry) _CRC ^= 0x1021; //_CRC = _CRC XOR G(x), if carry is true
|
19 | shifter >>= 1; //Set BitX to next lower Bit, shifter = shifter/2
|
20 | } while(shifter);
|
21 | }
|
22 | return _CRC;
|
23 | }
|
als genanntes Beispiel müßte der
Datensatz 0x10 0x01 0x03 0x20 0x01 0x02 0x00 0x00
0xA888 ergeben, ich komme so aber auf 0x497E
Weiß jemand vielleicht von Euch, ob man die Bitshift
Anweisungen in GGC so auch übernehmen kann?
(Ich kenne mich mit Bitshift leider nicht aus, habe ich
alles soweit übernommen)
Über Hilfe würde ich mich sehr freuen.
Vielen Dank & Viele Grüße
Jens