Increasing Sampling Frequency

17 Nov 2011

Hi Guys

I am brand new to mbed so I am still trying to get acquainted with everything. I tested the mbed with the code below to check its ADC sampling frequency. I did this by using a scope to measure the frequency of LED 1 turning on and off. The measured frequency I got was 13.5 kHz. I was wondering if anyone knew how I could increase this to somewhere around 20 kHz.

Thanks for your help!

#include "mbed.h"

DigitalOut myled(LED1);

AnalogIn Ain(p20);

AnalogOut Aout(p18);

int main() {

float x = 0;

while(1) {

myled = 1;

x = Ain;

myled = 0;

Aout=x;

}

}

18 Nov 2011

I was going to say to toggle a digital out pin after you sample and read the frequency from that for better results.. but.. upon checking this myself, the results are rather disappointing indeed.

With the following code, I get a 71us period between a rising and falling edge on pin 22. The frequency is a little over 14kHz. I am aware that there is a fair bit of abstraction for AnalogIn and DigitalOut, but this is pretty bad. I know the LPC1768 can sample faster than this... so what's up? It's not the toggle of the pin either. A toggle takes about 150ns on it's own.

The good(?) news is that floating point reads are the same.

#include "mbed.h"
AnalogIn Ain(p20);
DigitalOut pin22(p22);
int main (void) {

    float x;
    int a;
    pin22=0;

    while (1) {
        pin22=!pin22;  //Toggle Pin
        //x=Ain.read();
        a=Ain.read_u16();
    }
}

M C, do a search on the site for Analog in. There is a couple of user contributed libraries for the ADC that will yield better results. Although they're not as easy to use as the mbed library, they should get you the speed you need.

EDIT: There is a fairly comprehensive ADC driver written by Simon Blandford

Import programADC_test

Full ADC driver with demo

EDIT 2: I just remembered that the new library uses a 3 sample median filter. This means that the actual sampling rate is about 45kHz, but the median filter reduces it by 3. This means that the sampling frequency is 14k by design. If we need something faster, we have to code closer to the hardware.

21 Nov 2011

Hey Igor,

Thanks alot for all the information. I did take a look at the ADC_test program but I wasn't sure how to implement it since the sampling frequency is already around 13.5 k in design. When you say we have to code closer to the hardware does that mean look up the LPC1768 data sheet and see what specific pins and registers are in charge of sampling frequency and then manipulate them?

Thanks, MC

21 Nov 2011

Yup, that's the gist of it..

Before you do that however, you might want to try the v29 beta. I did not realize that it has not been released yet. I just tried the above program with the beta library and it looks like the final sampling rate is 49.8khz without any changes! :)

To use the beta library see Simon's post here

I hope that helps!

23 Nov 2011

Oh wow,

That's awesome! I just tried the library and I got a sampling rate of 40 kHz, without any extra register manipulation.

Thanks for much for your help Igor!