## Digital Logic: Designing Decimal to 4 bit Gray Code Converter

##### Gray Code:

Difference of gray code from binary is that, for each group only one bit changes when going from one number to the next. In case of binary it can be seen from the table that multiple bit may change when going from one number to next.

##### How to Create Gray Code Sequence:

There are multiple shortcut technique to write gray code. This is the technique I follow,

0
1


Next step, Add 0’s before both of them,

0 | 0
0 | 1


Next Mirror ( Write the values bottom to top instead of top to bottom ) all bits except for Left Most bit and Add 1’s in the place left most bit,

0 | 0
0 | 1
------> Mirror
1 | 1
1 | 0


Continuing this process again of adding 0’s before all numbers and Mirroring all bits except left most bit, then adding 1’s in place of left most bit,

0 | 0 0
0 | 0 1
0 | 1 1
0 | 1 0
-------> Mirror
1 | 1 0
1 | 1 1
1 | 0 1
1 | 0 0


Now following same procedure for 4 bits,

0 | 0 0 0
0 | 0 0 1
0 | 0 1 1
0 | 0 1 0
0 | 1 1 0
0 | 1 1 1
0 | 1 0 1
0 | 1 0 0
--------> Mirror
1 | 1 0 0
1 | 1 0 1
1 | 1 1 1
1 | 1 1 0
1 | 0 1 0
1 | 0 1 1
1 | 0 0 1
1 | 0 0 0


finally 4 bit gray code representing Decimal 0 to 15,

0 0 0 0
0 0 0 1
0 0 1 1
0 0 1 0
0 1 1 0
0 1 1 1
0 1 0 1
0 1 0 0
1 1 0 0
1 1 0 1
1 1 1 1
1 1 1 0
1 0 1 0
1 0 1 1
1 0 0 1
1 0 0 0


Now just start from the top with 0 and increment it by 1. That will be the decimal equivalent of the gray code.

##### Binary, Decimal and Gray Code Table:

Here I am representing gray code bits with A, Decimal values with D and Binary bits with B.

 Binary Decimal Gray Code B3 B2 B1 B0 D A3 A2 A1 A0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 1 0 0 1 0 2 0 0 1 1 0 0 1 1 3 0 0 1 0 0 1 0 0 4 0 1 1 0 0 1 0 1 5 0 1 1 1 0 1 1 0 6 0 1 0 1 0 1 1 1 7 0 1 0 0 1 0 0 0 8 1 1 0 0 1 0 0 1 9 1 1 0 1 1 0 1 0 10 1 1 1 1 1 0 1 1 11 1 1 1 0 1 1 0 0 12 1 0 1 0 1 1 0 1 13 1 0 1 1 1 1 1 0 14 1 0 0 1 1 1 1 1 15 1 0 0 0

##### Making Decimal to Gray Converter:

Since this is Decimal to Gray Code converter, the Binary part of the table could be omitted.

$A_3 = D_8 + D_9 + D_{10} + D_{11} + D_{12} + D_{13} + D_{14} + D_{15} \\ A_2 = D_4 + D_5 + D_6 + D_7 + D_8 + D_9 + D_{10} + D_{11} \\ A_1 = D_2 + D_3 + D_{4} + D_{5} + D_{10} + D_{11} + D_{12} + D_{13} \\ A_0 = D_1 + D_2 + D_5 + D_6 + D_9 + D_{10} + D_{13} + D_{14} \\$

Let,

$X_1 = D_4 + D_5 \\ X_2 = D_8 + D_9 \\ X_3 = D_{10} + D_{11} \\ X_4 = D_{12} + D_{13} \\ X_5 = X_2 + X_3 \\$

From the above expression,

$A_3 = X_2 + X_3 + X_4 + D_{14} + D_{15} \\ .... = X_5 + X_4 + D_{14} + D_{15} \\ \\ A_2 = X_1 + D_6 + D_7 + X_2 + X_3 \\ .... = X_1 + D_6 + D_7 + X_5 \\ \\ A_1 = D_2 + D_3 + X_1 + X_3 + X_4 \\ \\ A_0 = D_1 + D_2 + D_5 + D_6 + D_9 + D_{10} + D_{13} + D_{14} \\ \\$

##### Inputs and Outputs:

For the circuit there will be inputs each representing a decimal number. The number of outputs will be 4 from $A_3 \text{ to } A_0$ where, $A_0 \text{ is the LSB and } A_3 \text{ is the MSB}$.

##### How to test if its working:

Just press each buttons in the left one at a time then check to see if the values given match the value in table. The values are read bottom to top, equivalent to reading values from table left to right.

##### Circuit Diagram:

Download the file named 4 bit decimal to gray code converter circuit diagram from my Github.