Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
main.cpp
00001 00002 #define SAMPLE_RATE 150000 00003 00004 #include "mbed.h" 00005 #include "adc.h" 00006 00007 DigitalOut int_led(LED1); 00008 00009 //Initialise ADC to maximum SAMPLE_RATE and cclk divide set to 1 00010 ADC adc(SAMPLE_RATE, 1); 00011 00012 //Toggle LED on interrupt 00013 void led_toggle(int chan, uint32_t value) { 00014 int_led = !int_led; 00015 } 00016 00017 //Report ADC value on interrupt 00018 void print_value(int chan, uint32_t value) { 00019 printf("ADC interrupt on pin %u, value=%04u.\n", 00020 adc.channel_to_pin_number(chan), (value >> 4) & 0xFFF); 00021 } 00022 00023 00024 int main() { 00025 int i; 00026 00027 printf("Requested max sample rate is %u, actual max sample rate is %u.\n", 00028 SAMPLE_RATE, adc.actual_sample_rate()); 00029 while (1) { 00030 00031 //Set up ADC on pin 20 00032 adc.setup(p20,1); 00033 //Set up ADC on pin 19 00034 adc.setup(p19,1); 00035 wait(1); 00036 00037 //Measure pin 20 00038 adc.select(p20); 00039 //Start ADC conversion 00040 adc.start(); 00041 //Wait for it to complete 00042 while(!adc.done(p20)); 00043 printf("Measured value on pin 20 is %04u.\n", adc.read(p20)); 00044 wait(1); 00045 00046 //Measure pin 19 00047 adc.select(p19); 00048 //Start ADC conversion 00049 adc.start(); 00050 //Wait for it to complete 00051 while(!adc.done(p19)); 00052 printf("Measured value on pin 19 is %04u.\n", adc.read(p19)); 00053 wait(1); 00054 00055 00056 //Append an interrupt handler that prints the channel and value 00057 adc.append(print_value); 00058 //Measure pin 20 00059 adc.select(p20); 00060 //Enable the interrupt 00061 adc.interrupt_state(p20,1); 00062 //Start ADC conversion 00063 adc.start(); 00064 //Wait for it to complete 00065 while(!adc.done(p20)); 00066 wait(1); 00067 00068 //Unset pin 20 00069 adc.setup(p20,0); 00070 00071 //Togle LED on each converstion. 00072 //Should be 12.5KHz on LED for all 6 pins. 00073 //Sample rate=150KHz / 6 channels / 2 00074 adc.append(led_toggle); 00075 00076 //Prepare for burst mode on all ADC pins 00077 adc.startmode(0,0); 00078 adc.burst(1); 00079 adc.setup(p20,1); 00080 adc.setup(p19,1); 00081 adc.setup(p18,1); 00082 adc.setup(p17,1); 00083 adc.setup(p16,1); 00084 adc.setup(p15,1); 00085 //For burst mode, only one interrupt is required 00086 //which can be on any enabled pin. We have enabled all 00087 //of them here. 00088 adc.interrupt_state(p15,1); 00089 printf("Burst mode, printing once per second...\n"); 00090 for (i=0; i<5; i++) 00091 { 00092 printf("%04u %04u %04u %04u %04u %04u\n", 00093 adc.read(p20), 00094 adc.read(p19), 00095 adc.read(p18), 00096 adc.read(p17), 00097 adc.read(p16), 00098 adc.read(p15)); 00099 wait(1); 00100 } 00101 adc.burst(0); 00102 adc.setup(p20,0); 00103 adc.setup(p19,0); 00104 adc.setup(p18,0); 00105 adc.setup(p17,0); 00106 adc.setup(p16,0); 00107 adc.setup(p15,0); 00108 adc.interrupt_state(p20,0); 00109 adc.interrupt_state(p19,0); 00110 adc.interrupt_state(p18,0); 00111 adc.interrupt_state(p17,0); 00112 adc.interrupt_state(p16,0); 00113 adc.interrupt_state(p15,0); 00114 00115 printf("\n"); 00116 } 00117 }
Generated on Thu Jul 14 2022 07:45:03 by
1.7.2