![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
ADC Spike2
main.cpp@0:db49fab3bd78, 2010-03-09 (annotated)
- Committer:
- simonb
- Date:
- Tue Mar 09 15:58:33 2010 +0000
- Revision:
- 0:db49fab3bd78
- Child:
- 1:0d866e3f26ab
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
simonb | 0:db49fab3bd78 | 1 | |
simonb | 0:db49fab3bd78 | 2 | #define SAMPLE_RATE 150000 |
simonb | 0:db49fab3bd78 | 3 | |
simonb | 0:db49fab3bd78 | 4 | #include "mbed.h" |
simonb | 0:db49fab3bd78 | 5 | #include "adc.h" |
simonb | 0:db49fab3bd78 | 6 | |
simonb | 0:db49fab3bd78 | 7 | //Initialise ADC to maximum SAMPLE_RATE and cclk divide set to 1 |
simonb | 0:db49fab3bd78 | 8 | ADC adc(SAMPLE_RATE, 1); |
simonb | 0:db49fab3bd78 | 9 | |
simonb | 0:db49fab3bd78 | 10 | uint32_t adc_max[6], adc_av[6], adc_min[6]; |
simonb | 0:db49fab3bd78 | 11 | int snap = 0; |
simonb | 0:db49fab3bd78 | 12 | |
simonb | 0:db49fab3bd78 | 13 | void min_max_av(int chan, uint32_t value) { |
simonb | 0:db49fab3bd78 | 14 | static uint64_t adc_tot[6], counter = 0; |
simonb | 0:db49fab3bd78 | 15 | uint32_t adc_max_tmp[6], adc_av_tmp[6], adc_min_tmp[6]; |
simonb | 0:db49fab3bd78 | 16 | |
simonb | 0:db49fab3bd78 | 17 | |
simonb | 0:db49fab3bd78 | 18 | int i, adc_value; |
simonb | 0:db49fab3bd78 | 19 | |
simonb | 0:db49fab3bd78 | 20 | for (i=0; i<6; i++) { |
simonb | 0:db49fab3bd78 | 21 | adc_value = adc.read(adc.channel_to_pin(i)); |
simonb | 0:db49fab3bd78 | 22 | //Update min, max and average records and totals |
simonb | 0:db49fab3bd78 | 23 | if (adc_value > adc_max_tmp[i]) adc_max_tmp[i] = adc_value; |
simonb | 0:db49fab3bd78 | 24 | if (adc_value < adc_min_tmp[i]) adc_min_tmp[i] = adc_value; |
simonb | 0:db49fab3bd78 | 25 | adc_tot[i] += adc_value; |
simonb | 0:db49fab3bd78 | 26 | counter++; |
simonb | 0:db49fab3bd78 | 27 | //If snapshot requested then sample data and reset average |
simonb | 0:db49fab3bd78 | 28 | if (snap) { |
simonb | 0:db49fab3bd78 | 29 | adc_av[i] = (int)(adc_tot[i] / counter); |
simonb | 0:db49fab3bd78 | 30 | adc_max[i] = adc_max_tmp[i]; |
simonb | 0:db49fab3bd78 | 31 | adc_av[i] = adc_av_tmp[i]; |
simonb | 0:db49fab3bd78 | 32 | adc_min[i] = adc_min_tmp[i]; |
simonb | 0:db49fab3bd78 | 33 | adc_max_tmp[i] = 0; |
simonb | 0:db49fab3bd78 | 34 | adc_min_tmp[i] = 0xFFFF; |
simonb | 0:db49fab3bd78 | 35 | adc_av_tmp[i] = 0; |
simonb | 0:db49fab3bd78 | 36 | } |
simonb | 0:db49fab3bd78 | 37 | } |
simonb | 0:db49fab3bd78 | 38 | if (snap) counter = 0; |
simonb | 0:db49fab3bd78 | 39 | snap = 0; |
simonb | 0:db49fab3bd78 | 40 | } |
simonb | 0:db49fab3bd78 | 41 | |
simonb | 0:db49fab3bd78 | 42 | //Show a bargraph of ADC value |
simonb | 0:db49fab3bd78 | 43 | void bar(int value) { |
simonb | 0:db49fab3bd78 | 44 | int i, j; |
simonb | 0:db49fab3bd78 | 45 | |
simonb | 0:db49fab3bd78 | 46 | j = (value * 50) / 4096; |
simonb | 0:db49fab3bd78 | 47 | |
simonb | 0:db49fab3bd78 | 48 | if (j > 50) j = 50; |
simonb | 0:db49fab3bd78 | 49 | if (j < 0) j = 0; |
simonb | 0:db49fab3bd78 | 50 | |
simonb | 0:db49fab3bd78 | 51 | for (i = 0; i < j; i++) |
simonb | 0:db49fab3bd78 | 52 | printf("="); |
simonb | 0:db49fab3bd78 | 53 | printf("\n"); |
simonb | 0:db49fab3bd78 | 54 | } |
simonb | 0:db49fab3bd78 | 55 | |
simonb | 0:db49fab3bd78 | 56 | int main() { |
simonb | 0:db49fab3bd78 | 57 | int i; |
simonb | 0:db49fab3bd78 | 58 | |
simonb | 0:db49fab3bd78 | 59 | printf("\nRequested max sample rate is %u, actual max sample rate is %u.\n", |
simonb | 0:db49fab3bd78 | 60 | SAMPLE_RATE, adc.actual_sample_rate()); |
simonb | 0:db49fab3bd78 | 61 | adc.append(min_max_av); |
simonb | 0:db49fab3bd78 | 62 | |
simonb | 0:db49fab3bd78 | 63 | //Prepare for burst mode on all ADC pins |
simonb | 0:db49fab3bd78 | 64 | adc.startmode(0,0); |
simonb | 0:db49fab3bd78 | 65 | adc.burst(1); |
simonb | 0:db49fab3bd78 | 66 | adc.setup(p20,1); |
simonb | 0:db49fab3bd78 | 67 | adc.setup(p19,1); |
simonb | 0:db49fab3bd78 | 68 | adc.setup(p18,1); |
simonb | 0:db49fab3bd78 | 69 | adc.setup(p17,1); |
simonb | 0:db49fab3bd78 | 70 | adc.setup(p16,1); |
simonb | 0:db49fab3bd78 | 71 | adc.setup(p15,1); |
simonb | 0:db49fab3bd78 | 72 | //For burst mode, only one interrupt is required |
simonb | 0:db49fab3bd78 | 73 | //which can be on any enabled pin. We have enabled all |
simonb | 0:db49fab3bd78 | 74 | //of them here. |
simonb | 0:db49fab3bd78 | 75 | adc.interrupt_state(p15,1); |
simonb | 0:db49fab3bd78 | 76 | |
simonb | 0:db49fab3bd78 | 77 | printf("Burst mode, printing once per second...\n"); |
simonb | 0:db49fab3bd78 | 78 | while (1) { |
simonb | 0:db49fab3bd78 | 79 | snap = 1; |
simonb | 0:db49fab3bd78 | 80 | wait(0.5); |
simonb | 0:db49fab3bd78 | 81 | for (i=0; i<6; i++) { |
simonb | 0:db49fab3bd78 | 82 | printf ("CH %u Min :", i); |
simonb | 0:db49fab3bd78 | 83 | bar(adc_min[i]); |
simonb | 0:db49fab3bd78 | 84 | printf ("CH %u Av :", i); |
simonb | 0:db49fab3bd78 | 85 | bar(adc_av[i]); |
simonb | 0:db49fab3bd78 | 86 | printf ("CH %u Max :", i); |
simonb | 0:db49fab3bd78 | 87 | bar(adc_max[i]); |
simonb | 0:db49fab3bd78 | 88 | printf("\n"); |
simonb | 0:db49fab3bd78 | 89 | } |
simonb | 0:db49fab3bd78 | 90 | printf("......................................................\n"); |
simonb | 0:db49fab3bd78 | 91 | wait(0.5); |
simonb | 0:db49fab3bd78 | 92 | } |
simonb | 0:db49fab3bd78 | 93 | } |