Sort of Gray code to binary converter

Hardware? The only hardware I know to do something like this is a microcontroller and a bit of shifting and adding software.

Luhan

Reply to
Luhan
Loading thread data ...

In C it is simple. Don't do clever tricks, you'll only forget why they worked later on. Do it the obvious way:

char code2number(unsigned char code) { switch (code) { case 0x01: return 0; /* 00000001 */ case 0x03: return 1; /* 00000011 */ case 0x02: return 2; /* 00000010 */ case 0x06: return 3; /* 00000110 */ case 0x04: return 4; /* 00000100 */ case 0x0c: return 5; /* 00001100 */ case 0x08: return 6; /* 00001000 */ case 0x18: return 7; /* 00011000 */ case 0x10: return 8; /* 00010000 */ case 0x30: return 9; /* 00110000 */ case 0x20: return 10; /* 00100000 */ case 0x60: return 11; /* 01100000 */ case 0x40: return 12; /* 01000000 */ case 0xc0: return 13; /* 11000000 */ case 0x80: return 14; /* 10000000 */ case 0x81: return 15; /* 10000001 */ } return -1; /* Error */ }

This is actually very fast since you only have 16 values. It is certainly faster than processing the code in a loop since it avoids the loop overhead.

Reply to
slebetman

Good idea! I've already been poking at it... I just love a good problem.

How about some pre-process to get it to real gray code? Although I suspect a look-up table has to be easiest.

...Jim Thompson

--
|  James E.Thompson, P.E.                           |    mens     |
|  Analog Innovations, Inc.                         |     et      |
 Click to see the full signature
Reply to
Jim Thompson

I think I'd use:

switch(code){ case 0x01: Direction = 0; break; case 0x03: Direction = 45; break; .... default: /* do some error thing */ }

--
Peter Bennett, VE7CEI  
peterbb4 (at) interchange.ubc.ca  
 Click to see the full signature
Reply to
Peter Bennett

Why must there be?

/* dumb but simple way */ switch (code) { case 0x01: binout = 0x0; break; case 0x03: binout = 0x1; break; case 0x02: binout = 0x2; break; .. case 0x81: binout = 0xF; break; default: /* handle any of the 240 invalid input codes */ .. }

Best regards, Spehro Pefhany

--
"it\'s the network..."                          "The Journey is the reward"
speff@interlog.com             Info for manufacturers: http://www.trexon.com
 Click to see the full signature
Reply to
Spehro Pefhany

Exactly. A 256-entry lookup table, indexed by the eight input bits, delivers a 4-bit code for every input combination. Fast. That sure beats a bushel of logic.

I did a *very* fast 16-bit sine function a while back. It was 128 kbytes long.

There's nothing you can't do in assembly. If a compiler can generate machine code, I can too.

John

Reply to
John Larkin

For this to make any sense, you're going to have to talk a lot more about what "best" means. Fastest? Cheapest in 1-off? Cheapest in quantity? What you can make with the unknown contents of your junk box?

Also, a bit more about what "solution" means, since you have not defined the output for 93.75% of the possible inputs.

If this was a serious competition, you'd probably also have to clearly define "Christmas eve". You mean January 6, 2006 (Coptic/Orthodox)?

Cool!

Oh.

Would you at least guzzle down the entire bottle yourself in one sitting and post JPGs?

Best regards, Spehro Pefhany

--
"it\'s the network..."                          "The Journey is the reward"
speff@interlog.com             Info for manufacturers: http://www.trexon.com
 Click to see the full signature
Reply to
Spehro Pefhany

I say 'sort of' because the input I have is not true reflected binary Gray code. The input is a wind direction sensor with eight discrete positions. They are activated by a small magnet which triggers either one or two sensors at any time. Thus the eight sensors produce the following sequence:

00000001 00000011 00000010 00000110 00000100 00001100 00001000 00011000 00010000 00110000 00100000 01100000 01000000 11000000 10000000 10000001

What logic do I need to convert these 16 values to a 4-bit binary number.

Thanks for your attention.

--
John B
Reply to
John B

Probably/possibly it is in this situation, but there are other things a compiler can do with a switch/case construct than a simple series of comparisons (linear search). Binary tree, table, hash, etc.

Best regards, Spehro Pefhany

--
"it\'s the network..."                          "The Journey is the reward"
speff@interlog.com             Info for manufacturers: http://www.trexon.com
 Click to see the full signature
Reply to
Spehro Pefhany
[snip...snip...]

