PLCtalk.net
Volume 
Automating Manufacturing Systems with PLCs
Chapter
Numbering Systems and Data
PDF Version
Pages

Binary Coded Decimal (BCD) and Gray Code


Individual numerical values and encoder positions both follow altered forms of the binary system where each digit is treated as a unique value, rather than converting the complete number into its binary equivalent.


Two codes have been used for many years to easily convert multi-digit decimal values into binary while keeping each digit separate and distinct.

One standout application of this somewhat odd conversion method is a regular old alarm clock. Most clocks use 7-segment displays with separate hour and minute sections, like this: 00:00.

 

Figure 9.1. 7-segment display in a clock face. Image used courtesy of Adobe Stock

 

In this application, the driver chip for each digit of the clock display needs a command. In the illustration above, we must send a 1, then a 0, then a 2, and then a 3, each to a different chip on the display board.

This is accomplished using a strategy called binary-coded decimal.

 

BCD (Binary-Coded Decimal)

In the BCD system, each decimal digit is represented by a 4-bit code, or half a byte. We use 4 bits because 0-9 is too large to show with only 3 bits, and there is no need to use more than 4 bits.

In the clock example above, the time is 10:23. Since there are 4 digits, we have 4 groups of 4 bits, for a total of 16 bits. Any simple binary conversion chart can easily show the binary combination for each digit:

  • 1 = 0001
  • 0 = 0000
  • 2 = 0010
  • 3 = 0011

Thus, the final BCD equivalent of 1023 is: 0001 0000 0010 0011.

 

Gray Code

BCD is an effective way to represent unique decimal states in their binary equivalent. There is another, more industrial application of this conversion: it’s used in an absolute encoder for motor control feedback.

This kind of encoder measures rotational position by reading a pattern of light/dark marks on a disk, which changes as the disk rotates. A tiny sensor array reads this pattern many times per second for high-speed motion.

The Problem With BCD in an Encoder

Inside these rotary encoders, a change in any part of the pattern triggers a transistor-switching action that takes only a small amount of time, but that time adds up. When a motor is rotating quickly, there isn’t much time to spare. To avoid missing subsequent transitions, it would be best to force only one transistor to change between any two readings.

To illustrate this problem, imagine using a true binary (BCD) encoder transitioning from position 7 to position 8. On the physical marks around the edge of the encoder disk, the binary code would change from 0111 to 1000 in an instant. However, within the electronics of the sensing circuit, four transistors must switch, resulting in a delay that is 4x longer than if only one transistor were required to switch. Because of this delay, it will take a bit of time before the next sensor reading, and an extra step change may have occurred in the meantime.

So, the solution is to create a binary pattern in which only one bit changes at each step, and therefore only one transistor must change state. This is called Gray code.

Gray Code: No More than One Transition

In Gray Code, much like in BCD, each decimal value is assigned a unique binary pattern, but it deviates from normal binary almost immediately. In the Gray code table below, we see that only one bit changes from 0 to 1, or vice versa, between any two consecutive rows. This is by design, dating back to the code's foundation in the 1940s.

 

Table 9.16. Gray code chart.

 

Gray code is not random or arbitrarily designated, although it may confuse binary math experts at first glance.

  • In standard binary, the 1s place value switches in every consecutive row. Then, the 2s place value switches every two rows, then the 4s place switches every four rows, and so on. A very recognizable pattern.
  • In the Gray code list, the switching frequency is shifted by exactly one place value; the 1s place switches every two rows, the 2s place switches every four rows, and so on.

It’s critical to note that this is not a mathematical conversion, but rather an algorithmic pattern, so do not try to convert Gray code to a decimal equivalent using any previously discussed formulae. It’s simply a progression of binary digits carefully arranged to limit the number of switches per step.

When ordering absolute encoders, you are likely to see this counting pattern indicated in the product information or the datasheet. When reading the pattern for code execution, most PLCs include a function block for converting Gray code to standard binary, or subroutines that perform the conversion.

 

Conversions for Text and Characters

Next, we’ll examine another standardized binary code called ASCII, the American Standard Code for Information Interchange, which forms the basis for all string data types and is therefore a very important code to understand.