PDA

View Full Version : BCD conversion



Macgman2000
- 1st August 2005, 16:08
Hello All,

I have 3 bytes that represent a value 122880 . The problem is that the 3bytes are backwards in bit order.......

regular binary 122880 = (msb) 00000001 11100000 00000000

My buffer reads 122880 = (lsb) 00000000 00000111 10000000

How do I take the 3bytes that are backwards in bit order and convert it to BCD or to something I can throw up on an LCD.

Best Regards,
Nick

mister_e
- 1st August 2005, 16:33
it's sounds familiar...
http://www.picbasic.co.uk/forum/showthread.php?t=2117&highlight=reverse+order



regular binary 122880 = (msb) 00000001 11100000 00000000

My buffer reads 122880 = (lsb) 00000000 00000111 10000000


How do you get those bytes??? let's say by serial com... why not... it's possible :)


b0 var byte
b1 var byte
b2 var byte
w1 var byte
w0 var word

hserin [b0,b1,b2]
w1=b2 rev 8
w0.highbyte=b1 rev 8
w0.lowbyte=b0 rev 8
hserout [bin8 w1,bin16 w0]

now i leave you with the pleasure of doing some math and use modifiers like DEC, DIG and all the others.

Bruce
- 1st August 2005, 17:28
Just curious. If you're shifting in data LSB first, and this results in your data
being backwards, what happens if you shift in your data MSB first?

mister_e
- 1st August 2005, 17:35
i had this idea... but since we don't know how and where it come from...

NavMicroSystems
- 1st August 2005, 19:01
Bruce & Steve,

I guess Nick is receiving the bits from an external Master talking:
"Chinese Protocol"

See here (http://www.picbasic.co.uk/forum/showthread.php?t=2103&highlight=caliper) for reference

NavMicroSystems
- 1st August 2005, 19:03
Nick,

how are you getting on with the project?

Macgman2000
- 1st August 2005, 21:13
Hello All,

I have the hardware set up using an LM393 (0v to -1.55v) to shift to standard TTL/CMOS. I am able to see the data by setting the calipers to 1" and analyzing the waveform to get 20480 (which is the resolution per inch) . It is being clocked out LSB to MSB. I have the PIC packing 3bytes but like I said it is backwards.

I have most of the supporting readout code done and tested with dummy variables. The only thing holding me up is flipping the data around and scaling it (scaling factor already worked out). I will fiddle around with your suggestion.

Thanks again!!!

Best Regards,
Nick

NavMicroSystems
- 2nd August 2005, 12:04
Nick, here is a ready to run Code Snippet that does the conversion:



InBuffer VAR BYTE[3]
CounterA VAR BYTE

Main:

' Bits are shifted into an Array of Byte (InBuffer) here (LSB first)
'
' Place your Code to shift the bits in here...


FOR CounterA = 0 TO 2

'Revert the Bit order
InBuffer[CounterA] = Inbuffer[CounterA] REV 8

'Invert captured Data (if not required comment this line out)
InBuffer[CounterA] = InBuffer[CounterA]^$FF

NEXT CounterA

'Output Raw Data to LCD (24 Bits, MSB first)
LCDOUT BIN8 InBuffer[2],Bin8 InBuffer[1],Bin8 InBuffer[0]

GOTO Main

Macgman2000
- 2nd August 2005, 23:23
Thanks Ralph !!!! I will try it out this week.....geez, right now I have an urgent request for a speed controller FETs'n all ....hehehe.

Best Regards,
Nick

NavMicroSystems
- 17th October 2005, 19:24
Hi Nick,

RE: your PM

I haven't found time yet to work on this project.

It certailny is possible in PBP,
a 70-90 kHz clock is not that fast, so a tight loop could do.

Another option is to use the MSSP Module.

I'll have a look into it when I find time.