![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
ADC Spike2
main.cpp
- Committer:
- simonb
- Date:
- 2010-03-09
- Revision:
- 0:db49fab3bd78
- Child:
- 1:0d866e3f26ab
File content as of revision 0:db49fab3bd78:
#define SAMPLE_RATE 150000 #include "mbed.h" #include "adc.h" //Initialise ADC to maximum SAMPLE_RATE and cclk divide set to 1 ADC adc(SAMPLE_RATE, 1); uint32_t adc_max[6], adc_av[6], adc_min[6]; int snap = 0; void min_max_av(int chan, uint32_t value) { static uint64_t adc_tot[6], counter = 0; uint32_t adc_max_tmp[6], adc_av_tmp[6], adc_min_tmp[6]; int i, adc_value; for (i=0; i<6; i++) { adc_value = adc.read(adc.channel_to_pin(i)); //Update min, max and average records and totals if (adc_value > adc_max_tmp[i]) adc_max_tmp[i] = adc_value; if (adc_value < adc_min_tmp[i]) adc_min_tmp[i] = adc_value; adc_tot[i] += adc_value; counter++; //If snapshot requested then sample data and reset average if (snap) { adc_av[i] = (int)(adc_tot[i] / counter); adc_max[i] = adc_max_tmp[i]; adc_av[i] = adc_av_tmp[i]; adc_min[i] = adc_min_tmp[i]; adc_max_tmp[i] = 0; adc_min_tmp[i] = 0xFFFF; adc_av_tmp[i] = 0; } } if (snap) counter = 0; snap = 0; } //Show a bargraph of ADC value void bar(int value) { int i, j; j = (value * 50) / 4096; if (j > 50) j = 50; if (j < 0) j = 0; for (i = 0; i < j; i++) printf("="); printf("\n"); } int main() { int i; printf("\nRequested max sample rate is %u, actual max sample rate is %u.\n", SAMPLE_RATE, adc.actual_sample_rate()); adc.append(min_max_av); //Prepare for burst mode on all ADC pins adc.startmode(0,0); adc.burst(1); adc.setup(p20,1); adc.setup(p19,1); adc.setup(p18,1); adc.setup(p17,1); adc.setup(p16,1); adc.setup(p15,1); //For burst mode, only one interrupt is required //which can be on any enabled pin. We have enabled all //of them here. adc.interrupt_state(p15,1); printf("Burst mode, printing once per second...\n"); while (1) { snap = 1; wait(0.5); for (i=0; i<6; i++) { printf ("CH %u Min :", i); bar(adc_min[i]); printf ("CH %u Av :", i); bar(adc_av[i]); printf ("CH %u Max :", i); bar(adc_max[i]); printf("\n"); } printf("......................................................\n"); wait(0.5); } }