PDA

View Full Version : How to convert HEX Value as formatted BIN String ?



Robson
- 18th August 2007, 20:44
Hello,
thanks for all who helped me with my last prob. Very hot ways to realize that.
Now i want to shrink my code because the limit is coming (4k).
Is it possible to convert hex data read from eeprom or such like LOOKUP as a formatted 8-Bit String?

for expamle: Eeprom Location, hex value01, hex 2,.....
@$0 $23 = 00100011
@$1 $01 = 00000001
Now the string should be look like this : 0010001100000001 ...
And this string should be send via a portpin i.e. PORTB.5
Before i wrote like this and it works fine but really takes a lot of memory. One string can be 192 Bits long.

FOR lp = 0 TO 23 ' 24 BIT
LOOKUP lp,[1,1,1,0,1,0,1,1,1,0,1,0,1,1,1,0,1,0,1,0,1,1,1,0],PORTB.5
pauseus 660
next lp
return


FOR lp = 0 TO 2
LOOKUP lp,[$23,$01,$AE],BIN(PORTB.5) ' here should be send the binary string
pauseus 660
next lp
return
but how to realize that ?

Robson

Darrel Taylor
- 18th August 2007, 22:23
It seems odd to be sending data that way, but if you say it works, then ....
lp VAR BYTE
TempB VAR BYTE
BitLoop VAR BYTE

FOR lp = 0 TO 2
LOOKUP lp,[$23,$01,$AE],TempB
FOR BitLoop = 0 to 7
PORTB.5 = TempB.0[BitLoop]
pauseus 660
NEXT BitLoop
next lp
return

Robson
- 18th August 2007, 23:04
Darrel you are amazing ;-)
Only one small bug is inside
the first step will not recognize as show below i used AA 3 times = 1010101010101010
my watching LED is blinking 1st time but no flashing on remote ?
It seems that the PIC eats up the first bit. And at last the remote should be off but itīs always on. Looks like shifted one bit.
I tried something but cannot find where the problem is.


lp var byte
remote var PORTB.5
TempB VAR BYTE
BitLoop VAR Byte
ledstepper var PORTB.4 ' Control each step

remote = 0
LEDSTEPPER = 0


FOR lp = 0 TO 2
LOOKUP lp,[$AA,$AA,$AA],TempB
FOR BitLoop = 0 to 7
remote = TempB.0[BitLoop]
Pulsout ledstepper,1000
pause 500
NEXT BitLoop
next lp
end

Instead the sequence looks like that:
ledstepper : 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0
remotepin : 0 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 1

Robson
- 18th August 2007, 23:54
I got it.


FOR BitLoopH = 7 to 0 step -1
Pulsout ledstepper,1000
remote = tempb.0[bitloopH]
pause 500
NEXT BitLoopH


Thanks Darrel, sometimes is it good to try some things by myself. That was really bad to use $AA as testbyte. ;-)
The string comes from the last position.

Darrel Taylor
- 19th August 2007, 00:16
Ah Ha! MSB first.

I should have seen that.

Great, glad you figured it out.

regards,

Robson
- 19th August 2007, 02:16
@Darrel
thatīs really fantastic. Iīve shrunked my data code about the strings from 2,7kB to :
just listen 195 "BYTE" using the Eeprom now. Not the complete program but only the data space, which tooks a lot of memory. It was not possible before to store that whole data on the eeprom. But with this great snip of code, knowing now to realize for coming projects, can i save a hugh amount of space of my lovely PIC ;-)
I just canīt believe it now.
One complete string takes 31 byteīs of the eeprom space. I have 8 instructions to send by pressing a key.
Only for demonstration :


volume-:
a = 0
gosub loop

volume+:
a = $20
gosub loop

undershort:
a = $40
gosub loop

underlong:
a = $60
gosub loop

sourceleft:
a = $80
gosub loop

sourceright:
a = $A0
gosub loop

scrollup:
a = $C0
gosub loop

scrolldown:
a = $E0
gosub loop

end

loop:
b = a + $1D
FOR lp = a TO b
read lp, tempb
FOR BitLoopH = 7 to 0 step -1
remote = tempb.0[BitloopH]
pauseus 660
NEXT BitLoopH
next lp
PORTB.4 = 1 ' Control LED if sequence finished
pause 1000
PORTB.4 = 0
return


Maybe i forgot something ...
But itīs still working... So i donīt forget something :-)
Big thanks to YOU