LPC2148 – ADC Tutorial

This article is a continuation of the series of tutorials on the LPC2148 Microcontroller (ARM7). The aim of this series is to provide easy and practical examples that anyone can understand. In the previous tutorial, we have seen LPC2148 (ARM7) UART. Now, we will see LPC2148 ADC Tutorial…

Suggestion to read

LPC2148 ADC Tutorial

Introduction

Microcontrollers are very useful especially when it comes to communicating with other devices, such as sensors, motors, switches, memory, and even another microcontroller. As we all know many interface methods have been developed over years to solve the complex problems of balancing needs of features, cost, size, power consumption, reliability, etc. but the ADC Analog-to-Digital converter remains famous among all. Using this ADC we can connect any type of Analog sensor.

LPC2148 ADC

LPC2148 has two inbuilt 10-bit Successive Approximation ADC. ADC0 has six channels (AD0.1-AD0.6). ADC1 has 8-Channels (AD1.0-AD1.7)ADC operating frequency is 4.5 MHz (max.), operating frequency decides the conversion time.

 The ADC reference voltage is measured across GND to VREF, meaning it can do the conversion within this range. Usually, the VREFPis connected to VDD.

Features

  • 10-bit successive approximation analog to digital converter (one in LPC2141/2 and two in LPC2144/6/8).
  • Input multiplexing among 6 or 8 pins (ADC0 and ADC1).
  • Power-down mode.
  • Measurement range 0 V to VREF (typically 3 V; not to exceed VDDA voltage level).
  • 10 bit conversion time ≥2.44 µs.
  • Burst conversion mode for single or multiple inputs.
  • Optional conversion on transition on input pin or Timer Match signal.
  • Global Start command for both converters (LPC2144/6/8 only).

As LPC2148 works on 3.3 volts, this will be the ADC reference voltage.
Now the Resolution of ADC = 3.3/(2^{10}) = 3.3/1024 =0.003222 = 3.2mV

ADC Pins In LPC2148

Let’s have a look at the table which illustrate ADC related channels and pins:

ADC0

ADC ChannelPin NumberFunctions of that PinAssociated PINSEL Register Bits
ADC 0.1P0.28GPIO, AD0.1, CAP0.2, MAT0.224,25 bits of PINSEL1
ADC 0.2P0.29GPIO, AD0.2, CAP0.3, MAT0.326,27 bits of PINSEL1
ADC 0.3P0.30GPIO, AD0.3, EINT3, CAP0.028,29 bits of PINSEL1
ADC 0.4P0.25GPIO, AD0.4, AOUT18,19 bits of PINSEL1
ADC 0.6P0.4GPIO, SCK0, CAP0.1 , AD0.608,09 bits of PINSEL0
ADC 0.7P0.5GPIO, MISO0, MAT0.1 , AD0.710,11 bits of PINSEL0

ADC1

ADC ChannelPin NumberFunctions of that PinAssociated PINSEL Register Bits
ADC 1.0P0.6GPIO, MOSI0, CAP0.2, AD1.012,13 bits of PINSEL0
ADC 1.1P0.8GPIO, TXD1, PWM4, AD1.116,17 bits of PINSEL0
ADC 1.2P0.10GPIO, RTS1, CAP1.0, AD1.220,21 bits of PINSEL1
ADC 1.3P0.12GPIO, DSR1, MAT1.0, AD1.324,25 bits of PINSEL1
ADC 1.4P0.13GPIO, DTR1, MAT1.1 , AD1.426,27 bits of PINSEL3
ADC 1.5P0.15GPIO, RI1, EINT2 , AD1.530,31 bits of PINSEL3
ADC 1.6P0.21GPIO, PWM5, AD1.6, CAP1.310,11 bits of PINSEL1
ADC 1.7P0.22GPIO, AD1.7, CAP0.0, MAT0.012,13 bits of PINSEL1

Registers Used For ADC

There are several registers that will be used to set up and configure the ADC feature in LPC2148. The two registers we will be concerned about: ADCR (A/D Control Register) and ADGDR (A/D Global Data register).

RegisterDescription
ADxCRA/D COntrol Register: Used for Configuring the ADC
ADxGDRA/D Global Data Register: This register contains the ADC’s DONE bit and the result of the most recent A/D conversion
ADxINTENA/D Interrupt Enable Register
ADxDR0 - ADxDR7A/D Channel Data Register: Contains the recent ADC value for respective channel
ADxSTATA/D Status Register: Contains DONE & OVERRUN flag for all the ADC channels
ADxGSRA/D Global Start Register: This address can be written (in the AD0 address range) to start conversions in both A/D converters simultaneously.

Note : x = 0/1 (ie ADC0 or ADC1)

Here we will see only ADC0. But ADC1 also the same as ADC0 except for the Register name.

AD0CR – A/D Control Register

This is the main control register for AD0. Used for Configuring the ADC0.

AD0GDR – A/D Global Data Register

This is the global data register for the corresponding ADC module. It contains the ADC’s DONE bit and the result of the most recent A/D conversion.

AD0INTEN – A/D Interrupt Enable Register

Using this register interrupts can be enabled or disabled for any ADC channels.

AD0DR0 to AD0DR7 – A/D Data registers

This register contains the result of the most recent conversion completed on the corresponding channel [0 to 7].

AD0STAT – A/D Status register

