Dependencies:   mbed

Committer:
simon
Date:
Mon Jan 04 14:18:40 2010 +0000
Revision:
0:3e987e5f426b

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
simon 0:3e987e5f426b 1 // analog input p20 (ADC0[5], pin 3) test
simon 0:3e987e5f426b 2
simon 0:3e987e5f426b 3 #include "mbed.h"
simon 0:3e987e5f426b 4
simon 0:3e987e5f426b 5 int main() {
simon 0:3e987e5f426b 6
simon 0:3e987e5f426b 7 // power on, clk divider /4
simon 0:3e987e5f426b 8 LPC_SC->PCONP |= (1 << 12);
simon 0:3e987e5f426b 9 LPC_SC->PCLKSEL0 &= ~(0x3 << 24);
simon 0:3e987e5f426b 10
simon 0:3e987e5f426b 11 // software-controlled ADC settings
simon 0:3e987e5f426b 12 LPC_ADC->ADCR = (0 << 0) // SEL: 0 = no channels selected
simon 0:3e987e5f426b 13 | (25 << 8) // CLKDIV: PCLK max ~= 25MHz, /25 to give safe 1MHz
simon 0:3e987e5f426b 14 | (0 << 16) // BURST: 0 = software control
simon 0:3e987e5f426b 15 | (0 << 17) // CLKS: not applicable
simon 0:3e987e5f426b 16 | (1 << 21) // PDN: 1 = operational
simon 0:3e987e5f426b 17 | (0 << 24) // START: 0 = no start
simon 0:3e987e5f426b 18 | (0 << 27); // EDGE: not applicable
simon 0:3e987e5f426b 19
simon 0:3e987e5f426b 20 // setup P1_31 as sel 3 (ADC), mode 2 (no pull)
simon 0:3e987e5f426b 21 LPC_PINCON->PINSEL3 &= ~((unsigned int)0x3 << 30);
simon 0:3e987e5f426b 22 LPC_PINCON->PINSEL3 |= (unsigned int)0x3 << 30;
simon 0:3e987e5f426b 23
simon 0:3e987e5f426b 24 LPC_PINCON->PINMODE3 &= ~((unsigned int)0x3 << 30);
simon 0:3e987e5f426b 25 LPC_PINCON->PINMODE3 |= (unsigned int)0x2 << 30;
simon 0:3e987e5f426b 26
simon 0:3e987e5f426b 27 while(1) {
simon 0:3e987e5f426b 28 // Select channel and start conversion
simon 0:3e987e5f426b 29 LPC_ADC->ADCR &= ~0xFF;
simon 0:3e987e5f426b 30 LPC_ADC->ADCR |= 1 << 5; // ADC0[5]
simon 0:3e987e5f426b 31 LPC_ADC->ADCR |= 1 << 24;
simon 0:3e987e5f426b 32
simon 0:3e987e5f426b 33 // Repeatedly get the sample data until DONE bit
simon 0:3e987e5f426b 34 unsigned int data;
simon 0:3e987e5f426b 35 do {
simon 0:3e987e5f426b 36 data = LPC_ADC->ADGDR;
simon 0:3e987e5f426b 37 } while ((data & ((unsigned int)1 << 31)) == 0);
simon 0:3e987e5f426b 38
simon 0:3e987e5f426b 39 // Stop conversion
simon 0:3e987e5f426b 40 LPC_ADC->ADCR &= ~(1 << 24);
simon 0:3e987e5f426b 41
simon 0:3e987e5f426b 42 printf("0x%3X\n", data);
simon 0:3e987e5f426b 43 }
simon 0:3e987e5f426b 44 }