#### Lab 1770- Gray Code

##### Description

Most students are familiar with the binary number system. Here is a table of values comparing decimal numbers 0 through 8 with their binary equivalent:

```000   0
001   1
010   2
011   3
100   4
101   5
110   6
111   7
```

Now presume these same three bits, representing eight values, are used to encode a level from 0 to 7 using a mechanical sensor. To go from a level of 3 (code 011) to a level of 4 (code 100) requires all three bits change at the same time. This physically does not happen in the real world where mechanical sensors are involved. For example, if the bit on the left changes just before the other two, the output of the circuit would appear to go from 011 to 111 and then to 100, corresponding to a level change of 3 to 7 and then to 4. Clearly this is not what was meant to be encoded.

One way to get around this problem is to use a Gray code for the sequence. In this code, only one bit position changes as the level changes by one step. Here is a three-bit Gray code equivalent for the eight levels shown above:

```000   0
001   1
011   2
010   3
110   4
111   5
101   6
100   7
```

To see how this works, imagine a Gray code is being used to convert the angular position of a control or sensor into digital form. A line of sensors along the radius of the disk reads the code off the surface of the disk. If the disk is half-way between two positions each sensor might read its bit from both positions at once but since only one bit differs between the two, the value read is guaranteed to be one of the two valid values rather than some third invalid combination. So a Gray code wheel going from level 3 to level 4 in the above example would be either 010 (representing level 3) or 110 (representing level 4).

Here is a peek at a Gray scale encoded wheel with 28 levels. You should be able to spot the codes for 3 and 4 on the wheel to convince yourself that no glitches, like temporarily appearing to have some other output, are possible. Note that the bit position of any bit in the code is not an indication of the weight of the bit, as in the binary number system. The code must be decoded by looking up the level corresponging to the bits in the Gray code.

##### Assignment

From the Venus probe of October 18, 2010, raw 10 bit sensor data was received from the surface of the planet representing the ambient light over a 24 hour period. Higher numbers represented brighter conditions, based on the gray code contained in gray10.txt.

Process the raw data provided in the file sensor.dat to determine the brightest and the darkest level recorded by the Venus lander on that day. The data is stored in a sequence of 10 bit gray-codes with each step varying by one bit position from the previous measurement. Your answer should be in terms of decoded values, somewhere in the range of 0 to 1023.

For independent testing or performance analysis, here are data files for 5 bit (32 step), 10 bit (1024 step) and 12 bit (4096 step) Gray codes.