PDA

View Full Version : Reading a 8bit Binary word



Moz
- 31st August 2004, 20:46
Hi All,

After a little direction here if anyone can help.

What I need to do is read a 8 bit binary word say on porta, look up a value and output this binary value on portb.

But I would need to keep polling the trigger word on porta to see if it changes.

eg 00110011 input

lookup code 00110011 means an output of 11000011 is required.

output 11000011 on portb

keep the output constant until input changes (loop)

In the good old days - i would use a eprom and program it to act as a simple data converter with a bit of discrete logic to track any address changes on the address lines.

Any direction would be great

Many Thanks


Moz

Dwayne
- 31st August 2004, 21:09
Hello Moz,


Moz>>What I need to do is read a 8 bit binary word say on porta, look up a value and output this binary value on portb.<<

How is the word going to be read??? Via Serial data? Via parallel data? (Many A ports that I know of, can't handle a 8 bit Parallel read). So what kind of chip are you talking about?

Now about output... are you going to output as a parallel data output, and use the whole port B (like a 8 bit LCD)? Or are you going to serial output and use just a pin?(like a Backpack LCD)

These are untested, but should work with little change in coding.


a simple psuedo code parallel

Loop:
if Porta="00110011" then gosub Newdata
goto Loop

Newdata:
Portb=Porta
return.


a simple psuedocode Series:

Readdata var byte;
trisa="11111111"
trisb="00000000"
PinIn var Porta.1
PinOut var Portb.1

Loop:
Serin PinIn, T2400, Readdata
if Readdata="00110011" then gosub Newdata
goto Loop:

Newdata:
Portb=Readdata 'for parallel output
Serout PinOut,T2400,Readdata 'for serial output
return

Dwayne

Moz
- 1st September 2004, 12:35
Thanks Dwayne,

The chips I am playing / have most of are the 16f627, but whichever can do the job is what I will purchase.

The application is parallel read and parallel output, so what happens is I have a series of inputs which are in binary, I need to look up the read value and ouput a new value on the other port.

Similar to an eprom used in this way - where you would set up an address toggle it and the 8 bit code (whatever was programmed) in the chip would appear on the D0-D7 data lines.

It is going to be used as a "translator" from one test fixture to another to control some Automatic Test Equipment. The Original eprom that was used to do this has been damaged (always use ZIF sockets :-) ) and I thought a pic could be used instead.

There is about 40 codes that would need to be translated.

Many Thanks


Moz

Dwayne
- 1st September 2004, 15:06
Hello Moz,

Ok, now we have parallel to Parallel with a dinner table as a translator.
Here is a untested but probably very close working program you are looking for I think...

Dwayne

TRISA="11111111" 'set Porta to all input...8 bit
TRISB="00000000" 'set Portb to all output...8 bit.
ReadData var byte
HoldData var byte
TranslateData var byte
Counter var byte


MainLoop:
ReadData=Porta
If ReadData!=HoldData then Gosub TableLookup
HoldData=ReadData
Goto MainLoop

TableLookup:
Counter=0
Loop2:
Read Counter,Holddata 'read table
if Holdata=$FF then Goto MainLoop 'bad data
If Holddata=ReadData then goto TransmitPortb 'MATCH!!!
Counter=Counter+2 'Must skip every other byte for table!
GotoLoop2


TransmitPortb:
Read Counter+1, HoldData 'reads Translated value
Portb=Holddata 'outputs to portb
Return
End

Dwayne
- 2nd September 2004, 21:46
Hello Moz,

I made just a few corrections in that psuedo code...


TRISA="11111111" 'set Porta to all input...8 bit
TRISB="00000000" 'set Portb to all output...8 bit.
ReadData var byte
HoldData var byte
TranslateData var byte
Counter var byte


MainLoop:
ReadData=Porta
If ReadData!=HoldData then Gosub TableLookup
HoldData=ReadData
Goto MainLoop

TableLookup:
Counter=0
Loop2:
Read Counter,Holddata 'read table
if Holdata=$FF then Goto Baddata 'bad data
If Holddata=ReadData then goto TransmitPortb 'MATCH!!!
Counter=Counter+2 'Must skip every other byte for table!
Goto Loop2


TransmitPortb:
Read Counter+1, HoldData 'reads Translated value
Portb=Holddata 'outputs to portb
Baddata:
Return
End

Moz
- 3rd September 2004, 00:44
Hi Dwayne,

Thanks for your help, sorry about the delay in responding I have just got back from London on business.

OK I can follow most of this but dont understand 2 things, why have u declared the variable TranslateData when it does not appear to be used ?

And the other one is
Counter=Counter+2 'Must skip every other byte for table!

On the lookup's this has got me confused.

I did not know you could say read porta as a byte, so i was expecting the code to use a lookup table (if one can hold 38 values) as the input and output data has nothing in common and may in some circumstances be duplicated

eg if input is 11011011 then output 00001100
if input is 00011111 then output 00001100
if input is 10101010 then output 01111111

so i suppose a nasty and not efficient way would be something like this - psuedo code

TRISA="11111111" 'set Porta to all input...8 bit
TRISB="00000000" 'set Portb to all output...8 bit.
ReadData var byte

MainLoop:
ReadData=Porta
If ReadData=11001111 then Gosub SUB1
If ReadData=11001111 then Gosub SUB2
If ReadData=11001111 then Gosub SUB3
Goto MainLoop

SUB1:
Portb=00110100 'outputs to portb
return

SUB2:
Portb=10111000 'outputs to portb
return


SUB3:
Portb=11111100 'outputs to portb
return


It is important to note that there is nothing common between the output and input data.

Thanyou


Moz