This register contains DONE and OVERRUN flags for all of the A/D channels along with the A/D interrupt flag.

AD0GSR – A/D Global Start Register

This register is used to simultaneously start the conversion process of both ADC modules.

Note: Though there are some more registers, we are restricting ourselves to use these registers only as this will be more convenient.

Circuit Diagram

LCD:

  • RS:  P1.16
  • RW: P1.17
  • EN: P1.18
  • Data Lines: P1.24 – P1.31

ADC:

  • P0.4 (ADC0.6) : Potentiometer

LPC2148 ADC Tutorial

Programming Algorithm

Below are the steps for configuring the LPC1768 ADC.

  1. Configure the GPIO pin for the ADC function using the PINSEL register.
  2. Enable the CLock to ADC module.
  3. Power on the internal ADC module by setting ADCR.PDN bit (ADCR’s 21st bit).
  4. Select the Particular channel for A/D conversion by setting the corresponding bits in ADCR.SEL (bit 0 – bit 7).
  5. Set the ADCR.START bit for starting the A/D conversion for the selected channel (ADCR’s 24th bit).
  6. Wait for the conversion to complete, ADGDR. The DONE bit will be set once the conversion is over.
  7. Read the 10-bit A/D value from ADGdR.RESULT.

Code

In our tutorial, we are using ADC0.6 (ADC0 6th channel). So P0.4 is a pin for that ADC0.6.

You can download this project from here.

Code.c

#include<lpc214x.h>
#include "LCD.h"
#include "ADC.h"

unsigned int val;

int main()
{
    IO1DIR=0xffffffff;
    IO0DIR=0x00000000;
    PINSEL0=0x0300;
    VPBDIV=0x02;
    lcd_init();
    show("ADC Value : ");
    while(1) {
        cmd(0x8b);
        val=adc(0,6);
        dat((val/1000)+48);
        dat(((val/100)%10)+48);
        dat(((val/10)%10)+48);
        dat((val%10)+48);
    }
}

ADC.H

unsigned int val;
unsigned int adc(int,int);

unsigned int adc(int no,int ch)
{
    switch(no)                                    //select adc
    {
        case 0: AD0CR=0x00200600|(1<<ch);       //select channel
                AD0CR|=(1<<24);                 //start conversion
                while((AD0GDR& (1<<31))==0);
                val=AD0GDR;
                break;

        case 1: AD1CR=0x00200600|(1<<ch);       //select channel
                AD1CR|=(1<<24);                 //start conversion
                while((AD1GDR&(1<<31))==0);
                val=AD1GDR;
                break;
    }
    val=(val >> 6) & 0x03FF;                   // bit 6:15 is 10 bit AD value

    return val;
}

LCD.H

#define bit(x) (1<<x)

void lcd_init(void);
void cmd(unsigned char a);
void dat(unsigned char b);
void show(unsigned char *s);
void lcd_delay(void);

void lcd_init()
{
    cmd(0x38);
    cmd(0x0e);
    cmd(0x01);
    cmd(0x06);
    cmd(0x0c);
    cmd(0x80);
}

void cmd(unsigned char a)
{
    IO1CLR=0xFF070000;
    IO1SET=(a<<24);
    IO1CLR=bit(16);             //rs=0
    IO1CLR=bit(17);             //rw=0
    IO1SET=bit(18);             //en=1
    lcd_delay();
    IO1CLR=bit(18);             //en=0
}

void dat(unsigned char b)
{
    IO1CLR=0xFF070000;
    IO1SET=(b<<24);
    IO1SET=bit(16);             //rs=1
    IO1CLR=bit(17);             //rw=0
    IO1SET=bit(18);             //en=1
    lcd_delay();
    IO1CLR=bit(18);             //en=0
}

void show(unsigned char *s)
{
    while(*s) {
        dat(*s++);
    }
}

void lcd_delay()
{
    unsigned int i;
    for(i=0;i<=3000;i++);
}

Output – LPC2148 ADC Tutorial

LPC2148 ADC Tutorial[ Please find the output image Here ]

That’s all guys… Now you can play around ADC. If you have any doubt please let us know. You can download this project from here.

In our next tutorial, we will see how to interface the LCD with LPC2148 (ARM7). If you want to use FreeRTOS on LPC2148, then please refer FreeRTOS series.

You can also read the below tutorials.

Linux Device Driver TutorialsC Programming Tutorials
FreeRTOS TutorialsNuttX RTOS Tutorials
RTX RTOS TutorialsInterrupts Basics
I2C Protocol – Part 1 (Basics)I2C Protocol – Part 2 (Advanced Topics)
STM32 TutorialsLPC2148 (ARM7) Tutorials
PIC16F877A Tutorials8051 Tutorials
Unit Testing in C TutorialsESP32-IDF Tutorials
Raspberry Pi TutorialsEmbedded Interview Topics
Reset Sequence in ARM Cortex-M4BLE Basics
VIC and NVIC in ARMSPI – Serial Peripheral Interface Protocol
STM32F7 Bootloader TutorialsRaspberry PI Pico Tutorials
STM32F103 Bootloader TutorialsRT-Thread RTOS Tutorials
Zephyr RTOS Tutorials - STM32Zephyr RTOS Tutorials - ESP32
AUTOSAR TutorialsUDS Protocol Tutorials
Product ReviewsSTM32 MikroC Bootloader Tutorial
VHDL Tutorials
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

12 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Table of Contents