Goodday,
Finally got my LM76 Sign Digital i2c sensor working
but now have to multiply the 13-bit two complement word by
0.0625 (1 bit indicated 0.0625 degrees C)
Had a look in the archive but nothing applicable.
Anyone using a Lm76, and got around this multiply,
or where I can look for some fraction code.
Tks
Grant Forest
> Goodday,
> Finally got my LM76 Sign Digital i2c sensor working
> but now have to multiply the 13-bit two complement word by
> 0.0625 (1 bit indicated 0.0625 degrees C)
> Had a look in the archive but nothing applicable.
> Anyone using a Lm76, and got around this multiply,
> or where I can look for some fraction code.
> Tks
> Grant Forest
Well, .0625 is 1/16
a right shift is divide by 2,
4 right shifts are a divide by 16.
--
*
| __O Thomas C. Sefranek spam_OUTtcsTakeThisOuTcmcorp.com
|_-\<,_ Amateur Radio Operator: WA1RHP
(*)/ (*) Bicycle mobile on 145.41, 448.625 MHz
> Finally got my LM76 Sign Digital i2c sensor working
> but now have to multiply the 13-bit two complement word by
> 0.0625 (1 bit indicated 0.0625 degrees C)
Well, .0625 is 1/16
a right shift is divide by 2,
4 right shifts are a divide by 16.
Aren't you glad you spent all that time getting a nice clean analog section
of circuitry, worthy of a 13bit A-D converter, only to throw away four of
the bits?
Consider multiplying by 100 16ths, (100/16) to get the voltages as an
integer value equal to the number of hundredths of degrees. 100/16s is a
bit less than 8, so the result should still fit in 16 bits. One of the math
wizards might suggest an algorithm (to start with, 100/16 = 25/4) that will
let you escape going to a 24 bit int. (I wouldn't mind seeing a
simultaneous multiply/divide algorithm for handling fractions like this in
general! (ie where you're sure the final result will be less than N bytes,
but where the intermediate values can exceed N bytes.))
At 08:53 AM 3/26/00 +1000, you wrote:
>Goodday,
>Finally got my LM76 Sign Digital i2c sensor working
>but now have to multiply the 13-bit two complement word by
>0.0625 (1 bit indicated 0.0625 degrees C)
>Had a look in the archive but nothing applicable.
>Anyone using a Lm76, and got around this multiply,
>or where I can look for some fraction code.
>Tks
>Grant Forest
>
Looks like /16 here, but if you're interested in general in
fractional arithmetic methods, my old TI TMS320 guides describe
something called "Q-format". Extremely clever stuff.
> Well, .0625 is 1/16
> a right shift is divide by 2,
> 4 right shifts are a divide by 16.
Grant:
If you just do 4 right-shifts, you'll end up with a 9-bit two's-
complement number... Which is ok, I guess, but it'd probably be
easier to work with the value if it were represented as a straight 8-
bit number in the range [0-256], with a SEPARATE sign bit. To get
THAT, do this:
RRF HI
RRF LO
RRF HI
RRF LO
RRF HI
RRF LO
RRF HI
RRF LO,W
BTFSC HI,0
SUBLW 0
MOVWF LO
After executing that code, LO will contain a number between 0 and 256
(representing the absolute value of the temperature, in degrees C),
and bit 0 of HI will hold the sign of the temperature (1 = negative,
0 = positive). Bits 1-7 of HI will contain garbage; if that bothers
you, you can mask them by adding:
Grant,
In general, you may want to use a code generator at http://www.piclist.com/codegen/constdivmul
to multiply an integer variable by a floating point constant(0.0625,
6.25, or whatever). It will work also in case you have a variable with
both integer and fractional part(Q-format).
In your case, results are (0 - is the least significant byte):
; Acc = Acc * 0.062500
;
; ALGORITHM:
; Clear accumulator
; Add input / 16 to accumulator
; Move accumulator to result
;
; Error in constant approximation : 0.000000, %
clrc
rrf Acc1, f
rrf Acc0, f
clrc
rrf Acc1, f
rrf Acc0, f
clrc
rrf Acc1, f
rrf Acc0, f
clrc
rrf Acc1, f
rrf Acc0, f
;----------------------------------------
and
; Acc = Acc * 6.250000
;
; ALGORITHM:
; Clear accumulator
; Add input * 4 to accumulator
; Add input * 2 to accumulator
; Add input / 4 to accumulator
; Move accumulator to result
;
; Error in constant approximation : 0.000000, %
movf Acc0, w
movwf Temp0
movf Acc1, w
movwf Temp1
clrc
rrf Acc1, f
rrf Acc0, f
clrc
rrf Acc1, f
rrf Acc0, f
clrc
rlf Temp0, f
rlf Temp1, f
movf Temp0, w
addwf Acc0, f
movf Temp1, w
skpnc
incfsz Temp1, w
addwf Acc1, f
clrc
rlf Temp0, f
rlf Temp1, f
movf Temp0, w
addwf Acc0, f
movf Temp1, w
skpnc
incfsz Temp1, w
addwf Acc1, f
;----------------------------------------
Nikolai
On Sunday, March 26, 2000 Grant Forest wrote:
> Goodday,
> Finally got my LM76 Sign Digital i2c sensor working
> but now have to multiply the 13-bit two complement word by
> 0.0625 (1 bit indicated 0.0625 degrees C)
> Had a look in the archive but nothing applicable.
> Anyone using a Lm76, and got around this multiply,
> or where I can look for some fraction code.
> Tks
> Grant Forest