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 Previous  1, 2, 3, 4  Next
 
Post new topic   Reply to topic    Electronics Forum Index -> Design
Author Message
John B
Guest





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

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

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


<snip>

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

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

Back to top
Frank Bemelman
Guest





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

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

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

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





Posted: Mon Dec 12, 2005 12:33 am    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.

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

Back to top
Spehro Pefhany
Guest





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

On 11 Dec 2005 18:20:24 GMT, the renowned "John B"
<spamj_baraclough@blockerzetnet.co.uk> wrote:

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

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


snip

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

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

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

Quote:
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
--
"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
John B
Guest





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

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

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

Quote:

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.

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


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

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.

Quote:

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?

Definitely worth a try.

Quote:


Best regards,
Spehro Pefhany



--
John B
Back to top
Fred Bloggs
Guest





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

Frank Bemelman wrote:

Quote:
This is all very understandable,
as there exists no assembly equivalent for a switch statement, so ...

Whatever happened to doing an indirect jump through a table ( of
addresses) indexed into by the argument?
Back to top
John Larkin
Guest





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

On Sun, 11 Dec 2005 20:22:42 GMT, Fred Bloggs <nospam@nospam.com>
wrote:

Quote:


Frank Bemelman wrote:

This is all very understandable,
as there exists no assembly equivalent for a switch statement, so ...

Whatever happened to doing an indirect jump through a table ( of
addresses) indexed into by the argument?

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
Back to top
Fred Bloggs
Guest





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

John Larkin wrote:
Quote:
On Sun, 11 Dec 2005 20:22:42 GMT, Fred Bloggs <nospam@nospam.com
wrote:



Frank Bemelman wrote:


This is all very understandable,
as there exists no assembly equivalent for a switch statement, so ...

Whatever happened to doing an indirect jump through a table ( of
addresses) indexed into by the argument?


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


Don't these "Small C" compilers allow them to mix assembly with high
level? Seems dumb if they don't.
Back to top
Robert Scott
Guest





Posted: Mon Dec 12, 2005 1:35 am    Post subject: Re: Sort of Gray code to binary converter Reply with 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






-Robert Scott
Ypsilanti, Michigan
Back to top
Spehro Pefhany
Guest





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

On Sun, 11 Dec 2005 12:33:50 -0800, the renowned John Larkin
<jjlarkin@highNOTlandTHIStechnologyPART.com> wrote:

Quote:
On Sun, 11 Dec 2005 20:22:42 GMT, Fred Bloggs <nospam@nospam.com
wrote:



Frank Bemelman wrote:

This is all very understandable,
as there exists no assembly equivalent for a switch statement, so ...

Whatever happened to doing an indirect jump through a table ( of
addresses) indexed into by the argument?

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.

Something like this?

const unsigned char wlut[256] = {0xFF,0x00,0x02,0x01,0x04,0xFF,
0x03,0xFF,0x06,0xFF,0xFF,0xFF,0x05,0xFF,0xFF,0xFF,0x08,0xFF,0xFF,
0xFF,0xFF,0xFF,0xFF,0xFF,0x07,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
0x0A,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
0xFF,0xFF,0xFF,0x09,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0x0C,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,0x0B,
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,0x0E,0x0F,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,0xFF,0xFF,0xFF,0xFF,
0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
0xFF,0xFF,0xFF,0xFF,0x0D,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,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).

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

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
Embedded software/hardware/analog Info for designers: http://www.speff.com
Back to top
Frank Bemelman
Guest





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

"Fred Bloggs" <nospam@nospam.com> schreef in bericht
news:439C8A8E.9080203@nospam.com...
Quote:


Frank Bemelman wrote:

This is all very understandable,
as there exists no assembly equivalent for a switch statement, so ...

Whatever happened to doing an indirect jump through a table ( of
addresses) indexed into by the argument?

That's nice if you have some control over the argument, like being
restricted to 8 bit etc. In this case, only 16 different argument values
exist. You can sacrifice a table of 256 vectors to gain some speed,
if you must, and if you have the room for it.

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





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

On Sun, 11 Dec 2005 19:27:59 +0100, the renowned "Frank Bemelman"
<f.bemelmanq@xs4all.invalid.nl> wrote:

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

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
--
"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
Frank Bemelman
Guest





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

"Spehro Pefhany" <speffSNIP@interlogDOTyou.knowwhat> schreef in bericht
news:pvsop19okbua2anekr11fa3mvgnaqthq5v@4ax.com...
Quote:
On Sun, 11 Dec 2005 19:27:59 +0100, the renowned "Frank Bemelman"
f.bemelmanq@xs4all.invalid.nl> wrote:

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

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.

Right. If a large enough switch statement where an 8 bit switch argument
is used, a binary tree comes to mind, but with smaller switches it all
boils down to series of if/else cases. Writing it in if/else form, it
allows you to optimize for the situation at hand, put the most likely
possibilities at the top, things like that. This is all very understandable,
as there exists no assembly equivalent for a switch statement, so any
cleverness, if any, has to come from the compiler if you depend on the
switch statement. I have yet to see a compiler that uses anything else
than if/else approach, but perhaps I haven't used large enough switches
to see a different behaviour.

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





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

"Spehro Pefhany" <speffSNIP@interlogDOTyou.knowwhat> schreef in bericht
news:i54pp113kh23c3b7m55qp153ei79evkiu6@4ax.com...
Quote:

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

Ugliness is in the eye of the beholder. The CPU couldn't care less ;)

[snip]

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





Posted: Mon Dec 12, 2005 1:35 am    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.

16V8 PAL

Inputs = ABCDEFGH

0xxxxxx1 = 0 or 1
xxxxxx10 = 2 or 3
xxxxx10x = 4 or 5
xxxx10xx = 6 or 7
xxx10xxx = 8 or 9
xx10xxxx = 10 or 11
x10xxxxx = 12 or 13
10xxxxxx = 14 or 15

Output0 = AB + BC + CD + DE + EF + FG + GH + AH
Output1 = A&/B + C&/D + E&/F + G&/H
Output2 = A&/B + B&/C + E&/F + F&/G
Output3 = A&/B + B&/C + C&/D + D&/E
Back to top
 
Post new topic   Reply to topic    Electronics Forum Index -> Design All times are GMT
Goto page Previous  1, 2, 3, 4  Next
Page 2 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