piclist 2001\03\13\120916a >
Thread: math calibration algorithm ?
www.piclist.com/techref/microchip/math/index.htm?key=math
BY : Nikolai Golovchenko email (remove spam text)

I think it can be done a bit easier.

1. Calculating slope. slope=(full_scale-cal_zero)/100. Here a fixed
point division would be useful that takes at least 10 bits for
dividend, 7 bits for divisor and the result as fixed point, say 3Q8
(11 bits is 0.05% accuracy at the full scale).

2. Calculating real value. real_value=(measured_value-cal_zero)/slope.
Here you need a subtraction and division again. A nice trick is
possible to use just one divide routine! The only significant
difference is that you need the result as 7Q8 (0-100 range with a few
fraction bits).

So, the common divide routine should take:
dividend = 10 bits or more in integer format
divisor = 15 bits in 7Q8 format
and produce:
quotient = 15 bits in 7Q8 format

3. Displaying the results. Depending on how much digits after the
point you display, multiply the 7Q8 result by 10 (to get 1 fraction
decimal digit) or 100 (2 fraction decimal digits) and leave only the
integer bits of the result. Multiplication by a constant is easy, if
you use the online constant multiplication code generator or a
regular multiplication routine.

Then convert the integer result to decimal, using a routine at
piclist.com (there is one for 0-999 range and another for 0-65535 -
one of them should fit).

That's it!

Try this for division:

;-----------------------------------------------------------------------------
; Input:
;  a1:a0 - 10 bit dividend
;  b1:b0 - 15 bit divisor in 7Q8 format (b1 is integer, b0 is
;          fractional)
; Output:
;  c1:c0 - 15 bit quotient in 7Q8 format
;
; Temporary:
;  temp - current remainder extension (used for intermidiate calculations
;                only)
;  count - counter
;
;-----------------------------------------------------------------------------
div_uint10_fxp7q8_fxp7q8

;left align the dividend
; (shift accumulator left 1 bit to get the first result bit weight
;  equal to 128)
clrc
rlf     a0, f
rlf     a1, f
;initialize registers
clrf temp                  ;clear remainder
movlw 15                   ;15 iterations
movwf count
clrf c0                    ;clear result - it will be used
clrf c1                    ;to shift zeroes to dividend
div_loop
rlf c0, f                  ;shift in next result bit
rlf c1, f
rlf a0, f                  ;and shift out next bit of dividend
rlf a1, f                  ;to remainder
rlf temp, f

movf b0, w                 ;load w with lower divisor byte
btfsc temp, 7              ;if remainder positive - subtract,
;subract
subwf a0, f
movf b1, w
skpc
incfsz b1, w
subwf a1, f
movlw 1
skpc
subwf temp, f
goto div_next
movf b1, w
skpnc
incfsz b1, w
movlw 1
skpnc
div_next
;here carry has a new result bit
decfsz count, f
goto div_loop
;shift in last result bit
rlf c0, f
rlf c1, f
return
;-----------------------------------------------------------------------------

Good luck!
Nikolai

---- Original Message ----
From: Vasile Surducan <vasileL30.ITIM-CJ.RO>
Sent: Monday, March 12, 2001 9:01:13
To: PICLISTMITVMA.MIT.EDU
Subj: [PIC]: math calibration algorithm ?

{Quote hidden}

--
http://www.piclist.com hint: PICList Posts must start with ONE topic: