0% found this document useful (0 votes)
3 views8 pages

Lab 10

This document outlines the use of the Analog to Digital Converter (ADC) in the PIC18F452 microcontroller, detailing its configuration, operation, and programming using MikroC. It includes objectives, an introduction to ADC, clock selection for conversion, and example code for reading analog inputs and displaying digital outputs. Additionally, it provides lab tasks for practical implementation, including using sensors and interfacing with an LCD.

Uploaded by

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

Lab 10

This document outlines the use of the Analog to Digital Converter (ADC) in the PIC18F452 microcontroller, detailing its configuration, operation, and programming using MikroC. It includes objectives, an introduction to ADC, clock selection for conversion, and example code for reading analog inputs and displaying digital outputs. Additionally, it provides lab tasks for practical implementation, including using sensors and interfacing with an LCD.

Uploaded by

230609
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Lab 10

. Analogue to Digital Converter


(ADC) in PIC18F452
Objectives:
 Introduction to ADC
 Using library of ADC in MikroC
 Write a code for ADC

Introduction:

P
in Direction (TRIS):
The pins which are to be used as analog input, must be configured as INPUT (Corresponding bit
in TRIS register must be set).
All analog inputs have built-in 10 bit ADC
 Digital output is of 10bits

This is a 10bit ADC If Vref   3V then Step size


So Resolution
3/1023=2.93mv
10
2  1 =1023 For 2.93mv LSB will be 1 only
4 Special function registers are associated with A/D modules
1. ADRESH
 A/D Result High Register
2. ADRESL
 A/D Result Low Register
3. ADCON0
 A/D Control Register 0
4. ADCON1
 A/D Control Register 1
Clock Selection:
 TAD is conversion time per bit
 10 bit A/D conversion time = 12 TAD
 After a conversion has completed, a 2 TAD delay must be provided before the acquisition
starts again
 Clock must be selected such that
“TAD ≥ 1.6 μs”

Example:
For Crystal of 10 MHz, what should be the clock source?
Rule: Select clock such that clock cycle time ≥ 1.6 μs

FOR 10 MHz Xtal if Clock Source “Fosc/2” is used then TAD=2/10Mhz=0.2us<1.6us so this
cannot be used. If Clock Source “Fosc/4” is used then TAD=4/10Mhz=0.4us<1.6us so this
cannot be used also. If Clock Source “Fosc/8” is used then TAD=2/10Mhz=0.8us<1.6us so this
cannot be used as well. If Clock Source “Fosc/16” is used then TAD=16/10Mhz=1.6us≥1.6us so
clock source must be less than or equal to “Fosc/16”.

While using software mikroC a library of “ADC” is included to use ADC


comfortably.

Command Syntax Description


ADC=ADC_read(X); ADC=ADC_read(1); Input is analogue and output will be 10bit digital value
Procedure:
Open MikroC, make new project and perform the given tasks

Lab Task#1:
Write a code which takes analogue input and converts it into digital then sends its digital output
on LED’s.

CODE:
void main() {
unsigned int ADC;
Trisa=0b11011011; //RA0,RA1,RA3,RA4 marked as input RA2,RA5 marked as output on

Trisb=0; //Define PortB output Port

ADCON1=0b00000101; //Explained below

while(1){
ADC=ADC_read(1); //”1” is used for channel 1 (you can use 2,3,4,5,6,7 for channel 2,3,4,5,6,7)

Latb=ADC&255; //ADC is 10 bit (from bit0-bit9) value and LATB is 8 bit(bit0-bit7)

lata2_bit=(ADC>>8)&1; //8th bit is needed here see example

lata5_bit=(ADC>>9)&1; //9th bit is needed here which is MSB see example

}}

In above code ADCON1=0b00000101


1 1 0 0 0 1 0 1
AN7 AN6 AN5 AN4 AN3 AN2 AN1 AN0 Vref+ Vref-
0 0 Unimplemented D D D D Vref+ D A A AN3 Vss
Not necessary to use while Bit3-----------------------------------------------------------Bit0
Using built in Library

A=Analogue D=Digital
EXAMPLE: If ADC in above code has value “1111001011”
Then LatB=ADC&255=1111001011&0011111111 (this is bit by bit and so)
LatB=11001011 (8 least significant bits of ADC)
Now Lata2_bit= (ADC>>8) &1= 0000000011&0000000001
Lata2_bit=1 (8th bit)
Now Lata2_bit= (ADC>>9) &1= 0000000001&0000000001
Lata5_bit=1 (9th bit)
This can be done by direct shifting as well.

Schematic:
ADC is mostly used with sensors so if you want to use a sensor you can connect with Pic18F452
using simple circuitry. If you have a sensor like thermistor or LDR which will vary its resistance
according to environment then it can be connected in place of RV1.
Lab Task#2:
Perform Task#1 again but without using built in ADC Library

Lab Task#3:
1. Take a sensor which is available in lab then check its max and min resistance or Voltage
as it is Resistance effective or Voltage effective. LM35 and photocell is voltage effective
and LDR is resistance effective.
2. Make settings as you know Vref  and Vref  from previous step.
3. Make Connection with PIC18F452 on Hardware and Proteus and also interface LCD.
4. Write a code which can sense Temperature/Light according to your sensor and display its
output on LCD.

You might also like