Share |
Login Form
Newsletter(s)



Receive HTML?

Latest Members




Gray Codes (Part 2) Hot

 
User rating
 
0.0 (0)

The term Gray code is typically used to refer to a binary sequence in which only a single bit changes value when transitioning between adjacent states. In this, the second installment of our mini-series, we take a look at generating Gray codes and also Binary-to-Gray and Gray-to-Binary conversions.

Just to refresh our memories, in Part 1 we considered the concept of Gray codes in general; in Part 3 we will ponder the generation of sub-2n Gray code count sequences; in Part 4 we will consider the generation of sub-2n sequences with consecutive values; and in Part 5 we will take a leap into the unknown to wrestle with the concept of "n-ary" (non-Boolean) Gray codes.

Generating a Gray Code
Before we start, let's briefly remind ourselves as to the difference between a standard binary count and one of the many possible Gray code  equivalents as illustrated in Figure 2-1 (we'll use 4-bit sequences for the purpose of these examples).

 Binary versus Gray codes

Figure 2-1. Binary versus Gray codes (4-bit count sequences).

Commencing with a state of all zeros, a Gray code can be generated by always changing the least significant bit that results in a new state. An alternative method which may be easier to remember and use is as follows:

1) Commence with the simplest Gray code possible; that is, for a single bit.

2) Create a mirror image of the existing Gray code below the original values.

3) Prefix the original values with 0s and the mirrored values with 1s.

4) Repeat steps 2) and 3) until the desired width is achieved.

An example of this "mirroring process" used to generate a 4-bit Gray code is shown in Figure 2-2 (it might be more correct to call this a "recursive reverse-and-prefix" technique).

Using a mirroring process to generate a 4-bit Gray code.

Figure 2-2. Using a mirroring process to generate a 4-bit Gray code.


Binary-to-Gray and Gray-to-Binary Conversions
It is often required to convert a binary sequence into a Gray code or vice versa. Such converters are easy to create and are of especial interest here due to their affinity to Reed-Müller implementations (see also my paper on Reed-Müller Logic). First let's consider a Binary-to-Gray converter as illustrated in Figure 2-3.

Binary-to-Gray converter

Figure 2-3. Binary-to-Gray converter.

The checkerboard patterns of 0s and 1s in the Karnaugh Maps immediately indicate the potential for Reed-Müller implementations. Similar checkerboard patterns are also seen in the case of a Gray-to-Binary converter as illustrated in Figure 2-4.

Gray-to-Binary converter

Figure 2-4. Gray-to-Binary converter.

 

It's Very Noisy In here!
Gray code counters are of interest for a variety of applications, such as representing the state variables in state machines or acting as pointers in First-In First-Out (FIFO) memories. This is because only one output bit is ever toggling at a time in a Gray code "counter", as opposed to possibly multiple bits in a binary counter.

In addition to preventing intermediate states, Gray code counters consume only half the power of an equivalent binary counter and they generate correspondingly less noise. Actually, while the power and average noise difference between a Gray and a binary counter asymptotically approaches two, the peak noise difference is equal to the number of bits, since a Gray counter toggles only one bit at a time while a binary counter toggles all of its bits simultaneously two times over the course of a full-count cycle with fewer bits toggling proportionally more times.


Actually Implementing a Gray Code Counter

Before we proceed, let's briefly ponder the process of actually implementing a Gray code counter. Just to give us something to play with, let's suppose we wish to generate a Gray code that can be used to index into a memory array. For example, suppose we're implementing something sort-of like a FIFO, and that (for the purposes of this example), we simply wish to keep on cycling through all of the memory locations. The point is that we aren't particularly concerned as to the order in which we address the locations, just that we sequence our way through them visiting each one a single time before returning to the first location to do it all over again.

Assuming that we're dealing with a 16-word memory array, one scenario [as illustrated in Figure 2-5 (a)] starts with a standard 4-bit binary counter, in which the current count value is passed through a chunk of feedback logic to generate the next count value. Also, the current count value is passed through a Binary-to-Gray converter (as introduced in Figure 2-3) to generate a corresponding Gray code.

Two techniques for generating a Gray code sequence.

Figure 2-5. Two techniques for generating a Gray code sequence.

The alternative technique [as illustrated in Figure 2-5(b)] is to simply create a Gray code counter from the ground up. Now, if we were to compare the binary counter (including binary-to-Gray converter) with the Gray code counter, there are several things I don't know off the top of my head:

• How many logic gates are required for each implementation?

• How many levels of logic gates are there in the two feedback paths?

• What's the (relative) maximum frequency of each type of counter?

• What's the (relative) switching activity, noise, and power consumption of each type of counter?

I tell you, I'm constantly amazed by the number of things I don't know. Now, we could work this out, but I'm a tad busy at the moment, so we'll leave the pondering of these posers as an exercise for the reader (grin). Of course, if you do work all of this out, it would make the basis for a great article that you could post here on www.TechBites.com.

Now, on to Part 3 where we will take a look at generating sub-2n Gray code count sequences.



Author's Note:
This mini-series of articles was abstracted from my book Bebop to the Boolean Boogie (An Unconventional Guide to Electronics) 3rd Edition, with the kind permission of my publisher, Newnes (an imprint of Elsevier).

Author's Note: My friend Jay Dowling is an absolute expert with regard to all aspects of Gray codes. In fact, Jay leads the Gray Code Community here at TechBites.com. So if you're interested in learning more, bounce over to the Gray Code Community where you'll discover all sorts of "stuff" (including some cunning Gray-code related generator programs that Jay has created).

User reviews

There are no user reviews for this listing.

To write a review please register or login.
 
 
 
Written by :
Clive Maxfield