Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
Gorgon said:What is your number size(how many bits in each number) and format(integer or fixed / floating decimal)?
TOK
Gorgon said:What are the ranges for the different variables A-D? I suppose all is not 0-500?
What is the resolution of your A/D converter? (I would suppose that B is this value)
TOK
rrf ADRESH, w ;First multiply by 128 = 256 >> 1
movwf ADCVALH
rrf ADRESL, w
movwf ADCVALM
movf ADRESH, w ;Now add it once
addwf ADCVALM, f
btfsc STATUS, C ;carry thru from byte to byte
incf ADCVALH, f
movf ADRESL, w
movwf ADCVALL
rlf ADRESL, f ;Now multiply by four
rlf ADRESH, f
rlf ADRESL, w
rlf ADRESH, f
subwf ADCVALL, f ;Subtract 24 bit number
movf ADRESH, w
btfss STATUS, C ;carry thru from byte to byte
addlw 0x01 ;instead of subtracting 1, add one to what we subtract
subwf ADCVALM, f
btfss STATUS, C ;carry thru from byte to byte
decf ADCVALH, f ;Now Kelvin is ADCVALH:ADCVALM
jonw0224 said:Wow, you are in luck.
See, 500/1024 can be reduced to 125/256.
125 = b'01111101' = b'10000000' - b'00000100' + b'00000001' = 128 - 4 + 1
I would write as follows:
Code:rrf ADRESH, w ;First multiply by 128 = 256 >> 1 movwf ADCVALH rrf ADRESL, w movwf ADCVALM movf ADRESH, w ;Now add it once addwf ADCVALM, f btfsc STATUS, C ;carry thru from byte to byte incf ADCVALH, f movf ADRESL, w movwf ADCVALL rlf ADRESL, f ;Now multiply by four rlf ADRESH, f rlf ADRESL, w rlf ADRESH, f subwf ADCVALL, f ;Subtract 24 bit number movf ADRESH, w btfss STATUS, C ;carry thru from byte to byte addlw 0x01 ;instead of subtracting 1, add one to what we subtract subwf ADCVALM, f btfss STATUS, C ;carry thru from byte to byte decf ADCVALH, f ;Now Kelvin is ADCVALH:ADCVALM
This is a first attempt, and I haven't tested the code but it should get you started in the right direction
-jonathan
dont tell him make him work
or he will learn nothing !!!
bcf STATUS, C ;Clear C
rrf ADRESH, w ;First multiply by 128 = 256 >> 1. WREG = ADRESH/2, C = LSB of ADRESH
movwf ADCVALH ;Now, we move that into ADCVALH, effectively multipling by 256
rrf ADRESL, w ;WREG = (LSB of ADRESH) * 128 + ADRESL / 2, basically this
;completes the divide by 2 of the 10 bit number
movwf ADCVALM ;move to ADCVALM, effectively multiplying by 256
movf ADRESH, w ;Now add it once, WREG = ADRESH
addwf ADCVALM, f ;Add ADRESH to ADCVALM
btfsc STATUS, C ;carry thru from byte to byte, if the addition results in a carry
incf ADCVALH, f ;then increment ADCVALH
movf ADRESL, w ;This is a trick. I don't have to add ADRESL to ADCVALL because ADCVALL
;is assumed to be 0 initially.
movwf ADCVALL ;Therefore, I just move it thru WREG straight away
rlf ADRESL, f ;Now multiply by four. The order of the commands matters because now
;C = MSB of ADRESL
rlf ADRESH, f ;and now LSB of ADRESH = C = MSB of ADRESL
rlf ADRESL, w ;Do it again, but I want WREG to keep ADRESL so I can subtract it next
rlf ADRESH, f
subwf ADCVALL, f ;Subtract 24 bit number, start with ADCVALL = ADCVAL - ADRESL
movf ADRESH, w ;Prepare for the next subtraction
btfss STATUS, C ;carry thru from byte to byte, check if I need to borrow
addlw 0x01 ;instead of subtracting 1, add one to what we subtract, instead of
;borrowing, just add 1 to what I'm about to subtract.
;This takes fewer instructions (it's a short cut) which is possible
;because the high nibble of ADRESH is 0.
;Therefore, I don't have to take care of another overflow
;that I would have to take care of if I borrowed
subwf ADCVALM, f ;Now subtract the mid significant bytes
btfss STATUS, C ;carry thru from byte to byte, take care of the carry bit again
decf ADCVALH, f ;Now Kelvin is ADCVALH:ADCVALM, and I'm done
bcf STATUS, C ;Clear C
rrf ADRESH, w ;First multiply by 128 = 256 >> 1. WREG = ADRESH/2, C = LSB of ADRESH
movwf ADCVALH ;Now, we move that into ADCVALH, effectively multipling by 256
rrf ADRESL, w ;WREG = (LSB of ADRESH) * 128 + ADRESL / 2, basically this
;completes the divide by 2 of the 10 bit number
movwf ADCVALM ;move to ADCVALM, effectively multiplying by 256
clrf ADCVALL ;Take care of the carry
rrf ADCVALL, f
movf ADRESH, w ;Now add it once, WREG = ADRESH
addwf ADCVALM, f ;Add ADRESH to ADCVALM
btfsc STATUS, C ;carry thru from byte to byte, if the addition results in a carry
incf ADCVALH, f ;then increment ADCVALH
movf ADRESL, w
addwf ADCVALL
rlf ADRESL, f ;Now multiply by four. The order of the commands matters because now
;C = MSB of ADRESL
rlf ADRESH, f ;and now LSB of ADRESH = C = MSB of ADRESL
rlf ADRESL, w ;Do it again, but I want WREG to keep ADRESL so I can subtract it next
rlf ADRESH, f
subwf ADCVALL, f ;Subtract 24 bit number, start with ADCVALL = ADCVAL - ADRESL
movf ADRESH, w ;Prepare for the next subtraction
btfss STATUS, C ;carry thru from byte to byte, check if I need to borrow
addlw 0x01 ;instead of subtracting 1, add one to what we subtract, instead of
;borrowing, just add 1 to what I'm about to subtract.
;This takes fewer instructions (it's a short cut) which is possible
;because the high nibble of ADRESH is 0.
;Therefore, I don't have to take care of another overflow
;that I would have to take care of if I borrowed
subwf ADCVALM, f ;Now subtract the mid significant bytes
btfss STATUS, C ;carry thru from byte to byte, take care of the carry bit again
decf ADCVALH, f ;Now Kelvin is ADCVALH:ADCVALM, and I'm done
jonw0224 said:Cleong,
I told you the code was untested. Thanks to another poster who pointed out my code wouldn't work for odd numbers, I have corrected my code.
Thanks for your help, yager. If this isn't an appropriate fix, please let me know. No one is perfect.
Btw, when did the appropriate answer for "help me write this in assembly" become write it in C?
-jonathan
You never can do that!i stil wan to know how to make the ADRESH:ADRESL in one?