In this tutorial we are going to discuss how to use the inbuilt PIC16f877A ADC.
We will be reading the ADC values from chanel Zero and transmitted on UART at 9600 baudrate.
PIC16F877A ADC pins
PIC16F877A has inbuilt 8channel 10-bit ADC. Below tables shows the ADC pin mapping.
Adc Channel | Pic16f877a Pin | Pin Function |
---|---|---|
0 | PA.0 | AN0 |
1 | PA.1 | AN1 |
2 | PA.2 | AN2/VREF- |
3 | PA.3 | AN3/VREF+ |
4 | PA.5 | AN4 |
2 | PE.0 | AN5 |
3 | PE.1 | AN6 |
4 | PE.2 | AN7 |
Code
Below is the sample code to read the ADC value of channel0 and sent it on UART at 9600 baud rate.
As the POT is connected to ADC0, the values can be varied using this POT.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "adc.h" | |
#include "uart.h" | |
int main() | |
{ | |
int adcValue; | |
ADC_Init(); /* Initialize the ADC module */ | |
UART_Init(9600); /* Initialize UART at 9600 baud rate */ | |
while(1) | |
{ | |
adcValue = ADC_GetAdcValue(0); // Read the ADC value of channel zero | |
UART_Printf("ADC0 Value:%4d \n\r",adcValue); // Send the value on UART | |
} | |
return (0); | |
} |