L7. PIC16F877:ADC MODULE: Lab Notes 7 Mechatronics Lecture Notes by Dr. Can U. Dogruer
1. This document contains lecture notes on configuring and using the analog-to-digital converter (ADC) module of the PIC16F877 microcontroller.
2. It describes using the ADC to read the analog voltage from a potentiometer connected to PORTA channel 0 and displaying the 3 most significant bits of the 10-bit ADC output on LEDs connected to PORTB pins 0-2.
3. Sample code is provided to configure the ADC, read the analog value, shift it right by 7 bits, and assign the result to PORTB to light the LEDs based on the potentiometer position.
L7. PIC16F877:ADC MODULE: Lab Notes 7 Mechatronics Lecture Notes by Dr. Can U. Dogruer
1. This document contains lecture notes on configuring and using the analog-to-digital converter (ADC) module of the PIC16F877 microcontroller.
2. It describes using the ADC to read the analog voltage from a potentiometer connected to PORTA channel 0 and displaying the 3 most significant bits of the 10-bit ADC output on LEDs connected to PORTB pins 0-2.
3. Sample code is provided to configure the ADC, read the analog value, shift it right by 7 bits, and assign the result to PORTB to light the LEDs based on the potentiometer position.
Hacettepe University / Mechanical Engineering Department Automotive Engineering Program L7-1 L7. PIC16F877:ADC MODULE Objectives 1. Configure ADC PART LIST NO PART NAME QUANTITY 1 PIC16F877 / PIC16F877A 1 2 Push Button 1 3 10K Resistor 9 6 Potentiometer 1 7 MAX232 1 8 1-micro Farad Polarized Capacitor 5 9 RS-232 Connector 1 0 RS-232 Cable 1 1 20 MHz Crystal 1 2 22-picoFarad Capacitor 2 3 Copper Wire 2 meter 4 USB PIC Programmer [Hardware] 1 5 PIC Downloader Program [Bootloader; Software] 1
NOTE: Study ADC library of mikroC and read the relevant pages of PIC16877A about ADC and learn how to configure ADC module of PIC16F877. Read also the mikroC web book whose address is given in the web page of the lecture L6.1 EXPERIMENT V: DC Motor Interfacing In this experiment you will read the analog voltage from PORTA channel 0. The analog voltage is supplied by a potentiometer. As you change the variable resistance the voltage that is applied to PORTA channel 0 will change. The ADC module of the PIC16F877 will convert the input voltage to an integer number between 0 and 1024. Notice that the ADC module of the PIC 16F877 is a 10-bit module, so the there are 2 N=10 =1024 binary number to represent the input voltage range. In this experiment we fix the input voltage range to 0-5V. If we want to compute quantization size (or resolution) of the ADC module 1
= I mux -I mux 2 N -1
= S - u 2 10 - 1 = 4.8876 1u -3
The output of ADC module is a 10-bit binary number, and we will display this binary number on three LEDS connected to PORTB (pin 0-2). In order to display the most significant three bits of the output of ADC module, we
1 Read the relevant pages of PIC16F877 how to configure pins of PORTA as analog pin and how to fix the input voltage range. ADCON1 is the special function register that controls these properties of ADC module of PIC16F877. Lab Notes 7 Mechatronics Lecture Notes PIC16F877: ADC Module by Dr. Can U. Dogruer Hacettepe University / Mechanical Engineering Department Automotive Engineering Program L7-2 make a simple trick; we use shift operate >>. Shift operator >> shifts the digits of binary number to right and replaces the most significant bits with zero, for example Temp1=1010101111 Temp2=Temp>>4 Temp2=0000101010 Temp2=Temp>>7 Temp2=0000000101 After we use the shift operator on the output of ADC module we use the following command to assign this number to PORTB PORTB= temp_res>>7;
Lab Notes 7 Mechatronics Lecture Notes PIC16F877: ADC Module by Dr. Can U. Dogruer Hacettepe University / Mechanical Engineering Department Automotive Engineering Program L7-3 SAMPLE CODE unsigned int temp_res; void main() { ADCON1=0x00; TRISA = 0xFF; // All port A pins are configured as inputs TRISB=0x00; PORTB=0x00; do { temp_res = ADC_Read(0); // Result of A/D conversion is copied to temp_res PORTB= temp_res>>7; } while(1); // Endless loop }