Main Page | Modules | Class Hierarchy | Alphabetical List | Data Structures | File List | Data Fields | Globals | Related Pages

ADS868x - Code example
[ADC ADS8681/5/9 - Procedures for 16 bit multi range ADC.]

 #include <otMath.h>
 #include <otSerial.h>
 #include <otPrintf.h>
 #include <otTimer.h>
 #include <otMalloc.h>
 #include <otADS868x.h>
 #include <otStatistics.h>
 
 // Uncomment this define to test fast sampling
 //#define FAST_ACQ
 
 #ifdef FAST_ACQ
        // Embedded memory is very fast but with very small size!
        #define SAMPLES     1024
        S16     buff[SAMPLES];
 #else
        // Heap is very large but not fast!
        #define SAMPLES     100000
        S16     * buff;
 #endif
 
 otADS868x       adc;
 otStatistics    stat;
 
 void setup()
 {
        UartEnableSignals(UART0);
        UartSetBaud(UART0, 115200);
        TimerSetAsCounter(TIMER0);
        TimerEnable(TIMER0, true);
        setStdout(1024, UART0); // printf output is now directed on UART0
        adc.init();
        // Set ADC range
        adc.setRange(otADS868x::BIP_12V);
        
 #ifndef FAST_ACQ
        buff = (S16 *) _malloc(sizeof(S16) * SAMPLES);
 #endif
        
        // Specify data buffer, lenght and its type
        stat.setData(otStatistics::T_S16, (void *) buff, SAMPLES);
 }
  * 
 void loop()
 {
        U32     st, et;
        for(int i = 0; i < 10000; i++)
        {
                st = TimerGetCounts(TIMER0);    // Grab start time
                adc.read(buff, SAMPLES);
                et = TimerGetCounts(TIMER0);    // Grab end time
                et = ElapsedTimeUs(st, et) / 1000;
                printf("[%4d] Sampling time: %8u mS, bandwidth: %.1lf KSample/s\n", i, et, (double) SAMPLES / (double) et);
                
                st = TimerGetCounts(TIMER0);    // Grab start time
                stat.evaluate();
                et = TimerGetCounts(TIMER0);    // Grab end time
                et = ElapsedTimeUs(st, et) / 1000;
                printf("[%4d] Evaluate time: %8u mS, benchmark: %.1lf KSample/s\n", i, et, (double) SAMPLES / (double) et);
                
                printf("[%4d] STATISTICS   : min = %8.3lfV, max = %8.3lfV, average = %8.3lfV, sigma = %8.3lfV\n", 
                                i, adc.toVolt((S16) stat.min), adc.toVolt((S16) stat.max), adc.toVolt((S16) stat.average), (double) adc.toVolt((float) stat.sigma));
        }
        HIBE;
 }
footer
otStudio - Library Reference - (C) 2020-23 Officina Turini, All Rights Reserved
Document built with Doxygen 1.4.0