Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 // analog input p20 (ADC0[5], pin 3) test
00002 
00003 #include "mbed.h"
00004 
00005 int main() {
00006 
00007     // power on, clk divider /4
00008     LPC_SC->PCONP |= (1 << 12);          
00009     LPC_SC->PCLKSEL0 &= ~(0x3 << 24);    
00010         
00011     // software-controlled ADC settings
00012     LPC_ADC->ADCR = (0 << 0) // SEL: 0 = no channels selected
00013               | (25 << 8)    // CLKDIV: PCLK max ~= 25MHz, /25 to give safe 1MHz
00014               | (0 << 16)    // BURST: 0 = software control 
00015               | (0 << 17)    // CLKS: not applicable 
00016               | (1 << 21)    // PDN: 1 = operational
00017               | (0 << 24)    // START: 0 = no start
00018               | (0 << 27);   // EDGE: not applicable
00019 
00020     // setup P1_31 as sel 3 (ADC), mode 2 (no pull)    
00021     LPC_PINCON->PINSEL3 &= ~((unsigned int)0x3 << 30);
00022     LPC_PINCON->PINSEL3 |= (unsigned int)0x3 << 30;
00023     
00024     LPC_PINCON->PINMODE3 &= ~((unsigned int)0x3 << 30);
00025     LPC_PINCON->PINMODE3 |= (unsigned int)0x2 << 30;
00026 
00027     while(1) {
00028         // Select channel and start conversion
00029         LPC_ADC->ADCR &= ~0xFF;
00030         LPC_ADC->ADCR |= 1 << 5; // ADC0[5]
00031         LPC_ADC->ADCR |= 1 << 24;
00032     
00033         // Repeatedly get the sample data until DONE bit
00034         unsigned int data;
00035         do {
00036            data = LPC_ADC->ADGDR;
00037         } while ((data & ((unsigned int)1 << 31)) == 0);
00038 
00039         // Stop conversion    
00040         LPC_ADC->ADCR &= ~(1 << 24);
00041     
00042         printf("0x%3X\n", data);   
00043     }
00044 }