-->
Page 1 of 2

DEC-to-BCD and BCD-to-DEC conversions

PostPosted: Thu Feb 16, 2017 10:01 am
by trackerj
In the process of working on a new Driver Tutorial for the ESPBasic Series I am looking today to implement some Decimal-to-BCD and BCD-to-Decimal subroutines.

What I have done so far:

1. BCD-to-Decimal subroutine:
Code: Select all    [DEC]
    hv = val >> 4
    hl = val and 15
    val = hv*10 + hl
    wait 


2. Decimal-to-BCD subroutine:
Code: Select all    [BCD]
    d = int( val / 10 )
    d1 = d * 10
    d2 = val - d1
    val = d*16 + d2
    wait


If you have any other idea please feel free to share it, looking forward to see smarter solutions that that. I'm sure they are!

Happy breadboarding,
TJ.

Original Article with Test Program Code: ESPBasic Series - DEC to BCD and BCD to DEC conversions

Re: DEC-to-BCD and BCD-to-DEC conversions

PostPosted: Sun Feb 19, 2017 6:08 pm
by farkas
Function DecToBcd(val) as Byte
DecToBcd=(va/10)*16+va%10
End Function

Function BcdToDec(va) as byte
BcdToDec=(va/16)*10+va%16
End Function

Re: DEC-to-BCD and BCD-to-DEC conversions

PostPosted: Thu Feb 23, 2017 5:44 pm
by neuronesp
Interesting.
I have been looking at Decimal to Binary conversion as to translate for a string to SPI for a DigiPot.
256 steps of the wiper etc.
Any input welcome.

Re: DEC-to-BCD and BCD-to-DEC conversions

PostPosted: Fri Feb 24, 2017 3:11 am
by trackerj
neuronesp wrote:Interesting.
I have been looking at Decimal to Binary conversion as to translate for a string to SPI for a DigiPot.
256 steps of the wiper etc.
Any input welcome.


You can take a look here: ESPBasic decimal-to-binary conversion subroutine code and test program