How to Program ADC in PIC16F877A (Internal Voltage Reference)
The following code demonstrate, how to write a program that gets ADC sample from channel-0 (AN0) and 10-bits digital data is displayed on PORTC:PORTB. The internal reference voltage is used that is 5 VDC. The step voltage is 4.88 mv with 0.28125% error (1/1024 * 5v = 4.88mv). The code is written in “mikroC PRO for PIC v.5.6.1” IDE and simulation is done with Proteus 8.0 SP0. At the end of code, you can find complete project files for download.
Code in mikroC
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;
// Initialize ADC module with default settings
ADC_Init();
while(1)
{
// read analog value from ADC module channel 0
adc_value = ADC_Get_Sample(0);
PORTB = adc_value;
PORTC = (adc_value>>8) & 0x03;
}
}
{
unsigned adc_value = 0;
// set direction as output
TRISB = 0x00;
TRISC = 0x00;
// as input
TRISA = 0xFF;
// init to zero
PORTB = 0x00;
PORTC = 0x00;
// Initialize ADC module with default settings
ADC_Init();
while(1)
{
// read analog value from ADC module channel 0
adc_value = ADC_Get_Sample(0);
PORTB = adc_value;
PORTC = (adc_value>>8) & 0x03;
}
}
Download Files
For download “mikroC PRO for PIC” project and “Proteus 8.0” simulation files, click here.
Comments
Post a Comment