10 years, 8 months ago.

Is there sample code of this implementation

Hello All,

I need a constant ADC running in the background. The FastAnalogIn seems like it will do the trick, but I keep getting a variety of errors.

Does someone have a working code example, or an example that I could look through?

Thanks

Question relating to:

1 Answer

10 years, 8 months ago.

Hey,

What kind of errors are you getting? I currently don't have a real HelloWorld program for it, since it should be pretty much drop-in replacement. But this one I still had in my workspace, I used it when developing the lib:

#include "mbed.h"
#include "FastAnalogIn.h"


DigitalOut myled(LED1);
FastAnalogIn ain(p20, true);
FastAnalogIn ain2(p19, false);

Timer timer;

int main() {
    volatile float val[1000];
    wait(1);
    printf("\n\r");
    timer.reset();
    timer.start();
    for (int i=0; i<1000; i++)
        val[i]=ain.read_u16();
    timer.stop();
    printf("Took %dns per conversion\n\r", timer.read_us());
    
    ain.disable();
    timer.reset();
    timer.start();
    for (int i=0; i<1000; i++)
        val[i]=ain;
    timer.stop();
    printf("Took %dns per conversion\n\r", timer.read_us());
    
    ain2.enable();
    timer.reset();
    timer.start();
    for (int i=0; i<1000; i++)
        val[i]=ain;
    timer.stop();
    printf("Took %dns per conversion\n\r", timer.read_us());
    

    printf("Start\n\r");
    ain2.disable();
    while(1) {
        myled = 1;
        wait(0.2);
        myled = 0;
        wait(0.2);
        int value = ain.read_u16();
        printf("%d\n\r",value);
    }
}

Main question is which errors you are getting :)

Accepted Answer

I had ".cpp", not ".h" in the #include statement. Code works fine, I just need to be a little more diligent!

posted by Brad Young 04 Sep 2013

Small things :)

Anyway nice it works now. Made me also realise it was on my todo list to port this one to other devices. And apparantly I never bothered to seperate device specific and general code, something to do :P

posted by Erik - 04 Sep 2013