GreyCode2Bin PROC a_iNum:DWORD
mov eax, a_iNum
mov ecx, eax

REPEAT 31
shr ecx, 1
xor eax, ecx
ENDM

GreyCode2Bin ENDP

-- -------

A friend of mine came up with this algo.
I made an MMX version which takes about 27 cycles also but I don't remember how it was. I'll post it later.
BTW. Is there any use for grey codes at this time?
Posted on 2002-11-15 08:21:01 by gliptic
Erm... what's a grey code?
Posted on 2002-11-16 09:34:48 by iblis
In case you were not joking...

greycode is a counting system where each sequential number changes by only one binary bit. This allows for error detection during "counting", amongst other things.
It was common at one point in certain types of encoders used for things like positioning.
Posted on 2002-11-27 01:46:41 by Homer
Opps, thinken drunken backwards.

Kewl. I've wondered how to do that. Didn't realize that it was a modular function (in the mathmatical sense).

<begin snip>
I hope you didn't spend to much time reinventing the wheel: Bin2GreyCode -- In 3 lines of FAsm
<end Snip>
GreyCode is useful for tracking the direction of motion. Instead of checking x number of bits, you only need to check 2 bits. Thats how ball mice worked, and how the scroll wheel works.

Grey Code is also used by allPossibleImages.
Posted on 2002-12-08 03:03:00 by eet_1024
That's misleading - mice have generally used a variety of incremental rotary encoder, which has one, two or three "channels"...

Most common is the two channel optical encoder, where we use a spoked wheel or other slotted wheel to break two beams of light.

Either the channel optics or the encoder slots are offset by 50% of a slot.
This causes a situation where the two channels can have four possible values:

A B
0 0
0 1
1 0
1 1

In turn we can use this "quadrature output" to determine the direction and to identify miscounts. This is called quadrature decoding. I have a software version of it in another thread. You can buy a chip with 8 pins called a 7083 that can do the job very nicely, giving you a count up/count down output.
Better decoder ic's even have an onboard counter !!
This has nothing at all to do with gray code.
Again, this is called "quadrature decoding".
It's a method of obtaining four counts per encoder cycle, verifyiung direction, and identifying miscount. It's also known as "X4" in digital electronic circles.
Posted on 2002-12-08 07:04:13 by Homer
If you offset two sets of encoding slots by 50%, you get, depending on direction, the signal sequence

0 0
0 1
1 1
1 0

or

0 0
1 0
1 1
0 1

which are Gray code sequences.
Posted on 2002-12-10 16:33:00 by tenkey
strictly speaking, I guess so, but since the definition of Gray Code is that the numbers alter by one bit per increment and we have only one "other" bit, really that's pretty cheesy.
Try adding a third channel, you will see that we actually have a simple binary count, whether it be forward or backward... greater than 2 channels shows that this is not Gray Code per se...
The type of encoder wheel used for industrial machinery and newer carnival rides is the "absolute" type of encoder which has one channel per bit of resolution of the encoder output, and quite often has 12 or more bits, and thus, 12 or more channels, and 12 or more tracks on the encoder wheel (rather than offsetting the channel pickups)...
Although I have NEVER seen Grey Code applied to an absolute encoder wheel, I should think it would have been the obvious choice for that application!!
Has anyone seen Grey Code applied to an absolute encoder wheel?

Note on scroll wheel:
There's a weird sort of single-track 3-channel encoder which uses the Vernier Principle to produce a predictable output which is then decoded using usually a rom lookup table on a surface mount ic. Those are the ones used in the mouse scroll wheel (the mice with the wheel on the middle button)... that principle is also used in a kind of mechanical pump. They usually are not optical but use brushes and copper pads like some old dc electric motors...
Simply put, if there's 3 channels, thats an odd number, so the encoder wheel has an even number so that if the channel optics are offset by 120 degrees (radially equidistant) then the 3 channels will never be above the same relative point on the encoder at the same time.
Vernier measuring devices of all kinds rely on this principle... the idea being that we have two scales which just don't marry, our accuracy is enhanced by their dissimilarity when we compare them against one another.
Posted on 2002-12-10 23:01:04 by Homer
I stand corrected...
both binary AND GrayCode versions of incremental AND absolute encoders exist.
Posted on 2002-12-12 06:23:30 by Homer