AnalogIn Extremely Slow

04 Jun 2014

I just benchmarked the AnalogIn performance on several of my microcontrollers, and the LPCXpresso11U68 board is extremely slow! I measured 176µs for a floating point reading, and 173µs for an 16-bit integer reading. By comparison, the LPC11U24 (which has basically the same peripherals as the LPC11U68) only takes 18µs for a floating point reading, and 14µs for an 16-bit integer reading. Something is clearly amiss here... I've opened an issue over at GitHub, and I've attached my benchmark code and findings in case anyone is interested:

main.cpp

#include "mbed.h"

Timer timer;
AnalogIn adc(A0);

int main()
{
    float a = 0.0;
    unsigned short b = 0;

    printf("\nTesting floating point A/D performance...\n\n");

    timer.start();
    for (int i = 0; i < 1000000; i++) {
        a = adc.read();
    }
    timer.stop();
    printf("\tResult (%f): %fus\n", a, timer.read_us() / 1000000.0);
    timer.reset();

    printf("\nTesting integer A/D performance...\n\n");

    timer.start();
    for (int i = 0; i < 1000000; i++) {
        b = adc.read_u16();
    }
    timer.stop();
    printf("\tResult (0x%X): %fus\n", b, timer.read_us() / 1000000.0);
    timer.reset();
}


/media/uploads/neilt6/adc_benchmark_table.jpg

/media/uploads/neilt6/adc_benchmark_chart.jpg

05 Jun 2014

Hi,

Thanks for your detail report.

I have checked the LPC11U68 library source code and find the sampling clock is an issue. Current mbed library for the LPC11U68 use 500kHz sampling clock.

[CLKDIV = 100]

Testing floating point A/D performance...

        Result (0.263492): 175.767955us

Testing integer A/D performance...

        Result (0x4314): 172.541668us

I locally changed this value to be 1. i.e. 50MHz sampling clock.

[CLKDIV = 1]

Testing floating point A/D performance...

        Result (0.271306): 14.500005us

Testing integer A/D performance...

        Result (0x4574): 10.791682us

I will make a pull request for this when I finish other testing.

Regards, Toyo

05 Jun 2014

Cool thanks! Also, holy crap, that's as fast as the LPCXpresso1549!

05 Jun 2014

Btw it depends on the settings also for the other targets. The KL25 should be able to do that also, but currently it is set for 16-bit mode, and iirc also uses some hardware averaging.

05 Jun 2014

33µs on the KL25Z seems plenty fast enough to me. I think most people would prefer accuracy over raw speed (within reason of course).