0% found this document useful (0 votes)
36 views21 pages

Lab Program 12 - Analog To Digital Converter

This document describes how to use the analog to digital converter (ADC) on an ARM microcontroller to convert an analog input voltage to a digital value. It discusses the key ADC components like resolution and step size. It provides code examples to initialize the ADC, read the digital output, and display it on an LCD screen. The document is intended to demonstrate using the internal ADC of an ARM controller to convert an analog input to digital output.

Uploaded by

Rockstar Ujjwal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views21 pages

Lab Program 12 - Analog To Digital Converter

This document describes how to use the analog to digital converter (ADC) on an ARM microcontroller to convert an analog input voltage to a digital value. It discusses the key ADC components like resolution and step size. It provides code examples to initialize the ADC, read the digital output, and display it on an LCD screen. The document is intended to demonstrate using the internal ADC of an ARM controller to convert an analog input to digital output.

Uploaded by

Rockstar Ujjwal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 21

PART B

Lab Program No. 12


Determine Digital output for a given Analog input
using Internal ADC of ARM controller.

Dr. Harish Kumar B T, Dept of CSE, BIT, Bangalore


Analog to Digital Converter (ADC)
• Analog to Digital Converter (ADC) is used to convert analog signal/voltage into its equivalent digital number so that

microcontroller can process that numbers and make it human readable.

• The ADC characterized by resolution. The resolution of ADC indicates the number of digital values.

Let’s take example: In LPC2148 microcontroller we have in-built 10-bit ADC.

• So for 10-bit ADC resolution is 10-bit and maximum value will be 1023.

• This means our digital value or discrete level lies between 0 to 1023.

• There is one more term important to understand while dealing with ADC and it is step size.

• Step size is the minimum change in input voltage which can be resolved by ADC. The concept of step size is closely associated

with the resolution of ADC.

Dr. Harish Kumar B T, Dept of CSE, BIT, Bangalore


Step Size Computation

• Calculate: ADC Resolution in LPC2148 ARM7


• So in this case we can measure minimum 3.23 mV (Approx.) with our
microcontroller.
• This is how step size defines an accuracy of ADC circuit.

Dr. Harish Kumar B T, Dept of CSE, BIT, Bangalore


Features of ADC in ARM LPC2148
• The ADC in LPC2148 ARM7 Microcontroller is 10-bit successive approximation analog to
digital converter. The features are listed as:
 LPC2148 has two inbuilt ADC Modules, named as ADC0 & ADC1.

 ADC0 has 6-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.
Supports power down mode.
 Burst conversion mode for single or multiple inputs.

• There are several registers associated with ADC feature but we will mainly discussing about

• ADC Control Register (ADCR) & ADC Global Data Register (ADGDR).

Dr. Harish Kumar B T, Dept of CSE, BIT, Bangalore


ADC Related channels and pins

We use ADO.4 to give the analog input and AD0.4 is connected to Pin P0.25.
POT variable resistor is connected to the P0.25
Dr. Harish Kumar B T, Dept of CSE, BIT, Bangalore
ADC Registers
• The two registers Used:
1. ADCR (A/D Control Register) and
2. ADGDR (A/D Global Data register)

Dr. Harish Kumar B T, Dept of CSE, BIT, Bangalore


Displaying Digital O/P
• 16X2 LCD is used
• D0-D7 – Data pins
• LCD works in two modes
- Byte Mode (D0-D7)
- Nibble Mode (D4-D7)
• Pin6 (E) – Enable pin: used to send data or command from the port
pins to LCD.
• Pin4 (RS) –Register Select Pin : Make RS=0 when sending command to select
the command register and RS=1 when sending data to select the data
register.

Dr. Harish Kumar B T, Dept of CSE, BIT, Bangalore


LCD Commands
Sr.No. Hex Code/Command Command to LCD instruction Register
1 01 Clear display screen
2 02 Return home
3 04 Decrement cursor (shift cursor to left)

4 06 Increment cursor (shift cursor to right)


5 05 Shift display right
6 07 Shift display left
7 08 Display off, cursor off
8 0A Display off, cursor on
9 0C Display on, cursor off
10 0E Display on, cursor blinking

11 0F Display on, cursor blinking

12 10 Shift cursor position to left

13 14 Shift cursor position to right

14 18 Shift the entire display to the left

15 1C Shift the entire display to the right

16 80 Force cursor to beginning ( 1st line)

17 C0 Force cursor to beginning ( 2nd line)


18 38 To Make Line-2 and 5*7 matrix operate in Byte mode
19 30 To Make Line-1 and 5*7 matrix operate in Byte mode
20 20 Makes LCD Line-1 to operate in 4 bit mode (Nibble Mode)
Makes LCD Line-2 to operate in 4 bit mode (Nibble Mode)
21 28

Dr. Harish Kumar B T, Dept of CSE, BIT, Bangalore


LPC2148 Port Pins used
• Analog Input: 0V  000F is the digital value
• Analog Input: 3.3V  1023 in decimal or 3FF in hexadecimal is the
digital value
• POT variable resistor is used to vary the voltage from 0V to 3.3V
• AD0.4 is the analog input pin used from ADCO and is connected to
Pin P0.25.
• Hence make P0.25 to function as analog i/p.
PINSEL1=0x00040000 // 19th and 18th bit are made // as 0
and 1 respectively

Dr. Harish Kumar B T, Dept of CSE, BIT, Bangalore


AD0CR (Analog to Digital Converter-0 Register)

• Focus on the circled fields


