# PLC Program to Convert Gray Code to Binary

This is a PLC Program to Implement Gray Code to Binary Conversion.

Problem Description

Implementing Gray Code to Binary conversion in PLC using Ladder Diagram.

Problem Solution
• Writing truth table showing the relation between Gray Code as input and Binary as output.
• The MSB of Gray and Binary are same, so pass the same bit directly.
• Add Binary MSB to the next bit of Gray code. Record the result and ignore the carries.
• Similarly continue until the LSB is reached.
• To obtain these equations, Karnaugh-Map method is again used.
• For each Binary output D3, D2, D1 and D0, write Karnaugh-Map.
• From the K-Map, obtaining a simplified expression for each Binary output in terms of Gray Code inputs.
• Realize the code converter using the Logic Gates.
• By following the process to convert Gray Code into Binary, Truth Table can be written as given below.

Truth Table relating Binary to BCD

```Decimal	Gray Code Input	                Binary Output
D3	D2	D2	D0	B3	B2	B1	B0
0	0	0	0	0	0	0	0	0
1	0	0	0	1	0	0	0	1
3	0	0	1	1	0	0	1	0
2	0	0	1	0	0	0	1	1
6	0	1	1	0	0	1	0	0
7	0	1	1	1	0	1	0	1
5	0	1	0	1	0	1	1	0
4	0	1	0	0	0	1	1	1
12	1	1	0	0	1	0	0	0
13	1	1	0	1	1	0	0	1
15	1	1	1	1	1	0	1	0
14	1	1	1	0	1	0	1	1
10	1	0	1	0	1	1	0	0
11	1	0	1	1	1	1	0	1
9	1	0	0	1	1	1	1	0
8	1	0	0	0	1	1	1	1```

Boolean expression for each BCD bits can be written as

``` B3= m(8, 9, 10, 11, 12, 13, 14, 15)
B2= m(4, 5, 6, 7, 8, 9, 10, 11)
B1= m(2, 3, 4, 5, 8, 9, 14, 15)
B0= m(1, 2, 4, 7, 8, 11, 13, 14)```

Karnaugh-Map for each output

Realizing code conversion using Logic Gates

PLC Program

Here is PLC program to Implement Gray Code to Binary Conversion, along with program explanation and run time test cases.

```List of Inputs and Outputs
D3=		I:1/0	(Input)
D2=		I:1/1	(Input)
D1=		I:1/2	(Input)
D0=		I:1/3	(Input)
B3=		O:2/0	(Output)
B2=		O:2/1	(Output)
B1=		O:2/2	(Output)
B0=		O:2/3	(Output)```

Ladder Diagram to obtain Binary output

Program Description
• Simply by looking at the logic circuit, ladder diagram is made.
• RUNG000 for output bit B3 (O:2/0) is as described earlier that the MSB of Gray and Binary are same, so passed the same D3 (I:1/0) bit directly.
• By simplifying Boolean expressions, we can see that the output of RUNG001 is fed to input of RUNG002 and EX-ORed with D1 (I:1/2) bit.
• Similarly in RUNG003 comprises of RUNG002’s output as input and EX-ORed with bit D0 (I:1/3).
Runtime Test Cases
```Decimal	Gray Code Input	                Binary Output
D3	D2	D2	D0	B3	B2	B1	B0
0	0	0	0	0	0	0	0	0
1	0	0	0	1	0	0	0	1
3	0	0	1	1	0	0	1	0
2	0	0	1	0	0	0	1	1
6	0	1	1	0	0	1	0	0
7	0	1	1	1	0	1	0	1
5	0	1	0	1	0	1	1	0
4	0	1	0	0	0	1	1	1
12	1	1	0	0	1	0	0	0
13	1	1	0	1	1	0	0	1
15	1	1	1	1	1	0	1	0
14	1	1	1	0	1	0	1	1
10	1	0	1	0	1	1	0	0
11	1	0	1	1	1	1	0	1
9	1	0	0	1	1	1	1	0
8	1	0	0	0	1	1	1	1```

Sanfoundry Global Education & Learning Series – PLC Algorithms.
To practice all PLC programs, here is complete set of 100+ PLC Problems and Solutions.