1. Introduction

ADC stands for Analog to digital converter. This tutorial will teach you how to use the ADC to convert an analog voltage input to a digital number. For this tutorial the ADC library is required, this library can be downloaded from our repository. The library provides an easy way to convert an analog signal to a digital signal.

2. Includes

To begin it is needed to include the libraries. This is done by these commands:

#include "adc.h"
The libraries for the microcontroller itself and the variables have to be included to:

#include "lpc17xx.h"
#include "lpc_types.h"
3. Convert an analog signal

The ADC has 8 ports. The ports can be found in the following table:

ADC channel number

Port number

















In the main code the ADC has to be initialized with the command: ADCInit(ADC_CLK); ADC_CLK is a variable that is declared in the ADC library.
The command ADCInitExtra(ADC_CLK); only initializes Analog channel 0.4 and 0.5. When using this command you can still use the other ports as a digital port. When using a shield like the LCD shield that required the other ports to be a digital port use this command!

This is the command to read the ADC: ADCRead( channelNum ); The tutorial uses ADC channel AD0.5. The program needs a variable to put the data in.

The following commands are needed to read data from the ADC:
uint32_t Data = 0;
Data = ADCRead( 5 );

AD0.3 can also be used as analog output, AD0.6 and AD0.7 can also be used as UART. A pin can only use one function at the time.
Also, there is a setting in the adc.h file that needs to be 0 for normal use. Open adc.h, located in lpc17xx_lib --> include. look for the line "#define ADC_INTERRUPT_FLAG  1" and change the 1 in a 0. for the mode described below it should be a 1.
There is a faster way to use the ADC.

Normally the ADC starts, converts an analog value to a digital value and puts it in the variable. This takes some time and the code has to wait until the ADC is done. It is also possible to start the ADC, when the ADC is done it makes a variable high and then the data is ready to be read. The advantage is that the microcontroller can do other things and check once in a while if the ADC is done.
This is the code that you need for that:

        uint32_t Data = 0;

int main (void)
           ADCRead( 5 );
           //other code can be done here
           while ( !ADCIntDone );
           Data = ADCValue[5];
           ADCIntDone = 0;

With the command ADCRead(); the ADC conversion is started. When it is done the ADCIntDone variable will be 1. The ADC value is stored in the Data variable and when the ADCIntDone variable is 1 it will be made 0 again.



Copyright 2011. Joomla 1.7 templates - Joomla template maker. The Simplecortex is developed by BRC-Electronics