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
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.
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;
}
}