EmbeddedRelated.com
Forums
The 2024 Embedded Online Conference

Re: Looking for codes to average adc results

Started by Paul James E. February 20, 2004
If you are havin problems with code here is some to get you started.
I find this method simple and sufficiently accurate for most
programs. This routine works by reading the A/D signal 256 times
adding the result each time to a running total. With an 8 bit reading
you will have a 16 bit result after 256 readings. The most sig. 8
bits of this result is the average input.

With this method there is no maths involved apart from a simple
addition. If you are using a 10 bit A/D the same method can be used
with some mods. If you want you can drop the least sig 2 bits of the
10 bit reading and use the same routine! It depends on the number of
quantizing steps you require. You will need 4 working regs

count - used to count 256 loops.

result - holds your A/D reading (not really necessary you can read
it from your A/D reg.)

reshi - stores the most significant 8 bits of averaged signal.

reslo - stores the least sig 8 bits of averaged signal. clrf count ; first DECFSZ instruction will cause
; this reg to go from 00h to 0FFh
Loop
call (Your A/D read routine) ; get the reading, store it
; in the reg called result
; in your A/D routine

movf result,W ; Copy the reading to W reg

addwf reslo,F ; Add it to the LSB reg

btfsc STATUS,C ; if overflow occurs inc MSB reg
incf reshi,F

decfsz count,F ; do it 256 times to get average
goto Loop

movf reshi,W ; (256 readings)/256 in reshi

You're averaged 8 bit reading is now in the reshi reg.
Hope this helps. --- In , "burt0072003" <burt007@i...> wrote:
> Thank you all for your kind help, this gives me some idea for the
> above problem, but when it comes to coding in assembly I am not
good.
> The problem was, when you have 16 bit resolution, last bits will be
> going all over the place because of instability, noise, analoge
> voltage not settled etc. so some way of seeing them stable or
> changing slowly until it is stabilized. Once stable seeing on lcd a
> stable number especially the last digits not changing so much.


--- In , Chad Russel <chadrussel@y...> wrote:
> Same problem here. So what I did with moving average was to
subtract
> min from max of buffer. If less than certain value, then stable.
>
> Chad


One simple way to get rid of some of the noise is to delete the min
max readings.

since it seems you are talking about a 16 bit value, and if you want
to stay in 16 bit numbers, another way is to take the average you had
and then find out how much your current reading is different.

This is averaging the deviation. Then just adding it back to the
average to get a new average.

This method stops working when you have really noisy signals in the
upper end of the range. ie: 4.8 volts or so.

To handle that, you can just subtract some value from everything and
then add it back in the final stages.

Dave




The 2024 Embedded Online Conference