Sign in

username:

password:



Not a member?

Search piclist



Search tips

Subscribe to piclist



piclist by Keywords

12F675 | 16F628 | 16F84 | 16f877 | 16F877A | 16F88 | 18F458 | ADC | AVR | Bootloader | CAN | CCS | CRC | EAGLE | EEPROM | ICD | ICSP | IDE | JDM | LED | Macros | Microchip | MPLAB | PCB-CAD | PIC10F | Pic12f675 | PIC16F84 | PIC16F84A | PIC16F877 | PIC18 | PIC18F452 | PicBasic | PICC | PICSTART | PWM | RS-485 | RS232 | SMT | SPI | UART | USART | USB | Wireless | Wisp628 | Xilinx

Ads

Discussion Groups

Discussion Groups | Piclist | Division

A discussion group for the PICMicro microcontroller. Also called the Microchip PIC, this list is dedicated to the use and abuse of this fine, simple, microcontroller. Close to topic posts are welcome, ie. general electronics.

Division - Paul - Apr 18 16:39:42 2006

Hi.

I am trying to figure out how to do division using a PIC. Now, i'm
sure it can be done, cause i asume a calculator works the same way.
Has anyone done this before and could offer advice? I'm racking my
brains but getting no where really..

Thanks

to unsubscribe, go to http://www.yahoogroups.com and follow the instructions



(You need to be a member of piclist -- send a blank email to piclist-subscribe@yahoogroups.com )


Re: Division - Leon Heller - Apr 18 16:54:20 2006

----- Original Message -----
From: "Paul"
To:
Sent: Tuesday, April 18, 2006 9:35 PM
Subject: [piclist] Division
> Hi.
>
> I am trying to figure out how to do division using a PIC. Now, i'm
> sure it can be done, cause i asume a calculator works the same way.
> Has anyone done this before and could offer advice? I'm racking my
> brains but getting no where really..

The simplest (and slowest) way is to subtract the divisor inside a loop
whilst incrementing a counter. When the result becomes zero or negative,
exit the loop. The counter value is the result.

Leon

---
[This E-mail has been scanned for viruses but it is your responsibility
to maintain up to date anti virus software on the device that you are
currently using to read this email. ]

to unsubscribe, go to http://www.yahoogroups.com and follow the instructions



(You need to be a member of piclist -- send a blank email to piclist-subscribe@yahoogroups.com )

Re: Division - Paul_Lav - Apr 18 16:54:21 2006

I should really of said this...

Its not really much good to me giving a Answer with a remainder, i need the decimal, or at leat something i can use to get my answer. What i am doing is, i have a channel value and a master value. When the master is at 100% the channel is as it is set. When the master is say 90%, the value of the channel is 90% of its set value. So my equation is:

Channel Result = ( Master / 100 ) * Channel
So if Master = 90 and Channel = 80
( 90 / 100 ) * 80 = 72%

Can this be done on the PIC? Any advice or guidance would be great
----- Original Message -----
From: Paul
To: p...@yahoogroups.com
Sent: Tuesday, April 18, 2006 9:35 PM
Subject: [piclist] Division
Hi.

I am trying to figure out how to do division using a PIC. Now, i'm
sure it can be done, cause i asume a calculator works the same way.
Has anyone done this before and could offer advice? I'm racking my
brains but getting no where really..

Thanks

to unsubscribe, go to http://www.yahoogroups.com and follow the instructions

SPONSORED LINKS Microcontroller Microprocessor Microchip pic
Microcontrollers Picmicro 8051 microprocessor
------------------------------------------------------------------------------
YAHOO! GROUPS LINKS

a..
------------------------------------------------------------------------------


(You need to be a member of piclist -- send a blank email to piclist-subscribe@yahoogroups.com )

Re: Division - Eirik Karlsen - Apr 18 17:09:26 2006

Paul,
in some special cases it can be done very simple;
divide by 2 is a right shift, if you want the remainder you'll have
to save the bits that are shifted out and process them.
Any divide by 2^N (2-4-8-16-32...) can be done this way.
There are some other divisions like /10 that can be done by taking such
shortcuts. These routines typically run very fast.

