Professional Documents
Culture Documents
com/
wiki/LPC2148_ADC_Programming
The below block diagram shows the ADC input pins multiplexed with other GPIO
pins.
The ADC pin can be enabled by configuring the corresponding PINSEL register to
select ADC function.
When the ADC function is selected for that pin in the Pin Select register, other Digital
signals are disconnected from the ADC input pins.
ADC Registers
The below table shows the registers associated with LPC1768 ADC.
We are going to focus only on ADCR and ADGDR as these are sufficient for simple
A/D conversion.
However once you are familer with LPC1768 ADC, you can explore the other
features and the associated registers.
Register Description
ADGDR A/D Global Data Register: This register contains the ADC’s DONE bit and the result o
ADDR0 - ADDR7 A/D Channel Data Register: Contains the recent ADC value for respective channel
ADSTAT A/D Status Register: Contains DONE & OVERRUN flag for all the ADC channels
ADCR
Bit 16 – BURST
This bit is used for BURST conversion. If this bit is set the ADC module will do the
conversion for all the channels that are selected(SET) in SEL bits.
CLearing this bit will disable the BURST conversion.
Bit 20 – CLKS
This field selects the number of clocks used for each conversion in Burst mode, and
the number of bits of accuracy of the result in the RESULT bits of ADDR, between
11 clocks (10 bits) and 4 clocks (3 bits).
000-11 clocks / 10 bits accuracy
001-10 clocks / 9 bits accuracy
010- 9 clocks / 8 bits accuracy
011- 8 clocks / 7 bits accuracy
100- 7 clocks / 6 bits accuracy
101- 6 clocks / 5 bits accuracy
110- 5 clocks / 4 bits accuracy
111- 4 clocks / 3 bits accuracy
Bit 21 – PDN : Power Down Mode
Setting this bit brings ADC out of power down mode and makes it operational.
Clearing this bit will power down the ADC.
The remaining cases (010 to 111) are about starting conversion on occurrence of
edge on a particular CAP or MAT pin.
Bit 27 - EDGE
This bit is significant only when the START field contains 010-111. It starts
conversion on selected CAP or MAT input.
0 - On Falling Edge
1 - On Rising Edge
ADGDR
Bit 30 - OVERRUN
This bit is set during the BURST mode where the previous conversion data is
overwritten by the new A/D conversion value.
Bit 31 - DONE
This bit is set to 1 when an A/D conversion completes. It is cleared when this register
is read and when the ADCR is written. If the ADCR is written while a conversion is
still in progress, this bit is set and a new conversion is started.
Apart from ADC Global Data register there are more 8 ADC Data registers (one Data
register per ADC channel). DONE and OVERRUN bits for each channel can be
monitored separately from the bits present in ADC Status register.
One can use the A/D Global Data Register to read all data from the ADC else use
the A/D Channel Data Registers. It is important to use one method consistently
because the DONE and OVERRUN flags can otherwise get out of synch between
the AD0GDR and the A/D Channel Data Registers, potentially causing erroneous
interrupts or DMA activity.
Hardware Connections
Steps for Configuring ADC
Below are the steps for configuring the LPC1768 ADC.
1. Configure the GPIO pin for ADC function using PINSEL register.
2. Enable the CLock to ADC module.
3. Deselect all the channels and Power on the internal ADC module by setting
ADCR.PDN bit.
4. Select the Particular channel for A/D conversion by setting the corresponding
bits in ADCR.SEL
5. Set the ADCR.START bit for starting the A/D conversion for selected channel.
6. Wait for the conversion to complete, ADGR.DONE bit will be set once
conversion is over.
7. Read the 10-bit A/D value from ADGR.RESULT.
8. Use it for further processing or just display on LCD.
Code Examples
Example 1
Here we are going to do the A/D conversion for only AD0.1. The result of the A/D
conversion will be displayed on the LCD.
#include<lpc214x.h>
#include "lcd.h" //ExploreEmbedded LCD library which contains the lcd routines
#include "delay.h" //ExploreEmbedded delay library which contains the delay routines
#include "stdutils.h"
#include "systemInit.h"
int main()
{
uint16_t adc_result=0;
SystemInit(); //Clock and PLL configuration
AD0CR = ((1<<SBIT_PDN) | (10<<SBIT_CLCKDIV)); //Set the clock and Power ON ADC module
while(1)
{
AD0CR |= 0x02; /* Select Channel 0 by setting 0th bit of ADCR */
DELAY_us(10); /* allow the channel voltage to stabilize*/
adc_result = (AD0GDR >> SBIT_RESULT) & 0x3ff; /*Read the 10bit adc result*/