Internal ADC of PIC18F4520

Updated on 2017/11/20 12:49

Syllabus

  • Internal ADC Interfacing with PIC18F4520

Internal ADC Interfacing with PIC18F4520

part from a large number of digital I/O lines, the PIC18F4520 contains 8-13 analog inputs. They enable the microcontroller to recognize, not only whether a pin is driven to logic zero or one (0 or +5V), but to precisely measure its voltage and convert it into a numerical value, i.e. digital format. The whole procedure takes place in the A/D converter module which has the following features:

  • The converter generates a 10-bit binary result using the method of successive approximation and stores the conversion results into the ADC registers (ADRESL and ADRESH);
  • There are 14 separate analog inputs;
  • The A/D converter allows conversion of an analog input signal to a 10-bit binary representation of that signal; and
  • By selecting voltage references Vref- and Vref+, the minimal resolution or quality of conversion may be adjusted to various needs.

ADC Mode and Registers

Even though the use of A/D converter seems to be very complicated, it is basically very simple, simpler than using timers and serial communication module, anyway.

pic-microcontrollers-examples-in-assembly-language-chapter-07-fig7-1

ADC Mode and Registers
The module is under the control of the bits of four registers:

  • ADRESH – Contains high byte of conversion result;
  • ADRESL – Contains low byte of conversion result;
  • ADCON0 – Analog Digital Control register 0; 
  • ADCON1 -Analog Digital Control register 1
  • ADCON2 -Analog Digital Control register 2

ADRESH and ADRESL Registers

When converting an analog value into a digital one, the result of the 10-bit A/D conversion will be stored in these two registers. In order to deal with this value easier, it can appear in two formats- left justified and right justified. The ADFM bit of the ADCON1 register determines the format of conversion result (see figure 7-2). In the event that A/D converter is not used, these registers may be used as general-purpose registers.

pic-microcontrollers-examples-in-assembly-language-chapter-07-fig7-2

Fig. ADRESH and ADRESL Registers

A/D Acquisition Requirements

For the ADC to meet its specified accuracy, it is necessary to provide a certain time delay between selecting specific analog input and measurement itself. This time is called “acquisition time” and mainly depends on the source impedance. There is an equation used for accurately calculating this time, which in the worst case amounts to approximately 20uS. Briefly, after selecting (or changing) the analog input and before starting conversion it is necessary to provide at least 20uS time delay to enable the ACD maximal conversion accuracy.

ADC Clock Period

Time needed to complete a one-bit conversion is defined as TAD. The required TAD must be at least 1,6 uS. One full 10-bit A/D conversion is a bit longer than expected and amounts to 11 TAD periods. However, since both the conversion clock frequency and source are determined by software, one of the available combination of bits ADCS1 and ADCS0 should be selected before voltage measurement on some analog input starts. These bits are stored in the ADCON0 register.

ADC CLOCK SOURCEADCS1ADCS0DEVICE FREQUENCY (FOSC)
20 Mhz8 Mhz4 Mhz1 Mhz
Fosc/200100 nS250 nS500 nS2 uS
Fosc/801400 nS1 uS2 uS8 uS
Fosc/32101.6 uS4 uS8 uS32 uS
Frc112 – 6 uS2 – 6 uS2 – 6 uS2 – 6 uS

Table 7ADC Clock Period

Any change in the system clock frequency will affect the ADC clock frequency, which may adversely affect the ADC result. Device frequency characteristics are shown in the table above. The values in the shaded cells are outside of recommended range.

How to Use A/D Converter?

In order to enable the A/D converter to run without problems as well as to avoid unexpected results, it is necessary to consider the following:

  • A/D converter does not differ between digital and analog voltages. In order to avoid errors in measurement or chip damage, the pins should be configured as analog inputs before conversion starts. The bits used for this purpose are stored in the TRIS and ANSELH registers;
  • When the port with analog inputs marked as CH0-CH13 is read, the corresponding bits will be driven to logic zero (0); and
  • Roughly speaking, voltage measurement in the converter is based on comparing input voltage with internal scale which has 1024 marks (210=1024). The lowest scale mark stands for the Vref- voltage, whilst the highest mark stands for the Vref+ voltage. Figure 7-3 below shows selectable referent voltages and their minimum and maximum values as well.

pic-microcontrollers-examples-in-assembly-language-chapter-07-fig7-3

Fig. How to Use The A/D Converter

ADCON0 Register

--CHS3CHS2CHS1CHS0G0/DONEADON

                                                                 Fig. 7-4 ADCON0 Register

CHS3-CHS0 – Analog Channel Select bits select a pin or an analog channel for conversion, i.e. voltage measurement:

CHS3CHS2CHS1CHS0CHANNELPIN
00000RA0/AN0
00011RA1/AN1
00102RA2/AN2
00113RA3/AN3
01004RA5/AN4
01015RE0/AN5
01106RE1/AN6
01117RE2/AN7
10008RB2/AN8
10019RB3/AN9
101010RB1/AN10
101111RB4/AN11
110012RB0/AN12
1101Unimplemented
1110Unimplemented
1111Unimplemented

Table 7-3 Analog Channel Status Bits

GO/DONE – A/D Conversion Status bit determines current status of conversion:

  • 1 – A/D conversion is in progress; and
  • 0 – A/D conversion is complete. This bit is automatically cleared by hardware when the A/D conversion is completed.

ADON – A/D On bit enables A/D converter.

  • 1 – A/D converter is enabled; and
  • 0 – A/D converter is disabled.

ADCON1 Register

--VCFG1VCFG0PCFG3PCFG2PCFG1PCFG0

Fig. 7-5 ADCON1 Register

VCFG1 –

1 = VREF- (AN2)

0 = VSS

