Interchange Between Binary Gray Codes and natural binary codes
Youzhiyu, Institute of optoelectronic technology, Chinese Emy of Sciences Download Sample project In the precise positioning control system, it is very important to accurately measure and control the object location to improve the control accuracy. At present, there are two ways to detect the position: one is to use a location sensor, the displacement measured from the transmitter through A/D to digital volume sent to the system for further processing. This method is highly accurate, but it is not practical in multi-channel and long-distance location monitoring systems because it is expensive and difficult to install. The second is the use of photoelectric axis angle encoder for Precise Position Control. The photoelectric axis angle encoder can be divided into three types based on its scale method and signal output form: Incremental, absolute, and hybrid. The absolute encoder is a sensor that directly outputs numbers. It uses natural binary or cyclic binary (Gray Code) for photoelectric conversion, encoding is generally designed with natural binary code, cyclic binary code, binary complement code, and so on. It is characterized by no counter. A fixed digital code corresponding to the position can be read at any position on the rotating shaft; strong anti-interference ability, no accumulative error; the position information will not be lost after the power supply is cut off, however, the resolution is determined by the binary number of digits. You can select different resolutions, that is, the number of digits, based on different precision requirements. Currently, there are 10, 11, 12, 13, 14, or higher positions. An absolute encoder that uses cyclic binary encoding. Its output signal is a digital sorting, not a weight code. Each bit has no fixed size and cannot be directly compared with arithmetic operations, it cannot be directly converted to other signals. It must undergo a code conversion and be converted to a natural binary code, which is read by the upper computer for corresponding control. There are different processing methods in code conversion. This article focuses on the interchange between binary Gray Codes and natural binary codes. 1. Gray Code (also called cyclic binary code or reflected binary code) In a digital system, only 0 and 1 can be identified. All kinds of data must be converted to binary code for processing. Gray code is an unauthorizable code and adopts the absolute encoding method, A typical Gray code is a single-step self-complement code with reflection and loop characteristics. Its cycle and single-step characteristics eliminate the possibility of significant errors when random numbers are obtained, its reflection and self-population features facilitate reverse lookup. Gray code is a reliable encoding method that minimizes errors, because the natural binary code can be directly converted from a number/mode converter to a analog signal, but in some cases, for example, when converting from decimal 3 to 4, every bit of the binary code must be changed, so that the digital circuit generates a large peak current pulse. Gray Code does not have this disadvantage. It is a digital sorting system, where all adjacent integers have only one number in their numerical representation. When it is converted between any two adjacent numbers, only one digit changes. It greatly reduces the logic confusion from one State to the next state. In addition, because the maximum and minimum numbers are only different, it is usually called Gray reflection code or cyclic code. The following table compares several natural binary codes with gray codes:
Decimal number |
Natural binary |
Gray Code |
Decimal number |
Natural binary |
Gray Code |
0 |
0000 |
0000 |
8 |
1000 |
1100 |
1 |
0001 |
0001 |
9 |
1001 |
1101 |
2 |
0010 |
0011 |
10 |
1010 |
1111 |
3 |
0011 |
0010 |
11 |
1011 |
1110 |
4 |
0100 |
0110 |
12 |
1100 |
1010 |
5 |
0101 |
0111 |
13 |
1101 |
1011 |
6 |
0110 |
0101 |
14 |
1110 |
1001 |
7 |
0111 |
0100 |
15 |
1111 |
1000 |
Ii. Interchange Between Binary Gray Codes and natural binary codes 1. convert a natural binary code to a binary Gray Code The principle of converting a natural binary code into a binary Gray code is to retain the highest bit of a natural binary code as the highest bit of the gray code, and the secondary high gray code is the highest bit of a binary code and the secondary high is different or, the rest of the gray code are similar to the secondary high. 2. Convert binary Gray code to natural binary code To convert a binary Gray code to a natural binary code, the highest bit of the gray code is reserved as the highest bit of the natural binary code, the secondary high natural binary code is a high natural binary code that is different from the secondary high gray code, while the rest of the natural binary code are similar to the secondary high natural binary code. Iii. Implementation of interchange between binary Gray Codes and natural binary codes 1. convert a natural binary code to a binary Gray Code A) software implementation method (see binary to gray in the example project) According to the natural binary conversion to Gray code, you can get the following code:
Static unsigned int decimaltogray (unsigned int X) {return x ^ (x> 1);} // the above Code converts unsigned int data to Gray code, A maximum of 32-bit natural binary code can be converted. If the value exceeds 32-bit, the system will overflow. Static int decimaltogray (int x) {return x ^ (x> 1);} // The code above converts int data to Gray code, A maximum of 31 natural binary codes can be converted. If the value exceeds 31, the system will overflow. The above code can be used in the VC control program or in the single chip microcomputer control program. In the single-chip microcomputer program design, if the Assembly Language Programming, you can design the program according to the same principle; if the C language programming, you can directly use the above Code, but it is recommended to use the unsigned int function. B) Hardware Implementation Method According to the principle of converting a binary Gray code to a natural binary code, we can obtain the following circuit diagram: As shown in the circuit diagram, you can use an integrated circuit 74ls136 or programmable device PLD. 2. Convert binary Gray code to natural binary code A) software implementation method (see Gray to binary in the example project) According to the principle of converting a binary Gray code to a natural binary code, you can obtain the following three code methods:
// The preceding Code converts unsigned int data to a natural binary code. Up to 32-bit gray code can be converted, and 32-bit gray code will overflow. Change the data type to int type to realize 31-bit gray code conversion. The above code can be used in the VC control program or in the single chip microcomputer control program. In the single-chip microcomputer program design, if the Assembly Language Programming, you can design the program according to the same principle; if the C language programming, you can directly use the above Code, but it is recommended to use the unsigned int function. B) Hardware Implementation Method According to the principle of converting a binary Gray code to a natural binary code, we can obtain the following circuit diagram: As shown in the circuit diagram, you can use an integrated circuit 74ls136 or programmable device PLD. |