In addition to a large number of digital I/O pins used for communication with peripheral modules, the PIC16F887 also has 14 analog inputs. They enable the microcontroller to recognize not only whether a pin is driven low or high (0 or +5V), but to precisely measure its voltage and convert it into numerical value, i.e. digital format.One of the most important analog modules within the microcontroller is an A/D converter which has the following features:
|ADC CLOCK SOURCE||ADCS1||ADCS0||DEVICE FREQUENCY (FOSC)|
|20 Mhz||8 Mhz||4 Mhz||1 Mhz|
|Fosc/2||0||0||100 nS||250 nS||500 nS||2 uS|
|Fosc/8||0||1||400 nS||1 uS||2 uS||8 uS|
|Fosc/32||1||0||1.6 uS||4 uS||8 uS||32 uS|
|Frc||1||1||2 - 6 uS||2 - 6 uS||2 - 6 uS||2 - 6 uS|
|1||1||1||1||Vref = 0.6V|
'This example code reads analog value from channel 2 and displays it on PORTB and PORTC 'as a 10-bit binary number. dim adc_rd as word main: ANSEL = 0x04 ' Configure AN2 as an analog pin TRISA = 0xFF ' PORTA is configured as an input ANSELH = 0 ' Configure all other AN pins as digital I/O TRISC = 0x3F ' Pins RC7 and RC6 are configured as outputs TRISB = 0 ' PORTB is configured as an output while 1 temp_res = ADC_Read(2) ' Get a 10-bit result of AD conversion PORTB = temp_res ' Send lower 8 bits to PORTB PORTC = temp_res >> 2 ' Send 2 most significant bits to RC7 and RC6 wend ' Remain in the loop end.
* In order to enable the state of the C1OUT bit to be present on the pin, two conditions must be met: C1ON = 1 (comparator must be enabled) and the corresponding pin must be configured as an output (TRIS bit = 0).C1POL - Comparator C1 Output Polarity Select bit enables the state of the comparator C1 output to be inverted.
|C1CH1||C1CH0||COMPARATOR C1VIN- INPUT|
|0||0||Input C1Vin- is connected to the C12IN0- pin|
|0||1||Input C1Vin- is connected to the C12IN1- pin|
|1||0||Input C1Vin- is connected to the C12IN2- pin|
|1||1||Input C1Vin- is connected to the C12IN3- pin|
* In order to enable the state of the C2OUT bit to be present on the pin, two conditions must be met: C2ON = 1 (comparator must be enabled) and the corresponding pin must be configured as an output (TRIS bit = 0).C2POL - Comparator C2 Output Polarity Select bit enables the state of the comparator C2 output to be inverted.
|C2CH1||C2CH0||COMPARATOR C2VIN- INPUT|
|0||0||Input C2Vin- is connected to the C12IN0- pin|
|0||1||Input C2Vin- is connected to the C12IN1- pin|
|1||0||Input C2Vin- is connected to the C12IN2- pin|
|1||1||Input C2Vin- is connected to the C12IN3- pin|