3.9 Analog Modules
The A/D converter module has the following features:
- The converter generates a 10-bit binary result using the method of successive approximation and stores the conversion results into the ADC registers (ADRESL and ADRESH);
- There are 14 separate analog inputs;
- The A/D converter converts an analog input signal into a 10-bit binary number;
- The minimum resolution or 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 using timers and serial communication module, 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 result obtained after converting an analog value into digital is a10-bit number that is to be stored in the ADRESH and ADRESL registers. There are two ways of handling it - 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 the event that the A/D converter is not used, these registers may be used as general-purpose registers.
A/D ACQUISITION REQUIREMENTS
In order to enable the ADC to meet its specified accuracy, it is necessary to provide a certain time delay between selecting specific analog input and measurement itself. This time is called 'acquisition time' and mainly depends on the source impedance. There is an equation used to calculate this time accurately, which in the worst case amounts to approximately 20uS. So, if you want the conversion to be accurate, don’t forget this important detail.
ADC CLOCK PERIOD
The time needed to complete a one-bit conversion is defined as TAD. It is required to be at least 1,6 uS. One full 10-bit A/D conversion is slightly longer than expected and amounts to 11 TAD periods. Since both clock frequency and source of A/D conversion 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 in the system clock frequency will affect the ADC clock frequency, which may adversely affect the ADC result. Device frequency characteristics are shown in the table above. The values in the shaded cells are outside of the range recommended.
HOW TO USE THE A/D CONVERTER?
In order to enable the A/D converter to run without problems as well as to avoid unexpected results, it is necessary to consider the following:
- A/D converter does not differ between digital and analog signals. In order to avoid errors in measurement or chip damage, pins should be configured as analog inputs before the process of conversion starts. Bits used for this purpose are stored in the TRIS and ANSEL (ANSELH) registers;
- When reading the port with analog inputs, the state of the corresponding bits will be read as a logic zero (0); and
- Roughly speaking, voltage measurement in the converter is based on comparing input voltage with internal scale which has 1024 marks (210 = 1024). The lowest scale mark stands for the Vref- voltage, whilst its highest mark stands for the Vref+ voltage. Figure below shows selectable voltage references as well as their minimum and maximum values.
ADCON0 Register
ADCS1, ADCS0 - A/D Conversion Clock Select bits select clock frequency used for internal synchronization of A/D converter. It also affects duration of conversion.
ADCS1 |
ADCS2 |
CLOCK |
0 |
0 |
Fosc/2 |
0 |
1 |
Fosc/8 |
1 |
0 |
Fosc/32 |
1 |
1 |
RC * |
* Clock is generated by internal oscillator which is built in the converter.
CHS3-CHS0 - Analog Channel Select bits select a pin or an analog channel 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 by hardware when the A/D conversion is complete.
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 mikroC...
/* This example code reads analog value from channel 2 and displays it on PORTB and
PORTC as 10-bit binary number.*/
#include <built_in.h>
unsigned int adc_rd;
void main() {
ANSEL = 0x04; // Configure AN2 as analog pin
TRISA = 0xFF; // PORTA is configured as 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
do {
temp_res = ADC_Read(2); // Get 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, RC6
} while(1); // Remain in the loop
}
ADCON1 Register
ADFM - A/D Result Format Select bit
- 1 - Conversion result is right justified. Six most significant bits of the ADRESH are not used.
- 0 - Conversion result is left justified. Six least significant bits of the ADRESL are not used.
VCFG1 - Voltage Reference bit selects negative voltage reference source needed for the operation of A/D converter.
- 1 - Negative voltage reference is applied to the Vref- pin.
- 0 - Power supply voltage Vss is used as negative voltage reference source.
VCFG0 - Voltage Reference bit selects positive voltage reference source needed for the operation of A/D converter.
- 1 - Positive voltage reference is applied to the Vref+ pin.
- 0 - Power supply voltage Vdd is used as positive voltage reference source.
In Short
In order to measure voltage on an input pin by 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, thus configuring the appropriate pin as an input.
- Write a logic one (1) to a bit of the ANSEL register, thus configuring the appropriate pin as an analog input.
Step 2 - ADC module configuration:
- Configure voltage reference in the ADCON1 register.
- Select ADC 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 A/D converter by setting the ADON bit of the ADCON0 register.
Step 3 - ADC interrupt configuration (optionally):
- Clear the ADIF bit.
- Set the ADIE, PEIE and GIE bits.
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 ADC conversion to complete.
- It is necessary to check in the program loop whether the GO/DONE pin is cleared or wait for an A/D interrupt (must be previously enabled).
Step 7 - Read ADC results:
- Read the ADRESH and ADRESL registers.
ANALOG COMPARATOR
In addition to A/D converter, there is another module, which until quite recently has been embedded only in integrated circuits belonging to the so called analog electronics. Owing to the fact that it is hardly possible to find any more complex automatic device which in some way does not use these circuits, two high quality comparators, along with additional electronics, are integrated into the microcontroller and connected to its pins.
How does a comparator operate? Basically, the analog comparator is an amplifier which compares the magnitude of voltages at two inputs. It has two inputs and one output. Depending on which input has a higher voltage (analog value), a logic zero (0) or 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.
- When the analog voltage at Vin+ is higher than that at Vin-, the output of the comparator is a digital high level.
The PIC16F887 microcontroller has two such voltage comparators the inputs of which are connected to I/O pins RA0-RA3, whereas the outputs are connected to the RA4 and RA5 pins. There is also a voltage reference internal source on the chip itself, which will be discussed later.
These two circuits are under control of the bits stored in 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 comparator inputs is usually stable and unchangeable. It is called
'voltage reference'(Vref). To generate it, both external and special internal voltage source can be used. When the voltage source is selected, Vref is derived from it by means of a ladder network consisting of 16 resistors which form a voltage divider. The voltage source is selectable through the both ends of the divider by the VRSS bit of the VRCON register.
In addition, the voltage fraction provided by the resistor ladder network may be selected through the bits VR0-VR3 and used as a voltage reference. See figure below.
The comparator 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 reference CVref may be output to the RA2/AN2 pin.
Even though the main idea was to obtain varying voltage reference for the operation of analog modules, a simple A/D converter is obtained thereby as well. This converter is very useful in some situations. 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 flag bit CMIF of the register PIR to be set. Such changes will also cause 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, any change on the comparator’s output when the microcontroller is set in
Sleep mode can cause the microcontroller to exit that mode and proceed with normal operation.
OPERATION DURING SLEEP
The comparator, if enabled before entering the
Sleep mode, remains active during
Sleep. If the comparator is not used to wake up the device, power consumption can be minimized in the
Sleep mode by turning the comparator off. It is performed 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 instruction following the
Sleep instruction is always executed after exiting the
Sleep mode. If the GIE bit of the INTCON register is set, the device will execute the
Interrupt Service Routine.
CM1CON0 Register
Bits of this register are in control of the comparator C1. It mainly affects the configuration of its inputs. To understand it better, look at figure below which shows only a part of electronics 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 internal only.
* In order to enable the C1OUT bit to be present on the pin, two conditions must be met: C1ON = 1 (comparator must be on) and the corresponding TRIS bit = 0 (pin must be configured as an output).
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 reference voltage 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 this 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 C1Vin+ is lower than analog voltage at C1Vin-.
- 0 - Analog voltage at C1Vin+ is higher than analog voltage at C1Vin-.
If C2POL = 0 (comparator output 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-.
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 C2OUT bit to be present on the pin, two conditions must be met: C2ON = 1 (comparator must be on) and the corresponding TRIS bit = 0 (pin must be configured as an output).
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
MC1OUT Mirror Copy of C1OUT bit
MC2OUT Mirror Copy of C2OUT bit
C1RSEL Comparator C1 Reference Select bit
- 1 - Selectable 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 - Selectable voltage CVref is used in the voltage reference C2Vref source.
- 0 - Fixed voltage reference 0.6V is used in the voltage reference C2Vref source.
T1GSS - Timer1 Gate Source Select bit
- 1 - Timer T1gate source is T1G.
- 0 - Timer T1gate source is SYNCC2OUT.
C2SYNC - Comparator C2 Output Synchronization bit
- 1 - Comparator C2 output is synchronized to the falling edge of Timer TMR1 clock.
- 0 - Comparator output is asynchronous signal.
VRCON Register
VREN Comparator C1 Voltage Reference Enable bit
- 1 - Voltage reference CVref source is powered on.
- 0 - Voltage reference CVref source is powered off.
VROE Comparator C2 Voltage Reference Enable bit
- 1 - Voltage reference CVref is connected to the pin.
- 0 - Voltage reference CVref is disconnected from the pin.
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 of Vref+ to Vref-.
- 0 - Voltage reference source is in the range of Vdd to Vss (power supply voltage).
VR3 - VR0 CVref Value Selection
If VRR = 1 (low range)
Voltage reference is calculated using the formula: CVref = ([VR3:VR0]/24)Vdd
If VRR = 0 (high range)
Voltage reference is calculated using the formula: CVref = Vdd/4 + ([VR3:VR0]/32)Vdds
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 should be disabled on any change of mode.
Step 2 - Internal voltage reference Vref source configuration (only 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 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. After being set, this bit must be cleared in software.
In order to synchronize all the processes taking place within the microcontroller, a clock signal must be used, while in order to generate the clock signal, a clock oscillator must be used. As simple as that. This microcontroller has several oscillators capable of working in different modes and this is where the story becomes interesting...