So I'm trying to write a function that will convert a 7-bit Gray code to the corresponding 7-bit Binary Code.
Here's how to convert -
- Gray Value bits ---- MS bit > (G6) G5 G4 G3 G2 G1 G0 *
Binary Value bits -- MS bit > (B6) B5 B4 B3 B2 B1 B0 *
B6 = G6 // MS bits always the same
- B5 = B6 ^ G5 // Exclusive 'OR' the bits together to construct the 7 bit binary value
- B4 = B5 ^ G4
- B3 = B4 ^ G3
- B2 = B3 ^ G2
- B1 = B2 ^ G1
- B0 = B1 ^ G0
and here's my function so far-
unsigned short Gray_to_Bin(unsigned short Gray)
{
unsigned short Bin;
unsigned short i;
unsigned short mask;
mask = 0x40; // Initial mask
Bin = 0;
Gray &= 0x7f; // Mask bit 7 (Index Bit)
Bin = Gray & mask; // Set B6 = G6
for (i=0; i<6; i++) // Set B5, B4, ..., B0
{
// Code needed here!!
}
return Bin;
}
I need to find a way to access the required specific bits for each run of the loop...need to access the bits like I could with arrays somehow...
Any ideas/pointers? Thanks :)