Compute moving average using PIC

Discussion in 'The Projects Forum' started by RG23, Jul 8, 2011.

1. RG23 Thread Starter Active Member

Dec 6, 2010
301
2
I am incrementing a counter count209 every 10ms
As soon as I hit the switch I need to see the moving average value of count209
But since count209 is only 8 bit register so I used count2090 and count20900 to implement a 24 bit counter

10 ms subroutine:

movlw 0x01
btfsc CARRY
call label_2090

movf count209,0
btfsc CARRY
call label_20900
return
label_2090:
movlw 0x01
return
label_20900:
movlw 0x01
return

/////Moving Average Subroutine executes on hitting the switch

movlw 0x01
btfsc CARRY
incf BARGB0,1//////BARGB0 and BARGB1 are initialized to zero in main
/////each time I enter the moving average subroutine I increment the 16 bit counter BARGB1:BARGB0
movf count209,0
movf count2090,0
btfsc CARRY
incfsz count2090,0

movf count20900,0
btfsc CARRY
incfsz count20900,0
//////Then I call 24bits by 16 bits division subroutine by Nikolai Golovchenko

call FXD2416U///////http://www.piclist.com/techref/microchip/math/div/24by16.htm

/////then I try to display average but I am not getting the desired result

Can anyone please guide me in this?
Thanks

2. guitarguy12387 Active Member

Apr 10, 2008
359
12
This is a fairly convoluted post...

But anyway, why do you need to compute the moving average of a counter? Doesn't seem to be very useful...

3. wayneh Expert

Sep 9, 2010
12,368
3,224
+1
But what kind of moving average do you want? The problem is always to decide how many values you want to include, and how to weight them. You might choose, say, the last ten values. But do you really want to have the earliest of those ten values weighted the same as the most recent value? One solution often used is to give a higher weighting to the most recent value and declining weight to the older values. Of course the weighting factors need to sum to one.