How to Program ADC in PIC16F877A (External Voltage Reference)

The following code demonstrate how to use ADC with external voltage reference. The external reference voltage is applied on RA3/Vref+ pin. In this example 4.096V reference is used, for this precise reference you can use MCP1541 IC. The code is written in “mikroC PRO for PIC v.6.6.3” IDE and simulation is done with Proteus 8.0 SP0. At the end of code, you can find complete project files for download.
Picture 2017-12-19 20_52_00
Picture 2017-12-19 21_10_50
Picture 2017-12-19 21_26_46

Code in mikroC

// function proto-type
void ADC_Setting(void);
void main(void)
{
unsigned adc_value = 0;
// set direction as output
TRISB = 0x00;
TRISC = 0x00;
// as input
TRISA = 0xFF;
// init to zero
PORTB = 0x00;
PORTC = 0x00;
ADC_Setting();
while(1)
{
// read analog value from ADC module channel 0
adc_value = ADC_Get_Sample(0);
PORTB = adc_value;
PORTC = (adc_value>>8) & 0x03;
}
}
void ADC_Setting(void)
{
// Initialize ADC module with default settings
ADC_Init();
// override adc setting
// PCFG3:PCFG0: A/D Port Configuration Control bits
// external Vref+
ADCON1.PCFG3 = 0;
ADCON1.PCFG2 = 0;
ADCON1.PCFG1 = 0;
ADCON1.PCFG0 = 1;
}

Download Files

For download “mikroC PRO for PIC” project and “Proteus 8.0” simulation files, click here.

Comments

Popular posts from this blog

How to Program Interrupts in PIC16F877A

How to Program Parallel Slave Port (PSP) in PIC16F877A

How to Program SPI in PIC16F877A (Slave Mode)