Gray Code

S

Thread Starter

Shawn Hinchy

Does anybody know a simple method for converting gray code to integer (usable) form?

Shawn Hinchy
Philips Lighting Company
 
G
Hi Shawn

I'd say the quickest (and probably) most adaptable way of doing conversions like this is via a look up of one form or another.

(OK so you could do it in logic prior to the data actually being received but...)

For example a 4 bit gray code is just 16 unique binary patterns so you set up a 16 element static array containing the integer equivalent of the gray code input.

So when you get a gray code number you use it as the index into the array so that it points at it's own integer equivalent.

Its a single pass conversion so it always takes the same amount of time to convert the number regardless of the size of the number.

Its dead easy to implement in C or assembler.

Its fast on any processor from a PIC to a Pentium.

You can use it to convert from any base to any other base.

You can also use it to apply arithmetical/logical operations, apply offsets, multiply divide etc etc etc etc

You can end up with MASSIVE arrays if you're doing something complex with big numbers and you might even need to write a bit of code to generate the array for you if its REAL big.

Hope this is a help

Geoff Moore
Straight Forward Solutions Ltd
Maynooth Road, Prosperous, Naas,
Co.Kildare, Ireland
+353 (0)45 892739
[email protected]
 
M

Martin, Evan

Gray Code was designed because it makes conversion to binary accurate and simple from mechanical devices. I would convert to binary and use that. I can't think of any devices off the top of my head but there must be something since that was the purpose behind Gray Code.
 
C

Curt Wuollet

Hi Shawn,

I'm not sure really what you mean. Gray Code is a usable form, it's simply ordered so that only one bit changes at a time. If you need to transliterate, I would simply use it as the indes of an array that holds the values you want it to represent.

Hope this helps

Curt Wuollet
 
J

Jean-Pierre Segot

Suppose your Gray code be:

G3 G2 G1 G0 and your binary will be B3 B2 B1 B0

you obtain : B3 = G3
B2 = B3 exor G2
and most generally
Bj = Bj+1 exor Gj

"exor" is the exclusive OR ie a OR b but with ( a AND b excluded)

Jean-Pierre Segot
AFPA
 
J

Johan Bengtsson P&L Automatik AB

Start with MSB - it goes unchanged
Proceed with the bits in falling order and do the following:
if the previous (more significant bit) was 1 after conversion
invert the current bit, otherwise keep it unchanged.


GRAY BIN
IN OUT
------
LSB -----------------------------------| |
| =1 |--------- LSB
---| |
------ | ------
---------------------| | |
| =1 |-----------------------
---| |
------ | ------
-------| | |
| =1 |-------------------------------------
---| |
| ------
|
MSB -------------------------------------------------- MSB

This can be expanded to the number of bits desired.


A similar scheme may be used to convert in the other direction
note: the value on the binary side should control the inversion in this case too.



For anyone else interested in what this topic is about:
Consider you want to know the positon or angle of something and you find out you can measure it by using a nomber of LED/photosensor pairs and block the light with something simlar to this:

* * * * * * * * * * * * * * * *
** ** ** ** ** ** ** **
**** **** **** ****
******** ********
****************

This will give you the position in a binary code - easy to use
Unfortunately a lot of signals is supposed to change at the same time and this will not happen as it moves past the sensors and when the
position is at a transition point you can get any value from any of the changing sensors and this will probably give you the absolutely wrong value.

by changing the hole pattern into this instead:

** ** ** ** ** ** ** **
**** **** **** ****
******** ********
****************
***************

you have a couple of signals where only one at a time changes, the above conversion gives you the binary value you want - problem solved


binary gray
000000 000000
000001 000001
000010 000011
000011 000010
000100 000110
000101 000111
000110 000101
000111 000100
001000 001100
001001 001101
001010 001111
001011 001110
001100 001010
001101 001011
001110 001001
001111 001000
010000 011000
010001 011001
010010 011011
010011 011010
010100 011110
010101 011111
010110 011101
010111 011100
011000 010100
011001 010101
011010 010111
011011 010110
011100 010010
011101 010011
011110 010001
011111 010000
100000 110000
100001 110001
100010 110011
100011 110010
100100 110110
100101 110111
100110 110101
100111 110100
101000 111100
101001 111101
101010 111111
101011 111110
101100 111010
101101 111011
101110 111001
101111 111000
110000 101000
110001 101001
110010 101011
110011 101010
110100 101110
110101 101111
110110 101101
110111 101100
111000 100100
111001 100101
111010 100111
111011 100110
111100 100010
111101 100011
111110 100001
111111 100000


As you see this code wraps prefectly around to, only one bit changing...

/Johan Bengtsson

----------------------------------------
P&L, the Academy of Automation
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/
----------------------------------------
 
M

Mark Blunier

Its been a while since I've looked at it, but the
Arts of Electronics by Horowitz and Hill has the function that converts Gray code <=> Integer position. If you can't find a copy, email me back and I'll look it when I'm at home.

Mark Blunier
Any opinions expressed in this message to not necesarily those of the
company.
 
D

D.C. Pittendrigh

Hi All

Binary to gray code is simple, works as follows :

A 4 bit binary number consists of the numbers a, b, c, d and a 4 bit gray code number consists of the numbers e, f, g and h thus to acheive :-

abcd=efgh

then e = a, f = a <EXCLUSIVE OR> b, g = b <EXCLUSIVE OR> c and h = c
<EXCLUSIVE OR> d

The exclusive or function is :

