Searching \ for '[PIC]: PIC16F84 Math problem' in subject line. ()
Help us get a faster server
FAQ page: www.piclist.com/techref/microchip/math/index.htm?key=math
Search entire site for: 'PIC16F84 Math problem'.

Exact match. Not showing close matches.
'[PIC]: PIC16F84 Math problem'
2001\03\05@182012 by

Is it possible to do this math calculation in a 16f84 program:
X = (X*1.62) + 25
I'm using PBP and it tells me that .62 is a bad variable modifier.  If I
can't do decimal numbers then any ideas on how to do this same equation
another way?  I don't care if the answer is rounded or truncated.  It just
has to be somewhat close but not super accurate..

--
http://www.piclist.com hint: The PICList is archived three different
ways.  See http://www.piclist.com/#archives for details.

Hmmm... 1/2 + 1/8 = 0.625, which is pretty close to 0.62.

So your equation is close to:

f(X) = X * (1/2) + X * (1/8) + X + 25

X * (1/2) is one rotate right
X * (1/8) is three rotate rights.
(Clear the carry first, of course!)

Then add X and X/2 and X/8 to get 1.625X, then add 25.

Or is my algebra seriously impaired this late in the day?

-Matt

> {Original Message removed}
On Mon, Mar 05, 2001 at 03:19:10PM -0800, dans_pic_stuff wrote:
> Is it possible to do this math calculation in a 16f84 program:
> X = (X*1.62) + 25

Multiply by 162 then divide by 100.  Watch that you keep the
intermediate result in enough bits to retain the precision you desire.

I've not heard of PBP.

--
James Cameron    quozlus.netrek.org     http://quozl.netrek.org/

--
http://www.piclist.com hint: The PICList is archived three different
ways.  See http://www.piclist.com/#archives for details.

>Is it possible to do this math calculation in a 16f84 program:
>X = (X*1.62) + 25

You can't really do it straight like that.  Fortunately, Nikolai has the
answer in his constant mult/div generator:

; Approximated constant: 1.625, Error: 0.308642 %
;     Input: ACC0, 8 bits
;    Output: ACC0 .. ACC1, 9 bits
; Code size: 10 instructions

cblock
ACC0
ACC1
endc

;copy accumulator to temporary
movf    ACC0, w
;shift accumulator right 2 times
clrc
rrf     ACC0, f
clrc
rrf     ACC0, f
;shift accumulator right 1 times
rrf     ACC0, f
clrf    ACC1
rlf     ACC1, f

10 instructions is pretty nice.  And I believe you said you didn't need more
than 0.3% accuracy.
You'll have to take the value "X" and substitute for "ACC0" in this routine.
Also, since you're multiplying an 8-bit number, the result will be 9 bits,
requiring a high byte register (ACC1), which you can also change.

Adding the 25 after the multiplication is nothing:

.     movlw 0x19
.     btfsc STATUS, C
.     incf ACC1, f

(ignore the decimal places... my email editor strips off leading spaces)
--Andrew
_________________________________________________________________

--
http://www.piclist.com hint: The PICList is archived three different
ways.  See http://www.piclist.com/#archives for details.

ah, thanks. I should have thought of that. :-P
PBP is Pic Basic Pro.  It lets me program my PICs in a BASIC programming
language.  it generates the Assembly and Hex for me.  Some people here don't
like it but I love it. :-)

{Original Message removed}
The idea is pretty straightforward, however, there is the potential danger
of overflow. Because of 1.62 = 9 * 9 / 5 / 10, you can intermix these
operation in an arbitrary order, of course, at the cost of loss of
precision. I recommend as order / 10 * 9 / 5 * 9. Such way, overflow would
not occur if the result fits.
Regards,
Imre

On Tue, 6 Mar 2001, James Cameron wrote:

{Quote hidden}

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

dans_pic_stuff wrote:
>
> Is it possible to do this math calculation in a 16f84 program:
> X = (X*1.62) + 25
> I'm using PBP and it tells me that .62 is a bad variable modifier.  If I
> can't do decimal numbers then any ideas on how to do this same equation
> another way?  I don't care if the answer is rounded or truncated.  It just
> has to be somewhat close but not super accurate..

Are you converting miles to kilometers?? :o)
-Roman

PS. Try multiply by 26, divide by 16.
The divide by 16 is just a right shift.

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

> dans_pic_stuff wrote:
> >
> > Is it possible to do this math calculation in a 16f84 program:
> > X = (X*1.62) + 25
> > I'm using PBP and it tells me that .62 is a bad variable modifier.  If I
> > can't do decimal numbers then any ideas on how to do this same equation
> > another way?  I don't care if the answer is rounded or truncated.  It
just
> > has to be somewhat close but not super accurate..

If X is an 8 bit positive integer with a value <= 157 you could:

Temp16 = X * 415

X = Temp16 / 256    ; Just grab the MSByte of Temp16!
--- or ---
X = Temp16 >> 8

X += 25

Note that 415/256 = 1.6211

This is pretty accurate, the error is only 0.068 %.

Bob Ammerman
RAm Systems
(contract development of high performance, high function, low-level
software)

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

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