However for a general purpose routine one will have to implement
a 'pen & paper' method into software. This will somewhat complicated
for someone at your level I guess so have a look at the Microchip's app
notes for math routines.
Watch out as some of these routines are flawed (at least some of the
PIC18 routines).
I have corrected these errors and have over time built up a mathlib that
can do among other
things mul-div-add-sub-negate-bitshifts ++ of positive and negative
numbers 8 to 48bits with or without remainders.
I've found such a mathlib (macros) is essential for future productive
programming.
I've spent hundreds of hours creating and optimizing my mathlib and I
won't give it away (in case you'd ask).
Paul wrote:

> Hi.
>
> I am trying to figure out how to do division using a PIC. Now, i'm
> sure it can be done, cause i asume a calculator works the same way.
> Has anyone done this before and could offer advice? I'm racking my
> brains but getting no where really..
>
> Thanks
> to unsubscribe, go to http://www.yahoogroups.com and follow the
> instructions
>
> SPONSORED LINKS
>
Microcontroller Microprocessor Microchip pic

Microcontrollers Picmicro 8051 microprocessor

> -----------------------------------------------------------------------
> YAHOO! GROUPS LINKS
>
> +
>
> -----------------------------------------------------------------------
>
--
*******************************************
VISIT MY HOME PAGE:

LAST UPDATED: 23/08/2003
*******************************************
Regards
Eirik Karlsen


(You need to be a member of piclist -- send a blank email to piclist-subscribe@yahoogroups.com )

Re: Division - Harold Hallikainen - Apr 18 17:36:15 2006


> I should really of said this...
>
> Its not really much good to me giving a Answer with a remainder, i need
> the decimal, or at leat something i can use to get my answer. What i am
> doing is, i have a channel value and a master value. When the master is at
> 100% the channel is as it is set. When the master is say 90%, the value of
> the channel is 90% of its set value. So my equation is:
>
> Channel Result = ( Master / 100 ) * Channel
> So if Master = 90 and Channel = 80
> ( 90 / 100 ) * 80 = 72%
>

This is similar to my "scale" function, below. This is for a PIC18 and I
call it in C. Note that "full scale" is 0xff for both the "master" and the
"channel."

Harold

scale
; arg1*arg2/255. Arguments are on stack pointed to by FSR1. Args are at
FSR1-1 and FSR1-2
movf POSTDEC1,w ; gets garbage.
movf POSTDEC1,w ; gets arg and decrements FSR1 to point to next arg
mulwf INDF1 ; prod=indf1*w = product of args
movf PREINC1,w ; garbage in w, but restoring FSR1
movf PREINC1,w ; more garbage, but FSR1 is restored
; Now divide prod by 255 using the trick at
; http://www.infosite.com/~jkeyzer/piclist/1999/Oct/0049.html
movf PRODH,w ; Change /256 to /255
addlw 0x80
btfsc STATUS,C ; skpnc
incf PRODH
addwf PRODL,f
btfsc STATUS,C ;skpnc
incf PRODH
movf PRODH,w ; Copy result to w where 8 bit results are returned
return

--
FCC Rules Updated Daily at http://www.hallikainen.com
to unsubscribe, go to http://www.yahoogroups.com and follow the instructions



(You need to be a member of piclist -- send a blank email to piclist-subscribe@yahoogroups.com )

Re: Division - Leon Heller - Apr 18 18:09:13 2006

----- Original Message -----
From: Paul_Lav
To: p...@yahoogroups.com
Sent: Tuesday, April 18, 2006 9:54 PM
Subject: Re: [piclist] Division
I should really of said this...

Its not really much good to me giving a Answer with a remainder, i need the decimal, or at leat something i can use to get my answer. What i am doing is, i have a channel value and a master value. When the master is at 100% the channel is as it is set. When the master is say 90%, the value of the channel is 90% of its set value. So my equation is:

Channel Result = ( Master / 100 ) * Channel
So if Master = 90 and Channel = 80
( 90 / 100 ) * 80 = 72%

Can this be done on the PIC? Any advice or guidance would be great

Of course, you just need the Microchip fixed-point routines. Someone on the Microchip support forum has tidied them up, his version is available for downloading.

Leon


(You need to be a member of piclist -- send a blank email to piclist-subscribe@yahoogroups.com )

