Maker Pro
Maker Pro

Sort of Gray code to binary converter

J

John B

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.
 
L

Luhan

John said:
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.

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

Luhan
 
R

Rich Webb

On 11 Dec 2005 14:18:22 GMT, "John B"

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

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.
 
D

Dave

John B said:
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.

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
 
F

Frank Bemelman

John B said:
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.

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.
 
J

John B

On 11 Dec 2005 14:18:22 GMT, "John B"

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

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.

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.
 
J

John B

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

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

Dave

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?
 
J

Jim Thompson

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?

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
 
John said:
On 11 Dec 2005 14:18:22 GMT, "John B"

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

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.

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.

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.
 
S

Spehro Pefhany

On 11 Dec 2005 14:18:22 GMT, "John B"

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

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.

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.

Why must there be?
Must have had too much Fetzer Merlot at lunchtime, but it was worth it.

/* 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
 
M

martin griffith

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.
If you could input active lows, use a priority encoder, hc157 to get
the high bit sorted, but with the ouputs shifted up by one use diodes
and resistors as a 2bit detector into a comparitor to see if the next
bit down is set.This would be the lsb

maybe not.

(I prefer Speff's switch statment)


martin
 
K

Ken Smith

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:

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

01234567 Priority Even Parity
 
K

Ken Smith

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) {

or

Y = Table[X];
 
J

John B

On 11 Dec 2005 16:56:00 GMT, "John B"



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

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!!
 
K

Ken Smith

John B said:
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.

Since there's going to be a table involved anyway you could just have a
256 location table with most of them blank.

If you don't like that big of a table how about:

if ( X and $0F0 ) > 0 then Y := Table1[ X shr 3]
else Y := Table2[X]
 
F

Frank Bemelman

John B said:
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;

That is not so bad at all. It looks perhaps bad, but it isn't.
It's the same as a switch statement.
 
P

Peter Bennett

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.

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
new newsgroup users info : http://vancouver-webpages.com/nnq
GPS and NMEA info: http://vancouver-webpages.com/peter
Vancouver Power Squadron: http://vancouver.powersquadron.ca
 
S

Spehro Pefhany

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.

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)?
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!!).
Cool!

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!!

Oh.

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


Best regards,
Spehro Pefhany
 
J

John B

On 11/12/2005 the venerable Spehro Pefhany etched in runes:

..
..
..
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?

Oh, what a pedant you are.

OK, in this case 'best' means least amount of 'C' code or smallest
amount of external hardware. Cost of components can be ignored since
this has become an academic exercise and academics tend to ignore costs.
Also, a bit more about what "solution" means, since you have not
defined the output for 93.75% of the possible inputs.

The unspecified 93.75% of cases can only happen in a situation where
the external sensor has failed in some way. Let's assume that this wont
happen and all unspecified cases can return any random value. That
makes it easy for you.
If this was a serious competition, you'd probably also have to clearly
define "Christmas eve". You mean January 6, 2006 (Coptic/Orthodox)?

As a pagan I personally have no Christmas so the closing date of the
competition will be three days after the winter solstice. Just for the
pedants, that will be the winter solstice in the Northern hemisphere!


Oh no, another pagan who drinks his red wine cool. Red wine should be
drunk at room temperature and allowed to breathe for some time before
drinking. That's always the hardest part.
Oh.

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

Definitely worth a try.
 
S

Spehro Pefhany

That is not so bad at all. It looks perhaps bad, but it isn't.
It's the same as a switch statement.

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
 
Top