Hallo,
wie in einem vorigen Thread von mir angekündigt, muss ich meine
Codegröße verringern, damit das Zeug in den Flash des MSP430G2231 paßt.
Folgende Funktion bereitet mir Kopfzerbrechen (sie extrahiert aus einer
empfangenen Nachricht vom Typ "unsigned long" Befehl-, Daten- und
CRC-Byte:
1 | // Befehl, Daten und CRC aus empfangener Nachricht extrahieren
|
2 | void byte_extract()
|
3 | {
|
4 | BefehlByte = 0x00;
|
5 | DatenByte = 0x00;
|
6 | CRCByte = 0x00;
|
7 |
|
8 | if (RXByte & 0x1) // Das Befehlbyte extrahieren
|
9 | BefehlByte |= 1 << 0;
|
10 | if (RXByte & 0x2)
|
11 | BefehlByte |= 1 << 1;
|
12 | if (RXByte & 0x4)
|
13 | BefehlByte |= 1 << 2;
|
14 | if (RXByte & 0x8)
|
15 | BefehlByte |= 1 << 3;
|
16 | if (RXByte & 0x10)
|
17 | BefehlByte |= 1 << 4;
|
18 | if (RXByte & 0x20)
|
19 | BefehlByte |= 1 << 5;
|
20 | if (RXByte & 0x40)
|
21 | BefehlByte |= 1 << 6;
|
22 | if (RXByte & 0x80)
|
23 | BefehlByte |= 1 << 7;
|
24 |
|
25 | if (RXByte & 0x100) // Das Datenbyte extrahieren
|
26 | DatenByte |= 1 << 0;
|
27 | if (RXByte & 0x200)
|
28 | DatenByte |= 1 << 1;
|
29 | if (RXByte & 0x400)
|
30 | DatenByte |= 1 << 2;
|
31 | if (RXByte & 0x800)
|
32 | DatenByte |= 1 << 3;
|
33 | if (RXByte & 0x1000)
|
34 | DatenByte |= 1 << 4;
|
35 | if (RXByte & 0x2000)
|
36 | DatenByte |= 1 << 5;
|
37 | if (RXByte & 0x4000)
|
38 | DatenByte |= 1 << 6;
|
39 | if (RXByte & 0x8000)
|
40 | DatenByte |= 1 << 7;
|
41 |
|
42 | if (RXByte & 0x10000) // Das CRCbyte extrahieren
|
43 | CRCByte |= 1 << 0;
|
44 | if (RXByte & 0x20000)
|
45 | CRCByte |= 1 << 1;
|
46 | if (RXByte & 0x40000)
|
47 | CRCByte |= 1 << 2;
|
48 | if (RXByte & 0x80000)
|
49 | CRCByte |= 1 << 3;
|
50 | if (RXByte & 0x100000)
|
51 | CRCByte |= 1 << 4;
|
52 | if (RXByte & 0x200000)
|
53 | CRCByte |= 1 << 5;
|
54 | if (RXByte & 0x400000)
|
55 | CRCByte |= 1 << 6;
|
56 | if (RXByte & 0x800000)
|
57 | CRCByte |= 1 << 7;
|
58 |
|
59 | }
|
Der Code funktioniert, ist aber mit Sicherheit viel zu umständlich.
Ich könnte mir vorstellen, dass man da prima eine Schleife programmieren
könnte. Mit dem Bitshifting 0 - 7 ist das ja kein Problem, da einfach
eine Zählvariable zu nehmen, aber wie mache ich das mit den
Hex-Bitmasken?