Re: Division - Daryl - Apr 18 22:41:24 2006


if your only talking about whole percent numbers then just use your
formula and divide by 100 last.

EX: Channel Result = (Master * Channel ) / 100
= 90 * 80
72 = 7200 / 100

some of the other methods other people have said will work too
subtraction loop --- takes too long, watch out for zero,
negative, repeating and non repeating decimals
floating point routine - some compilers have built-in routines
but use alot of flash memory, Micro C 4K+

*Best method for doing alot of floating point math is to
use a external floating point processor - they are fast, easy
to use, low overhead and they will do graphs too.
check out the uM-FPU by Micromega
http://www.sparkfun.com/commerce/product_info.php?products_id=302

At 03:54 PM 4/18/2006, you wrote:
>I should really of said this...
>
>Its not really much good to me giving a Answer with a remainder, i
>need the decimal, or at leat something i can use to get my answer.
>What i am doing is, i have a channel value and a master value. When
>the master is at 100% the channel is as it is set. When the master
>is say 90%, the value of the channel is 90% of its set value. So my
>equation is:
>
>Channel Result = ( Master / 100 ) * Channel
>So if Master = 90 and Channel = 80
>( 90 / 100 ) * 80 = 72%
>
>Can this be done on the PIC? Any advice or guidance would be great
>----- Original Message -----
>From: Paul
>To: p...@yahoogroups.com
>Sent: Tuesday, April 18, 2006 9:35 PM
>Subject: [piclist] Division
>
>Hi.
>
>I am trying to figure out how to do division using a PIC. Now, i'm
>sure it can be done, cause i asume a calculator works the same way.
>Has anyone done this before and could offer advice? I'm racking my
>brains but getting no where really..
>
>Thanks
>to unsubscribe, go to
>http://www.yahoogroups.com and follow
>the instructions
>
>----------
>>Yahoo! Terms of Service.
>
>----------



(You need to be a member of piclist -- send a blank email to piclist-subscribe@yahoogroups.com )

Re: Division - dlc - Apr 18 23:34:37 2006

Or the Al Williams PAK series of co-processors are reasonable and VERY
capable: http://www.awce.com/pak1.htm

DLC

Daryl wrote:
>
> if your only talking about whole percent numbers then just use your
> formula and divide by 100 last.
>
> EX: Channel Result = (Master * Channel ) / 100
> = 90 * 80
> 72 = 7200 / 100
>
> some of the other methods other people have said will work too
> subtraction loop --- takes too long, watch out for zero, negative,
> repeating and non repeating decimals
> floating point routine - some compilers have built-in routines but
> use alot of flash memory, Micro C 4K+
>
> *Best method for doing alot of floating point math is to
> use a external floating point processor - they are fast, easy to
> use, low overhead and they will do graphs too.
> check out the uM-FPU by Micromega
> http://www.sparkfun.com/commerce/product_info.php?products_id=302
>
> At 03:54 PM 4/18/2006, you wrote:
>
>> I should really of said this...
>>
>> Its not really much good to me giving a Answer with a remainder, i
>> need the decimal, or at leat something i can use to get my answer.
>> What i am doing is, i have a channel value and a master value. When
>> the master is at 100% the channel is as it is set. When the master is
>> say 90%, the value of the channel is 90% of its set value. So my
>> equation is:
>>
>> Channel Result = ( Master / 100 ) * Channel
>> So if Master = 90 and Channel = 80
>> ( 90 / 100 ) * 80 = 72%
>>
>> Can this be done on the PIC? Any advice or guidance would be great
>>
>> ----- Original Message -----
>> From: Paul
>> To: p...@yahoogroups.com
>> Sent: Tuesday, April 18, 2006 9:35 PM
>> Subject: [piclist] Division
>>
>> Hi.
>>
>> I am trying to figure out how to do division using a PIC. Now, i'm
>> sure it can be done, cause i asume a calculator works the same way.
>> Has anyone done this before and could offer advice? I'm racking my
>> brains but getting no where really..
>>
>> Thanks
>> to unsubscribe, go to http://www.yahoogroups.com and follow the
>> instructions
>> ------------------------------------------------------------------------
>> >.
>> ------------------------------------------------------------------------
>
> to unsubscribe, go to http://www.yahoogroups.com and follow the
> instructions
> ------------------------------------------------------------------------
> >.
> ------------------------------------------------------------------------
>