• SEL (0-7): Used to select the required analog i/p pin. We are used
AD0.4 hence we got to make 4th bit in (0-7) as 1.
• PND (21): Made as 1 to make P0.25 to work as analog i/p
• Start (24-26): To initiate the analog to digital conversion process
happen. By making 26th bit=0, 25th bit=0 and 24th bit=1
AD0CR=0x01200010

Dr. Harish Kumar B T, Dept of CSE, BIT, Bangalore


AD0GDR (ADC-0 Global Data Register)
• This register holds the digital data after conversion and also tell
whether the analog to digital conversion process complete or not.

• Focus only on fields circled in red:


• Result (6th to 15th): Holds the digital data after conversion
• Done (31st bit): Used to check whether conversion process is
complete or not. If this bit is set to 1 conversion is complete.

Dr. Harish Kumar B T, Dept of CSE, BIT, Bangalore


Analog to Digital Conversion Code
unsigned int adc_value=0,temp_adc=0,temp1,temp2;
#define vol 3.3 //Reference voltage
#define fullscale 0x3ff //10 bit adc
//CONTROL register for ADC
AD0CR = 0x01200010; //command register for ADC-AD0.4
while(((temp_adc = AD0GDR) &0x80000000) == 0x00000000); //to check the interrupt bit

adc_value = AD0GDR; //reading the ADC value


adc_value >>=6;
adc_value &= 0x000003ff;
temp = ((float)adc_value * (float)vol)/(float)fullscale;
sprintf(var1,"%4.2fV",temp);
sprintf(var,"%3x",adc_value);

Dr. Harish Kumar B T, Dept of CSE, BIT, Bangalore


LCD Initialization Code
temp2 is a global variable

void lcd_init()
{
temp2=0x30; // To Make Line-1 and 5*7 matrix operate in Byte mode
lcd_com();
delay(800);

temp2=0x38; // To Make Line-2 and 5*7 matrix operate in Byte mode


lcd_com();
delay(800);

temp2=0x20; // To Make Line-1 and 5*7 matrix operate in Nibble mode


lcd_com();
delay(800);

temp1 = 0x28; // To Make Line-2 and 5*7 matrix operate in Nibble mode
lcd_com();
delay(800);

temp1 = 0x0c; // Display on, cursor off


lcd_com();
delay(800);

temp1 = 0x06; // Auto Increment cursor (shift cursor to right)


lcd_com();
delay(800);

temp1 = 0x80; // Force cursor to beginning ( 1st line)


lcd_com();
delay(800);
}

Dr. Harish Kumar B T, Dept of CSE, BIT, Bangalore


lcd_com () function
void lcd_com(void)
{
temp2= temp1 & 0xf0; // Mask Lower Nibble and
wr_cn(); //send Higher nibble command to LCD
temp2 = temp1 & 0x0f; // Mask Higer Nibble
temp2 = temp2 << 4; // To take 4 least significant bits to the position D4 to D7
wr_cn(); // Send Lower nibble command to LCD
delay(500);
}

Dr. Harish Kumar B T, Dept of CSE, BIT, Bangalore


Wr_cn() function
void wr_cn(void) // write command reg
{
IO0CLR = 0x000000FC; // clear the port lines. 1 1 1 1 1(E) 1(RS) 0 0
IO0SET = temp2; // Assign the value to the PORT lines 3
IO0CLR = 0x00000004; // clear bit RS = 0 0 0 0 0 0(E) 1(RS) 0 0
IO0SET = 0x00000008; // E=1 0 0 0 0 1(E) 0(RS) 0 0
delay(10);
IO0CLR = 0x00000008;
}

Dr. Harish Kumar B T, Dept of CSE, BIT, Bangalore


Writing data to LCD
char *ptr,arr[]= "ADC O/P= ";
char *ptr1,dis[]="A I/P = ";
while(*ptr!='\0')
{
temp1 = *ptr;
lcd_data();
ptr ++;
}

ptr1 = arr;
temp1 = 0xC0; //Display starting address of second line 4 th pos
lcd_com();
delay(800);

while(*ptr1!='\0')
{
temp1 = *ptr1;
lcd_data();
ptr1 ++;
}

Dr. Harish Kumar B T, Dept of CSE, BIT, Bangalore


lcd_data()
void lcd_data(void)
{
temp2 = temp1 & 0xf0; // Mask Lower Nibble and
wr_dn(); // send Higher nibble data to LCD
temp2= temp1 & 0x0f; //Mask Higer Nibble
temp2= temp2 << 4; //To take 4 least significant bits to the position D4 to D7
wr_dn(); //Send Lower nibble data to LCD
delay(100);
}

Dr. Harish Kumar B T, Dept of CSE, BIT, Bangalore


wr_dn() function
void wr_dn(void)
{
IO0CLR = 0x000000FC; // clear the port lines. 1 1 1 1 1(E) 1(RS) 0 0
IO0SET = temp2; // Assign the value to the PORT lines
IO0SET = 0x00000004; // set bit RS = 1 0 0 0 0 0(E) 1(RS) 0 0
IO0SET = 0x00000008; // E=10 0 0 0 1(E) 0(RS) 0 0
delay(10);
IO0CLR = 0x00000008;
}

Dr. Harish Kumar B T, Dept of CSE, BIT, Bangalore


We will go through entire program now

Dr. Harish Kumar B T, Dept of CSE, BIT, Bangalore


Now the execution demo

Dr. Harish Kumar B T, Dept of CSE, BIT, Bangalore


Watch quick demo

Dr. Harish Kumar B T, Dept of CSE, BIT, Bangalore

You might also like