please dont rip this site

PIC Microcontoller Radix Math Method

Binary to BCD unpacked 17 bit to 5 full digits

From: Mike Mansheim of Graco, Inc.

/*

- 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
  read to 99,999
- 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[3];

void BIN2DEC(void)
{
     // operates on global variables:
     // set num[0]-[3] (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[2];
     a3 = num[1] / 16;
     a2 = num[1] & 0b00001111;
     a1 = num[0] / 16;
     a0 = num[0] & 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:
       addwf   Ones,f
       decf    Tens,f
       btfss   3,0
       goto   Lb1
     Lb2:
       addwf   Tens,f
       decf    Hund,f
       btfss   3,0
       goto   Lb2
     Lb3:
       addwf   Hund,f
       decf    Thou,f
       btfss   3,0
       goto   Lb3
     Lb4:
       addwf   Thou,f
       decf    TenK,f
       btfss   3,0
       goto   Lb4
#endasm
}

See also:


file: /Techref/microchip/math/radix/b2bu-17b5d.htm, 4KB, , updated: 2006/5/19 15:59, local time: 2017/11/20 22:17,
TOP NEW HELP FIND: 
54.198.2.110:LOG IN

 ©2017 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! / 

<A HREF="http://www.piclist.com/techref/microchip/math/radix/b2bu-17b5d.htm"> PIC Microcontoller Radix Math Method - Binary to BCD unpacked 17 bit to 5 full digits</A>

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 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.


Link? Put it here: 
if you want a response, please enter your email address: 
Attn spammers: All posts are reviewed before being made visible to anyone other than the poster.
Did you find what you needed?

  PICList 2017 contributors:
o List host: MIT, Site host massmind.org, Top posters @20171120 RussellMc, Van Horn, David, James Cameron, Sean Breheny, alan.b.pearce, IVP, Neil, David C Brown, Bob Blick, Denny Esterline,
* 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.
 

Welcome to www.piclist.com!

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  .