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
Decimal to BCD conversion
- 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
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)
Also this may help for the following line bcd = bcd + units
A more reliable yet slightly larger version of this would be
[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...]
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.
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
Code: Select all
bcd = bcd | units
Code: Select all
bcd = (bcd & 0xf0) | (units & 0x0f)
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
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.
Regards Ben Rowland - MatrixTSL
Flowcode Product Page - Flowcode Help Wiki - Flowcode Examples - Flowcode Blog - Flowcode Course - My YouTube Channel
Flowcode Product Page - Flowcode Help Wiki - Flowcode Examples - Flowcode Blog - Flowcode Course - My YouTube Channel