Gray code is a kind of reliability coding. In the encoding of a set of numbers, if any two adjacent codes are different in only one binary digit, then this encoding is called Gray code. In addition, since the maximum number and the minimum number are only different in one digit, that is, " Connected end to end", so it is also called a cyclic code. In digital systems, codes are often required to change in a certain order.
For example, according to the natural number increment technology, if the 8421 code is used, all four bits will change when the number 0111 changes to 1000. However, in the actual circuit, the changes in the four bits cannot occur absolutely at the same time, so the count Other codes may appear briefly (1100, 1111, etc.). Under certain circumstances this may result in incorrect circuit status or input errors. This error can be avoided by using Gray code.
Extended information:
History of development of Gray Code:
Gray Code was developed due to the patent "Pulse" applied by Frank Gray in 1947 and approved in 1953. It is named after "Code Communication". It was originally used for communication, but now it is often used in analog-to-digital conversion. The Baud code used by French engineer Jean-Maurice-Eacute;mlle Baudot in 1880 is a variation of the typical Gray code. In 1941, George Stibitz designed an 8-element Gray code counter.
Advantages of Gray code:
1. Gray code is an absolute coding method. A typical Gray code is a single-step self-complement code with reflective characteristics and cyclic characteristics. Its loop and single-step characteristics eliminate the possibility of significant errors when randomly picking numbers, and its reflection and self-complementary characteristics make inversion very convenient.
Gray code belongs to reliability coding and is a coding method that minimizes errors, because although natural binary codes can be directly converted into analog signals by digital/analog converters, in some cases, such as When converting from decimal 3 to 4, each bit of the binary code must change, which can cause the digital circuit to generate a large peak current pulse. Gray code does not have this shortcoming. When it converts between adjacent bits, only one bit changes. It greatly reduces logical confusion when going from one state to the next.
Since there is only one bit difference between two adjacent code groups of this encoding, in the conversion of the angular displacement of the wind direction to the digital quantity, when the angular displacement of the wind direction changes slightly (When the digital quantity may change, Gray code only changes one bit, which is more reliable than other codes that change two or more bits at the same time, thus reducing the possibility of errors.
2. Gray code is a variable-weight code. Each bit of code has no fixed size. It is difficult to directly compare the size and arithmetic operations. It cannot be directly converted into a liquid level signal. It needs to undergo a code transformation to become a natural binary. code, and then read by the host computer.
The decoding method is to use '0' and the highest bit (the 4th bit) of the collected 4-bit Gray code, and the result is retained to 4 bits, and then XOR the XOR value with the next bit (the 3rd bit), and keep the result to the 3rd bit, then XOR the exclusive OR value with the next bit (the 2nd bit), keep the result to the 2nd bit, and so on XOR, until the lowest bit, the value after XOR conversion (binary number) is the value of the natural code after Gray code conversion.
Reference: Baidu Encyclopedia-Gray code