VCFG0 – Voltage Reference bit selects positive voltage reference source needed for A/D converter operating.

1 = VREF+ (AN3)
0 = VDD 

bit 3-0 PCFG<3:0>: A/D Port Configuration Control bits

PCFG3-PCFG0AN12AN11AN10AN9AN8AN8AN7AN6AN5AN4AN3AN2AN1AN0
0000AAAAAAAAAAAAAA
0001AAAAAAAAAAAAAA
0010AAAAAAAAAAAAAA
0011DAAAAAAAAAAAAA
0100DDAAAAAAAAAAAA
0101DDDAAAAAAAAAAA
0110DDDDAAAAAAAAAA
0111DDDDDDAAAAAAAA
1000DDDDDDDAAAAAAA
1001DDDDDDDDAAAAAA
1010DDDDDDDDDAAAAA
1011DDDDDDDDDDAAAA
1100DDDDDDDDDDDAAA
1101DDDDDDDDDDDDAA
1110DDDDDDDDDDDDDA
1111DDDDDDDDDDDDDD

A = Analog input      D = Digital I/O 

ADCON2

ADFM-ACQT2ACQT1ACQT0ADCS2ADCS1ADCS0

ADFM – A/D Result Format Select bit

  • 1 – Conversion result right justified. Six most significant bits of the ADRESLH are not used; and
  • 0 – Conversion result left justified. Six least significant bits of the ADRESL are not used.
ACQT<2:0>: A/D Acquisition Time Select bits
111 = 20 TAD
110 = 16 TAD
101 = 12 TAD
100 = 8 TAD
011 = 6 TAD
010 = 4 TAD
001 = 2 TAD
000 = 0 TAD

ADCS2,ADCS1, ADCS0 – A/D Conversion Clock Select bits select clock frequency used for internal synchronization of A/D converter. It also affects duration of conversion.

 ADCS<2:0>: A/D Conversion Clock Select bits 
111 = FRC (clock derived from A/D RC oscillator)
110 = FOSC/64
101 = FOSC/16
100 = FOSC/4
011 = FRC (clock derived from A/D RC oscillator)
010 = FOSC/32
001 = FOSC/8
000 = FOSC/2

Table 7-2 A/D Conversion Select Bits

* Clock is generated by internal oscillator which is built in converter.

In Short:

In order to measure voltage on an input pin by A/D converter the following should be done:

Step 1 – Configuring port:

  • Write logic one (1) to the corresponding bit of the TRIS register to configure it as input; and
  • Write logic one (1) to the corresponding bit of the ANSEL register to configure it as analog input.

Step 2 – Configuring ADC module:

  • Configure voltage reference in the ADCON1 register;
  • Select ADC conversion clock in the ADCON0 register;
  • Select one of input channels CH0-CH13 of the ADCON0 register;
  • Select data format using the ADFM bit of the ADCON1 register; and
  • Enable A/D converter by setting the ADON bit of the ADCON0 register.

Step 3 – Configuring ADC interrupt (optionally):

  • Clear the ADIF bit; and
  • Set the ADIE, PEIE and GIE bits.

Step 4 – Wait for the required acquisition time (approximately 20uS) to pass.

Step 5 – Start conversion by setting the GO/DONE bit of the ADCON0 register.

Step 6 – Wait for ADC conversion to complete.

  • It is necessary to check in program loop whether the GO/DONE pin is cleared or wait for an A/D interrupt (must be previously enabled).

Step 7 – Read ADC results:

  • Read the ADRESH and ADRESL registers.

Interfacing Diagram

internal-adc-interfacing-with-pic18.png

Video lecture on ADC


Video Proteus Simulation


Embedded C program

#include<P18F4520.h>

#pragma config OSC=HS
#pragma config PWRT=OFF
#pragma config WDT=OFF
#pragma config DEBUG=OFF, LVP=OFF

void lcdcmd(unsigned char value);
void lcddata(unsigned char value);
void msdelay(unsigned int itime);

#define ldata PORTD
#define rs PORTEbits.RE0
#define rw PORTEbits.RE1
#define en PORTEbits.RE2
                                                                  
void main(void)
{
unsigned int i, d;
unsigned char val,
temp[3];
TRISD=0;
PORTD=0;
TRISE=0x00;
PORTE=0;

ADCON0 = 0X01;
ADCON1 = 0X0E;
ADCON2=0b10001010;

msdelay(15);
lcdcmd(0x38);
msdelay(15);
lcdcmd(0x0E);
msdelay(15);
lcdcmd(0x01);
msdelay(15);
lcdcmd(0x06);
msdelay(15);

while(1)

{

lcdcmd(0x81);
msdelay(20);

ADCON0bits.GO = 1;

while
(ADCON0bits.DONE ==1);
temp[0]= (ADRESH & 0x0f);
temp[1]= (ADRESL & 0xf0)>>4;
temp[2]= (ADRESL & 0x0f);

for(d=0; d<3; d++)
{
if (temp[d] < 10)
temp[d] = temp[d]+0x30;
else
temp[d] = temp[d]+0x37;
lcddata(temp[d]);

msdelay(15);
}
msdelay(10);
}
}
void lcdcmd (unsigned char value)
{
ldata=value;
rs=0;
rw=0;
en=1;
msdelay(1);
en=0;
}
void lcddata (unsigned char value)
{
ldata=value;
rs=1;
rw=0;
en=1;
msdelay(1);
en=0;
}
void msdelay (unsigned int itime)
{
int i,j;
for(i=0;i<itime;i++)
for(j=0;j<135;j++);
}

References

  • Edited, Created and Notes by Prof. Sujit Wagh, SKNCOE, Pune
  • WikiNote Foundation
Tags:
Created by Sujit Wagh on 2017/11/13 16:59