Exact match. Not showing close matches.
PICList
Thread
'[PIC]: PIC16F84 Math problem'
2001\03\05@182012
by
dans_pic_stuff
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.
2001\03\05@184128
by
mmucker
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}
2001\03\05@185324
by
James Cameron
2001\03\05@193327
by
Drew Vassallo

>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
;add temporary to accumulator
addwf ACC0, f
;shift accumulator right 1 times
rrf ACC0, f
;add temporary to accumulator
addwf 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 8bit 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
. addwf ACC0, f
. btfsc STATUS, C
. incf ACC1, f
(ignore the decimal places... my email editor strips off leading spaces)
Andrew
_________________________________________________________________
Get your FREE download of MSN Explorer at http://explorer.msn.com

http://www.piclist.com hint: The PICList is archived three different
ways. See http://www.piclist.com/#archives for details.
2001\03\05@195114
by
dans_pic_stuff
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}
2001\03\06@030905
by
dr. Imre Bartfai
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 listservKILLspammitvma.mit.edu with SET PICList DIGEST in the body
2001\03\06@031924
by
Roman Black
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 .....listservKILLspam.....mitvma.mit.edu with SET PICList DIGEST in the body
2001\03\06@073849
by
Bob Ammerman
> 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, lowlevel
software)

http://www.piclist.com#nomail Going offline? Don't AutoReply us!
email EraseMElistservspam_OUTTakeThisOuTmitvma.mit.edu with SET PICList DIGEST in the body
More... (looser matching)
 Last day of these posts
 In 2001
, 2002 only
 Today
 New search...