Internal Comparator Setup On 8-bit Devices
The comparator peripheral inside many 8-bit PIC® microcontrollers (MCUs), is a useful module to connect the analog world to the digital world by comparing two analog signals and sending a digital result. Specifically, it compares the amplitudes or the voltage level of the analog signals and then outputs a digital one or zero based on the difference between the two. This module is particularly useful because it can operate independently of the software execution of the microcontroller. Figure 18-1 from the PIC16F18855 data sheet, gives a visual example of this:
Configurability Options
Enable Comparator
Checking this box triggers the register responsible for enabling the comparator for operation. This allows you to choose whether or not to run the comparator on start-up. Disabling on start-up may be useful for power savings depending upon your project’s necessity.
Enable Synchronous Mode
This mode can synchronize the output of the comparator with Timer1, specifically connecting on the falling edge of the Timer1 source clock. If you want to time an analog event, this is useful for use with a Timer1 Gate.
Enable Comparator Hysteresis
Hysteresis alternately adjusts the trip point to prevent repeated switching when hovering around a fixed trip point. This can help reduce noise due to small changes in the input signal.
Positive Input
The non-inverting input can be connected to one of four sources in the drop-down menu. CIN0+ is the analog input pin that would allow you to connect the comparator to an external analog input, such as a potentiometer or temperature sensor. DACOUT connects to the output of the digital-to-analog converter of the microcontroller. FVR_buf2 connects the V+ input to a Fixed Voltage Reference of the microcontroller. VSS connects the V+ input to ground.
Negative Input
The inverting input can be connected to one of three sources in the drop-down menu. CxIN- will connect to one of the given analog input pins. FVR will connect the inverting input to the Fixed Voltage Reference. Vss will connect the inverting input to analog ground.
Some of the inputs to the inverting input may be shared with the operational amplifier output, in which case the output of the opamp will be connected to the inverting input of the comparator.
Output Polarity
This will determine whether V+ > V- or V- > V+ will trigger a 1 or 0 output from the comparator. This is functionally the same as switching the inputs to V+ and V-.
Input Condition | Output Polarity | CMPx Output |
---|---|---|
V+ > V- | Not Inverted | 1 |
V+ < V- | Not Inverted | 0 |
V+ > V- | Inverted | 0 |
V+ < V- | Inverted | 1 |
Enable Comparator Interrupt
When the relationship between VIN+ and VIN- changes, this option allows an interrupt to be triggered. The two options, Rising Edge and Falling Edge determine when the interrupt is triggered.