0% found this document useful (0 votes)
85 views

EXPERIMENT 9: Interfacing ADC With Sensors: Objectives

This is the lab report assignment sheet. The solutions are provided in Exp9 Interfacing ADC With Sensors (1). Additional info can be seen in Exp9 Interfacing ADC With Sensors (2).

Uploaded by

Lim Wei Xen
Copyright
© © All Rights Reserved
0% found this document useful (0 votes)
85 views

EXPERIMENT 9: Interfacing ADC With Sensors: Objectives

This is the lab report assignment sheet. The solutions are provided in Exp9 Interfacing ADC With Sensors (1). Additional info can be seen in Exp9 Interfacing ADC With Sensors (2).

Uploaded by

Lim Wei Xen
Copyright
© © All Rights Reserved
You are on page 1/ 3

EXPERIMENT 9: Interfacing ADC with Sensors

Objectives
To interface ADC module of PIC microcontroller with Sensors.

Components
Software: MPLAB X IDE (latest version)
Software: XC8 C Compiler (latest version)
IC type: PIC16F877A microcontroller
LEDs: Red
Crystal: 8MHz
Capacitors: 22pF
Resistors: 470Ω ±5%
Potentiometer: 10KΩ
Light Sensor: GL5528

Introduction
Every physical quantity found in nature like temperature, humidity, pressure, force is
analog. These analog quantities need to be converted to digital to process it using a digital
computer or a microcontroller. This is done by using Analog to Digital Converters. An Analog
to Digital Converter or ADC is a device which converts continuous analog quantity (here:
voltage) to corresponding discrete digital values.

PIC 16F877A microcontroller has eight ADC inputs and it will convert analog inputs to a
corresponding 10-bit digital number. For example, assuming the ADC Lower Reference is 0V
and Higher Reference is 5V.
 𝑉𝑟𝑒𝑓− = 0𝑉
 𝑉𝑟𝑒𝑓+ = 5𝑉
 𝑛 = 10 𝑏𝑖𝑡𝑠

(𝑉𝑟𝑒𝑓+ − 𝑉𝑟𝑒𝑓− ) 5
𝑅𝑒𝑠𝑜𝑙𝑢𝑡𝑖𝑜𝑛 = 𝑛
= = 0.0048876𝑉 = 4.8876𝑚𝑉
2 −1 1023

Therefore, ADC resolution is 4.8876mV, which is the minimum required voltage to change a
bit.

Table 9-1.
Digital Output
Analog Input
Binary Hexadecimal Decimal
0.000000V 0b0000000000 0x000 0
0.004887V 0b0000000001 0x001 1
0.009774V 0b0000000010 0x002 2
0.014661V 0b0000000011 0x003 3
4.999401V 0b1111111111 0x3FF 1023

Prepared by Steven Khoo. Page 1 of 3


Procedure
1. Construct the schematic diagram as shown in Figure 9-1. A 10KΩ potentiometer is
connected to the RA0/AN0 pin of PIC16F877A microcontroller.

Figure 9-1 10-bit ADC operation using PIC16F877A microcontroller.

2. Write the C program as shown below.


Line Code
1 #pragma config FOSC = HS
2 #pragma config WDTE = OFF
3 #pragma config PWRTE = OFF
4 #pragma config BOREN = OFF
5 #pragma config LVP = OFF
6 #pragma config CPD = OFF
7 #pragma config WRT = OFF
8 #pragma config CP = OFF
9
10 #include <xc.h>
11 #include <pic16f877a.h>
12 #define _XTAL_FREQ 8000000
13
14 void ADC_Init()
15 {
16 ADCON0 = 0x81;
17 ADCON1 = 0x00;
18 }
19
20 unsigned int ADC_Read(unsigned char channel)
21 {
22 if(channel > 7)
23 return 0;
24
25 ADCON0 &= 0xC5;
26 ADCON0 |= channel<<3;
27 __delay_ms(2);
28 GO_nDONE = 1;
29 while(GO_nDONE);

Prepared by Steven Khoo. Page 2 of 3


30 return ((ADRESH<<8)+ADRESL);
31 }
32
33 void main()
34 {
35 unsigned int a;
36 TRISA = 0xFF;
37 TRISB = 0x00;
38 TRISC = 0x00;
39 ADC_Init();
40
41 do
42 a = ADC_Read(0);
43 PORTB = a;
44 PORTC = a>>8;
45 __delay_ms(100);
46 }while(1);
47 }

3. Ensure that the program can be compiled without errors.


4. Modify the schematic diagram from 10KΩ potentiometer to the LDR sensor. Provide
a complete new schematic diagram for the light intensity representation system.
5. Modify the program so that the equivalent light intensity can be represented using
10 LEDs. Provide the complete coding for the light intensity representation system.

Questions
1. List the registers used in the ADC module of PIC16F877A microcontroller.
2. What is the minimum required acquisition time?
3. Provide the comment for Line 1 to Line 8.
4. What is the purpose of Line 16 and Line 17?
5. Explain the function of unsigned int ADC_Read(unsigned char channel) from
Line 20 to Line 31.
6. Describe how the 10-bit data is being stored and processed.

Prepared by Steven Khoo. Page 3 of 3

You might also like