ADC Spike2

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 
00002 #define SAMPLE_RATE    150000
00003 
00004 #include "mbed.h"
00005 #include "adc.h"
00006 
00007 //Initialise ADC to maximum SAMPLE_RATE and cclk divide set to 1
00008 ADC adc(SAMPLE_RATE, 1);
00009 
00010 uint32_t adc_max, adc_av, adc_min;
00011 int snap = 0;
00012 
00013 void min_max_av(int chan, uint32_t value) {
00014     static uint64_t adc_tot, counter = 0;
00015     static uint32_t adc_max_tmp, adc_min_tmp;
00016     
00017     
00018     int adc_value;
00019     
00020     adc_value = adc.read(p20);
00021     //Update min, max and average records and totals
00022     if (adc_value > adc_max_tmp) adc_max_tmp = adc_value;
00023     if (adc_value < adc_min_tmp) adc_min_tmp = adc_value;
00024     adc_tot += adc_value;
00025     counter++;
00026     //If snapshot requested then sample data and reset average
00027     if (snap) { 
00028         adc_max = adc_max_tmp;
00029         adc_av = (int)(adc_tot / counter);
00030         adc_min = adc_min_tmp;
00031         adc_max_tmp = 0;
00032         adc_min_tmp = 0xFFFF;
00033         adc_tot = 0;
00034     }
00035     if (snap) counter = 0;
00036     snap = 0;
00037 }
00038 
00039 //Show a bargraph of ADC value
00040 void bar(int value) {
00041     int i, j;
00042     
00043     j = (value * 50) / 4096;
00044     
00045     if (j > 50) j = 50;
00046     if (j < 0) j = 0;
00047     
00048     for (i = 0; i < j; i++)
00049         printf("=");
00050      printf("\n");
00051 }
00052 
00053 int main() {
00054     
00055     printf("\nRequested max sample rate is %u, actual max sample rate is %u.\n",
00056         SAMPLE_RATE, adc.actual_sample_rate());
00057     adc.append(min_max_av);
00058 
00059     //Prepare for burst mode on all ADC pins
00060     adc.startmode(0,0);
00061     adc.burst(1);
00062     adc.setup(p20,1);
00063     adc.interrupt_state(p20,1);
00064     
00065     printf("Burst mode, printing once per second...\n");
00066     while (1) {
00067         snap = 1;
00068         wait(0.5);
00069             printf ("Min :");
00070             bar(adc_min);
00071             printf ("Av  :");
00072             bar(adc_av);
00073             printf ("Max :");
00074             bar(adc_max);
00075             printf("\n");
00076         printf("......................................................\n");    
00077         wait(0.5);
00078     }
00079 }