User manual

LPCXpresso Experiment Kit - User’s Guide
Page 53
Copyright 2013 © Embedded Artists AB
7.6 Read an Analog Input
In this experiment you will learn how to convert an analog signal to a digital value. There is a 10-bit
ADC (Analog to Digital Converter) on the LPC111x microcontroller. The ADC is described in chapter
25 - LPC111x/LPC11Cxx ADC in the LPC111x user’s manual. The ADC peripheral needs some
initialization before it can be used. Also, the pin-muxing needs to set the analog input functionality to
the pins. There are 8 inputs to the ADC and hence 8 pins that can be initialized. The function outlined
below initialize the first four pins as analog inputs. The function also initializes the ADC to be ready for
conversion commands. Check the LPC111x user’s manual and make sure you understand the
different register initializations below.
/*****************************************************************************
** Function name: ADCInit
**
** Descriptions: initialize ADC channel
**
** parameters: ADC clock rate
** Returned value: None
**
*****************************************************************************/
void ADCInit( uint32_t ADC_Clk )
{
/* Disable Power down bit to the ADC block. */
LPC_SYSCON->PDRUNCFG &= ~(0x1<<4);
/* Enable AHB clock to the ADC. */
LPC_SYSCON->SYSAHBCLKCTRL |= (1<<13);
/* Set pin-mux correct for ADC-IN0, -IN1, -IN2 and IN3 */
LPC_IOCON->JTAG_TDI_PIO0_11 &= ~0x8F;
LPC_IOCON->JTAG_TDI_PIO0_11 |= 0x02; /* ADC IN0 */
LPC_IOCON->JTAG_TMS_PIO1_0 &= ~0x8F;
LPC_IOCON->JTAG_TMS_PIO1_0 |= 0x02; /* ADC IN1 */
LPC_IOCON->JTAG_TDO_PIO1_1 &= ~0x8F;
LPC_IOCON->JTAG_TDO_PIO1_1 |= 0x02; /* ADC IN2 */
LPC_IOCON->JTAG_nTRST_PIO1_2 &= ~0x8F;
LPC_IOCON->JTAG_nTRST_PIO1_2 |= 0x02; /* ADC IN3 */
LPC_ADC->CR =
( 0x01 << 0 ) | /* SEL=1, select channel 0~7 on ADC0 */
/* CLKDIV = Fpclk / 1000000 - 1 */
(((SystemCoreClock/LPC_SYSCON->SYSAHBCLKDIV)/ADC_Clk-1)<<8) |
( 0x0 << 16 ) | /* BURST = 0, no BURST, software controlled */
( 0x0 << 17 ) | /* CLKS = 0, 11 clocks/10 bits */
( 0x0 << 24 ) | /* START = 0 A/D conversion stops */
( 0x0 << 27 ); /* EDGE = 0 (CAP/MAT singal falling,trigger A/D conversion) */
}
7.6.1 Lab 5a: Read Trimming Potentiometer
In this experiment we shall read the value of analog input #0. There are two trimming potentiometers,
R7 and R20, on page 4 in the schematic. One of them, R7, is connected to GPIO_11-AIN0 and the
other one, R20, is connected to GPIO_12-AIN1. These signals correspond to AIN0 and AIN1 on the
ADC. The trimming potentiometers are connected to ground and +3.3V in each end. By rotating the
trimming potentiometers it is possible to adjust the analog voltage to any value between 0V and +3.3V.
This corresponds exactly to the input range of the ADC. 0V input gives the converted value 0 and
+3.3V input gives the converted value 1023.
Figure 23 below shows the schematics around R7. The series resistor, R6 (330ohm), is just for
protection in case GPIO_11-AIN0 (by mistake) becomes an output. Figure 24 shows the breadboard
setup for connecting the trimming potentiometer to the LPCXpresso board, pin 15.