3.9 Analog Modules
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:
- Conversion is performed by applying the method of successive approximation;
- There are 14 separate analog inputs connected to the microcontroller port pins;
- The A/D converter converts an analog input signal into a 10-bit binary number;
- The resolution, i.e. the quality of conversion may be adjusted to various needs by selecting voltage references Vref- and Vref+.
A/D CONVERTER
Even though the use of A/D converter seems to be very complicated, it is basically very simple, simpler than the use of timers and serial communication modules, anyway.
The operation of A/D converter is in control of the bits of four registers:
- ADRESH Contains high byte of conversion result;
- ADRESL Contains low byte of conversion result;
- ADCON0 Control register 0; and
- ADCON1 Control register 1.
ADRESH and ADRESL Registers
The process of converting an analog value into digital has as a result a 10-bit number to be stored in the ADRESH and ADRESL registers. There are two ways of handling this number - left and right justification which simplifies its use to a great extent. The format of conversion result depends on the ADFM bit of the ADCON1 register. In case the A/D converter is not used, ADRESH and ADRESL registers may be used as general-purpose registers.
A/D ACQUISITION REQUIREMENTS
For the voltage to be accurately measured by means of the A/D converter, it is necessary to provide a certain time delay between the selection of an analog input and measurement itself. Such time delay is called ‘acquisition time’ and mainly depends on the source impedance. There is an equation used to calculate this time delay accurately, which in the worst case amounts to approximately 20uS. So, if you want the A/D conversion to be accurate, don’t forget this small detail.
ADC CLOCK PERIOD
The time needed to complete a single-bit conversion is referred to as TAD. It is required to be at least 1,6 uS. One full 10-bit A/D conversion is a little bit longer than expected and amounts to 11 TAD periods. Since both device clock frequency and A/D conversion clock source are specified by software, it is necessary to select one of the available combinations of bits ADCS1 and ADCS0 before the voltage measurement on some of the analog inputs starts. These bits are stored in the ADCON0 register.
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 |
Any change of the microcontroller clock frequency will affect the A/D conversion speed, which may adversely affect the ADC result. The table abowe shows some of the possible device clock frequencies as well as how they affect the speed of A/D conversion. The values in the shaded cells are outside of the recommended range.
HOW TO USE THE A/D CONVERTER?
In order to enable the A/D converter to run properly as well as to avoid unexpected results, it is necessary to consider the following:
- The A/D converter does not differ between digital and analog signals. In order to avoid errors in measurement or to prevent the chip from being damaged, all A/D input pins should be configured as analog inputs before the process of conversion starts. The appropriate bits are stored in the TRIS and ANSEL (ANSELH) registers;
- When reading the port with analog inputs marked as CH0-CH13, the state of the corresponding bits, i.e. their digital value will be read as a logic zero (0); and
- Roughly speaking, the process of measuring voltage by the converter is based on comparing input voltage to an immaginary scale with 1024 grades (210 = 1024). The lowest mark is referred to as the Vref- voltage, while the highest one is referred to as the Vref+ voltage. Figure below shows selectable voltage references and their cutoff values, as well.
ADCON0 Register
ADCS1, ADCS0 - A/D Conversion Clock Select bits select clock frequency used for internal synchronization of A/D converter. It also affects the speed of A/D conversion.
ADCS1 |
ADCS2 |
CLOCK |
0 |
0 |
Fosc/2 |
0 |
1 |
Fosc/8 |
1 |
0 |
Fosc/32 |
1 |
1 |
RC * |
* Clock is generated by the internal oscillator built into the A/D converter.
CHS3-CHS0 - Analog Channel Select bits select a pin (an analog input) for A/D conversion, i.e. voltage measurement:
CHS3 |
CHS2 |
CHS1 |
CHS0 |
CHANNEL |
PIN |
0 |
0 |
0 |
0 |
0 |
RA0/AN0 |
0 |
0 |
0 |
1 |
1 |
RA1/AN1 |
0 |
0 |
1 |
0 |
2 |
RA2/AN2 |
0 |
0 |
1 |
1 |
3 |
RA3/AN3 |
0 |
1 |
0 |
0 |
4 |
RA5/AN4 |
0 |
1 |
0 |
1 |
5 |
RE0/AN5 |
0 |
1 |
1 |
0 |
6 |
RE1/AN6 |
0 |
1 |
1 |
1 |
7 |
RE2/AN7 |
1 |
0 |
0 |
0 |
8 |
RB2/AN8 |
1 |
0 |
0 |
1 |
9 |
RB3/AN9 |
1 |
0 |
1 |
0 |
10 |
RB1/AN10 |
1 |
0 |
1 |
1 |
11 |
RB4/AN11 |
1 |
1 |
0 |
0 |
12 |
RB0/AN12 |
1 |
1 |
0 |
1 |
13 |
RB5/AN13 |
1 |
1 |
1 |
0 |
CVref |
1 |
1 |
1 |
1 |
Vref = 0.6V |
GO/DONE - A/D Conversion Status bit determines current status of conversion:
- 1 - A/D conversion is in progress.
- 0 - A/D conversion is complete. This bit is automatically cleared after completing A/D conversion.
ADON - A/D On bit enables A/D converter.
- 1 - A/D converter is enabled.
- 0 - A/D converter is disabled.
Let's do it in mikroBasic...
'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.
ADCON1 Register
ADFM - A/D Result Format Select bit
- 1 - Conversion result is right justified. Six most significant bits of the ADRESH register are not used.
- 0 - Conversion result is left justified. Six least significant bits of the ADRESL register are not used.
VCFG1 - Voltage Reference bit selects the negative voltage reference source for the operation of A/D converter.
- 1 - The Vref- pin is fed with negative voltage reference.
- 0 - Power supply voltage Vss is used as a negative voltage reference source.
VCFG0 - Voltage Reference bit selects the positive voltage reference source for the operation of A/D converter.
- 1 - The Vref+ pin is fed with positive voltage reference.
- 0 - Power supply voltage Vdd is used as a positive voltage reference source.
In Short
Using A/D converter
In order to measure voltage on an input pin by means of the A/D converter, the following should be done:
Step 1 - Port configuration:
- Write a logic one (1) to a bit of the TRIS register to configure the appropriate pin as an input.
- Write a logic one (1) to a bit of the ANSEL register to configure the appropriate pin as an analog input.
Step 2 - ADC module configuration:
- Configure voltage reference in the ADCON1 register.
- Select A/D conversion clock in the ADCON0 register.
- Select one of input channels CH0-CH13 of the ADCON0 register.
- Select data format using the ADFM bit of the ADCON1 register.
- Enable the A/D converter by setting the ADON bit of the ADCON0 register.
Step 3 - ADC interrupt configuration (optionally):
- Clear the ADIF bit.
- Set bits ADIE, PEIE and GIE.
Step 4 - Wait for the required acquisition time to pass (approximately 20uS).
Step 5 - Start conversion by setting the GO/DONE bit of the ADCON0 register.
Step 6 - Wait for the A/D conversion to be completed.
- Check in the program whether the GO/DONE bit is cleared or wait for an A/D interrupt to occur (must be previously enabled).
Step 7 - Read conversion results:
- Read registers ADRESH and ADRESL.
ANALOG COMPARATOR
In addition to the A/D converter, there is another module, which until quite recently was only built into integrated circuits belonging to the so called analog electronics. Owing to the fact that it is hardly possible to find any more complex device in automatics which in some way does not use these modules, two high quality analog comparators are integrated into the microcontroller and connected to its pins.
How does an analog comparator operate? Basically, the analog comparator is an amplifier provided with two inputs and one output. It compares the magnitude of voltages at two inputs. Depending on which input has a higher voltage (an analog value), a logic zero (0) or a logic one (1) (digital values) will appear on its output:
- When the analog voltage at Vin- is higher than that at Vin+, the output of the comparator is a digital low level (0).
- When the analog voltage at Vin+ is higher than that at Vin-, the output of the comparator is a digital high level (1).
The PIC16F887 microcontroller is provided with two such analog comparators the inputs of which are connected to I/O pins RA0-RA3, while their outputs are connected to the RA4 and RA5 pins. There is also an adjustable voltage reference internal source within the microcontroller itself, which will be discussed later.
These two analog modules are under control of the bits of the following registers:
- CM1CON0 is in control of comparator C1;
- CM2CON0 is in control of comparator C2;
- CM2CON1 is in control of comparator C2;
VOLTAGE REFERENCE INTERNAL SOURCE
One of two analog voltages provided on the analog comparator inputs is fixed and constant. This voltage is known as a ‘voltage reference’ (Vref) and can be generated either by external or internal voltage source. The voltage source is selectable via both ends of a ladder network consisting of 16 resistors which together form a voltage divider. The VRSS bit of the VRCON register is used for selecting the source of reference voltage.
The comparator voltage reference module provides an internally generated voltage reference for the comparators (CVref). The CVref voltage reference has 2 ranges each containing 16 voltage levels. Range selection is controlled by the VRR bit of the VRCON register. The selected voltage range may be output to the RA2/AN2 pin using the VROE bit. Furthermore, bits VR0-VR3 are used to select appropriate voltage levels. See figure below.
Although the main idea with the voltage reference module was to provide analog modules with varying voltage reference, it is now used as a D/A converter as well. As such, it proved to be very useful in some applications. Its operation is under control of the VRCON register.
COMPARATORS AND INTERRUPT
Every change of the logic state of any comparator’s output causes the CMIF flag bit of the PIR register to be set. Such changes will also generate an interrupt if the following bits are set:
- The CMIE bit of the PIE register = 1;
- The PEIE bit of the INTCON register = 1; and
- The GIE bit of the INTCON register = 1.
If an interrupt is enabled when the microcontroller is in Sleep mode, any change on the comparator’s output will cause the microcontroller to exit this mode and proceed with the normal operation.
OPERATION OF COMPARATOR IN SLEEP MODE
The comparator, if enabled before the microcontroller enters Sleep mode, remains active. In case the comparator is not used to wake up the microcontroller, it can be turned off to minimize power consumption. It is done by clearing the CxON bit of the CMxCON0 register.
To enable the comparator to wake up the microcontroller from sleep, the CxIE bit of the IE2 register and the PEIE bit of the INTCON register must be set. The first instruction to be executed after the microcontroller exits the sleep mode is the one following the sleep instruction. If the GIE bit of the INTCON register is set, the microcontroller will execute the Interrupt Service Routine first.
CM1CON0 Register
Bits of the CM1CON0 register are in control of the comparator C1. It mainly refers to the connection of its inputs. Figure below shows a part of the module directly affected by the bits of this register.
C1ON - Comparator C1 Enable bit enables comparator C1.
- 1 - Comparator C1 is enabled.
- 0 - Comparator C1 is disabled.
C1OUT - Comparator C1 Output bit is the output of the comparator C1.
If C1POL = 1 (comparator output is inverted)
- 1 - Analog voltage at C1Vin+ is lower than analog voltage at C1Vin-.
- 0 - Analog voltage at C1Vin+ is higher than analog voltage at C1Vin-.
If C1POL = 0 (comparator output is non-inverted)
- 1 - Analog voltage at C1Vin+ is higher than analog voltage at C1Vin-.
- 0 - Analog voltage at C1Vin+ is lower than analog voltage at C1Vin-.
C1OE Comparator C1 Output Enable bit.
- 1 - Comparator C1OUT output is connected to the C1OUT pin.*
- 0 - Comparator output is only used internally.
* 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.
- 1 - Comparator C1 output is inverted.
- 0 - Comparator C1 output is non-inverted.
C1R - Comparator C1 Reference Select bit
- 1 - Non-inverting input C1Vin+ is connected to the voltage reference C1Vref.
- 0 - Non-inverting input C1Vin+ is connected to the C1IN+ pin.
C1CH1, C1CH0 - Comparator C1 Channel Select bit
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 |
CM2CON0 Register
Bits of the CM2CON0 register are in control of the comparator C2. Similar to the previous case, figure below shows a simplified schematic of the circuit affected by the bits of this register.
C2ON - Comparator C2 Enable bit enables comparator C2.
- 1 - Comparator C2 is enabled; and
- 0 - Comparator C2 is disabled.
C2OUT - Comparator C2 Output bit is the output of the comparator C2.
If C2POL = 1 (comparator output inverted)
- 1 - Analog voltage at C2Vin+ is lower than analog voltage at C2Vin-.
- 0 - Analog voltage at C2Vin+ is higher than analog voltage at C2Vin-.
If C2POL = 0 (comparator output is non-inverted)
- 1 - Analog voltage at C2Vin+ is higher than analog voltage at C2Vin-.
- 0 - Analog voltage at C2Vin+ is lower than analog voltage at C2Vin-.
C2OE - Comparator C2Output Enable bit
- 1 - Comparator C2OUT output is connected to the C2OUT pin.*
- 0 - Comparator output is internal only.
* 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.
- 1 - Comparator C2 output is inverted.
- 0 - Comparator C2 output is non-inverted.
C2R - Comparator C2 Reference Select bit
- 1 - Non-inverting input C2Vin+ is connected to the reference voltage C2Vref.
- 0 - Non-inverting input C2Vin+ is connected to the C2IN+ pin.
C2CH1, C2CH0 Comparator C2 Channel Select bit
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 |
CM2CON1 Register
Mirror Copy of the C1OUT bit
Mirror Copy of the C2OUT bit
C1RSEL Comparator C1 Reference Select bit
- 1 - Varying voltage CVref is used in the voltage reference C1Vref source.
- 0 - Fixed voltage reference 0.6V is used in the voltage reference C1Vref source.
C2RSEL - Comparator C2 Reference Select bit
- 1 - Varying voltage CVref is used as a voltage reference C2Vref source.
- 0 - Fixed voltage reference 0.6V is used as a voltage reference C2Vref source.
T1GSS - Timer1 Gate Source Select bit
- 1 - Timer1 gate uses the T1G pin signal as a clock source.
- 0 - Timer1 gate uses the SYNCC2OUT comparator output as a clock source.
C2SYNC - Comparator C2 Output Synchronization bit
- 1 - Comparator C2 output is synchronized with the Timer1 clock.
- 0 - Comparator C2 output is asynchronous signal.
VRCON Register
VREN Comparator C1 Voltage Reference Enable bit
- 1 - Voltage reference source CVref enabled.
- 0 - Voltage reference source CVref disabled.
VROE Comparator C2 Voltage Reference Enable bit
- 1 - The CVref pin is fed with voltage reference source CVref.
- 0 - The CVref pin is disconnected from reference source CVref.
VRR - CVref Range Selection bit
- 1 - Voltage reference source is set to low range.
- 0 - Voltage reference source is set to high range.
VRSS - Comparator Vref Range selection bit
- 1 - Voltage reference source is in the range between Vref+ and Vref-.
- 0 - Voltage reference source is in the range between Vdd and Vss (power supply voltage).
VR3 - VR0 CVref Value Selection
If VRR = 1 (low range)
Voltage reference is calculated by means of the formula: CVref = ([VR3:VR0]/24)Vdd
If VRR = 0 (high range)
Voltage reference is calculated by means of the formula: CVref=Vdd/4+([VR3:VR0]/32)Vdd
In Short
In order to properly use built-in comparators, it is necessary to do the following:
Step 1 - Module Configuration:
- In order to select the appropriate mode, bits of the CM1CON0 and CM2CON0 registers should be configured. Interrupt on mode change should be disabled.
Step 2 - Internal voltage reference source configuration (when used). In the VRCON register it is necessary to:
- Select one of two voltage ranges using the VRR bit.
- Configure necessary Vref using bits VR3 - VR0.
- Set the VROE bit if needed.
- Enable voltage reference Vref source by setting the VREN bit.
Formula used to calculate voltage reference:
VRR = 1 (low range)
CVref = ([VR3:VR0]/24)VLADDER
VRR = 0 (high range)
CVref = (VLADDER/4) + ([VR3:VR0]VLADDER/32)
Vladder = Vdd or ([Vref+] - [Vref-]) or Vref+
Step 3 - Start of operation:
- Enable an interrupt by setting bits CMIE (PIE register), PEIE and GIE (INTCON register).
- Read the C1OUT and C2OUT bits of the CMCON register.
- Read the CMIF flag bit of the PIR register. When set, this bit must be cleared from within the software.