源自於http://verticalhorizons.in/binary-to-gray-code-converter-gray-to-binary-code-converter/
Binary To Gray Code Converter – Gray to Binary Code Converter
The conversion between gray to binary and binary to gray is very common in digital systems. In one of our previous post we explained what are gray codes and how to convert from binary to gray and gray to binary. This post focus on implementing code converter using logic gates.
Binary to Gray Code Converter
Gray code equivalent of the given binary number is computed as follows:
g3 = b3
g2 = b3 ⊕ b2
g1 = b2 ⊕ b1
g0 = b1 ⊕ b0
g2 = b3 ⊕ b2
g1 = b2 ⊕ b1
g0 = b1 ⊕ b0
A binary to gray code converter can be implemented using XORgates. For n input, n-1 gates are required. As shown in the image below for 4 inputs, 3 XOR gates are used:
Gray to Binary Code Converter
Binary code equivalent of the given gray number is computed as follows:
b3 = g3
b2 = b3 ⊕ g2
b1 = b2 ⊕ g1
b0 = b1 ⊕ g0
b2 = b3 ⊕ g2
b1 = b2 ⊕ g1
b0 = b1 ⊕ g0
A gray to binary code converter can be implemented using XORgates. For n input, n-1 gates are required. As shown in the image below for 4 inputs, 3 XOR gates are used:
//--------------------------------------
//Convert 8-bit Gray code to binary code
//using for loop
//filename : gra2bin.v
//--------------------------------------
module gray2bin(SW, LEDR, LEDG , CLOCK_27 ,KEY ,HEX0 ,HEX1 ,HEX2,HEX3 );
input [17:0] SW; // toggle switches
input [7:0] KEY; // Push bottom
input CLOCK_27; //Clock 27MHz , 50Mhz
output [17:0] LEDR; // red LEDS
output [7:0] LEDG; // green LEDs
output [6:0] HEX0,HEX1,HEX2,HEX3; //7-segment display
parameter length = 8; //8-bit length
//set original program input , output
//bin2gra(Gry, Bin);
//output [length-1:0] Gry; //Gray code putput
//input [length-1:0] Bin; //Binary input
reg [length-1:0] Bin;
wire [length-1:0] Gry;
//mapping to hardware
assign LEDR = SW;
assign Gry=SW[7:0];
integer i;
always @ (Gry)
begin
Bin[length-1]=Gry[length-1];
for (i=length-2; i>=0; i = i-1)
Bin[i]=Bin[i+1] ^ Gry[i];
end
assign LEDG[7:0]=Bin;
endmodule
沒有留言:
張貼留言