Sort of Gray code to binary converter
Electronics Forum Index Electronics
Circuits, theory, electrons and discussions.
 
 FAQFAQ   MemberlistMemberlist     RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 
 
Google
 
Web ElectronicsHelp.net
Sort of Gray code to binary converter
Goto page 1, 2, 3, 4  Next
 
Post new topic   Reply to topic    Electronics Forum Index -> Design
Author Message
John B
Guest





Posted: Sun Dec 11, 2005 5:35 pm    Post subject: Sort of Gray code to binary converter Reply with quote

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

Back to top
Luhan
Guest





Posted: Sun Dec 11, 2005 5:35 pm    Post subject: Re: Sort of Gray code to binary converter Reply with quote

John B wrote:
Quote:
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
Back to top
Rich Webb
Guest





Posted: Sun Dec 11, 2005 5:35 pm    Post subject: Re: Sort of Gray code to binary converter Reply with quote

On 11 Dec 2005 14:18:22 GMT, "John B"
<spamj_baraclough@blockerzetnet.co.uk> wrote:

[snip...snip...]
Quote:

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.

--
Rich Webb Norfolk, VA

Back to top
Dave
Guest





Posted: Sun Dec 11, 2005 5:35 pm    Post subject: Re: Sort of Gray code to binary converter Reply with quote

"John B" <spamj_baraclough@blockerzetnet.co.uk> wrote in message
news:439c352e$0$28626$4c56ba96@master.news.zetnet.net...
Quote:
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


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
Back to top
Frank Bemelman
Guest





Posted: Sun Dec 11, 2005 5:35 pm    Post subject: Re: Sort of Gray code to binary converter Reply with quote

"John B" <spamj_baraclough@blockerzetnet.co.uk> schreef in bericht
news:439c352e$0$28626$4c56ba96@master.news.zetnet.net...
Quote:
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.

--
Thanks, Frank.
(remove 'q' and '.invalid' when replying by email)
Back to top
Jim Thompson
Guest





Posted: Sun Dec 11, 2005 5:35 pm    Post subject: Re: Sort of Gray code to binary converter Reply with quote

On 11 Dec 2005 16:56:00 GMT, "John B"
<spamj_baraclough@blockerzetnet.co.uk> wrote:

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

snip

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?

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 |
| Analog/Mixed-Signal ASIC's and Discrete Systems | manus |
| Phoenix, Arizona Voice:(480)460-2350 | |
| E-mail Address at Website Fax:(480)460-2142 | Brass Rat |
| http://www.analog-innovations.com | 1962 |

I love to cook with wine. Sometimes I even put it in the food.
Back to top
John B
Guest





Posted: Sun Dec 11, 2005 5:35 pm    Post subject: Re: Sort of Gray code to binary converter Reply with quote

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

<snip>

Quote:
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?

--
John B
Back to top
John B
Guest





Posted: Sun Dec 11, 2005 5:35 pm    Post subject: Re: Sort of Gray code to binary converter Reply with quote

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

Quote:
On 11 Dec 2005 14:18:22 GMT, "John B"
spamj_baraclough@blockerzetnet.co.uk> wrote:

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

--
John B
Back to top
slebetman@yahoo.com
Guest





Posted: Sun Dec 11, 2005 5:35 pm    Post subject: Re: Sort of Gray code to binary converter Reply with quote

John B wrote:
Quote:
On 11/12/2005 the venerable Rich Webb etched in runes:

On 11 Dec 2005 14:18:22 GMT, "John B"
spamj_baraclough@blockerzetnet.co.uk> wrote:

[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.
Back to top
Spehro Pefhany
Guest





Posted: Sun Dec 11, 2005 5:35 pm    Post subject: Re: Sort of Gray code to binary converter Reply with quote

On 11 Dec 2005 16:48:34 GMT, the renowned "John B"
<spamj_baraclough@blockerzetnet.co.uk> wrote:

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

On 11 Dec 2005 14:18:22 GMT, "John B"
spamj_baraclough@blockerzetnet.co.uk> wrote:

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

Quote:
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
--
"it's the network..." "The Journey is the reward"
speff@interlog.com Info for manufacturers: http://www.trexon.com
Embedded software/hardware/analog Info for designers: http://www.speff.com
Back to top
Ken Smith
Guest





Posted: Sun Dec 11, 2005 6:04 pm    Post subject: Re: Sort of Gray code to binary converter Reply with quote

In article <439c352e$0$28626$4c56ba96@master.news.zetnet.net>,
John B <spamj_baraclough@blockerzetnet.co.uk> wrote:
Quote:
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
Quote:
00000001 111 1
00000011 111 0
00000010 110 1
00000110 110 0
00000100 101 1
00001100 101 0
00001000 ... etc ...
00011000
00010000
00110000
00100000
01100000
01000000
11000000
10000000
10000001 Use quad NAND to condition the 7th bit based on the 0th

--
--
kensmith@rahul.net forging knowledge
Back to top
Ken Smith
Guest





Posted: Sun Dec 11, 2005 6:06 pm    Post subject: Re: Sort of Gray code to binary converter Reply with quote

In article <1134321036.940842.244290@f14g2000cwb.googlegroups.com>,
slebetman@yahoo.com <slebetman@gmail.com> wrote:
[...]
Quote:
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];


--
--
kensmith@rahul.net forging knowledge
Back to top
Ken Smith
Guest





Posted: Sun Dec 11, 2005 6:21 pm    Post subject: Re: Sort of Gray code to binary converter Reply with quote

In article <439c5a1f$0$12562$4c56ba96@master.news.zetnet.net>,
John B <spamj_baraclough@blockerzetnet.co.uk> wrote:
[...]
Quote:
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]

--
--
kensmith@rahul.net forging knowledge
Back to top
Ken Smith
Guest





Posted: Sun Dec 11, 2005 10:57 pm    Post subject: Re: Sort of Gray code to binary converter Reply with quote

In article <439c9707.5949614@news.provide.net>,
Robert Scott <no-one@dont-mail-me.com> wrote:
Quote:
Using 74LS00 series logic I can do it with the following chips:

74LS00 x 4
74LS20 x 2
74LS30 x 1
74LS04 x 1

I've got you very beat on chip count but mine are higher level chips.
See my post elsewhere in this thread.

Since this doesn't have to be fast, you could use open collector/drain
gates to wire-or things. 8 chips seems like way too many for this job.

A SIP R-pack and a comparitor could do the LSB if the high levels are well
known.




--
--
kensmith@rahul.net forging knowledge
Back to top
martin griffith
Guest





Posted: Mon Dec 12, 2005 12:02 am    Post subject: Re: Sort of Gray code to binary converter Reply with quote

On 11 Dec 2005 14:18:22 GMT, in sci.electronics.design "John B"
<spamj_baraclough@blockerzetnet.co.uk> wrote:

Quote:
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
Back to top
 
Post new topic   Reply to topic    Electronics Forum Index -> Design All times are GMT
Goto page 1, 2, 3, 4  Next
Page 1 of 4

 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum



Home & Living New Topics
Contact Us
Powered by phpBB