8.program For Internal ADC of PIC: Objective

Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

8.

Program For Internal ADC Of PIC

OBJECTIVE : To perform Analog to Digital Conversion with the help of


internal ADC of PIC16f877a microcontroller.

REQUIREMENT : MPLAB IDE, Proteus Software , PICSIM LAB.

THEORY :
Analog to Digital Converter (ADC) is a device that converts an analog quantity
(continuous voltage) to discrete digital values. This is very useful when we want to do
some processing on physical quantities, which are normally analog in nature. Most of
the PIC Microcontrollers have built in ADC Module.

Analog to Digital Converter -


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 module has four registers. These registers are:
• A/D Result High Register (ADRESH)
• A/D Result Low Register (ADRESL)
• A/D Control Register 0 (ADCON0)
• A/D Control Register 1 (ADCON1)

Steps for ADC Operation -

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. The A/D conversion time
per bit is defined as TAD.

PROGRAM :
list p=16f877A ; list directive to define processor
#include <p16f877A.inc> ; processor specific variable definitions
__CONFIG _CP_OFF & _WDT_OFF & _BODEN_ON & _PWRTE_ON & _RC_OSC &
_LVP_ON & _DEBUG_OFF & _CPD_OFF

ORG 0x000 ; processor reset vector


goto main ; go to beginning of program

ORG 0x004 ; interrupt vector location


retfie ; return from interrupt
main
BANKSEL PORTD ;PORTD and ADCON0are in bank0
clrf PORTD ;Clear port D
movlw B'01000001' ;Fosc/8, A/D enabled
movwf ADCON0 ;A/D control register 0
BANKSEL TRISD ;TRISD and ADCON1 are in bank1
clrf TRISD
;Port D out port
movlw B'00001110' ;Left justify,1 analog channel
movf ADCON1 ;VDD and VSS references
BANKSEL PORTD
Loop
clrf ADRESH
clrf ADRESL
bsf ADCON0,GO ;Start A/D conversion for channel 0
Wait btfsc ADCON0,GO ;Wait for conversion to complete
goto Wait
movf ADRESH,W
movwf PORTD ;Write A/D result to PORTD LED'S
bcf PIR1,ADIF ;clear the complition flag
goto Loop ;Do it again.
END ; directive 'end of program'
CONCLUSION :
From this we concluded that, the ADC module of PIC microcontroller is a very
useful feature . Its use is immense in practical application and hence , one must know
its proper application.

QUESTIONS :
Q1 . Draw bit format of ADCON0 & ADCON1 register.

You might also like