 ## Binary to BCD unpacked 17 bit to 5 full digits

/*

```- based on the 16 bit to 5 digit conversion written by John Payson
& explained by Scott Dattalo
- this version adds the extra bit so that the 5 digit display can
- want to express the b_i's in terms of the a_i's for the equations:
N = a4*16^4 + a3*16^3 + a2*16^2 + a1*16 + a0
N = b4*10^4 + b3*10^3 + b2*10^2 + b1*10 + b0
the solution coded below:
b0 = a0 - (4 * (a4 + a3 + a2 + a1)) - 20
b1 = (4 * a4) + (6 * a2) + (2 * a1) - 198
b2 = (5 * a4) + a3 + (2 * a2) - 140
b3 = (5 * a4) + (4 * a3) - 144
b4 = (6 * a4) + 16
- written in CCS C
- since ram was plentiful, I used a 3 byte number - only one bit
of the extra byte is actually required.  I also used three bytes
of scratchpad ram to increase the efficiency of the compiled code.
The equations can be coded in C exactly as shown above, but that
doesn't take advantage of common terms & progressive multiplies.
- even with just the one extra bit, the incoming number  can exceed
99,999, so it needs to be checked.  This code doesn't show that.
- CCS C is limited to 16 bit as the largest integer data type.  To
use a 24 bit number, I use an array to make sure the bytes are
consecutive.  This function operates on the array as a global,
because I think that is more efficient than passing arrays to
the function.
- the code could be quite a bit smaller - in John's original, the
portion of the code that isolated the nibbles and calculated the
digits took 32 instructions.  If I code that solution in C, using
two scratchpad bytes to improve things, it compiles to 64 instructions.
This version compiles to 85 instructions, so I'm sure an asm whiz
could show quite an improvement.  The C version is still much faster
than the the original way I did this, and is adequate for what I need.
*/

unsigned int  TenK, Thou, Hund, Tens, Ones;
unsigned int  num;

void BIN2DEC(void)
{
// operates on global variables:
// set num- (lo-hi) before calling
// returns with TenK...Ones set from 0-9

unsigned int a0, a1, a2, a3, a4;
unsigned int t1, t2, t3;			// scratchpad variables

// isolate the 4 bit nibbles
// for the purposes of this routine, a4 can be only 0 or 1
a4 = num;
a3 = num / 16;
a2 = num & 0b00001111;
a1 = num / 16;
a0 = num & 0b00001111;

// calculate the decimal digits (the b_i's are expressed here
// as TenK...Ones, as in the original)
// all are negative, except TenK
t1 = 4 * a4;
Ones = a0 - t1 - 20;
t2 = 2 * a1;
Tens = t1 + t2 - 198;
t3 = 2 * a2;
Hund = a3 + t3 - 140;
t3 = t3 * 2;              // now = 4 * a2
t2 = t2 * 2;              // now = 4 * a1
Ones = Ones - t3 - t2;
t3 = t3 + a2 + a2;        // now = 6 * a2
Tens = Tens + t3;         // Tens done
t1 = t1 + a4;             // now = 5 * a4
Hund = Hund + t1;
t3 = 4 * a3;
Thou = t1 + t3 - 144;     // Thou done
Ones = Ones - t3;         // Ones done
TenK = t1 + a4 + 16;      // Tenk done

// "normalize" the digits - this asm code was
// copied directly from the original
#asm
movlw   0x0A
Lb1:
decf    Tens,f
btfss   3,0
goto   Lb1
Lb2:
decf    Hund,f
btfss   3,0
goto   Lb2
Lb3:
decf    Thou,f
btfss   3,0
goto   Lb3
Lb4:
decf    TenK,f
btfss   3,0
goto   Lb4
#endasm
}
```
~NOSPAM~anhaltspam at -Remove-zoominternet.net
I tweaked Mike Mansheim's algorithm a little and would like to offer this code:
PICLIST Submission
17BIN2BCD
```;February 6, 2018
;John Anhalt
;41 Tcy to Normalize
;Enhanced mid-range PIC16F1829
;"#" marks final polynomial for each BCD

cblock    0x20
binU
binH
binL
ones
tens
hund
thou
tenK
endc

org 0x0000
nop
bra  Start
Start

