PIC16F877A – ADC Tutorial

Now we are going to see PIC16F877A ADC Tutorial.

PIC16F877A ADC Tutorial

Prerequisites

Introduction

Microcontrollers are very useful especially when it comes to communicate with other devices, such as sensors, motors, switches, memory and even other microcontroller. As we all know many interface methods have been developed over years to solve complex problem of balancing need of features, cost, size, power consumption, reliability etc. but ADC Analog-to-Digital converter remains famous among all. Using this ADC we can connect any type of Analog sensor.

PIC16F877A ADC Module

The Analog-to-Digital (A/D) Converter module has eight for the 40/44-pin devices.

The conversion of an analog input signal results in a corresponding 10-bit digital number. The A/D module has high and low-voltage reference input that is software selectable to some combination of VDD, VSS, RA2 or RA3.

The A/D converter has a unique feature of being able to operate while the device is in Sleep mode. To operate in Sleep, the A/D clock must be derived from the A/D’s internal RC oscillator.

PIC16F877A ADC Pins

ADC ChannelPin
Channel 0RA0/AN0 (Port A)
Channel 1RA1/AN1 (Port A)
Channel 2RA2/AN2/VRef- (Port A)
Channel 3RA3/AN3/VRef+ (Port A)
Channel 4RA5/AN4 (Port A)
Channel 5RE0/AN5 (Port E)
Channel 6RE1/AN6 (Port E)
Channel 7RE2/AN7 (Port E)

Registers used for ADC

  • A/D Control Register 0 (ADCON0)
  • A/D Control Register 1 (ADCON1)
  • A/D Result High Register (ADRESH)
  • A/D Result Low Register (ADRESL)

A/D Control Register 0 (ADCON0)

The ADCON0 register, shown in below image, controls the operation of the A/D module i.e. Used to Turn ON the ADC, Select the Sampling Freq and also Start the conversion.

ADCS1-ADCS0: A/D Conversion Clock Select bits This bits are based on ADCON1 Reister’s ADCS2 bit.

CHS2-CHS0: Analog Channel Select bits

000 = Channel 0 (AN0)
001 = Channel 1 (AN1)
010 = Channel 2 (AN2)
011 = Channel 3 (AN3)
100 = Channel 4 (AN4)
101 = Channel 5 (AN5)
110 = Channel 6 (AN6)
111 = Channel 7 (AN7)

GO/DONE:  A/D Conversion Status bit

When ADON = 1:
1 = A/D conversion in progress (setting this bit starts the A/D conversion which is automatically cleared by hardware when the A/D conversion is complete)
0 = A/D conversion not in progress

ADON: A/D On bit

1 = A/D converter module is powered up
0 = A/D converter module is shut-off and consumes no operating current

A/D Control Register 1 (ADCON1)

The ADCON1 register, shown below, configures the functions of the port pins i.e Used to configure the GPIO pins for ADC. The port pins can be configured as analog inputs (RA3 can also be the voltage reference) or as digital I/O.

ADFM: A/D Result Format Select bit

1 = Right justified. Six (6) Most Significant bits of ADRESH are read as ‘0’.
0 = Left justified. Six (6) Least Significant bits of ADRESL are read as ‘0’.

ADCS2: A/D Conversion Clock Select bit

PCFG3-PCFG0: A/D Port Configuration Control bits

A/D Result High Register (ADRESH) &  A/D Result Low Register (ADRESL)

The ADRESH:ADRESL registers contain the 10-bit result of the A/D conversion. When the A/D conversion is complete, the result is loaded into this A/D Result register pair, the GO/DONE bit (ADCON0<2>) is cleared and the A/D interrupt flag bit ADIF is set. The block diagram of the A/D module is shown below.

Steps to follow

To do an A/D Conversion, follow these steps:
1. Configure the A/D module:

• Configure analog pins/voltage reference 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 bit
• Set ADIE bit
• Set PEIE bit
• Set GIE bit

3. Wait the required acquisition time.

4. Start conversion:

• Set GO/DONE bit (ADCON0)

5. Wait for A/D conversion to complete by either:

• Polling for the GO/DONE bit to be cleared (interrupts disabled); OR
• Waiting for the A/D interrupt

6. Read A/D Result register pair (ADRESH:ADRESL), clear bit ADIF if required.

7. For the next conversion, go to step 1 or step 2 as required.

Circuit Diagram

LCD:

RS –  RC0

RW – RC1

EN – RC2

Data Lines – Port B

POT:

Out – RA0

Programming

Whenever value changes in Potentiometer it will displayed in LCD Module. In this pin you can connect any sensors. You can download the full project Here.

Output

PIC16F877A ADC Tutorial[ Please find the output image Here ]

 

0 0 vote
Article Rating
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Vineeth S.R
Vineeth S.R
November 7, 2018 10:32 AM

Very good tutorial 😀 thank you very much!!
Can you please explain the line 73:adcval=(adcval/3)-1; didnt understand that one though.
It would be really great if you could explain what exactly this line will do too
adcval=((ADRESH<<8)|(ADRESL));

1
0
Would love your thoughts, please comment.x
()
x
%d bloggers like this: