Академический Документы
Профессиональный Документы
Культура Документы
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.
The below table shows the ADC input pins multiplexed with other GPIO pins.
The ADC pin can be enabled by configuring the corresponding ACON1 register.
When the ADC function is selected for a pin ,then other Digital signals are disconnected from the ADC input pins.
0 RA0 AN0
1 RA1 AN1
2 RA2 AN2/VREF-
3 RA3 AN3/VREF+
4 RA5 AN4
2 RE0 AN5
3 RE1 AN6
4 RE2 AN7
ADC Registers
The below table shows the registers associated with PIC16F877A ADC.
Register Description
ADCON0 Used to Turn ON the ADC, Select the Sampling Freq and also Start the conversion.
Now lets see how to configure the individual registers for UART communication.
ADCON0
7 6 5 4 3 2 1 0
Code
Below is the sample code to read the ADC value of channel0 and send it on UART at 9600 baud rate.
1 #include<pic16f877a.h>
2
3 #define SBIT_ADON 0
4 #define SBIT_CHS0 3
5 #define SBIT_ADFM 7
6
7 void delay(int cnt)
8 {
9 while(cnt--);
10 }
11
12 void ADC_Init()
13 {
14 ADCON0=0x00; // sampling freq=osc_freq/2,ADC off initially
15 ADCON1=(1<<SBIT_ADFM); // All pins are configured as Analog pins and ADC result is right justified
16 }
17
18
19 int ADC_Read(int adcChannel)
20 {
24
25 GO=1; // Start ADC conversion
28
29 return((ADRESH<<8) + ADRESL); // return right justified 10-bit result
30 }
31
32
33 int main()
34 {
35 int adcValue=0;
36
37 TRISB = 0x00; // Configure PORTB and PORTD as output to display the ADC values on LEDs
38 TRISD = 0x00;
39
40
41 ADC_Init(); //Initialize the ADC module
42
43 while(1)
44 {
49 }
50 }
3
4
5
6 int main()
7 {
8 int adcValue;
9
10 ADC_Init(); /* Initialize the ADC module */
11 UART_Init(9600); /* Initialize UART at 9600 baud rate */
12
13 while(1)
14 {
17 }
18
19 return (0);
20 }
Downloads
Download the complete project folder from the below link:
Hardware design Files and Code Library
Have a opinion, suggestion , question or feedback about the article let it out here!
name@example.com SUBSCRIBE
Now shipping worldwide from India with