x <EXCLUSIVE OR> y = (<NOT>x<AND>y) <OR> (x<AND><NOT>y)

The result of an exclusive or function is simply that the result is HI if the two values being "exclusive ored" are unequal to each other.

Binary to integer should be easy enough.

Regards
Donald Pittendrigh
 
If unable to find software, you might consider the "old fashioned" Karnaugh mapping method for converting a cyclical-code to BCD or vice versa.

Phil Corso, PE
Trip-A-Larm Corp.
 
Responding to Shawn Hinchys query:

Are you looking for the four Boolean equations relating the complete cyclical Gray code [W,X,Y,Z] to the 16-Digit Binary Coded Decimal
[A,B,C,D]? Or to just the 10-digit decimal?

Phil Corso, PE
Trip-A-Larm Corp
 
P

Paul Anderson

Gray code is unique because only 1 bit at a time changes in the progression (forward or backwards). Because of this, it is well suited for positioning applications. Gray code encoders use this method for their position information.

Paul Anderson
Electro Cam Corp.
www.electrocam.com
 
Martin, your perception of the Gray code is analogous to stating that Einstein's Theory of Relativity led to the equation E=MC^1.999+.

Give Dr. Gray (I never met the man) credit for being a major contributor in the development of commutator codes. They are often referred to as
cyclical, progressive, or Gray codes. They are less prone to error because as the code progresses, consecutive groups differ by just one
bit (or "digit" in the Golden days).

Phil Corso, PE
Trip-A-Larm Corp
 
R
Gray code was invented because only 1 bit changes when passing from one state to the next, so it avoids 'intermediate problems' when passing from one state to another without resorting to a clock (if more than one bit changed there would be a brief moment when 1 bit has already changed and the next has not yet changed). This is as true for optical encoders as it is for mechanical, but modern optical encoders will often be 'buffered', and the onboard chip will take care of things.

Gray Binary
000 000
001 001
011 010
010 011
110 100
111 101
101 110
100 111

Here you will notice that the most signitficant bit in the binary code is the same as the Gray ode, whilst succesive bits are simply the
exclusive OR of the bit with the preceeding bit (B1=G1 xor G2). That may sound simple, but needs quite a bit of shifting and twiddling, look up
tables or almost always better in todays memory swamped world.

If your encoder is monodirectional, or always moving in a known state, you may be able to simply syncronize on zero and simply inc (or dec)
each time you get an input change interupt. bsolute position encoders are often employed by the pedantic when a quadrature encoder would have
sufficed!
 
Further to my earlier responses n the subject:

To Evan Martin,
I apologize for not getting your name correctly. Also, please do not take my response as a putdown. It's a "gray hair" thing.

To Shawn Hinchey,
If it will help, and you are still interested, I can send you solution(s) in Boolean form using combinational logic, symmetric function form, or minimal relay contact realization.

Regards,

Phil Corso, PE
Trip-A-Larm Corp
 
An application of Gray Code is from the altitude encoder that feeds mode C Aircraft transponders so the the altitude is displayed with the radar image.

Don Thomas
 
You can use something called UBASIC, a high precision flavor of good old basic, its on the net as shareware.

Try this Shawn
**********clip here************
10 'bingry a binary <---> gray code converter
11 'copyright Patrick Kirol, [email protected], 2000.9.17, Seoul
20 input "Convert from binary to gray (1) or gray to binary (0)";Ms
30 if Ms=1 then goto 50
40 if Ms=0 then goto 140
50 'cls:'bing.ub -- Gray code to binary, decimal conversion
60 strinput "Input Gray Code to convert";Btc
70 K=len(Btc)
80 dim Bc(K):Bc(1)=val(left(Btc,1))
90 for N=2 to K:Aa=val(mid(Btc,N,1)):Bb=Bc(N-1)
100 Bc(N)=bitxor(Aa,Bb)
110 next
120 print Btc:for N=1 to K:print Bc(N);:next N
130 end
140 cls:'gray binary to Gray code convert dec,gray,hex output
150 kill "grout.txt"
160 input "Input nr of binary digits";Ff
170 dim E(Ff)
180 print "dec";spc(2);"bin code";spc(2);"gray";spc(2);"hex"
190 print " 0";spc(Ff+1);"0";Spc;"0";spc(Ff+2);"0";spc(3);"0"
200 for Bin=1 to 2^Ff:A=Bin:B=int(Bin/2):Gray=bitxor(A,B)
210 D=Gray/2:H=int(D)
220 print Bin;spc(2);
230 gosub 310
240 for K=0 to Ff
250 print using(1,0),E(Ff-K);
260 next K
270 open "grout.txt" for append as #1:print #1,Bin,Gray,hex(Gray):close #1
280 print spc(2);Gray;spc(2);hex(Gray)
290 next Bin
300 end
310 for G=0 to Ff:C=D-H
320 if C=0 then E(G)=0 else E(G)=1
330 if C=D/2 then E(G)=0
340 D=H/2:H=int(D):next G
350 return
************** clip here ***********
 
J

Jerry Spencer

To Phil,
I would appreciate seeing the minimal relay contact solution, if you still have it, as I just came across a need to translate a 16-bit encoder gray code to integer in a small PLC.
Thank you,
Jerry Spencer
[email protected]
 
J

Jerry Spencer

To Phil,
I would appreciate seeing the minimal relay contact solution, if you still have it, as I just came across a need to translate a 16-bit encoder gray code to integer in a small PLC.
Thank you,
Jerry Spencer
[email protected]
 
Top