Searching \ for '[PIC]: Scaling numbers' in subject line. ()
Help us get a faster server
FAQ page: www.piclist.com/techref/microchip/devices.htm?key=pic
Search entire site for: 'Scaling numbers'.

Exact match. Not showing close matches.
'[PIC]: Scaling numbers'
2001\11\24@144018 by

Hi all. Just as a quick aside, I hate the American holidays...the list
slows down too much. I hate it when people go tend to their "real
lives".

Anyway, I have a question about scaling. Basically what I want to do is
to have a few (8-32 maybe) registers with 8 bit values in them. Then,
based on another 8 bit value I want to scale them. Basically, the second
register becomes a percentage multiplication factor. So if I have a
value of d'204' in the first register, and a value of d'128' in the
second (50%), after the operation I want a result of d'102' (half of the
original value). It can be in a second register. At first I though of
just using multiplication routines, but I can't figure how to multiply
by less than 1 (ie 0.5). I had a flash that maybe this could all be
accomplished by shifting bits, but I don't know how. Any pointers to
info would be great!

Thanks,

Josh Koffman
--
A common mistake that people make when trying to design something
completely foolproof is to underestimate the ingenuity of complete
fools.

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

Yep, just shift right one place and you effectivley divide by two.

Regards,

`                    Jim
{Original Message removed}
Hi

Basically you want to multiply your number by 102 and then divide it by 256.

y = (x * % byte)/256

An 8 bit by 8 bit multiply would give you a 16 bit answer.  You can divide
by 256 by just using the high byte and discarding the low byte.

204 * 128 == 26112 (6600 Hex)  66 hex is 102 decimal.

MChip has some multiply routines on their site.

Dave

{Original Message removed}
> > just using multiplication routines, but I can't figure how to multiply
> > by less than 1 (ie 0.5). I had a flash that maybe this could all be
> > accomplished by shifting bits, but I don't know how. Any pointers to
> > info would be great!

I can't get through to the site (?!?) at the moment but I'm pretty sure
percentages are dealt with in the maths section of http://www.piclist.com
I know it's come up a couple of times in the mail group and think
routines were posted

Basically what you do is not think of the multiplier as a fraction. Do
the multiplication anyway (to get a 16 or 24 bit result), then calculate
how many significant places you want to keep. Use shifts or base10
divisions to get the result into the correct decade for the true answer

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

On Sat, 24 Nov 2001, Josh Koffman wrote:

> Hi all. Just as a quick aside, I hate the American holidays...the list
> slows down too much. I hate it when people go tend to their "real
> lives".
>
> Anyway, I have a question about scaling. Basically what I want to do is
> to have a few (8-32 maybe) registers with 8 bit values in them. Then,
> based on another 8 bit value I want to scale them. Basically, the second
> register becomes a percentage multiplication factor. So if I have a
> value of d'204' in the first register, and a value of d'128' in the
> second (50%), after the operation I want a result of d'102' (half of the
> original value). It can be in a second register. At first I though of
> just using multiplication routines, but I can't figure how to multiply
> by less than 1 (ie 0.5). I had a flash that maybe this could all be
> accomplished by shifting bits, but I don't know how. Any pointers to
> info would be great!

In general, if you wish to multiply by a "fraction" you'll need to convert
it into a format suitable for the PIC. Suppose your fraction is:

f = X/Y

and you want to multiply some register by f. If you only are interested in
8-bits of precision then you can convert f into a fraction of 256:

X       b
----- = -----
Y      256

or,

b = 256 * X / Y

Note that b is number between 0 and 255 and can be fed into any 8-bit
multiplication routine. After the multiplication, you can divide the
product by 256 and you'll have your answer. Of course, "division" by 256
is trivial.

Often time you know before hand the value of Y. Thus you can precompute
the 256/Y factor. However, if not done carefully you can inadvertantly
throw away precision. For example, if Y is 10 then 256/Y is 25.6. What do
you do with that .6? Well it depends...

If you want to see an optimized "scaling" routine used to convert numbers
into percentages, then look here:

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

Josh,

When you have finished the lighting desk ( I am guessing this is what you
are doing from the questions) please post it, as lots of designs these days
are for PC controlled, and some of us love faders still :o)

Basically what you do is multiply the two numbers, which gives a 17 bit
result and then use the top 8 bits, if you want to make life easier, just
subtract 1 from one of the numbers and multiply, then it will always be a 16
bit result.

Hope this helps,

Regards,

Kat.

____________________________________________________________________________
/"\   ASCII Ribbon Campaign  |        K.A.Q. Electronics
\ / - NO HTML/RTF in e-mail | Software and Electronic Engineering
X  - NO Word docs in e-mail  |      Perth Western Australia
/ \                                            |        Ph +61 419 923 731
____________________________________________________________________________

{Original Message removed}
Kathy Quinlan <PICLISTmitvma.mit.edu> wrote:

> Basically what you do is multiply the two [8-bit] numbers, which
> gives a 17 bit result and then use the top 8 bits, if you want to
> make life easier, just subtract 1 from one of the numbers and
> multiply, then it will always be a 16 bit result.

Kat:

There's no need to subtract 1 from either number before the
multiplication; the product of two 8-bit numbers ALWAYS fits in 16
bits.

-Andy

=== Andrew Warren -- aiwcypress.com
=== Principal Design Engineer
=== Cypress Semiconductor Corporation
===
=== Opinions expressed above do not
=== necessarily represent those of
=== Cypress Semiconductor Corporation

--
http://www.piclist.com#nomail Going offline? Don't AutoReply us!
email listservmitvma.mit.edu with SET PICList DIGEST in the body

brain fart

sorry guys, max number for 8 bits is 255 not 256 (should stop doing all
nighters or drink more coffee)

Regards,

Kat.

____________________________________________________________________________
/"\   ASCII Ribbon Campaign  |        K.A.Q. Electronics
\ / - NO HTML/RTF in e-mail | Software and Electronic Engineering
X  - NO Word docs in e-mail  |      Perth Western Australia
/ \                                            |        Ph +61 419 923 731
____________________________________________________________________________

{Original Message removed}

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