Maker Pro
Maker Pro

Sort of Gray code to binary converter

J

John B

Branch on Too F***ing Scared to Continue.

Exclusive OR Last Week (a threaded instruction).

At least with a PIC there are only 35 instructions to remember and only
one register to worry about.
 
G

Georg Acher

John Larkin said:
Good grief, what an instruction set! How do you pronounce "btfsc" or
"xorlw"?

Although the PIC origins go back into the 1970s, it does not mean that newer CPUs
have better opcodes...

The MAC-instructions of the PPC: About 40 in all combinations from halfword/word,
(un)signed, (un)saturated, overflow en/disabled and so on. So it should be clear
what the difference between "maclhwsuo" and "machhwso" is...

PS:Ah, wait, I've forgot another 20 "negative MAC" variants like nmachhwso:
"Negative Multiply Accumulate High Halfword to Word Saturate Signed with Overflow"

;-)
 
M

Meindert Sprang

Georg Acher said:
Although the PIC origins go back into the 1970s, it does not mean that newer CPUs
have better opcodes...

The MAC-instructions of the PPC: About 40 in all combinations from halfword/word,
(un)signed, (un)saturated, overflow en/disabled and so on. So it should be clear
what the difference between "maclhwsuo" and "machhwso" is...

That's why I have always loved Analog Devices DSP's. A signed MAC
instruction with post fetch for instance was:

mr = mr1 + mx0 * my0 (ss), mx0 = dm(i0,m0), my0 = pm(i4,m4)

And yes, this is one assembly instruction doing a multiply accumulate and
load the mac input with two new values from pointers with auto-de/increment.

Meindert
 
R

Rich Grise

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.

A look-up table. Set aside 256 bytes, and just use the 8 bits from your
thingie as the address lines, and the data is either your direction code
or "error", since you're only using 16 of 256 bytes.

i.e.:

Address Data
00000000 ****
00000001 N
00000010 NE
00000011 NNE
00000100 E
00000101 ****
00000110 ENE
00000111 ****
00001000 SE
00001001 ****
00001010 ****
00001011 ****
00001100 ESE

and so on.

Cheers!
Rich
 
R

Rich Grise

Y = Table[X];

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

In ASM you can do like this:

Table:
rept 256 ; Repeat 256 times
db 0FFH ; Fill with error
endm ; End repeat

org Table+00000011 ; Address the cell
db 1 ; Put the value in
org Table+00000110
db 2
.... etc .......
org Table + 256 ; put the pointer to after the table
... more code ...

As is often the case, it is easier to do in ASM.[/QUOTE]

How is that easier than

unsigned int table[256]; /* Usually compilers initialize arrays to 0 */

table[1] = 1;
table[3] = 2;
table[2] = 3;

etc?

Thanks,
Rich
 
R

Rich Grise

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.

It must be, since that was my first choice, and I'm the laziest guy
I know; I might be the laziest guy there is. ;-)

Cheers!
Rich
 
R

Rich Grise

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

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.

OK, here's my nominee for "junk-boxiest" - a piece of stripboard and
a diode matrix. ;-) (in effect, 16x 8-input ANDs) I guess you'd need
eight inverters too.

Cheers!
Rich
 
R

Rich The Newsgroup Wacko

.
[Spehro Pefhany wrote:]
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.

Of course, you know how to tell a connoisseur from a wino?

The connoisseur takes the bottle out of the paper sack before he drinks
it. ;-)

--
Cheers!
Rich
------
"A cowhand way out in Seattle
Had a dooflicker flat as a paddle.
He said, "No, I can't ****
A lamb or a duck,
But golly! it just fits the cattle.""
 
R

Rich Grise

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

Is it even possible for a PIC to _do_ a look-up table?

Thanks,
Rich
 
R

Rich Grise

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

How about comparing solutions on the basis of equivalent number of
gates? Obviously an ASIC could be built to do this job, but the
puzzle, as an intellectual exercise, is more interesting if you place
complexity limitations on the solution.

(For a practical solution I would just program a PIC.)

A PIC has a really huge number of gates inside. Personally, I'd more
likely put it into a CMOS 22V10. Both you and my practical solutions
contain more logic than the ones we are comparing.

A real contest would be to do it in terms of transistors.

Nothing to it - 16 transistors, about 24 diodes, And a handful or so of
resistors. ;-)

Depending on your inputs, you might not even need the buffers, so 8
transistors. :)

Cheers!
Rich
 
S

Spehro Pefhany

Good grief, what an instruction set! How do you pronounce "btfsc" or
"xorlw"?

I don't subvocalize or communicate code verbally, so I generally
don't, but--

btfsc is "bit test file (register) skip (if) clear"
xorlw is "xor literal with w" (result left in w)


where---

- w is the 'accumulator'

- 'file registers' are on-chip RAM or SFRs, organized in banks
on most chips, with some overlap on some chips, etc. real pug-ugly.

It's not my favorite 8-bit uC instruction set, but one can generally
get the job done with it. In this particular case, the code is almost
bordering on near-elegance.


Best regards,
Spehro Pefhany
 
J

John B

Stop, stop, stop.

I have just counted 65 messages in this thread and we have already
descended into bickering. That wasn't meant to happen until at least
the middle of next week.

