Gray code, a code invented by French engineer Jean-maurice-emlle Baudot in 1880, is an absolute encoding, and the typical gray code is a one-step complement with reflective and cyclic characteristics, its cyclic, The single-step feature eliminates the possibility of significant errors in random fetching, and its reflection and self-reinforcing properties make the negation very convenient. Gray code is a reliability code, is a fault minimization of the encoding method, because, although the natural binary code can be directly from the digital/analog converter to the analog signal, but in some cases, such as the conversion from the decimal 3 to 4 o'clock binary code every bit to change, can make the digital circuit produces a large peak current pulse. Gray code, however, does not have this disadvantage, and when it converts between adjacent bits, only one is changed. It greatly reduces the confusion of logic from one state to the next. Since there is only one difference between the two code groups adjacent to this encoding, the gray code changes only one bit when the number is changed, so that it is more reliable than other encodings that change two or more bits at the same time, thus reducing the likelihood of errors.
Gray code is a set of series, adjacent to the two numbers only one bit change, for the power of the digital, and gray code sequence is not unique.
Gray code construction method is: directly arranged in binary 0 value of the gray code as the 0th item, the first change the rightmost bit, the second change the right from the first 1 bits of the left bit, the third to fourth method with the first to second, so the reverse, you can arrange the gray code of N-bit.
The following is mainly about the conversion between Gray code and natural binary code.
1) The method of converting natural binary code into gray code
Natural binary code conversion to binary Gray code, the law is to retain the highest bit of natural binary code as the highest bit of gray code, and the second high-level gray code for the binary code of the high and sub-high-level difference or, and Gray code the rest of you and the sub-high method of seeking a similar. principle: If binary code is expressed as: b[n-1]b[n-2] ... B[2]B[1]B[0]; Accordingly, the binary Gray code is expressed as: g[n-1]g[n-2] ... G[2]g[1]g[0]. One of the highest highs reserved: g[n-1] = b[n-1]; Other Members: G[i] = b[i+1] xor B[i]. (i = 0, 1, 2, ..., n-2) is shown as follows:
BIN[3] bin[2] bin[1] bin[0]---binary value: binary
+ 0 Bin[3] bin[2] bin[1]---Right shift value (binary)
GRAY[3] gray[2] gray[1] gray[0]---corresponding gray code value
Summary: Gray code values only need to be on the original binary based on the right to move one plus the original binary value can be obtained.
The implementation code is as follows: module bin2gry (Gry,bin);
Parameter length = 8; With a eight-bit example
Output [length-1:0] Gry;
input [length-1:0] Bin; reg [length-1:0] Gry;
Integer i; Always @ (Bin)
Begin
for (i=0;i<length-1;i=i+1)
GRY[I]=BIN[I]^BIN[I+1];
Gry[i]=bin[i];
End/* Another simple implementation method is as follows: */
Assign Gray = (bin >> 1) ^ bin; Endmodule
2) The implementation method of converting gray code to binary code
Binary Gray code converted into natural binary code, the law is to keep the highest bit of gray code as the highest bit of natural binary code, and the second high natural binary code for high-level natural binary code and the second high gray code different or, while the rest of the natural binary code and the second high natural binary code of the same method. principle: If the binary Gray code is expressed as: g[n-1]g[n-2] ... G[2]G[1]G[0]; Accordingly, the binary code is represented as: b[n-1]b[n-2] ... B[2]b[1]b[0]. One of the highest highs reserved: b[n-1] = g[n-1]; Other Members: b[i-1] = g[i-1] xor B[i]. (i = 1, 2, ..., n-1)
This is illustrated below:
Module Gry2bin (gry,bin); Parameter length = 8; input [length-1:0] Gry; Output [length-1:0] Bin; reg [length-1:0] Bin; 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 Endmodule Comprehensive results are as follows: