AN003(8-Bit Ad Conversion Using PIC)

7
Lavatory Pros AN003 APPLICATION NOTE 1 8-BIT ANALOG-TO-DIGITAL CONVERSION (ADC) USING PIC16F877A By Azreen Jafaar (HK2006-2351) Date: 24 March 2009 Keywords: ADC, PIC16F877A 1.0 Abstract The Analog-to-digital conversion (ADC) is very useful to convert the analog input to digital output. This application notes describe about the ADC conversion using PIC16F877A. In the issues section,the 4 type of control registers are elaborated. Also explain how to start the 8-bit conversion and to program it. The 8-bit source codes are also attached. 2.0 Introduction An analog-to-digital converter (ADC, A/D or A to D) is a device which converts continuous signals to discrete digital numbers. Typically, an ADC is an electronic device that converts an input analog voltage (or current ) to a digital number. However, some non-electronic or only partially electronic devices, such as rotary encoders , can also be considered ADCs. The digital output may use different coding schemes, such as binary , Gray code or two’s complement binary. The resolution of the converter indicates the number of discrete values it can produce over the range of analog values. The values are usually stored electronically in binary form, so the resolution is usually expressed in bits . In consequence, the number of discrete values available, or levels, is usually a power of two. For example, an ADC with a resolution of 8 bits can encode an analog input to one in 256 different levels, since 2 8 = 256. The values can represent the ranges from 0 to 255 (i.e. unsigned integer) or from - 128 to 127 (i.e. signed integer), depending on the application. Resolution can also be defined electrically, and expressed in volts . The voltage resolution of an ADC is equal to its overall voltage measurement range divided by the number of discrete intervals as in the formula: ܧிʹ ܧி Where: Q is the resolution in volts per step (volts per output code), E FSR is the full scale voltage range = V RefHi V RefLo , M is the ADC’s resolution in bits, and N is the number of intervals, given by the number of available levels (output codes), which is: N = 2 M Example: Full scale measurement range = 0 to 5 volts ADC resolution is 8 bits: 2 8 = 256 quantization levels (codes) ADC voltage resolution is: (5V - 0V) / 256 codes = 5V / 256 codes= 0.01953 volts/code =19.53 mV/code. pdfMachine Is a pdf writer that produces quality PDF files with ease! Produce quality PDF files in seconds and preserve the integrity of your original documents. Compatible across nearly all Windows platforms, if you can print from a windows application you can use pdfMachine. Get yours now!

Transcript of AN003(8-Bit Ad Conversion Using PIC)

Lavatory ProsAN003 APPLICATION NOTE

8-BIT ANALOG-TO-DIGITAL CONVERSION (ADC) USING PIC16F877ABy Azreen Jafaar (HK2006-2351) Date: 24 March 2009

Keywords: ADC, PIC16F877A

1.0 Abstract

The Analog-to-digital conversion (ADC) is very useful to convert the analog input to digital output. This application notes describe about the ADC conversion using PIC16F877A. In the issues section,the 4 type of control registers are elaborated. Also explain how to start the 8-bit conversion and to program it. The 8-bit source codes are also attached.

2.0 Introduction

An analog-to-digital converter (ADC, A/D or A to D) is a device which converts continuous signals to discrete digital numbers. Typically, an ADC is an electronic device that converts an input analog voltage (or current) to a digital number. However, some non-electronic or only partially electronic devices, such as rotary encoders, can also be considered ADCs. The digital output may use different coding schemes, such as binary, Gray code or two's complement binary. The resolution of the converter indicates the number of discrete values it can produce over the range of analog values. The values are usually stored electronically in binary form, so the resolution is usually expressed in bits. In consequence, the number of discrete values available, or levels, is usually a power of two. For example, an ADC with a resolution of 8 bits can encode an analog input to one in 256 different levels, since 28 = 256. The values can represent the ranges from 0 to 255 (i.e. unsigned integer) or from 128 to 127 (i.e. signed integer), depending on the application. Resolution can also be defined electrically, and expressed in volts. The voltage resolution of an ADC is equal to its overall voltage measurement range divided by the number of discrete intervals as in the formula:

Where: Q is the resolution in volts per step (volts per output code), EFSR is the full scale voltage range = VRefHi VRefLo, M is the ADC's resolution in bits, and N is the number of intervals, given by the number of available levels (output codes), which is: N = 2M

Example:

Full scale measurement range = 0 to 5 volts ADC resolution is 8 bits: 28 = 256 quantization levels (codes) ADC voltage resolution is: (5V - 0V) / 256 codes = 5V / 256 codes= 0.01953 volts/code =19.53 mV/code.

pdfMachine Is a pdf writer that produces quality PDF files with ease! Produce quality PDF files in seconds and preserve the integrity of your original documents. Compatible across nearly all Windows platforms, if you can print from a windows application you can use pdfMachine. Get yours now!

1

Lavatory ProsAN003 APPLICATION NOTE

The ADC is controlled from special function registers ADCON0 and ADCON1, and can generate a peripheral interrupt if required. The output from the converter is stored in ADRESH (analogue to digital conversion result, high byte) and ADRESL (low byte). The processing for an 8-bit result is simpler. The A/D module has high and low-voltage reference inputs that are software selectable to some combination of VDD, VSS, RA2 or RA3.

3.0 Objectives

This application note will help users of 8-bit analog-to-digital converters (ADC) understand how to build and write the program for ADC operation using PIC16F877A and to learn how the 8-bit conversion works in an ADC.

4.0 Control Registers

a) ADCON0 REGISTER (ADDRESS 1Fh) R/W-0 R/W-0 R/W-0 R/W-0 R/W-0 ADCS1 ADCS0 CHS2 CHS1 CHS0 bit 7

R/W-0 GO/DONE

U-0

R/W-0 ADON bit 0

bit 7-6 ADCS1:ADCS0: A/D Conversion Clock Select bits (ADCON0 bits in bold) ADCON1 ADCON0 Clock Conversion 0 00 FOSC/2 0 01 FOSC/8 0 10 FOSC/32 0 11 FRC (clock derived from the internal A/D RC oscillator) 1 00 FOSC/4 1 01 FOSC/16 1 10 FOSC/64 1 11 FRC (clock derived from the internal A/D RC oscillator)

bit 5-3 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)pdfMachine Is a pdf writer that produces quality PDF files with ease! Produce quality PDF files in seconds and preserve the integrity of your original documents. Compatible across nearly all Windows platforms, if you can print from a windows application you can use pdfMachine. Get yours now!

2

Lavatory ProsAN003 APPLICATION NOTE

bit 2 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 bit 1 Unimplemented: Read as 0. bit 0 ADON: A/D On bit. 1= A/D converter module is powered up and 0= A/D converter module is shut-off and consumes no operating current.

b) ADCON1 REGISTER (ADDRESS 9Fh)

R/W-0 ADFM bit 7

R/W-0 ADCS2

U-0

U-0

R/W-0 PCFG3

R/W-0 PCFG2

R/W-0 PCFG1

R/W-0 PCFG0 bit 0

bit 7 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. bit 6 ADCS2: A/D Conversion Clock Select bit (ADCON1 bits in shaded area and in bold)

ADCON1

0 0 0 0

ADCON0 00 01 10 11

Clock Conversion

1 1 1 1

00 01 10 11

FOSC/2 FOSC/8 FOSC/32 FRC (clock derived from the internal A/D RC oscillator) FOSC/4 FOSC/16 FOSC/64 FRC (clock derived from the internal A/D RC oscillator)

bit 5-4 Unimplemented: Read as 0. bit 3-0 PCFG3:PCFG0: A/D Port Configuration Control bits. PCFG AN7 AN6 AN5 AN4 AN3 AN2 AN1 0000 A A A A A A A 0001 A A A A VREF+ A A 0010 D D D A A A A 0011 D D D A VREF+ A A 0100 D D D D A D A 0101 D D D D VREF+ D A 011X D D D D D D D

AN0

VREF+

VREF-

C/R

A A A A A A D

VDD AN3 VDD AN3 VDD AN3

VSS VSS VSS VSS VSS VSS

8/0 7/1 5/0 4/1 3/0 2/1 0/0

pdfMachine Is a pdf writer that produces quality PDF files with ease! Produce quality PDF files in seconds and preserve the integrity of your original documents. Compatible across nearly all Windows platforms, if you can print from a windows application you can use pdfMachine. Get yours now!

3

Lavatory ProsAN003 APPLICATION NOTE

1000 A A A 1001 D D A 1010 D D A 1011 D D A 1100 D D D 1101 D D D 1110 D D D 1111 D D D A = Analog input D = Digital I/O C/R = # of analog input channels/#

A A A A A D D D

VREF+ A VREF+ VREF+ VREF+ VREF+ D VREF+

VREFA A VREFVREFVREFD VREF-

A A A A A A D D

A A A A A A A A

AN3 VDD AN3 AN3 AN3 AN3 VDD AN3

AN2 VSS VSS AN2 AN2 AN2 VSS AN2

6/2 6/0 5/1 4/2 3/2 2/2 1/0

of A/D voltage references.

c) ADRESH & ADRESL REGISTER 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) is cleared and the A/D interrupt flag bit ADIF is set.

8-bit Conversion Circuit

5.0

A test circuit to demonstrate 8-bit conversion and display is shown in Figure1. The 16F877A MCU has eight analogue inputs available, at RA0, RA1, RA2, RA3, RA5, RE0, RE1 and RE2. These have alternate labels AN0AN7 for this function. RA2 and RA3 may be used as reference voltage inputs, setting the minimum and maximum values for the measured voltage range. But in these papers we just use the maximum 5V as reference voltage input. These inputs default to analogue operation, so the register ADCON1 has to be initialized explicitly to use these pins for digital input or output. In Figure1 show 5V of VREF+ is connected to pin RA0 through RV1(potentiometer) to get the difference input.

pdfMachine Is a pdf writer that produces quality PDF files with ease! Produce quality PDF files in seconds and preserve the integrity of your original documents. Compatible across nearly all Windows platforms, if you can print from a windows application you can use pdfMachine. Get yours now!

Figure1 4

Lavatory ProsAN003 APPLICATION NOTE

6.0 Input and OutputThe test voltage input at RA0 (analogue input AN0) is derived from a port across the 5 V supply. A reference voltage is connected to the variable resistor to control the current flow into the input at RA0, which sets the maximum voltage to be converted, and thus the conversion factor required in the software. The minimum value defaults to 0 V. The eight LED is connected to Port D (RD0, RD1, RD2, RD3, RD5, RD6 and RD7) to operate in 8-bit mode and represented the digital output.

7.0 Steps

Start Analog-Digital (A/D) Conversion

Firstly, Configure the A/D module by configuring the analog pins/voltage reference and digital I/O (ADCON1), select A/D input channel (ADCON0), select A/D conversion clock (ADCON0) and turn on A/D module (ADCON0). Secondly, configure A/D interrupt (if desired) with clear ADIF bit, Set ADIE bit, Set PEIE bit, Set GIE bit. Then, wait the required acquisition time. After that, start conversion with set GO/DONE bit (ADCON0) and 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. Lastly, Read A/D Result register pair (ADRESH: ADRESL), clear bit ADIF if required and for the next conversion, go to first and second step as required. The A/D conversion time per bit is defined as TAD (AD clock sources).

8-bit Conversion Program The test program is outlined in below (ADC8), and the source code listed in Program1.ADC8 Convert the analogue input to 8-bits and display Hardware: P16F877A (4MHz), Vref+ =5V, 8 LED Initialise PortA = Analogue inputs (default) PortD = LED outputs ADC = Select f/8, RA0 input, left justify result, enable LCD = default setup (include LCD driver routines) Main REPEAT Get ADC 8-bit input Convert to BCD Display on LED ALWAYS Subroutines Get ADC 8-bit input Start ADC and wait for done Store result Convert to BCD Calculate hundreds digit Calculate tens digit Remainder = ones digit Display pdfMachine Display LED Is Repeated a pdf writer that produces quality PDF files with ease! Produce quality PDF files in seconds and preserve the integrity of your original documents. Compatible across ENDnearly all Windows platforms, if you can print from a windows application you can use pdfMachine. Get yours now!

5

Lavatory ProsAN003 APPLICATION NOTE

The output port and ADC control registers are initialized in the first block, with the LED including file providing the display initialization, and driver routines. The main loop contains subroutine calls to read the ADC input, convert it from binary to BCD and display it. The routine to read the ADC sets the GO/DONE bit and then polls it until it is cleared at the end of the conversion. The 8-bit result from ADRESH is converted to BCD digits. Full-scale input is 255, which is displayed as 11111111(LED TURNs ON).

8.0 Results

We have tested out our 8-bit conversion program and it works as shown in figure3. When we change the resistance by increasing or decreasing the variable resistor (potentiometer), the LED turns ON or OFF simultaneously, that represents the output digital signal. When 0 it is OFF and 1 it is ON. Volt (V) Digital (binary) 0.0 2.0 4.0 5.0 Figure3

0000 0000 0110 0110 1100 1100 1111 1111

Example:

where: x= Voltage we need, Y=value in decimal, Z=value in binary code and =5V.

9.0 Conclusion

The PIC16F877A is convenient for the building of the ADC and is also used in our team project design. Once the user has enough knowledge to use, define and write the analog-to-digital converters, he/she can use it in their own design project. A good understanding of the terms used to specify the converter can help the user sort out the most important design parameters for their system and enable them to get the best cost and performance for the design.

10.0 Reference

1. Website: www.freescale.com 2. Interfacing PIC Microcontrollers Embedded Design by Interactive Simulation, Martin Bates (2006) Elsevier. 3. PIC16F87XA Data Sheet28/40/44-Pin Enhanced Flash Microcontrollers, 2003 Microchip Technology Inc. 4. Basic 8-bit A/D Converter, 2003 Microchip Technology Inc.pdfMachine Is a pdf writer that produces quality PDF files with ease! Produce quality PDF files in seconds and preserve the integrity of your original documents. Compatible across nearly all Windows platforms, if you can print from a windows application you can use pdfMachine. Get yours now!

6

Lavatory ProsAN003 APPLICATION NOTE

11.0 AppendixPROCESSOR 16F877 ERRORLEVEL -302 ERRORLEVEL -305 __CONFIG 0x3F32 #INCLUDE "P16F877A.INC" COUNT ADBIN HUNDS TENS ONES STATUS EQU EQU EQU EQU EQU EQU 30h; 31h; 32h; 33h; 34h; 03h; counter for ADC setup delay binary input value hundreds digit in decimal value tens digit in decimal value ones digit in decimal value

; PROGRAM BEGINS;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ORG 0h; default start address NOP ; required for ICD mode ; PORT & DISPLAY SETUP;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; BSF STATUS,5 CLRF TRISD ; display port is output MOVLW B'00000011' ; analogue input setup code MOVWF ADCON1 ; left justify result ; port A = analogue inputs BCF CLRF MOVLW MOVWF STATUS,5 PORTD ; B'01000001' ADCON0

clear displays outputs ; analogue input setup code ; f/8, RA0, done, enable

; MAIN LOOP;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; START CALL ADCSTART CALL DISPLAY GOTO START

ADCSTART WAIT

BSF BTFSC GOTO MOVF RETURN MOVWF RETURN END

ADCON0,GO ADCON0,GO WAIT ADRESH,W

DISPLAY

PORTD

pdfMachine Is a pdf writer that produces quality PDF files with ease! Produce quality PDF files in seconds and preserve the integrity of your original documents. Compatible across nearly all Windows platforms, if you can print from a windows application you can use pdfMachine. Get yours now!

7