DoA2
movf      binH,w
andlw     0x0F      ;w=a2
movwf     ones      ;ones=a2
movwf     tens      ;tens=a2
lslf      WREG      ;w=2a2
movwf     hund      ;hund=2a2-57
DoA4
movf      binU,w
movwf     thou      ;thou=a4
lslf      WREG      ;w=2a4
movwf     tenK      ;tenK=2a4
lslf      WREG      ;w=4a4
DoA3
swapf     binH,w
andlw     0x0F      ;w=a3
lslf      WREG      ;w=2a3
lslf      WREG      ;w=4a3
DoA1                     ;same as Payson/Dattalo, binL preserved
swapf     binL,w
andlw     0x0F      ;w=a1
lslf      tens,f    ;tens=4a4+6a2+2a1-128,C is set #
rlf       ones,f    ;moves C into ones
comf      ones,f
rlf       ones,f    ;w/ carry ones is complement 2 negated
movf      binL,w
andlw     0x0F      ;w=a0
Normalize
movlw     0x0A
Lb1
decf      tens,f
btfss     STATUS,0
bra       Lb1
Lb2
decf      hund,f
btfss     STATUS,0
bra       Lb2
Lb3
decf      thou,f
btfss     STATUS,0
bra       Lb3
Lb4
decf      tenK,f
btfss     STATUS,0
bra       Lb4
bra       Done
Done
```
Explanation (Based on Mike Mansheim)

Consider a 17-bit binary (N)= a4(2^16) +a3(2^12) + a2(2^8) + a1(2^4) +a0(2^0) where a_i is a 4-bit nibble.

A set of polynomials can be derived where b_i is the BCD digit:

b0(ones) = a0-4(a4+a3+a2+a1)
b1 (tens) = 4a4+6s2+2a1
b2(hund) = 5a4+a3+2a2
b3(thou) = 5a4+4a3
b4(tenK) = 6a4

Now include adjustment to ensure each equation, except tenK, is negative:

b0(ones) = a0-4(a4+a3+a2+a1)-20
b1 (tens) = 4a4+6s2+2a1-128
b2(hund) = 5a4+a3+2a2-57
b3(thou) = 5a4+4a3-73
b4(tenK) = 6a4+8

The above polynomials are slightly different from those derived by Mansheim and result in code that runs a little faster. Since this set is for only 17 bits, the maximum value for a_4 was 1. John Anhalt
+

 file: /Techref/microchip/math/radix/b2bu-17b5d.htm, 7KB, , updated: 2018/2/8 10:37, local time: 2021/12/7 07:14, TOP NEW HELP FIND:  54.158.251.104:LOG IN

 ©2021 These pages are served without commercial sponsorship. (No popup ads, etc...).Bandwidth abuse increases hosting cost forcing sponsorship or shutdown. This server aggressively defends against automated copying for any reason including offline viewing, duplication, etc... Please respect this requirement and DO NOT RIP THIS SITE. Questions?Please DO link to this page! Digg it! / MAKE! PIC Microcontoller Radix Math Method - Binary to BCD unpacked 17 bit to 5 full digits

After you find an appropriate page, you are invited to your to this massmind site! (posts will be visible only to you before review) Just type a nice message (short messages are blocked as spam) in the box and press the Post button. (HTML welcomed, but not the <A tag: Instead, use the link box to link to another page. A tutorial is available Members can login to post directly, become page editors, and be credited for their posts.

Attn spammers: All posts are reviewed before being made visible to anyone other than the poster.
 Did you find what you needed? "No. I'm looking for: " "No. Take me to the search page." "No. Take me to the top so I can drill down by catagory" "No. I'm willing to pay for help, please refer me to a qualified consultant" "No. But I'm interested. me at when this page is expanded." PICList 2021 contributors: o List host: MIT, Site host massmind.org, Top posters @20211207 Neil, Harold Hallikainen, Alan Pearce, RussellMc, Bob Blick, Allen Mulvey, Justin Richards, Dwayne Reid, madscientistatlarge, Sean Breheny, * Page Editors: James Newton, David Cary, and YOU! * Roman Black of Black Robotics donates from sales of Linistep stepper controller kits. * Ashley Roll of Digital Nemesis donates from sales of RCL-1 RS232 to TTL converters. * Monthly Subscribers: Gregg Rew. on-going support is MOST appreciated! * Contributors: Richard Seriani, Sr.

.