Standard for Encoder Gray-Code

M

Thread Starter

Mark K

Hi...I'm curious if there is a standard (ANSI or otherwise) for the output of gray-code encoders. My understanding is that true gray-code is any bit wise code that only changes one bit at a time. If you can decode it reliably, that's gray-code. There must however be an accepted standard for the output of gray-code encoders otherwise everone would be different. Does anyone know of a decoding table for that standard. Where can I find it.

 
R

Richard Mahn

<pre>
To form the Gray Code equivalent of a binary value, shift the value right and xor with the original value. For the shift, a zero is shifted
in from the left and the bit shifted out on the right is ignored.

i.e.
+---------+---------+---------+
! Binary ! Shifted ! Gray !
! Value ! Value ! Coded !
! ! ! Value !
! (A) ! (B) !(A Xor B)!
+---------+---------+---------+
! 0000 ! 0000 ! 0000 !
! 0001 ! 0000 ! 0001 !
! 0010 ! 0001 ! 0011 !
! 0011 ! 0001 ! 0010 !
! 0100 ! 0010 ! 0110 !
! 0101 ! 0010 ! 0111 !
! 0110 ! 0011 ! 0101 !
! 0111 ! 0011 ! 0100 !
! 1000 ! 0100 ! 1100 !
! 1001 ! 0100 ! 1101 !
! 1010 ! 0101 ! 1111 !
! 1011 ! 0101 ! 1110 !
! 1100 ! 0110 ! 1010 !
! 1101 ! 0110 ! 1011 !
! 1110 ! 0111 ! 1001 !
! 1111 ! 0111 ! 1000 !
+---------+---------+---------+
</pre>
 
B

Bruce Durdle

I've never come across this way of defining a Gray code before. I like it!

Now, is there a simple logic formula to reverse the process?


Bruce.
 
R

Richard Mahn

I wondered if that question would come up.

Previously I've used a bit-wise algorithm to convert Gray to Binary. But looking at it now, I think it can be done with a simple equation. The
equation changes for the number of bits being converted, though. For Example a 4 bit Gray value the converstion to Binary, in no particular
language, is as follows;

Bin = Gray Xor (Gray Shr 1) Xor (Gray Shr 2) Xor (Gray Shr 3)

or in a iterative approach, that most compilers can optimize easily, it would be;

Bin = Gray
Gray = Gray Shr 1
Bin = Bin Xor Gray
Gray = Gray Shr 1
Bin = Bin Xor Gray
Gray = Gray Shr 1
Bin = Bin Xor Gray

By adding more Shr and Xor pairs, wider Gray coded values can be converted.

Even though there is repeated sequence that could lend itself to looping, its probably not worth it, even for a 32 bit converstion. The loop overhead would cost more than the size increase of doing it inline, IHMO.

Enjoy.
 
L

List Manager

Might as well use a 256-byte lookup table for an 8-bit code. I'm not sure how far back the archives go, but ISTR a grey code discussion
before. (of course, it might have been 3-4 or more years..)
 
B

Bruce Durdle

Richard and others,

I've just done a simulation using a single Right Shift-XOR on an 8-bit value to generate a Gray sequence, and been able to reconstruct the
original count with another 7 Right Shift-XOR combinations.

I guess it would be a good bet that, for an n-bit code, a single Right Shift-XOR gives a Gray code, and a further (n-1) Right Shift-XORs will convert the Gray code back to the original value.

I've always felt there had to be a relatively easy way to deconstruct a Gray code, but been too busy (read "unmotivated") to sit down and thrash it out.

Thanks for that input.

Bruce.
 
J

Johan Bengtsson

I think this might be in the archives somewhere since I know I have answered it before, but anyway....

Done with logic you can do like this:

Binary Gray
MSB MSB
3 -*------------------------ 3
|
|-|---|
|=1 |------------------ 2
2 -*-|---|
|
|-|---|
|=1 |------------------ 1
1 -*-|---|
|
|-|---|
|=1 |------------------ 0
0 ---|---|

And to convert back:
Gray Binary
MSB MSB
3 -*------------------------ 3
|
|-|---|
|=1 |-*---------------- 2
2 ---|---| |
|-|---|
|=1 |-*-------- 1
1 ------------|---| |
|-|---|
|=1 |-- 0
0 --------------------|---|


Expand as far as you want.
Quite easy implemented in C if you want.

(this code is untested but it should work):
unsigned short gray,binary,mask;
//assign gray to a value from somewhere
for (mask=0x8000,binary=0;mask;mask>>=1)
binary|=(gray ^(binary>>1)) & mask;


/Johan Bengtsson

----------------------------------------
P&L, Innovation in training
Box 252, S-281 23 Hässleholm SWEDEN
Tel: +46 451 49 460, Fax: +46 451 89 833
E-mail: [email protected]
Internet: http://www.pol.se/
----------------------------------------

 
I was wondering about this solution as well, I have seen high-speed applications where all the calculations and bit-shifting took quite some
time, a lookup would be far more efficient, the lookup table could be populated automatically with the algorithm described below

Regards
Ronald
 
J

Johan Bengtsson

Doing it in hardware is quite easy, hmm... manufacturers of microprocessrs should really implement an instruction for converting gray->binary just as they have with BCD

Ok, I realize they probably won't but it is an idea...


/Johan Bengtsson

----------------------------------------
P&L, Innovation in training
Box 252, S-281 23 H{ssleholm SWEDEN
Tel: +46 451 49 460, Fax: +46 451 89 833
E-mail: [email protected]
Internet: http://www.pol.se/
----------------------------------------
 
N

Nebojsa Mrmak

> Might as well use a 256-byte lookup table for an 8-bit code. I'm not sure how far back the archives go,
> but ISTR a grey code discussion before. (of course, it might have been 3-4 or more years..)

I was browsing through the archives and even though this is a very, very old post, this gray code lookup table could be interesting to others:
bit.ly/16N5W1E

It's good for 8-bits and under. I'd paste it here to avoid "advertising" that site, but it's 256 lines long...
 
Top