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
    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
    addwf count209,1
    btfsc CARRY
    call label_2090

    movf count209,0
    btfsc CARRY
    call label_20900
    movlw 0x01
    addwf count2090,1
    movlw 0x01
    addwf count20900,1

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

    movlw 0x01
    addwf BARGB1,1
    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
    addwf AARGB2,1
    movf count2090,0
    btfsc CARRY
    incfsz count2090,0
    addwf AARGB1,1

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

    call FXD2416U///////

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

    Can anyone please guide me in this?
  2. guitarguy12387

    Active Member

    Apr 10, 2008
    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


    Sep 9, 2010
    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.