Shown below is a 'C' function that converts 6 bit grey to decimal. It can be easily extended to 10 bits. The binary bit values are developed internally. Essentially it involves bitmasking to extract the bits from the grey code, and exclusive ORing the bits.
int grey_decimal(int grey_value) { int bit5=0, bit4=0, bit3=0, bit2=0, bit1=0, bit0=0; bit5 = (grey_value&0x20)>>5; /* bit mask and shift bit into LSB position */ bit4 = (grey_value&0x10)>>4; bit3 = (grey_value&0x08)>>3; bit2 = (grey_value&0x04)>>2; bit1 = (grey_value&0x02)>>1; bit0 = grey_value&0x01; bit4 = bit5^bit4; /* exclusive OR bit5 and bit4 to produce a new value of bit4 */ bit3 = bit4^bit3; bit2 = bit3^bit2; bit1 = bit2^bit1; bit0 = bit1^bit0; return 32*bit5+16*bit4+8*bit3+4*bit2+2*bit1+bit0; /* calculate a deciaml number */ }
There was an extensive discussion on this subject in February under the subject "Re: SOFT: Gray Code". Johan Bengtsson included a response with a short code example, and various other people suggested other methods including look-up tables. You should be able to search the archive for it. If you cannot find it, I still have the old e-mails and can forward them directly to you.
For a range the size of the one you have specified (10 bit), I believe the code version is probably better than a look-up table for a PLC. When you have finished writing and testing this function, you will contribute it to the PLC archive, won't you? In the conversations which led to the establishment of the archive this was given as a prime example of the sort of algorithm desired.
Please note that the discussion is listed under "Gray" code, not "Grey" code. I had recalled this subject being discussed before, but almost gave up searching my old e-mails for it until I remembered to try the odd American spelling of the word. Yet another example of how this industry is being crippled by a lack of adherance to standards! I hereby challenge everyone to denounce the proprietary "gray" and adopt the superior open standard "grey"!
********************** Michael Griffin London, Ont. Canada [email protected] **********************
Perhaps that's why I didn't find it when I searched control.com. Anthony.
(Moderator's Note -- Both threads now list both spellings for easier hunting. --JP)
.... > being crippled by a lack of adherance to standards! I hereby challenge > everyone to denounce the proprietary "gray" and adopt the superior open > standard "grey"!
720 position? that doesn't sound like standard grey code then, normal grey code would have 1024 positions for 10 bits (same as binary code with the same number of bits)
Normal grey code can be coded (and decoded) using xor blocks, one for each bit - 1. I suppose that isn't what you are asking about...
If anyone can provide the table it should probably be fairly simple to convert that into ladder.
It is of course quite possible to reduce the number of positions (I have not thought about it before, but it could easily enough be done I suppose (within limits)). The decoding is however heavily dependant on exactly how the number of positions is reduced, unless there is some kind of standard that information have to be provided in some way.
I suppose the reduction is done in something like this way (the steps surrounded with () can be skipped reducing the normal 8 positions could be reduced down to 6) How this is done with 10 bits reducing from 1024 to 720 is a too long excercise for me right now, and besides, there is most certainly more than one way it could be done.
We have recently completed an application where we converted a grey coded number to a integer using ladder logic routine. If interested I will send to you.
In a message dated 00-12-20 14:47:19 EST,mgriffin writes: > > Please note that the discussion is listed under "Gray" code, not > "Grey" code. I had recalled this subject being discussed before, but almost > gave up searching my old e-mails for it until I remembered to try the odd > American spelling of the word. ....
ISTR the name came from the person's name who invented it, so there should be only one spelling of it. I believe its Gray.
>720 position? >that doesn't sound like standard grey code then, normal >grey code would have 1024 positions for 10 bits (same as >binary code with the same number of bits)
360 and 720 positions encoders are pretty standard and they use regular gray coding. The trick is that the 0 degree position is offsetted from the gray 0. For example, 0 degree will be 152 and 359.5 degrees (position 719) is encoded as 871. It is a true gray code because between encoded 152 and encoded 871 there is only one bit change.
The comments I made at the end of my previous message were intended as a satire on the communications standards problems in this industry. Yes I am aware that the "Gray" in "Gray Code" comes from Mr. Gray's name and not from the colour "grey". It is however an easy mistake to make (as Mr. Kerstens also discovered).
The point of the satirical comment was that certain people routinely condem others for not conforming to some communications standard (the fieldbus wars), yet I suspect that the Americans find nothing unusual in using the "proprietary" "gray" (the colour that is, not the name) instead of the "superior, open standard, lower cost" "grey" which the rest of the English speaking world uses. This occured to me when I discovered that my problem in finding the relavent data was due to an incompatability in the "communications protocol". We should perhaps all keep this point in mind in our other discussions on more conventional communications problems. Many of these discussions tend to generate more heat than light.
As for Mr. Gray's name, well I'm sure there will be a few minor inconveniences during the brief inevitable transition to the open standard ...
********************** Michael Griffin London, Ont. Canada [email protected] **********************
Then the answer (you have probably already figured that out) is simple: Do normal decoding and subtract 152
If you for any reason would like to do the subtraction with ladder rungs then that would actually be quite easy too, but I suppose you want the result in some register anyway...
/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/ ----------------------------------------
Here's a web page that explains a lot. http://www.motion.org/9801vina.htm I don't believe that a simple subtraction will convert between Gray and Binary as the conversion from Binary to Gray involves a shift and XOR.
I wasn't suggesting that either, read again "Do normal decoding AND subtract 152" This involves two steps, step 1: normal Gray->binary conversion step 2: subtraction of 152
/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/ ----------------------------------------