Continue to Site

Welcome to

Welcome to our site! is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Modalities Of Using The ADC Module Of PIC 16F877A - Part 4

ADC pinout of PIC 16F877A


PIC16F877A has 8 ADC input channels. That means that 8 pins are multiplexed (connected) to the PIC ADC module. So, you can use the ADC module to convert analogue input from 8 inputs. PIC16F877A cannot do the conversions simultaneously, so it must be done one by one. This will be shown later on. The 8 ADC input pins are PORTA0, PORTA1, PORTA2, PORTA3, PORTA5, PORTE0, PORTE1, PORTE2. Each of these is an ADC channel. PORTA0 is analogue channel 0, PORTA1 is analogue channel 1, PORTA2 is analogue channel 2, PORTA3 is analogue channel 3, PORTA5 is analogue channel 4, PORTE0 is is analogue channel 5, PORTE1 is analogue channel 6, PORTE2 is analogue channel 7. Each of these channels is read one by one as required (set in code). By default all the ADC input pins are configured as analogue pins. When a pin is configured as an analogue pin, it can be used by the ADC for conversion or by the analogue comparator. But we can not use it as a digital pin: we cannot check the logic state of the pin – if it is 0 or 1.

AN is used to denote analogue channel. AN0 means analogue channel 0. AN5 means analogue channel 5. And so on.
__________________________________________________ __________________________________________________ ________________

Register Description: ADCON0 and ADCON1

There may be situations when we need only one or two channels to be used – only that one or two pins to be configured as analogue pin(s). We may need the other pins as digital pins. We may have switches connected to them and need to read the digital state of those pins. The ADCON1 register allows us to configure which pins will be configured as analogue, which pins will be configured as digital and which pins will be used as VREF+ and VREF- if they are to be connected externally.

But before discussing the ADCON1 register, let's discuss the ADCON0 register.


Bits 7 and 6 of the ADCON0 register, along with bit 6 of the ADCON1 register select the ADC conversion clock, used to define TAD. I'll discuss that a little later.

Bits 5, 4 and 3 are used to select which channel is to be “measured” and the corresponding analogue voltage converted. The value of these 3 bits – bit 5 being the MSB and bit 3 the LSB – defines which channel is to be measured and converted. If bit 5 = 1, bit 4 = 1, bit 3 = 0, the corresponding binary value of these 3 pins is, 110, which in decimal is equal to 6. So, channel 6 will be measured and the analogue voltage at channel 6 will be converted. The default value of these 3 bits at chip reset (start-up) is 0, so the default channel to be measured (unless you change the value of bits 5, 4 and/or 3) is channel 0 (PORTA0). If you are curious, the name of bit 5 is CHS2, the name of bit 4 is CHS1 and the name of bit 3 is CHS0. So, in conclusion, it is the value of CHS[2:0] that determines which channel will be measured.

Bit 2 of the ADCON0 register is GO/DONE. To start an analogue to digital conversion, this bit must be set. After that, you must monitor the value of this bit. When the analogue to digital conversion is complete, GO/DONE is cleared by hardware automatically, ie, GO/DONE equals to 0. At any time, the value of GO/DONE can be monitored to check if any conversion is taking place or if a conversion has ended. If the conversion is going on, bit 2 – GO/DONE – equals to 1. If no conversion is going on, or if the conversion is complete, bit 2 – GO/DONE – equals 0. To start the conversion, by setting GO/DONE to one, the ADC must first be turned on by setting bit 0 – ADON – discussed in the paragraph below.

Bit 0 of the ADCON0 register is ADON. If this bit is set (ADON = 1), the PIC ADC is turned on and can be used for conversions. If this bit is cleared (ADON = 0), the PIC ADC is turned off and can not be used for conversions. (in this situation, the PIC ADC will consume no operating current) ADON may be set to 1 at the beginning of the code and it is not necessary to turn off the ADC, unless power is to be saved.

Now, let's discuss the ADCON1 register.


Bit 7 is ADFM, which defines the justification discussed above. Setting ADFM to 1 selects right-justification. Clearing ADFM to 0 selects left-justification.

Bit 6 is ADCS2, which along with ADCON0 bit 7 (ADCS1) and ADCON0 bit 6 (ADCS0), define TAD. The possible values are:


Bits 3, 2, 1 and 0 named PCFG3, PCFG2, PCFG1 and PCFG0 are used to define which pins will be analogue pins to be used by the ADC, which pins will be digital and which pins (if any) will be used for external reference – VREF+ and VREF-.


So, let's discuss this a bit. PCFG<3:0> refers to the values of PCFG3, PCFG2, PCFG1 and PCFG0 in that order. Let's say we need 2 ADC channels and will use VDD and VSS for reference. There is no setting for 2 analogue channels and 0 external reference pins. So, we'll go with 3 analogue channels and 0 external reference pins by using 0100 for PCFG<3:0>. For this setting, channels 0, 1 and 3 are analogue. So, PORTA0, PORTA1 and PORTA3 are analogue pins. The rest are digital pins. VREF+ = VDD and VREF- = VSS. Alternatively, we can use the setting 0101 and have channels 0 and 1 as analogue and AN3 as VREF+ (and VREF- = VSS). So, we can just connect AN3, ie PORTA3 to VDD externally.

When any pin is used for analogue circuitry, the corresponding TRIS bit must be set to 1, as it becomes an input pin.

That should clear you up on how to use the above table for PCFG setting for selecting which pins will be analogue or digital. The following examples should clear any doubts.

__________________________________________________ __________________________________________________ ________________

If we're going to need 4 analogue pins, and will use VREF+ = 3.3V and VREF- = 2.0V, what setting of PCFG will we need?

We'll need 4 pins for ADC conversion and 2 for reference. PCFG setting of 1011 gives us exactly that. AN3 is VREF+ where we must provide +3.3V. AN2 is VREF- where we must provide 2.0V.
__________________________________________________ __________________________________________________ ________________


If we're going to need 4 analogue pins, and will use VREF+ = 3.3V and VREF- = 0V, what setting of PCFG will we need?

We'll need 4 pins for ADC conversion and 1 for reference for VREF+ as VREF- will be VSS. PCFG setting of 0011 gives us exactly that. Alternatively, we can use PCFG setting of 1011 like in the previous example and connect VREF- externally to ground.
__________________________________________________ __________________________________________________ ________________


If we’re going to need 5 analogue pins, will use VREF+ = 5V and VREF- = 0V and the PIC is powered off 5V, what setting of PCFG will be needed?

We can use 0010 as, VREF+ can be VDD and VREF- can be VSS.
__________________________________________________ __________________________________________________ ________________

When AN3 is selected as VREF+, we must connect the positive voltage reference to this pin – PORTA3. When AN2 is selected as VREF-, we must connect the negative voltage reference to this pin – PORTA5. So, if we have VREF+ = 4.0V, VREF- = 2.0V, 4.0V is connected to PORTA3 and 2.0V is connected to PORTA5. Refer to Part 6 for basic hardware connections (circuit diagram) for using the ADC module of PIC 16F877A.

__________________________________________________ __________________________________________________ ________________
Continued in Part 5............


There are no comments to display.

Part and Inventory Search

Blog entry information

Read time
5 min read
Last update

More entries in Uncategorized

More entries from Tahmid

Share this entry