by Rob Toulson and Tim Wilmshurst from textbook "Fast and Effective Embedded Systems Design: Applying the ARM mbed"

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /* Program Example 14.6: Explore ADC conversion times, programming control registers directly. ADC value is transferred to DAC, while an output pin is strobed to indicate conversion duration. Observe on oscilloscope
00002                                                                             */
00003 //….
00004 //….
00005 
00006 int main() {
00007   FIO2DIR0=0xFF;                      // set lower bits port 2 to output
00008   PINSEL1=0x00210000;  //set bits 21-20 to 10 for analog output (mbed p18) 
00009          //and bits 17-16 to 01 to enable ADC channel 1 (AD0.1, mbed pin 16)
00010 
00011 //initialise the ADC. 
00012 //….
00013 //….
00014   while(1){         // infinite loop
00015     // start A/D conversion by modifying bits in the AD0CR register 
00016     AD0CR &= (AD0CR & 0xFFFFFF00);  
00017     FIO2PIN0 |= 0x01;               // OR bit 0 with 1 to set pin high
00018     AD0CR |= (1 << ADC_channel) | (1 << 24);       
00019     // wait for it to finish by polling the ADC DONE bit         
00020     while((AD0GDR & 0x80000000) == 0) {
00021     }
00022     FIO2PIN0 &= ~0x01;             // AND bit 0 with 0 to set pin low
00023 
00024     ADCdata = AD0GDR;              // get the data from AD0GDR
00025     AD0CR &= 0xF8FFFFFF;           //stop ADC by setting START bits to zero
00026         
00027    // shift data 4 bits to right justify, and 2 more to give 10-bit ADC value
00028     ADCdata=(ADCdata>>6)&0x03FF;   //and mask      
00029     DACR=(ADCdata<<6);     //could be merged with previous line,
00030                               // but separated for clarity
00031     //delay();               //insert delay if wished
00032   }
00033 }
00034