

Initialize the ADC Module: We have already learnt how to initialize an ADC so we just call this below function to initialize the ADC Get the ADC result from ADRESH and ADRESL registerġ.To read an ADC value the following steps has to be followed. Now after initializing the ADC module inside our main function, lets get into the while loop and start reading the ADC values. Complete details available on datasheet page 128. The other bits remain zero as we have planned to use the internal reference voltage. In this register we have to make A/D Result Format Select bit high by ADFM=1 and make ADCS2 =1 to select the Fosc/16 again. Now the ADCON1 register has the following bits: Hence ADCON0 will be initialised as follows. Complete details available on datasheet’s page 127. In our program the A/D conversion clock is selected as Fosc/16 you can try your own frequencies and see how the result changes.

In this register we have to turn on the ADC module by making ADON=1 and turn on the A/D Conversion Clock by using the bits ADCS1 and ADCS0 bits, the rest will not be set for now.

The ADCON0 register has the following bits: Inside the void main() we have to initialize our ADC by using the ADCON1 register and ADCON0 register. As usual initialize the configuration bits and let’s start with the void main(). The program for using ADC with PIC Microcontroller is very simple, we just have to understand these four registers and then reading any analog voltage will be simple. The A/D module has four registers which has to be configured to read data from the Input pins. In this tutorial we will use channel 4 with a potentiometer to read the analog voltage at this channel.
#Mplab xc8 lcd library xlcd code
So before reading an input voltage we have to specify in our code which channel has to be used to read the input voltage. Only these pins will be able to read analog voltage. The analog channels AN0 to AN7 are highlighted for you. The eight pins which can read the analog voltage are mentioned in the datasheet. The value 1024 is obtained by 2^10 since our ADC is 10 bit. This means the output value of our ADC will be 0-1024 (2^10) and there are 8 pins (channels) on our MCU which can read analog voltage. The PIC we are using has 10-bit 8-channel ADC. Now that we know some basics on ADC, lets open our datasheet and learn how to use the ADC on our PIC16F877A MCU. The speed of the comparison depends on the Clock frequency (Fosc) on which the PIC is operating. This type of ADC uses a reference voltage (which is variable) and compares the input voltage with the reference voltage using a comparator and difference, which will be a digital output, is saved from the Most significant bit (MSB). Successive Approximation ADC: The SAR ADC works with the help of a comparator and some logic conversations. So let’s learn a bit about SAR ADC before we start using it. The type of ADC used in PIC16F877A is called as the Successive approximation ADC or SAR in short. The most common types of ADCs are flash, successive approximation, and sigma-delta. There are many types of ADC available and each one has its own speed and resolution.

If you are an absolute beginner, then please visit the complete list of PIC tutorials here and start learning. Till now, we have covered many basic tutorial like getting started with MPLABX, LED blinking with PIC, Timers in PIC, interfacing LCD, interfacing 7-segment etc. This is our 9th tutorial of Learning PIC microcontrollers using MPLAB and XC8.
