Decimal to BCD conversion

For Flowcode users to discuss projects, flowcharts, and any other issues related to Flowcode 2 and 3.

Moderators: Benj, Mods

Post Reply
MJU
Posts: 502
Joined: Wed Nov 07, 2007 6:51 pm
Location: Antwerp Belgium
Has thanked: 121 times
Been thanked: 108 times

Decimal to BCD conversion

Post by MJU »

Hi there,

I'm searching for the most economic way to convert decimal number into BCD code.
I'm working with an I2C IC that uses BCD format to store numbers.
I have my own way to convert numers into BCD but I guess that there must be a more "economic" way to convert these numbers.
(economic: in processor use)

Now I do it this way (the variable I need to convert: "start")
start = 59
thens = start/10
units = start MOD 10
bcd = thens << 4
bcd = bcd + units

When I check this, it seems to work.
The five becomes: 0101
The nine becomes: 1001
The variable BCD becomes: 01011001, this is 89 decimal which is good.

Is there a more easy way doing this??
Any help is welcome!
Thanks

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: Decimal to BCD conversion

Post by Benj »

Hello

Your routine looks about spot on. I don't think that there is much improvement to be had there. One thing could be to put the algorithm into a loop so you can convert numbers greater then 99. However this would require an array of output data to store the nibble values.

Also there is an assembler command called swapf that swaps the nibbles. This may be good to replace the bcd = thens << 4.

Eg (My assembler is a bit sketchy, You may want to verify that my code is correct by referring to the BoostC manual)

Code: Select all

asm
{
swapf _thens, 1
}
bcd=thens
Also this may help for the following line bcd = bcd + units

Code: Select all

bcd = bcd | units
A more reliable yet slightly larger version of this would be

Code: Select all

bcd = (bcd & 0xf0) | (units & 0x0f)
[note this would only be used if your start variable had the possibility to stray outside the bounds of 0 - 99]

But these measures are only if you are desperate to reclaim every last possible instruction cycle.

Reduced and optimised version of your code using a C code icon(I think this is as good as it gets but I may be wrong) [...note C code does not simulate...]

Code: Select all

Calc Icon
start = 59

C Code Icon
FCV_BCD = FCV_START / 10;
FCV_UNITS = FCV_START % 10;
asm swapf _FCV_BCD, 1
FCV_BCD = FCV_BCD | FCV_UNITS;

Output Icon PortB
bcd
Everything except for the assembler command can be reproduced in the Flowcode calculation icons.

If you wanted to you could use the custom component to turn this into a function that can be called via a hardware macro. See posts on the forum regarding the custom component.

MJU
Posts: 502
Joined: Wed Nov 07, 2007 6:51 pm
Location: Antwerp Belgium
Has thanked: 121 times
Been thanked: 108 times

Re: Decimal to BCD conversion

Post by MJU »

Thanks Benj!

Post Reply