It's probably only lunchtime for you guys across the pond, but I have
just had a wonderful dinner of roast chicken, roast parsnips, baked
potatoes, carrots and brussle sprouts (why do we call them that? I
don't think they came from Brussles) accompanied by a bottle of Fetzer
Sundial Merlot 2004. Why do those Californian winemakers insist on
pouring molten wax on top of the cork, it never comes off cleanly and
there's always bits floating about, but that's another story. Followed
by chocolate and coffee cheesecake with fresh cream. Mmmm, and all
cooked by me!

Well I think it's time to announce the winners already, otherwise I'll
never be able to toast them all between now and the New Year. My wine
cellar only has 47 bottles in it at the moment!!

First prize for most elegant hardware solution goes to Andrew Holme for
his 16V8 code.

First prize for lowest number of discrete components to Ken Smith.

First prize for the best off the wall solution goes to Rich for his
diode matrix.

This is just a personal thing but anyone who even mentioned PIC has
been killfiled (no not really, but you get my meaning) as I'm an AVR
and ARM protagonist. I have to say that all of the software solutions
have been as ugly as my original code. OK, a look-up table is probably
the fastest solution but it is soooo UGLY. 'if' 'else if' etc. or
switch{} are also OK but have no elegance.

Sorry guys, but so far the software solutions have been meagre but what
else did I expect from a group whose favourite programming language is
'Solder'.

Jim appeared to be working on a solution but faded at the last furlong,
but where was Win? He doesn't seem to have even made it out of the
starting gate.

Anyway, I congratulate all of the hardware winners, and will toast them
all very soon. As for you software guys, well you've failed me and I'll
just have to drink an extra toast to Anon in the hope that he/she will
come up with the magic bullet solution. Why did we always use that
phrase along with 'Paradigm shift' in the batwing corporation?

Merry Christmas to all from the pagan in the West of Scotland.
 
J

Jim Thompson

On 13 Dec 2005 21:15:27 GMT, "John B"

[snip]
Jim appeared to be working on a solution but faded at the last furlong,

[snip]

I quit because I thought you were leaning toward one of those gross uP
solutions ;-)

...Jim Thompson
 
S

Spehro Pefhany

Is it even possible for a PIC to _do_ a look-up table?

Thanks,
Rich

Some can, but with others you have to do an indexed jump (handling any
program memory page boundaries) into a table of retlw xx instructions.
You only get 8 bits back for each 12 or 14 bit 'return literal in w'
instruction.


Best regards,
Spehro Pefhany
 
C

Charlie Edmondson

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.

Ok, this is a good question.

First, the uC solution. Need a uC with 12 digital I/O's, and then it is
a simple case statement. Question is, What are you doing with the
output? Is this going to a display? Is it being logged? Is it going
to a PC for display and logging? In which case, you don't want a 4 bit
output, but a serial output.

Now, assuming that this is going to another system that needs a 4 bit
output, then you could build this into any FPGA or PAL that has at least
4 flip-flops. Then it is just create the state machine, and burn it.
Could be interesting, as some FF would only need some of the inputs, so
they could be bridged across some parts where the input logic is limited
to just a few signals per FF.

Or, for the easiest one part solution, burn the whole table into a ROM.
Then, you just translate instantly the input to the corrected output.

Charlie

PS: I did the ROM trick to take a bunch of little state machines and
combine them into a single chip. Just fed the output lines back to some
of the input lines...
 
C

Charlie Edmondson

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.

Ok, after reading all the other solutions, I would definitely say go
with an EPROM. Simple, straight forward, a single chip. Biggest
problem is debouncing the inputs a little, and maybe having to add logic
level translations.

Charlie
 
R

Richard Henry

John B said:
Stop, stop, stop.

I have just counted 65 messages in this thread and we have already
descended into bickering. That wasn't meant to happen until at least
the middle of next week.

It's probably only lunchtime for you guys across the pond, but I have
just had a wonderful dinner of roast chicken, roast parsnips, baked
potatoes, carrots and brussle sprouts (why do we call them that? I
don't think they came from Brussles) accompanied by a bottle of Fetzer
Sundial Merlot 2004. Why do those Californian winemakers insist on
pouring molten wax on top of the cork, it never comes off cleanly and
there's always bits floating about, but that's another story. Followed
by chocolate and coffee cheesecake with fresh cream. Mmmm, and all
cooked by me!

Well I think it's time to announce the winners already, otherwise I'll
never be able to toast them all between now and the New Year. My wine
cellar only has 47 bottles in it at the moment!!

First prize for most elegant hardware solution goes to Andrew Holme for
his 16V8 code.

First prize for lowest number of discrete components to Ken Smith.

First prize for the best off the wall solution goes to Rich for his
diode matrix.

This is just a personal thing but anyone who even mentioned PIC has
been killfiled (no not really, but you get my meaning) as I'm an AVR
and ARM protagonist. I have to say that all of the software solutions
have been as ugly as my original code. OK, a look-up table is probably
the fastest solution but it is soooo UGLY. 'if' 'else if' etc. or
switch{} are also OK but have no elegance.

Sorry guys, but so far the software solutions have been meagre but what
else did I expect from a group whose favourite programming language is
'Solder'.

Jim appeared to be working on a solution but faded at the last furlong,
but where was Win? He doesn't seem to have even made it out of the
starting gate.

Anyway, I congratulate all of the hardware winners, and will toast them
all very soon. As for you software guys, well you've failed me and I'll
just have to drink an extra toast to Anon in the hope that he/she will
come up with the magic bullet solution. Why did we always use that
phrase along with 'Paradigm shift' in the batwing corporation?

Merry Christmas to all from the pagan in the West of Scotland.

I thought we had another week, at least.
 
J

John Larkin

On 13 Dec 2005 21:15:27 GMT, "John B"

[snip]
Jim appeared to be working on a solution but faded at the last furlong,

[snip]

I quit because I thought you were leaning toward one of those gross uP
solutions ;-)

...Jim Thompson

uP's are not gross! Overkill here, but definitely cool in lots of
apps. Can *you* do an analog 16x16 multiply accurate to one LSB? Hell,
you don't even *have* LSBs!

John
 
Top