Depends somewhat on the resources that you have available. Personally I'd take the lazy way out and either program it into a 16V8 GAL chip or an inexpensive uC like an 89C2051.

--
Rich Webb   Norfolk, VA
Reply to
Rich Webb

Lookup table in a PROM ?

Program a PAL ?

Just about any micro ?

Random logic - updown 4 bit counter, and a simple state machine - because you know that from any position there can be only two changes.

Mostly depends on what else you want to do with the number when you've got it !

Dave

Reply to
Dave

Something like this?

const unsigned char wlut[256] = {0xFF,0x00,0x02,0x01,0x04,0xFF,

0x03,0xFF,0x06,0xFF,0xFF,0xFF,0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F,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF, 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF, 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF, 0xFF,0xFF,0xFF};

direction = wlut[input];

How fast is that wind direction going to change? ;-) The table can consume 448 bytes in a typical midrange PIC, which may not be insignificant, and table lookup is pretty ugly as well (none of your dedicated CPU32 table instructions.. and paging has to be handled).

Frank's point is that there is no exact equivalent, so the compiler at least has a chance to do it better than the most obvious way- for example by presorting the cases and doing a tree. But I agree with him, most compilers on most platforms will generate a series of comparisons for a small switch/case construct.

Best regards, Spehro Pefhany

--
"it\'s the network..."                          "The Journey is the reward"
speff@interlog.com             Info for manufacturers: http://www.trexon.com
 Click to see the full signature
Reply to
Spehro Pefhany

On 11/12/2005 the venerable Rich Webb etched in runes:

OK, so let's say I'm going to do it in 'C'. I've played with shifting and XOR'ing values but can't find the right combination. Converting

4-bit real Gray code to 4-bit binary is simple:

code ^= (code>>2); code ^= (code>>1);

There must be a similar solution to my input, but I just can't see it.

Must have had too much Fetzer Merlot at lunchtime, but it was worth it.

--
John B
Reply to
John B

On 11/12/2005 the venerable Dave etched in runes:

I want to use it as an index into a look-up table of wind directions in degrees from North. What I have just now is very ugly and I don't like it.

if(code == 0x01) Direction = 0; else if(code == 0x03) Direction = 45; else if(code == 0x02) Direction = 90;

... etc.

I thought "There must be an easy way to convert this to binary" and I've been looking at different options for nearly a day and a half now.

How about I make it into a competition ala Jim?

--
John B
Reply to
John B

Any ugly CPU like a PIC probably looks forward having its PCLATH diddled once in a while.

Best regards, Spehro Pefhany

--
"it\'s the network..."                          "The Journey is the reward"
speff@interlog.com             Info for manufacturers: http://www.trexon.com
 Click to see the full signature
Reply to
Spehro Pefhany

"John B" schreef in bericht news:439c352e$0$28626$ snipped-for-privacy@master.news.zetnet.net...

I'd use a small microcontroller, eprom or something, but I guess it also can be made of 8 inverters, a 74HCT148 priority encoder to get bits 2-4-8 and 8 resistors + resistor to ground to detect 2 highs, using a comparator, to get bit 1.

--
Thanks, Frank.
(remove \'q\' and \'.invalid\' when replying by email)
Reply to
Frank Bemelman

I considered that one. That requires an array of 256 elements 93.75% or which simply store errors/illegal values. The switch statement on the other hand requires less typing (16 cases instead of 256 values).

Reply to
slebetman

A priority decoder + parity generator + quad NAND: Use a fixed font:

01234567 Priority Even Parity
--
--
kensmith@rahul.net   forging knowledge
Reply to
Ken Smith

In article , snipped-for-privacy@yahoo.com wrote: [...]

or

Y = Table[X];

--
--
kensmith@rahul.net   forging knowledge
Reply to
Ken Smith

On 11/12/2005 the venerable Jim Thompson etched in runes:

Yep, it's the pre-process stuff that I can't figure.

OK here's the deal, it's officially a competition to find the best solution by Christmas Eve. In the tradition of all good competitions there will be a prize of a bottle of really good red wine (probably from CA or Oz, although Argentina is producing some good stuff these days certainly won't be from Frogland - if they won't eat our apples then I won't drink their wine!!).

Since I'm on an island in the West of Scotland transporting such a prize could be difficult so what I'll do is toast the winner with said red wine on Christmas Day.

Good luck!!

--
John B
Reply to
John B

ElectronDepot website is not affiliated with any of the manufacturers or service providers discussed here. All logos and trade names are the property of their respective owners.