Voltage Measurement and FFT

15 Nov 2012

I've been given a part in a project that consists of reading in voltage samples and performing a FFT to obtain harmonics. My goal is to sample the input and manipulate it at 10kHz, with the output being put in to a FFT function (treat as black box) that should be called every 20ms(50Hz). Ideally then these would be using interrupts (perhaps using Ticker) however the ADC sampling function must continue to interrupt the FFT function. Is there any way possible that I can have a higher and a lower priority ticker? Or is this achievable by some other means (RTOS)?

I'm very new to the world of microcontrollers so be kind!

15 Nov 2012

Yes the mbed microcontrollers allow for quite some different interrupt levels. Somewhere on forums it is explained, however what I would do is simply use interrupts to let the ADC sample at 10kHz, and in your normal main function you run your FFT stuff. In order to make sure you it is called every 20ms you make another ticker object that just sets a variable.

So something like:

Ticker FFTTicker;
Ticker ADCTicker;

bool FFTTicked;

void FFTTickFunction( void ) {
  FFTTicked = true;
}

void ADCTickFunction( void ) {
  //Do your ADC reading. BTW by default it isnt exactly running at max speed,
  //Most efficient would be to only start ADC here, and let an interrupt read the data when the ADC is done
  //I think there are custom libraries which can do that
}

int main() {
  FFTTicked = false;
  FFTTicker.attach_us(&FFTTickFunction, 100);
  ADCTicker.attach(&ADCTickFunction, 0.02);

  while(1)
  {
    while(!FFTTicked);  //Wait here until FFT is ticked
    FFTTicked=false;
    //Do your calculations here
  }
}

Something to remember in general is that you dont want to be long in interrupt routines. Yes you can solve it by using different interrupt levels, but best is to keep interrupt routines short functions.

16 Nov 2012

Perfect! Exactly what I was looking for thanks!

The only issue with this though would be on adding further code to the main?