![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Full ADC driver with demo
main.cpp@0:a2562dfbf543, 2010-02-10 (annotated)
- Committer:
- simonb
- Date:
- Wed Feb 10 13:11:26 2010 +0000
- Revision:
- 0:a2562dfbf543
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
simonb | 0:a2562dfbf543 | 1 | |
simonb | 0:a2562dfbf543 | 2 | #define SAMPLE_RATE 150000 |
simonb | 0:a2562dfbf543 | 3 | |
simonb | 0:a2562dfbf543 | 4 | #include "mbed.h" |
simonb | 0:a2562dfbf543 | 5 | #include "adc.h" |
simonb | 0:a2562dfbf543 | 6 | |
simonb | 0:a2562dfbf543 | 7 | DigitalOut int_led(LED1); |
simonb | 0:a2562dfbf543 | 8 | |
simonb | 0:a2562dfbf543 | 9 | //Initialise ADC to maximum SAMPLE_RATE and cclk divide set to 1 |
simonb | 0:a2562dfbf543 | 10 | ADC adc(SAMPLE_RATE, 1); |
simonb | 0:a2562dfbf543 | 11 | |
simonb | 0:a2562dfbf543 | 12 | //Toggle LED on interrupt |
simonb | 0:a2562dfbf543 | 13 | void led_toggle(int chan, uint32_t value) { |
simonb | 0:a2562dfbf543 | 14 | int_led = !int_led; |
simonb | 0:a2562dfbf543 | 15 | } |
simonb | 0:a2562dfbf543 | 16 | |
simonb | 0:a2562dfbf543 | 17 | //Report ADC value on interrupt |
simonb | 0:a2562dfbf543 | 18 | void print_value(int chan, uint32_t value) { |
simonb | 0:a2562dfbf543 | 19 | printf("ADC interrupt on pin %u, value=%04u.\n", |
simonb | 0:a2562dfbf543 | 20 | adc.channel_to_pin_number(chan), (value >> 4) & 0xFFF); |
simonb | 0:a2562dfbf543 | 21 | } |
simonb | 0:a2562dfbf543 | 22 | |
simonb | 0:a2562dfbf543 | 23 | |
simonb | 0:a2562dfbf543 | 24 | int main() { |
simonb | 0:a2562dfbf543 | 25 | int i; |
simonb | 0:a2562dfbf543 | 26 | |
simonb | 0:a2562dfbf543 | 27 | printf("Requested max sample rate is %u, actual max sample rate is %u.\n", |
simonb | 0:a2562dfbf543 | 28 | SAMPLE_RATE, adc.actual_sample_rate()); |
simonb | 0:a2562dfbf543 | 29 | while (1) { |
simonb | 0:a2562dfbf543 | 30 | |
simonb | 0:a2562dfbf543 | 31 | //Set up ADC on pin 20 |
simonb | 0:a2562dfbf543 | 32 | adc.setup(p20,1); |
simonb | 0:a2562dfbf543 | 33 | //Set up ADC on pin 19 |
simonb | 0:a2562dfbf543 | 34 | adc.setup(p19,1); |
simonb | 0:a2562dfbf543 | 35 | wait(1); |
simonb | 0:a2562dfbf543 | 36 | |
simonb | 0:a2562dfbf543 | 37 | //Measure pin 20 |
simonb | 0:a2562dfbf543 | 38 | adc.select(p20); |
simonb | 0:a2562dfbf543 | 39 | //Start ADC conversion |
simonb | 0:a2562dfbf543 | 40 | adc.start(); |
simonb | 0:a2562dfbf543 | 41 | //Wait for it to complete |
simonb | 0:a2562dfbf543 | 42 | while(!adc.done(p20)); |
simonb | 0:a2562dfbf543 | 43 | printf("Measured value on pin 20 is %04u.\n", adc.read(p20)); |
simonb | 0:a2562dfbf543 | 44 | wait(1); |
simonb | 0:a2562dfbf543 | 45 | |
simonb | 0:a2562dfbf543 | 46 | //Measure pin 19 |
simonb | 0:a2562dfbf543 | 47 | adc.select(p19); |
simonb | 0:a2562dfbf543 | 48 | //Start ADC conversion |
simonb | 0:a2562dfbf543 | 49 | adc.start(); |
simonb | 0:a2562dfbf543 | 50 | //Wait for it to complete |
simonb | 0:a2562dfbf543 | 51 | while(!adc.done(p19)); |
simonb | 0:a2562dfbf543 | 52 | printf("Measured value on pin 19 is %04u.\n", adc.read(p19)); |
simonb | 0:a2562dfbf543 | 53 | wait(1); |
simonb | 0:a2562dfbf543 | 54 | |
simonb | 0:a2562dfbf543 | 55 | |
simonb | 0:a2562dfbf543 | 56 | //Append an interrupt handler that prints the channel and value |
simonb | 0:a2562dfbf543 | 57 | adc.append(print_value); |
simonb | 0:a2562dfbf543 | 58 | //Measure pin 20 |
simonb | 0:a2562dfbf543 | 59 | adc.select(p20); |
simonb | 0:a2562dfbf543 | 60 | //Enable the interrupt |
simonb | 0:a2562dfbf543 | 61 | adc.interrupt_state(p20,1); |
simonb | 0:a2562dfbf543 | 62 | //Start ADC conversion |
simonb | 0:a2562dfbf543 | 63 | adc.start(); |
simonb | 0:a2562dfbf543 | 64 | //Wait for it to complete |
simonb | 0:a2562dfbf543 | 65 | while(!adc.done(p20)); |
simonb | 0:a2562dfbf543 | 66 | wait(1); |
simonb | 0:a2562dfbf543 | 67 | |
simonb | 0:a2562dfbf543 | 68 | //Unset pin 20 |
simonb | 0:a2562dfbf543 | 69 | adc.setup(p20,0); |
simonb | 0:a2562dfbf543 | 70 | |
simonb | 0:a2562dfbf543 | 71 | //Togle LED on each converstion. |
simonb | 0:a2562dfbf543 | 72 | //Should be 12.5KHz on LED for all 6 pins. |
simonb | 0:a2562dfbf543 | 73 | //Sample rate=150KHz / 6 channels / 2 |
simonb | 0:a2562dfbf543 | 74 | adc.append(led_toggle); |
simonb | 0:a2562dfbf543 | 75 | |
simonb | 0:a2562dfbf543 | 76 | //Prepare for burst mode on all ADC pins |
simonb | 0:a2562dfbf543 | 77 | adc.startmode(0,0); |
simonb | 0:a2562dfbf543 | 78 | adc.burst(1); |
simonb | 0:a2562dfbf543 | 79 | adc.setup(p20,1); |
simonb | 0:a2562dfbf543 | 80 | adc.setup(p19,1); |
simonb | 0:a2562dfbf543 | 81 | adc.setup(p18,1); |
simonb | 0:a2562dfbf543 | 82 | adc.setup(p17,1); |
simonb | 0:a2562dfbf543 | 83 | adc.setup(p16,1); |
simonb | 0:a2562dfbf543 | 84 | adc.setup(p15,1); |
simonb | 0:a2562dfbf543 | 85 | //For burst mode, only one interrupt is required |
simonb | 0:a2562dfbf543 | 86 | //which can be on any enabled pin. We have enabled all |
simonb | 0:a2562dfbf543 | 87 | //of them here. |
simonb | 0:a2562dfbf543 | 88 | adc.interrupt_state(p15,1); |
simonb | 0:a2562dfbf543 | 89 | printf("Burst mode, printing once per second...\n"); |
simonb | 0:a2562dfbf543 | 90 | for (i=0; i<5; i++) |
simonb | 0:a2562dfbf543 | 91 | { |
simonb | 0:a2562dfbf543 | 92 | printf("%04u %04u %04u %04u %04u %04u\n", |
simonb | 0:a2562dfbf543 | 93 | adc.read(p20), |
simonb | 0:a2562dfbf543 | 94 | adc.read(p19), |
simonb | 0:a2562dfbf543 | 95 | adc.read(p18), |
simonb | 0:a2562dfbf543 | 96 | adc.read(p17), |
simonb | 0:a2562dfbf543 | 97 | adc.read(p16), |
simonb | 0:a2562dfbf543 | 98 | adc.read(p15)); |
simonb | 0:a2562dfbf543 | 99 | wait(1); |
simonb | 0:a2562dfbf543 | 100 | } |
simonb | 0:a2562dfbf543 | 101 | adc.burst(0); |
simonb | 0:a2562dfbf543 | 102 | adc.setup(p20,0); |
simonb | 0:a2562dfbf543 | 103 | adc.setup(p19,0); |
simonb | 0:a2562dfbf543 | 104 | adc.setup(p18,0); |
simonb | 0:a2562dfbf543 | 105 | adc.setup(p17,0); |
simonb | 0:a2562dfbf543 | 106 | adc.setup(p16,0); |
simonb | 0:a2562dfbf543 | 107 | adc.setup(p15,0); |
simonb | 0:a2562dfbf543 | 108 | adc.interrupt_state(p20,0); |
simonb | 0:a2562dfbf543 | 109 | adc.interrupt_state(p19,0); |
simonb | 0:a2562dfbf543 | 110 | adc.interrupt_state(p18,0); |
simonb | 0:a2562dfbf543 | 111 | adc.interrupt_state(p17,0); |
simonb | 0:a2562dfbf543 | 112 | adc.interrupt_state(p16,0); |
simonb | 0:a2562dfbf543 | 113 | adc.interrupt_state(p15,0); |
simonb | 0:a2562dfbf543 | 114 | |
simonb | 0:a2562dfbf543 | 115 | printf("\n"); |
simonb | 0:a2562dfbf543 | 116 | } |
simonb | 0:a2562dfbf543 | 117 | } |