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.
c_set rrf w
addlw b'10000000'
goto c_done
sample movfw avg
movwf sample1
movfw _adres ;sample 2
addfw sample1,w ;add sample1 to sample2, store result in w.
btfsc _status,0 ;if carry is set...
goto c_set
rrf w
c_done movwf avg
TSample
movlw d'100' ; load value 5
movwf Cnt ; into file Cnt
TS1
call Sample ; waits 1 second
decfsz Cnt ; does this 5 times
goto TS1 ; loop till Cnt=0
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
c_set rrf w,w ; rotate accu right 1 (div by 2), put in w
addlw b'10000000' ; add to existing value of w (due to carry)
goto c_done ; by-pass 'rrf' if c isn't set.
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
measure movlw d'100' ;
movwf cnt ;
movfw _adres ; take the very first sample
movwf avg ; put it in avg
loop movfw avg
movwf sample1
movfw _adres ; sample 2
addwf sample1,w ; add sample1 to sample2, store result in w.
btfsc _status,0 ; if carry is set...
goto c_set
rrf w,w
c_done movwf avg
decfsz cnt,f ;
goto loop ; loop till cnt=0
movfw avg ; copy the final avg sample to accu
return ; return without altering accu value
movwf sample1
movfw _adres ; sample 2
addwf _adres, f ; add sample1 to sample2, store result in _adres.
rrf _adres, f ; rotate right, including carry, result in _adres
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
measure movlw d'10' ; This defines the number of samples taken
movwf cnt ; 10 * 8µs (sample time) = 96µs (inc +20%)
movfw _adres ; take the very first sample
movwf avg ; put it in avg
loop movfw avg ; current/previous avg stored in sample1
movwf sample1
movfw _adres ; take new sample and store in accu
addwf sample1,w ; add sample1 to sample2, store result in w
rrf w,f ; rotate right including carry, result in w
movwf avg ; avg is used as sample1 when cnt != 0
decfsz cnt,f ; perform cnt - 1, skip next if result is 0
goto loop ; cnt != 0 so take another reading and avg
movfw avg ; copy the final avg sample to accu
return ; return without altering accu value
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
forget rotate commands - in the sense of using them for the actal averaging maths that is!but each rrf would result in a div by 2.....
Procedure: Double the divisor until it is just less than the divi-
dend. Then try to subtract the doubled divisors, starting with the
largest, from the dividend. Note a 1 if the subtraction is possible
otherwise, note a zero and do not perform the subtraction.
The 1s and Os constitute the binary form of the quotient. To ob-
tain the decimal form, multiply the latter digits with the corre-
sponding terms in a power of 2 series, arranged in reverse order.
The quotient is the sum of the resultant terms.
To obtain decimal accuracy, multiply the dividend initially by an
Nth power of 10. Then, after the division is complete, divide the
quotient by the same power of 10 (moving the decimal point N
places).
Example: 2246/51 Counter
Double: 51
0
102
1
204
2
408
3
816
4
1632
5
Subtract: 2246
-1632
614
1 X 32 = 32 5
-816
0 x 16 = 0 4
614
-408
206
1 x 8 = 8 3
-204
2
1 x 4 = 4 2
-102
0 x 2 = 0 1
2
-51
0 x 1 = O 0
Remainder: 2
Quotient: 101100 = 44
bsodmike said:So the max reading is 255, so if I were to add up 5 samples we are already in the land of a 16bit number - and for the life of me I can not see how I can divide this through.
As you said a rounded off value is all I need ; nothing with floating points.
measure movlw d'3' ; This defines the number of samples taken
movwf cnt ; excluding the 'first sample'
movfw _adres ; read the very first sample and store it
movwf sum ; in the variable 'sum'
loop movfw _adres ; read the second sample into accu
addwf sum,f ; add both together, put answer back in sum
btfsc _status,0 ; check carry bit for overflow
incf overflow ; if carry bit is set, inc overflow by 1
decfsz cnt,f ; perform cnt - 1, skip next if result is 0
goto loop ; cnt != 0 so loop back for another sample
rrf overflow ; shift the overflow
rrf sum ; rrf carry (div by 2)
rrf overflow ; shift the overflow
rrf sum ; rrf carry (div by 2)
movfw sum ; copy the average to accu
return ; return without altering accu value