Microprocessor & Interfacing
Fall 2019
Target CLO Evaluation: CLO2-C4
Assignment No. 2
Class: BCE- 5 Submission Date: 1-12-19 Total Marks: 10 Marks
Q#1
Design a circuit of PIC16f877 with following conditions and inquire:
a) How to Interface circuit with one Analog sensor as input.
b) How to convert analog signal into digital signal.
In PIC16F877, conversion of analog signal results in 10-bit digital number. A/D module
has low and high voltage reference input. It is software selectable for VSS, VDD, RA2,
RA3. The A/D converter device can operate in Sleep mode. During Sleep, clock of the
converter must be derived from the RC oscillator.
The A/D in PIC16F877A has four registers. These registers are:
• A/D Control Register 0 (ADCON0)
• A/D Result High Register (ADRESH)
• A/D Result Low Register (ADRESL)
• A/D Control Register 1 (ADCON1)
To do an A/D Conversion, follow these steps:
1. Configure A/D module
• Configure analog pins and digital I/O (ADCON1)
• Select A/D input channel (ADCON0)
• Select A/D conversion clock (ADCON0)
• Turn ON A/D module (ADCON0)
2. Configure A/D interrupt (if desired)
• Clear ADIF
• Set ADIE
• Set PEIE
• Set GIE
3. Wait for acquisition time.
4. Start conversion
• Set GO/DONE bit (ADCON0)
5. Wait for A/D conversion to complete by either:
• Polling GO/DONE bit to be cleared or,
• Wait for A/D interrupt.
6. Read A/D Result register, clear ADIF.
7. For next conversion, go to step 1 or step 2 as required. A/D conversion time per bit is
defined as TAD.
Program:
#include <pic.h>
#include"delay.h"
int adcval; // defining adc integer
void ADC_Init()
TRISA = 0XEF;//configuring adc ports as input
TRISE = 0X07;
ADCON1 = 0X80;// configuring all ports as analog ports
// fucntion for reading the adc value
int read_adc(char channel)
int val;
ADCON1 = 0x80; //port config
ADCON0 = 0x81 | channel << 3;// channel selection SET THE CHANNEL
NUMBER
DelayUs(50);
ADGO = 1;//turn on the adc
while(ADGO) // checking the adc conversion
continue;
val = ADRESH;
val = val<<8 | ADRESL;// combining the both adresh and adresl values to a
single variable
return (val);
void main()
TRISD=0X00;
PORTD=0X00;
ADC_Init();// adcintialising
while(1)// loop for ever
adcval=read_adc(0);
if(adcval> 500) //checking the adc value
PORTD=0XFF; //setting the port value as high if adc value greater than
500
else // reseting the port value
PORTD=0X00;
}
}
}
c) Display the digital data on output varying with Analog input variations.
Deliverables:
I) Interfacing Circuit Diagram
II) Code
III) Analog and Digital signals comparison figures on each input.
IV) Calculation of Analog to Digital Conversion