--
-------------------------------------------------
Dennis Clark TTT Enterprises
www.techtoystoday.com
-------------------------------------------------
to unsubscribe, go to http://www.yahoogroups.com and follow the instructions



(You need to be a member of piclist -- send a blank email to piclist-subscribe@yahoogroups.com )

Re: Division - rj_satterlee - Apr 19 1:57:21 2006

--- In p...@yahoogroups.com, "Paul" wrote:
>
> Hi.
>
> I am trying to figure out how to do division using a PIC. Now, i'm
> sure it can be done, cause i asume a calculator works the same way.
> Has anyone done this before and could offer advice? I'm racking my
> brains but getting no where really..
>
> Thanks
>
Hiya-

Microchip has an app note on the subject. You can certianly check
there for information. I didn't really find the algorythums very
usefull, but YMMV.

Integer divison is similar to a simple integer multiply. Except,
I would suggest that you do a shift left of the divisor while keeping
track of the number of bits you have shifted left. This is
"scaling" the divisor (finding the msbit of the answer). In a
standard multiply, one does not have to do this. But you kinda,
gotta do something like this. For an integer division for binary
arithmetic you might find the following usefull.

Shift the divisor left a bit (while shifting a "partial" answer) left
one bit.

Subtract the scaled divisor from the dividend and see if the result
is positive or negative. If the result was
positive, shift left again and try again. If negative, the we
add back the scaled partial divisor back to the dividend.

You have finished "scaling" the divisor.

Start a shift right loop. Clear out the answer. Subtract the
divisor from the dividend. If the subraction yields a positive
number add the "partial" answer into the answer.
If negative, do NOT add the "partial" answer into answer. Add
the shifted dividend back into the divisor so the divisor is
back to the original value before the last subtraction.
Shift both the divisor and the "partial" answer right one bit.

Continue again until the divisor is shifted right back to the
original position. At this point, all the "partials" that have
been added to the answer (or you could just or them, you don't
need to "add" them). Any remainder with be what was left in
the original dividend.

So, there 'ya go.

I've done 16 bit integer divides and they can take awhile, like
a couple thousand instructions on the pic for worst cases.
So, it's not the speediest, but it certianly can be done!

Hope this helps!

Cheers,

Rich S.

to unsubscribe, go to http://www.yahoogroups.com and follow the instructions



(You need to be a member of piclist -- send a blank email to piclist-subscribe@yahoogroups.com )

Re: Division - Paul - Apr 19 2:43:42 2006

THanks for all ya replies. I shall look into your comments. Bit
confusing at the moment!

Cheers,
Paul

--- In p...@yahoogroups.com, "Paul_Lav" wrote:
>
> I should really of said this...
>
> Its not really much good to me giving a Answer with a remainder, i
need the decimal, or at leat something i can use to get my answer.
What i am doing is, i have a channel value and a master value. When
the master is at 100% the channel is as it is set. When the master
is say 90%, the value of the channel is 90% of its set value. So my
equation is:
>
> Channel Result = ( Master / 100 ) * Channel
> So if Master = 90 and Channel = 80
> ( 90 / 100 ) * 80 = 72%
>
> Can this be done on the PIC? Any advice or guidance would be great
> ----- Original Message -----
> From: Paul
> To: p...@yahoogroups.com
> Sent: Tuesday, April 18, 2006 9:35 PM
> Subject: [piclist] Division
> Hi.
>
> I am trying to figure out how to do division using a PIC. Now,
i'm
> sure it can be done, cause i asume a calculator works the same
way.
> Has anyone done this before and could offer advice? I'm racking
my
> brains but getting no where really..
>
> Thanks
> to unsubscribe, go to http://www.yahoogroups.com and follow the
instructions
>
> SPONSORED LINKS Microcontroller Microprocessor Microchip pic
> Microcontrollers Picmicro 8051 microprocessor
> -------------------------------------------------------------------
-----------
>



(You need to be a member of piclist -- send a blank email to piclist-subscribe@yahoogroups.com )