News:

Let's find out together what makes a PIC Tick!

Main Menu

Double Dabble or Similar Procedure

Started by GDeSantis, Sep 16, 2025, 12:27 AM

Previous topic - Next topic

GDeSantis

I am working on several projects that involve processing binary output data from 16-Bit and 24-BIT ADCs.  Ideally, I would like to use a Double Dabble scheme to translate binary to BCD data.

Before coding my version of a Positron BASIC Double Dabble algorithm, if anyone can advise regarding a similar procedure, that would be greatly appreciated.

Double Dabble Example:
https://www.youtube.com/watch?v=mhzbMYY0P8w

top204

#1
If it is for general purpose conversions for decimal display purposes, you can let the compiler do most of the work.

For example, the code listing below may suit your needs:

'
'   /\\\\\\\\\
'  /\\\///////\\\
'  \/\\\     \/\\\                                                 /\\\          /\\\
'   \/\\\\\\\\\\\/        /\\\\\     /\\\\\\\\\\     /\\\\\\\\   /\\\\\\\\\\\  /\\\\\\\\\\\  /\\\\\\\\\
'    \/\\\//////\\\      /\\\///\\\  \/\\\//////    /\\\/////\\\ \////\\\////  \////\\\////  \////////\\\
'     \/\\\    \//\\\    /\\\  \//\\\ \/\\\\\\\\\\  /\\\\\\\\\\\     \/\\\         \/\\\        /\\\\\\\\\\
'      \/\\\     \//\\\  \//\\\  /\\\  \////////\\\ \//\\///////      \/\\\ /\\     \/\\\ /\\   /\\\/////\\\
'       \/\\\      \//\\\  \///\\\\\/    /\\\\\\\\\\  \//\\\\\\\\\\    \//\\\\\      \//\\\\\   \//\\\\\\\\/\\
'        \///        \///     \/////     \//////////    \//////////      \/////        \/////     \////////\//
'                                  Let's find out together what makes pMin PIC Tick!
'
' Demonstrate binary to BCD conversion.
' Written for the Positron8 compiler by Les Johnson.
'
    Device = 18F25K20                                       ' Tell the compiler what device to compile for
    Declare Xtal = 16                                       ' Tell the compiler what frequency the device will be operating at (in MHz)
'
' Setup USART1
'
    Declare Hserial_Baud = 9600                             ' Set the Baud rate to 9600
    Declare HRSOut1_Pin  = PORTC.6                          ' Set the TX pin
'
' Create global variables here
'       
    Dim Wordin   As Word                                    ' Holds the value to convert
    Dim MyString As String * 24                             ' Holds the decimal result of the integer to ASCII conversion
   
'----------------------------------------------------------------------------
' The main program starts here
' Convert 16-bit binary to BCD, and transmit the results to a serial terminal.
'
Main:
    Wordin = 12345                                                  ' Set the value to convert to BCD
   
    MyString = Str$(Dec5, Wordin)                                   ' Convert the integer value into 5-digit ASCII
    HRSOut1Ln Dec MyString#0, " is BCD ", Bin8 BinToBCD(MyString#0) ' Convert the first digit's value into BCD, and transmit it
    HRSOut1Ln Dec MyString#1, " is BCD ", Bin8 BinToBCD(MyString#1) ' Convert the second digit's value into BCD, and transmit it
    HRSOut1Ln Dec MyString#2, " is BCD ", Bin8 BinToBCD(MyString#2) ' Convert the third digit's value into BCD, and transmit it
    HRSOut1Ln Dec MyString#3, " is BCD ", Bin8 BinToBCD(MyString#3) ' Convert the fourth digit's value into BCD, and transmit it
    HRSOut1Ln Dec MyString#4, " is BCD ", Bin8 BinToBCD(MyString#4) ' Convert the fifth digit's value into BCD, and transmit it
   
'----------------------------------------------------------------------------
' Binary to BCD converter
' Input     : pDecIn holds the 8-bit binary value
' Output    : Returns the BCD equivalent of pDecIn
' Notes     : Result = ((pDecIn / 10) << 4) + (pDecIn // 10)
'
Proc BinToBCD(pDecIn As Byte), Byte
    Result = (pDecIn / 10) << 4
    Result = Result + (pDecIn // 10)
EndProc

On the serial terminal, it will display:

49 is BCD 01001001
50 is BCD 01010000
51 is BCD 01010001
52 is BCD 01010010
53 is BCD 01010011


Which are the BCD value equivalents for the binary values.

If it is for digits only, subtract 48 from the String element, and that will give the digit value. i.e. BinToBCD(MyString#0 - 48).

GDeSantis

#2
Thanks for the quick response.  The program looks impressive and I will check it out.

Frizie

Ohm sweet Ohm | www.picbasic.nl

top204

That is such a good looking web site Frizie.

Frizie

Thanks Les.
I haven't updated the website in decades(!).
Most program examples still show ALL_DIGITAL = TRUE and are missing the DECLARE XTAL statement.
I simply don't have the time to update the website, unfortunately...  :-\
Ohm sweet Ohm | www.picbasic.nl