Section 19. Dual Comparator Module HIGHLIGHTS This section of the manual contains the following major topics: 19.1 19.2 19.3 19.4 19.5 19.6 19.7 19.8 19.9 19.10 19.11 19.12 19.13 Introduction.................................................................................................................. 19-2 Control Register .......................................................................................................... 19-3 Comparator Operation................................................................................................. 19-5 Comparator Reference................................................................................................ 19-5 Comparator Response Time ....................................................................................... 19-5 Comparator Outputs .................................................................................................... 19-6 Analog Input Connection Considerations .................................................................... 19-7 Initialization.................................................................................................................. 19-7 Comparator Interrupts ................................................................................................. 19-8 Comparator Operation During Sleep and Idle Modes ................................................. 19-8 Effects of a Reset ........................................................................................................ 19-8 Related Application Notes ........................................................................................... 19-9 Revision History ........................................................................................................ 19-10 19 Dual Comparator Module © 2010 Microchip Technology Inc. DS39710B-page 19-1 PIC24F Family Reference Manual 19.1 INTRODUCTION The analog dual comparator module contains two comparators that can be configured in a variety of ways. The inputs can be selected from the analog inputs multiplexed with the I/O pins, as well as the on-chip voltage reference (see Section 20. “Comparator Voltage Reference Module”). Block diagrams of the various comparator configurations are shown in Figure 19-1. Note: 19.1.1 A comparator module with two analog comparators may also be the scalable comparator module, described in the “PIC24F Family Reference Manual”, Section 46. “Scalable Comparator Module” (DS39734). Check the device data sheet to verify which comparator module is included in a particular device. Comparator Configuration PIC24F devices offer near 100% flexibility in configuration of the comparator module, allowing individual control over many of the options that are fixed on most PIC18 devices. The PIC24F comparator module has individual control over the enables, output inversion, output on I/O pin and input selections. The VIN- pin of each comparator can select from either I/O pin (CxIN+ or CxIN-) and the VIN+ input of the comparator comes from the comparator voltage reference, or the positive I/O pin (CxIN+ or CVREF). In addition, the PIC24F has 2 individual comparator event control bits. These control bits can be used for detecting when an individual comparator output changes states. If the Comparator mode is changed, the comparator output level may not be valid for the specified mode change delay. Note: Figure 19-1: Comparator interrupts should be disabled during a Comparator mode change; otherwise, a false interrupt may occur. Comparator I/O Operating Modes C1NEG C1IN+ C1EN VIN- C1IN- C1OUT C1POS C1IN+ C1 VIN+ CVREF C2NEG C2IN+ C2EN DS39710B-page 19-2 CMCON<7> C2INV C2OUT C2POS C2IN+ C1OUTEN VIN- C2IN- CVREF CMCON<6> C1INV C2 VIN+ C2OUTEN © 2010 Microchip Technology Inc. Section 19. Dual Comparator Module 19.2 CONTROL REGISTER The CMCON register (Register 19-1) is used to configure the comparator and present their output status. The C2EN and C1EN bits (CMCON<11:10>) enable or disable the individual comparators. Comparator input configuration is accomplished with the CxNEG and CxPOS bits (CMCON<3:0>). These determine which of the differential input channels is connected to the non-inverting input, and if either a differential comparator input or the comparator voltage reference is connected to the inverting input. A total of 16 possible configurations are available for both comparators. The CxOUT bits (CMCON<7:6>) directly report the output state of the comparator, as determined by the relative values of Vin+ and Vin-, and the CxINV bits (CMCON<5:4>). The CxOUTEN bits (CMCON<9:8>) enable the output of the comparator to appear on the corresponding CxOUT pin. The CxEVT pins (CMCON<13:12>) indicate when any change in output state has occurred. These bits are set by hardware and can only be cleared in software. Register 19-1: CMCON: Comparator Control Register R/W-0 U-0 R/C-0 R/C-0 R/W-0 R/W-0 R/W-0 R/W-0 CMIDL — C2EVT C1EVT C2EN C1EN C2OUTEN C1OUTEN bit 15 bit 8 R-0 R-0 R/W-0 R/W-0 R/W-0 R/W-0 R/W-0 R/W-0 C2OUT C1OUT C2INV C1INV C2NEG C2POS C1NEG C1POS bit 7 bit 0 Legend: C = Clearable bit R = Readable bit W = Writable bit U = Unimplemented bit, read as ‘0’ -n = Value at POR ‘1’ = Bit is set ‘0’ = Bit is cleared x = Bit is unknown CMIDL: Stop in Idle Mode bit 1 = When device enters Idle mode, the module does not generate interrupts; the module is still enabled 0 = Continue normal module operation in Idle mode bit 14 Unimplemented: Read as ‘0’ bit 13 C2EVT: Comparator 2 Event bit 1 = Comparator output changed states 0 = Comparator output did not change states bit 12 C1EVT: Comparator 1 Event bit 1 = Comparator output changed states 0 = Comparator output did not change states bit 11 C2EN: Comparator 2 Enable bit 1 = Comparator is enabled 0 = Comparator is disabled bit 10 C1EN: Comparator 1 Enable bit 1 = Comparator is enabled 0 = Comparator is disabled bit 9 C2OUTEN: Comparator 2 Output Enable bit 1 = Comparator output is driven on the output pad 0 = Comparator output is not driven on the output pad bit 8 C1OUTEN: Comparator 1 Output Enable bit 1 = Comparator output is driven on the output pad 0 = Comparator output is not driven on the output pad © 2010 Microchip Technology Inc. 19 Dual Comparator Module bit 15 DS39710B-page 19-3 PIC24F Family Reference Manual Register 19-1: CMCON: Comparator Control Register (Continued) bit 7 C2OUT: Comparator 2 Output bit When C2INV = 0: 1 = C2IN+ > C2IN0 = C2IN+ < C2INWhen C2INV = 1: 0 = C2IN+ > C2IN1 = C2IN+ < C2IN- bit 6 C1OUT: Comparator 1 Output bit When C1INV = 0: 1 = C1IN+ > C1IN0 = C1IN+ < C1INWhen C1INV = 1: 0 = C1IN+ > C1IN1 = C1IN+ < C1IN- bit 5 C2INV: Comparator 2 Output Inversion bit 1 = C2 output is inverted 0 = C2 output is not inverted bit 4 C1INV: Comparator 1 Output Inversion bit 1 = C1 output is inverted 0 = C1 output is not inverted bit 3 C2NEG: Comparator 2 Negative Input Configure bit 1 = Input is connected to C2IN+ 0 = Input is connected to C2INSee Figure 19-1 for the Comparator modes. bit 2 C2POS: Comparator 2 Positive Input Configure bit 1 = Input is connected to C2IN+ 0 = Input is connected to CVREF See Figure 19-1 for the Comparator modes. bit 1 C1NEG: Comparator 1 Negative Input Configure bit 1 = Input is connected to C1IN+ 0 = Input is connected to C1INSee Figure 19-1 for the Comparator modes. bit 0 C1POS: Comparator 1 Positive Input Configure bit 1 = Input is connected to C1IN+ 0 = Input is connected to CVREF See Figure 19-1 for the Comparator modes. DS39710B-page 19-4 © 2010 Microchip Technology Inc. Section 19. Dual Comparator Module 19.3 COMPARATOR OPERATION A single comparator is shown in Figure 19-2, along with the relationship between the analog input levels and the digital output. When the analog input at VIN+ is less than the analog input VIN-, the output of the comparator is a digital low level. When the analog input at VIN+ is greater than the analog input, VIN-, the output of the comparator is a digital high level. The shaded areas of the output of the comparator in Figure 19-2 represent the uncertainty due to input offsets and response time. Figure 19-2: Single Comparator VIN+ + VIN- – Output VINVIN+ Output 19.4 COMPARATOR REFERENCE Depending on the comparator operating mode, either an external or internal voltage reference may be used. The analog signal present at VIN- is compared to the signal at VIN+ and the digital output of the comparator is adjusted accordingly (Figure 19-2). 19.4.1 External Reference Signal 19.4.2 Internal Reference Signal The comparator module also allows the selection of an internally generated voltage reference from the comparator voltage reference module. This module is described in more detail in Section 20. “Comparator Voltage Reference Module”. The internal reference is available when C1POS = 0, C2POS = 0 and the CVRSS bit (CVRCON<4>) = 0. In this mode, the internal voltage reference is applied to the VIN+ pin of both comparators. 19.5 COMPARATOR RESPONSE TIME Response time is the maximum time, after selecting a new reference voltage or input source, before the comparator output has a valid level. If the internal reference is changed, the maximum delay of the internal voltage reference must be considered when using the comparator outputs. Otherwise, the maximum delay of the comparators should be used. © 2010 Microchip Technology Inc. DS39710B-page 19-5 19 Dual Comparator Module When external voltage references are used, the comparator module can be configured to have the comparators operate from the same, or different, reference sources. However, threshold detector applications may require the same reference. PIC24F Family Reference Manual 19.6 COMPARATOR OUTPUTS The comparator outputs are read through the CMCON register. These bits are read-only. The comparator outputs may also be directly output to the I/O pins via C1OUT and C2OUT. When enabled, multiplexers in the output path of the I/O pins will switch and the output of each pin will be the unsynchronized output of the comparator. The uncertainty of each of the comparators is related to the input offset voltage and the response time given in the specifications. Figure 19-3 shows the comparator output block diagram. The associated TRIS bits will still function as an output enable/disable for the I/O pins while in this mode. The polarity of the comparator outputs can be changed using the C2INV and C1INV bits (CMCON<5:4>). Note 1: When reading the PORT register, all pins configured as analog inputs will read as ‘0’s. Pins configured as digital inputs will convert an analog input according to the Schmitt Trigger input specification. 2: Analog levels on any pin defined as a digital input may cause the input buffer to consume more current than is specified. PORT Pins Comparator Output Block Diagram Multiplex Figure 19-3: + To CxOUT Pin – D Q Bus Data Q Set CMIF bit CxINV Read CMCON EN D EN Reset DS39710B-page 19-6 CL From Other Comparator © 2010 Microchip Technology Inc. Section 19. Dual Comparator Module 19.7 ANALOG INPUT CONNECTION CONSIDERATIONS A simplified circuit for an analog input is shown in Figure 19-4. A maximum source impedance of 10 k is recommended for the analog sources. Any external component connected to an analog input pin, such as a capacitor or a Zener diode, should have very little leakage current. Figure 19-4: Comparator Analog Input Model RIC RS < 10k AIN ILEAKAGE 500 nA CPIN 5 pF VA Comparator Input VSS Legend: 19.8 CPIN ILEAKAGE RIC RS VA = = = = = Input Capacitance Leakage Current at the pin due to various junctions Interconnect Resistance Source Impedance Analog Voltage INITIALIZATION This initialization sequence configures the comparator module as two independent comparators with outputs enabled and the Comparator 1 output inverted. The comparator voltage reference module is configured for output enable and set for 0.25 * VDD. Example 19-1 shows a program sequence to configure the voltage reference and comparator module. The delay used in this example is based off of an 8 MHz oscillator. Example 19-1: Comparator Configuration = 0x0F10; //Initialize Comparator Module CVRCON = 0x00C0; //Initialize Voltage Reference Module CMCONbits.C1EVT = 0; CMCONbits.C2EVT = 0; //Clear Comparator 1 Event //Clear Comparator 2 Event asm volatile("repeat #40"); Nop(); //Delay 10us © 2010 Microchip Technology Inc. DS39710B-page 19-7 Dual Comparator Module CMCON 19 PIC24F Family Reference Manual 19.9 COMPARATOR INTERRUPTS The Comparator Interrupt Flag, CMIF (IFS1<2>), is set whenever there is a change in the output value of either comparator. Software can read C1EVT and C2EVT to determine the actual change that occurred. Since it is also possible to write a ‘1’ to this register, a simulated interrupt may be initiated. The CMIF and CxEVT bits must be reset by clearing them in software. If the CMIE bit (IEC1<2>) is cleared, the interrupt is not enabled, though the CMIF bit will still be set if an interrupt condition occurs. Note: If a change in the CMCON register (C1OUT or C2OUT) should occur when a read operation is being executed (start of the Q2 cycle), then the CMIF (IFS1<2>) interrupt flag may not get set. The user, in the Interrupt Service Routine, can clear the interrupt by clearing CMIF. See Section 8. “Interrupts” in this manual for more information. 19.10 COMPARATOR OPERATION DURING SLEEP AND IDLE MODES 19.10.1 Comparator Operation During Sleep When a comparator is active and the device is placed in Sleep mode, the comparator remains active and the interrupt is functional, if enabled. This interrupt will wake-up the device from Sleep mode, when enabled. Each operational comparator will consume additional current, as shown in the comparator specifications. To minimize power consumption while in Sleep mode, turn off the comparators, CxEN = 0 (CMCON<11:10>), before entering Sleep. If the device wakes up from Sleep, the contents of the CMCON register are not affected. See Section 10. “Power-Saving Features” in this manual for additional information on Sleep. 19.10.2 Comparator Operation During Idle When a comparator is active and the device is placed in Idle mode, the comparator remains active and interrupts are generated, if enabled, and CMIDL = 0 (CMCON<15>). If it is desired for the comparators to operate in Idle mode without generating interrupts, configure CMIDL = 1 (CMCON<15>). See Section 10. “Power-Saving Features” in this manual for more information on Idle. 19.11 EFFECTS OF A RESET A device Reset forces the CMCON register to its Reset state, causing the comparator modules to be turned off (CxEN = 0). However, the input pins multiplexed with analog input sources are configured as analog inputs, by default, on device Reset. The I/O configuration for these pins is determined by the setting of the ADxPCFG register. Therefore, device current is minimized when analog inputs are present at Reset time. DS39710B-page 19-8 © 2010 Microchip Technology Inc. Section 19. Dual Comparator Module 19.12 RELATED APPLICATION NOTES This section lists application notes that are related to this section of the manual. These application notes may not be written specifically for the PIC24F device family, but the concepts are pertinent and could be used with modification and possible limitations. The current application notes related to the Comparator module are: Title Application Note # Resistance and Capacitance Meter Using a PIC16C622 AN611 Make a Delta-Sigma Converter Using a Microcontroller’s Analog Comparator Module AN700 A Comparator Based Slope ADC AN863 Oscillator Circuits for RTD Temperature Sensors AN895 Temperature Measurement Circuits for Embedded Applications AN929 Analog Sensor Conditioning Circuits – An Overview AN990 Note: Please visit the Microchip web site (www.microchip.com) for additional application notes and code examples for the PIC24F family of devices. 19 Dual Comparator Module © 2010 Microchip Technology Inc. DS39710B-page 19-9 PIC24F Family Reference Manual 19.13 REVISION HISTORY Revision A (June 2006) This is the initial released revision of this document. Revision B (March 2010) Renamed as “Dual Comparator Module”, to distinguish from the recently introduced Section 46. “Scalable Comparator Module” (DS39734). Reorganized all topics to reflect the order used in Section 46. “Scalable Comparator Module”. Replaced the text of Section 19.2 “Control Register” with a new version, to provide a clearer explanation of the register. Revised Figure 19-3 to correct the “Read CMCON” circuit input. Revised Figure 19-4 with an updated input model. DS39710B-page 19-10 © 2010 Microchip Technology Inc. Note the following details of the code protection feature on Microchip devices: • Microchip products meet the specification contained in their particular Microchip Data Sheet. • Microchip believes that its family of products is one of the most secure families of its kind on the market today, when used in the intended manner and under normal conditions. • There are dishonest and possibly illegal methods used to breach the code protection feature. All of these methods, to our knowledge, require using the Microchip products in a manner outside the operating specifications contained in Microchip’s Data Sheets. Most likely, the person doing so is engaged in theft of intellectual property. • Microchip is willing to work with the customer who is concerned about the integrity of their code. • Neither Microchip nor any other semiconductor manufacturer can guarantee the security of their code. Code protection does not mean that we are guaranteeing the product as “unbreakable.” Code protection is constantly evolving. We at Microchip are committed to continuously improving the code protection features of our products. Attempts to break Microchip’s code protection feature may be a violation of the Digital Millennium Copyright Act. If such acts allow unauthorized access to your software or other copyrighted work, you may have a right to sue for relief under that Act. Information contained in this publication regarding device applications and the like is provided only for your convenience and may be superseded by updates. It is your responsibility to ensure that your application meets with your specifications. MICROCHIP MAKES NO REPRESENTATIONS OR WARRANTIES OF ANY KIND WHETHER EXPRESS OR IMPLIED, WRITTEN OR ORAL, STATUTORY OR OTHERWISE, RELATED TO THE INFORMATION, INCLUDING BUT NOT LIMITED TO ITS CONDITION, QUALITY, PERFORMANCE, MERCHANTABILITY OR FITNESS FOR PURPOSE. Microchip disclaims all liability arising from this information and its use. Use of Microchip devices in life support and/or safety applications is entirely at the buyer’s risk, and the buyer agrees to defend, indemnify and hold harmless Microchip from any and all damages, claims, suits, or expenses resulting from such use. No licenses are conveyed, implicitly or otherwise, under any Microchip intellectual property rights. Trademarks The Microchip name and logo, the Microchip logo, dsPIC, KEELOQ, KEELOQ logo, MPLAB, PIC, PICmicro, PICSTART, PIC32 logo, rfPIC and UNI/O are registered trademarks of Microchip Technology Incorporated in the U.S.A. and other countries. FilterLab, Hampshire, HI-TECH C, Linear Active Thermistor, MXDEV, MXLAB, SEEVAL and The Embedded Control Solutions Company are registered trademarks of Microchip Technology Incorporated in the U.S.A. Analog-for-the-Digital Age, Application Maestro, CodeGuard, dsPICDEM, dsPICDEM.net, dsPICworks, dsSPEAK, ECAN, ECONOMONITOR, FanSense, HI-TIDE, In-Circuit Serial Programming, ICSP, Mindi, MiWi, MPASM, MPLAB Certified logo, MPLIB, MPLINK, mTouch, Octopus, Omniscient Code Generation, PICC, PICC-18, PICDEM, PICDEM.net, PICkit, PICtail, REAL ICE, rfLAB, Select Mode, Total Endurance, TSHARC, UniWinDriver, WiperLock and ZENA are trademarks of Microchip Technology Incorporated in the U.S.A. and other countries. SQTP is a service mark of Microchip Technology Incorporated in the U.S.A. All other trademarks mentioned herein are property of their respective companies. © 2010, Microchip Technology Incorporated, Printed in the U.S.A., All Rights Reserved. Printed on recycled paper. ISBN: 978-1-60932-146-8 Microchip received ISO/TS-16949:2002 certification for its worldwide headquarters, design and wafer fabrication facilities in Chandler and Tempe, Arizona; Gresham, Oregon and design centers in California and India. The Company’s quality system processes and procedures are for its PIC® MCUs and dsPIC® DSCs, KEELOQ® code hopping devices, Serial EEPROMs, microperipherals, nonvolatile memory and analog products. In addition, Microchip’s quality system for the design and manufacture of development systems is ISO 9001:2000 certified. 2010 Microchip Technology Inc. DS39710B-page 11 WORLDWIDE SALES AND SERVICE AMERICAS ASIA/PACIFIC ASIA/PACIFIC EUROPE Corporate Office 2355 West Chandler Blvd. Chandler, AZ 85224-6199 Tel: 480-792-7200 Fax: 480-792-7277 Technical Support: http://support.microchip.com Web Address: www.microchip.com Asia Pacific Office Suites 3707-14, 37th Floor Tower 6, The Gateway Harbour City, Kowloon Hong Kong Tel: 852-2401-1200 Fax: 852-2401-3431 India - Bangalore Tel: 91-80-3090-4444 Fax: 91-80-3090-4123 India - New Delhi Tel: 91-11-4160-8631 Fax: 91-11-4160-8632 Austria - Wels Tel: 43-7242-2244-39 Fax: 43-7242-2244-393 Denmark - Copenhagen Tel: 45-4450-2828 Fax: 45-4485-2829 India - Pune Tel: 91-20-2566-1512 Fax: 91-20-2566-1513 France - Paris Tel: 33-1-69-53-63-20 Fax: 33-1-69-30-90-79 Japan - Yokohama Tel: 81-45-471- 6166 Fax: 81-45-471-6122 Germany - Munich Tel: 49-89-627-144-0 Fax: 49-89-627-144-44 Atlanta Duluth, GA Tel: 678-957-9614 Fax: 678-957-1455 Boston Westborough, MA Tel: 774-760-0087 Fax: 774-760-0088 Chicago Itasca, IL Tel: 630-285-0071 Fax: 630-285-0075 Cleveland Independence, OH Tel: 216-447-0464 Fax: 216-447-0643 Dallas Addison, TX Tel: 972-818-7423 Fax: 972-818-2924 Detroit Farmington Hills, MI Tel: 248-538-2250 Fax: 248-538-2260 Kokomo Kokomo, IN Tel: 765-864-8360 Fax: 765-864-8387 Los Angeles Mission Viejo, CA Tel: 949-462-9523 Fax: 949-462-9608 Santa Clara Santa Clara, CA Tel: 408-961-6444 Fax: 408-961-6445 Toronto Mississauga, Ontario, Canada Tel: 905-673-0699 Fax: 905-673-6509 Australia - Sydney Tel: 61-2-9868-6733 Fax: 61-2-9868-6755 China - Beijing Tel: 86-10-8528-2100 Fax: 86-10-8528-2104 China - Chengdu Tel: 86-28-8665-5511 Fax: 86-28-8665-7889 Korea - Daegu Tel: 82-53-744-4301 Fax: 82-53-744-4302 China - Chongqing Tel: 86-23-8980-9588 Fax: 86-23-8980-9500 Korea - Seoul Tel: 82-2-554-7200 Fax: 82-2-558-5932 or 82-2-558-5934 China - Hong Kong SAR Tel: 852-2401-1200 Fax: 852-2401-3431 Malaysia - Kuala Lumpur Tel: 60-3-6201-9857 Fax: 60-3-6201-9859 China - Nanjing Tel: 86-25-8473-2460 Fax: 86-25-8473-2470 Malaysia - Penang Tel: 60-4-227-8870 Fax: 60-4-227-4068 China - Qingdao Tel: 86-532-8502-7355 Fax: 86-532-8502-7205 Philippines - Manila Tel: 63-2-634-9065 Fax: 63-2-634-9069 China - Shanghai Tel: 86-21-5407-5533 Fax: 86-21-5407-5066 Singapore Tel: 65-6334-8870 Fax: 65-6334-8850 China - Shenyang Tel: 86-24-2334-2829 Fax: 86-24-2334-2393 Taiwan - Hsin Chu Tel: 886-3-6578-300 Fax: 886-3-6578-370 China - Shenzhen Tel: 86-755-8203-2660 Fax: 86-755-8203-1760 Taiwan - Kaohsiung Tel: 886-7-536-4818 Fax: 886-7-536-4803 China - Wuhan Tel: 86-27-5980-5300 Fax: 86-27-5980-5118 Taiwan - Taipei Tel: 886-2-2500-6610 Fax: 886-2-2508-0102 China - Xian Tel: 86-29-8833-7252 Fax: 86-29-8833-7256 Thailand - Bangkok Tel: 66-2-694-1351 Fax: 66-2-694-1350 Italy - Milan Tel: 39-0331-742611 Fax: 39-0331-466781 Netherlands - Drunen Tel: 31-416-690399 Fax: 31-416-690340 Spain - Madrid Tel: 34-91-708-08-90 Fax: 34-91-708-08-91 UK - Wokingham Tel: 44-118-921-5869 Fax: 44-118-921-5820 China - Xiamen Tel: 86-592-2388138 Fax: 86-592-2388130 China - Zhuhai Tel: 86-756-3210040 Fax: 86-756-3210049 01/05/10 DS39710B-page 12 2010 Microchip Technology Inc.