Professional Documents
Culture Documents
Lab 5:
ADC of 16f877A uC
Objectives:
1. Learn to use the ADC module on the 16f877A uC.
Introduction:
An analog-to-digital converter (A/D) is another important peripheral component of a
microcontroller. The A/D converts an analog input voltage into a digital number so it
can be processed by a microcontroller or any other digital system.
The PIC 16F877A microcontroller contain a 8-channel 10-bit ADC. If the chosen
voltage refrence is +5V, the voltage step (resolution) value is:
Therefore, for example, if the input voltage is 1.0V the converter will generate a
digital output of 1.0/0.00489 = 205 decimal. Similarly, if the input voltage is 3.0V,
the converter will generate 3.0/0.00489 = 613.
The ADC used by the PIC 16F877A microcontroller has eight channels, named AN0AN7, which are shared by the PORTA and PORTE pins.
The ADC module has four registers. These registers are:
ADC Result High Register (ADRESH)
ADC Result Low Register (ADRESL)
ADC Control Register 0 (ADCON0)
ADC Control Register 1 (ADCON1)
In this experiment only the ADC Control Register 1 (ADCON1) is used to control the
channels of the converter.
ADC of 16f877A uC
ADCON1 Register:
The ADCON1 register configures the functions of the port pins. The port pins can be
configured as analog inputs (RA3 can also be the voltage reference) or as digital I/O.
Bits 0 to 3 i.e. PCFG0-PCFG3 are the ADC port configuration control bits. Figure 6.1
shows all possible settings for these bits.
Figure 6.1
ADC of 16f877A uC
...
tmp = ADC_Read(2);
Example 1:
Connect the circuit shown in Figure 6.2 in Proteus and compile the program of
example 1 given below.
//
//
//
//
ADC of 16f877A uC
RC0
} while(1);
}
Exercise 1:
1. What are the digital values for the following analog DC voltages at AN0:
Analog
Voltage
0.25 V
Digital Value
0000110011
1.49 V
0100110000
2.48 V
0111111010
3.47 V
1011000101
4.94 V
1111110101
Example 2:
Connect the circuit shown in Figure 6.3 in Proteus and compile the program of
example 2 given below.
ADC of 16f877A uC
Figure 6.3
ADC of 16f877A uC
TRISA = 0xFF;
Lcd_Init();
Lcd_Cmd(_LCD_CURSOR_OFF);
// PORTA is input
// Initialize LCD
// Cursor off
while(1)
{
Lcd_Cmd(_LCD_CLEAR);
Vin = Adc_Read(0);
mV = (Vin * 5000) >> 10;
LongToStr(Vin,op1);
LongToStr(mV,op2);
//
//
//
//
0 (AN0)
1024
in "op1"
in "op2"