Searching \ for '[PIC]: Percentage calculations' in subject line. ()
Make payments with PayPal - it's fast, free and secure! Help us get a faster server
FAQ page: www.piclist.com/techref/microchip/devices.htm?key=pic
Search entire site for: 'Percentage calculations'.

Exact match. Not showing close matches.
PICList Thread
'[PIC]: Percentage calculations'
2002\10\20@055230 by XygaX

flavicon
face
Morning everyone I'm back after a long brake...

I am having trouble with mchip maths! (nothing new there) I need to
divide two 15 bit numbers and generate a percentage as a result the
result will always be in the range 0 to 1 as the divisor is always
greater than or equal to the dividend which is the correct section of
the math's library to use or does anybody have a better idea tried AN575
and I cant make any sence of it at all

TiA Steve....

--
http://www.piclist.com hint: The list server can filter out subtopics
(like ads or off topics) for you. See http://www.piclist.com/#topics


2002\10\20@105151 by Scott Dattalo

face
flavicon
face
On Sun, 20 Oct 2002, XygaX wrote:

> Morning everyone I'm back after a long brake...
>
> I am having trouble with mchip maths! (nothing new there) I need to
> divide two 15 bit numbers and generate a percentage as a result the
> result will always be in the range 0 to 1 as the divisor is always
> greater than or equal to the dividend which is the correct section of
> the math's library to use or does anybody have a better idea tried AN575
> and I cant make any sence of it at all


In C, you'd want:

float num;
float den;
float percent;

...

percent = 100 * num/den;


For integer arithmetic (more conducive to a PIC) most people will probably
tell you to do something like this:

uint16 num;
uint16 den;
uint16 percent;
uint32 temp;

 temp = num;
 temp = temp * 2^16;
 temp = temp / den;

 temp = temp * 100;

 temp = temp / 2^16;


In other words, scale the numerator by multiplying it by a big number and
thus ensuring that it's greater than the denominator. Then divide this big
number by the denominator. Now convert to a percentage by multiplying by
100. As a final step, remove the scaling factor by dividing it out.

On a PIC however (which I assume is your target controller [can't tell
anymore on this list]), you'll probably want to do something like this:

uint16 num;
uint16 den;
uint8 percent;

 for(i=0; i<8; i++) {

   percent = percent << 1;

   if(num >= den) {
     den = den - num;
     percent = percent | 1;
   }

   den = den / 2;

 }

If you look at:
http://www.dattalo.com/technical/software/pic/arctan.asm

You'll find a "fractional division" routine for 8-bit numbers that
implements this concept:

;-------------------
;Fractional division
;
; Given x,y this routine finds:
;  a = 256 * y / x
;

   movlw  8    ;number of bits in the result
   movwf  cnt
   clrf   a    ; the result
   movf   x,w

L1:

   clrc
   rlf    y,f   ;if msb of y is set we know x<y
   rlf    a,f   ;and that the lsb of 'a' should be set
   subwf  y,f   ;But we still need to subtract the
                ;divisor from the dividend just in
                ;case y is less than 256.
   skpnc        ;If y>x, but y<256
    bsf   a,0   ; we still need to set a:0


   btfss  a,0   ;If y<x then we shouldn't have
    addwf y,f   ;done the subtraction

   decfsz cnt,f
    goto  L1

   return


---------

After the division, percent is a number between 0 and 255. The percentage
can be expressed between 0 and 100 by:

  percent = percent * 100/255;

And this is efficiently implemented by:

http://www.piclist.com/techref/microchip/math/scale/8bxpercent.htm



Scott

--
http://www.piclist.com hint: The list server can filter out subtopics
(like ads or off topics) for you. See http://www.piclist.com/#topics


More... (looser matching)
- Last day of these posts
- In 2002 , 2003 only
- Today
- New search...