MP Lab Report # 5
MP Lab Report # 5
AIR UNIVERSITY
LAB ASSESSMENT:
Data presentation
Experimental results
Conclusion
Date: Signature:
L
a Lab 5
Analaog to Digital converter
(ADC) in PIC18F452
Objectives:
Introduction to ADC
Using library of ADC in MikroC
Write a code for ADC
Introduction:
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”.
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
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)
}}
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)
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.
Hardware:
Lab Task#2:
Perform Task#1 again but without using built in ADC Library
Proteus:
Hardware:
Conclusion:
In this lab we experienced the code and hardware of the conversion of analogue signal to digital
signal through the P18F452, we set the oscillation frequency so that TAD will always greater than
1.6us. First, we gave the Vpp the voltage of 5V and then we gave the analogue pin the signal of 3V,
and attached the led bar graph with the digital pins. We clearly see the perfext results of the
conversion on the LED bar, as it recueves 1 it turns on, while when it receives the 0 signal turns off.