Skip to main content
Visitor II
March 25, 2017
Question

Hello, I had developed one application using STVD

  • March 25, 2017
  • 2 replies
  • 1974 views
Posted on March 25, 2017 at 10:03

#include 'Filter.h'

#define Samples 3

#include <math.h>

#include <stdlib.h>

extern unsigned long int data;

long int Result_Previous,Average,;

unsigned char Stable;

void FIR_Filter(unsigned long int Clean)

{

    static unsigned char Index=0;

//    static bit_16 Index1=4000;

    static unsigned long int ADC_Mean[Samples];

    

    unsigned long int Absolute;

    unsigned char Count;

//    Average = Clean>>5;

    Clean >>= 5;

    ADC_Mean[Index] = Clean;

    Average = 0;

    {    

        for(Count=0;Count<Samples;Count++)

        {

            Average+=ADC_Mean[Count];

             }

        Average /= Samples;

    }

    if(Index >= (Samples-1))

        Index=0;

    else

        Index++;

    Absolute = abs(Average - Result_Previous);

    

   if(Absolute <5)

    {

        Average = Result_Previous +((Average - Result_Previous)>>2);

    }

    if(Absolute<3)

        Stable++;

    else

        Stable=0; 

    

    Result_Previous = Average;

//    return (Average);

        data = Average;

}

this is my code in interrupt function.. i think problem with this statement addition in variable not working in interrupt function  ' Average+=ADC_Mean[Count];'

how can addition in that variable??

Help me for this.. Thank you..

    This topic has been closed for replies.

    2 replies

    Visitor II
    March 25, 2017
    Posted on March 25, 2017 at 15:07

    Unclear. Interrupt routine should be as short as possible = as little computation as possible.

    What is the signal triggering the interrupt? Which frequency?

    Try to do the minimum in the interrupt, pass the result in a dedicated global register and set a RAM flag (to prevent future interrupts to corrupt the data). The main loop will wait for the flag, grab the data, divide, and clear the flag.

    Visitor II
    March 27, 2017
    Posted on March 27, 2017 at 07:01

    Signal triggering is a falling edge and frequency is 2MHz.

    Visitor II
    March 25, 2017
    Posted on March 25, 2017 at 15:25

    If you're modifying global variables inside an interrupt handler, and you want to access them outside, you need to declare them as 'volatile'.