The PIC16F877A’s integrated Analog to Digital Converter (ADC).

27 January, 2007 (10:43) | Microchip PIC, Projects | By: glogin

1. Introduction.

This is an example how to use integrated Analog to Digital Converter (ADC) of the PIC16F877A. The code was written in the CCS C and tested on the PIC-01 Development Board.

2. The ADC.

The PIC16F877A microcontroller has a 10-bit 8-channel integrated Analog to Digital Converter. In the analog to digital conversion the ADC can use either on-board RC oscillator or divided clock signal (Fosc). The freqency can be chosen in programming from Fosc/2, Fosc/4, Fosc/8, Fosc/16, Fosc/32, Fosc/64. The voltage input range depends on the reference voltege on inputs Vref+ (RA3) and Vref- (RA2). If the inputs are not connected the reference volage ranges from Vss to Vcc.

3. Circuit.

There is a 4.7k pot installed on the PIC-01 Development Board for the purpose of the ADC. The pot is connected to the RA0 input and between the GND and Vcc of the power supply.

4. Listing.

The program was written in the CCS C and uses the PIC16F877A standard library. The RA0 pin has to be set as the analog input. The ADC uses the internal RC oscillator.

The ADC has the 10-bit resolution, which means that the 0-5V range is divided between 210 = 1024 levels. It means that 1-bit represents 5V / 1024 = 0.0048V = 4.8mV voltage.

The voltage read from the RA0 input is displayed on the PIC-01 Development Board’s LCD in the format “Voltage = X.XX V”.

//————————————————————
//
// acd.c
//
//————————————————————-

#include<16f877a.h> //Device
#fuses xt,nowdt,protect,noput,nolvp //Fuses
#device adc=10 //10-bit ADC
#use delay (clock=4000000) //4.00 MHz
#use rs232 (Baud=9600) //RS-232 parametres
//(only for printf function)
#include”llcd_new.h” //LCD driver

main()
{

float c;
lcd_init();
set_tris_a(0b000001); //RA0 anlog
setup_adc(adc_clock_internal); //internal clock
setup_adc_ports(ra0_analog); //RA0 analog input
set_adc_channel(0); //set channel

while(1)
{
c=read_adc(); //voltage conversion
delay_ms(150); //10-bit = 1024
lcd_gotoxy(1,1); //0-5 V and 1024 levels
printf(lcd_putc,”Voltage = %1.2f V “,(c*5)/1024); //calculate voltage
delay_ms(150);
}
}

Files are available here:
adc.c (.zip)
adc.hex (.zip)

Bad Behavior has blocked 58 access attempts in the last 7 days.