www.piclist.com/techref/microchip/math/index.htm?key=math

> Post to the site and the list...

> On the site, just be sure to select code listing and put the HTML tags

> <PRE>

> before the code and

> </PRE>

> after it.

OK, here 'tis. I don't know anything about HTML tags - are the <> part

of what is typed in? I will post to the site as soon as I hear back.

Thanks for the help.

/*

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

}

--

http://www.piclist.com hint: PICList Posts must start with ONE topic:

[PIC]:,[SX]:,[AVR]: ->uP ONLY! [EE]:,[OT]: ->Other [BUY]:,[AD]: ->Ads

See also: www.piclist.com/techref/microchip/math/index.htm?key=math