Program for ADC in PIC16F877A

Topic Progress:

Register description of ADC in PIC16F877A

Most of the configurations to set up the ADC has to be done in the ADCON register. The ADCON register is the Control Register of the ADC. The ADC conversion is given in the user manual of PIC16F877A. Under the register description, you will notice the various bits associated with the ADCON register. The following are the necessary configurations of the bits in the ADCON register which we will be using for the ADC setup.

  • ADCON0: This register uses to select input channels and control conversion process.
  1. ADCON0 needs to be configured for enabling the ADC peripheral (ADON bit).
  2. GO bit in the register starts the conversion of the input analog signal when set. The bit is also used as A/D conversion status bit.
  3. CHS2:CHS0 bits are the channel select bits from 7(0-7) variable channels which are multiplexed with digital I/O pins.
  4. ADCS1:ADCS0 configures the clock option for ADC peripheral. The system clock is divided to configure clock for AD conversion configurations for different clocks can be obtained from the datasheet.
  • ADCON1:
  1. PCFG0:PCFG3 bits in ADCON1 determines which pins of the controller should be configured as analog inputs. We have seen that the ADC module has 7 channels which are multiplexed with the I/O pins. Configuration of pins as digital or analog can be achieved through configuring these bits in ADCON1.
  2. ADFMthis bit is used to select the format by which result should be stored int the ADRESH and ADRESL
    1. ADRESH(High byte) and ADRESL(Low byte)Registers are used to store the converted data.i.e,Digital data.But the data is 10bit wide, remaining six-bit are not used.

Steps to perform ADC Conversion

  • Configure the ADC module
  1. Configuring analog pin, voltage reference, and digital I/O. Select A/D acquisition time and conversion clock, output result format. Right Justified

ADCON1 = 0x8E;            / /AN0 only will analog

  1.    Turn on the ADC by setting ADON bit

ADCON0 = (1<<0) | (0<3);

  • Start the conversion
  1. Wait for the required acquisition time
  2. Start the conversion by setting ADON/GO bit.

GO = 1;

  1.   Wait for A/D conversion to complete. As long as the GODONE bit HIGH, the conversion is not complete.

while(GO == 1);

  1.    Read the result from ADRES register.

ADC_Value = ((ADRESH<<8) + ADRESL);

  1.     Repeat the start conversion section if it needs further conversion.

Now that you have an idea of what should be done on a register level, creating a structure for this program is easier.

The basic idea is that your ADC set-up has to be done at the start of the program and the process that involves the Start of Conversion, Selection of the channel, waiting for EOC and reading the ADC result must be done periodically. (HINT: Include this in a continuous while loop)

Your final result should be displayed in an LCD or a UART. But you cannot write this directly. If the obtained result is 123, you must split this number and display it character by character. Therefore, create a function to split the obtained result.