ON 20070820@12:20:46 PM at page: http://www.piclist.com/piclist/codegen/constdivmul.htm#39313.1049768519 James Newton[JMN-EFP-786] removed post 39313.1049768519 |Delete 'steidley@comcast.net
I am converting the Celsius output of a DS1620 to Fahrenheit. So, noting the 5/9 = 1.8 I thought I would give the code generator a try. Basically, the only thing I changed from the default values is Multiply by Constant value... which of course I entered 1.8.
This is the code it generated, plus a few additions to set ACC0 with a starting value, and code at the end to sit there and loop. Here it is:
; ACC = ACC * 1.8
; Temp = TEMP
; ACC size = 8 bits
; Error = 0.5 %
; Bytes order = little endian
; Round = no
; ALGORITHM:
; Clear accumulator
; Add input * 2 to accumulator
; Substract input / 4 from accumulator
; Add input / 32 to accumulator
; Add input / 64 to accumulator
; Move accumulator to result
;
; Approximated constant: 1.79688, Error: 0.173611 %
; Input: ACC0, 8 bits
; Output: ACC0 .. ACC1, 9 bits
; Code size: 29 instructions
cblock 0x020
ACC0
ACC1
TEMP0
TEMP1
endc
; added this just to get a value into ACC0 for testing
movlw 21
movwf ACC0
;copy accumulator to temporary
movf ACC0, w
movwf TEMP0
;shift accumulator right 1 times
clrc
rrf ACC0, f
;add temporary to accumulator
addwf ACC0, f
;shift accumulator right 3 times
rrf ACC0, f
clrc
rrf ACC0, f
clrc
rrf ACC0, f
;substract temporary from accumulator
subwf ACC0, f
;shift accumulator right 2 times
rrf ACC0, f
movlw 0x80
xorwf ACC0, f ;invert bit shifted from carry
rlf ACC0, w
rrf ACC0, f
;shift temporary left 1 times
clrc
rlf TEMP0, f
clrf TEMP1
rlf TEMP1, f
;add temporary to accumulator
clrf ACC1
btfsc ACC0, 7
comf ACC1, f
movf TEMP0, w
addwf ACC0, f
movf TEMP1, w
skpnc
incfsz TEMP1, w
addwf ACC1, f
; The end.
goto $
end
Now, 21 * 1.8 = 37.8... So when the code generated an answer of 59, I was puzzled. What it looks like is it generated code for a value of 2.8, not 1.8. Since, honestly, I don't really follow this code, I am not sure how to modify it to produce 1.8. Any help is greatly appreciated.