PIC24 Support Libraries
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
comparator_example.c
Go to the documentation of this file.
1 /*
2  * "Copyright (c) 2008 Robert B. Reese, Bryan A. Jones, J. W. Bruce ("AUTHORS")"
3  * All rights reserved.
4  * (R. Reese, reese_AT_ece.msstate.edu, Mississippi State University)
5  * (B. A. Jones, bjones_AT_ece.msstate.edu, Mississippi State University)
6  * (J. W. Bruce, jwbruce_AT_ece.msstate.edu, Mississippi State University)
7  *
8  * Permission to use, copy, modify, and distribute this software and its
9  * documentation for any purpose, without fee, and without written agreement is
10  * hereby granted, provided that the above copyright notice, the following
11  * two paragraphs and the authors appear in all copies of this software.
12  *
13  * IN NO EVENT SHALL THE "AUTHORS" BE LIABLE TO ANY PARTY FOR
14  * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
15  * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE "AUTHORS"
16  * HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
17  *
18  * THE "AUTHORS" SPECIFICALLY DISCLAIMS ANY WARRANTIES,
19  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
20  * AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
21  * ON AN "AS IS" BASIS, AND THE "AUTHORS" HAS NO OBLIGATION TO
22  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS."
23  *
24  * Please maintain this header in its entirety when copying/modifying
25  * these files.
26  *
27  *
28  */
29 #include "pic24_all.h"
30 #include <stdio.h>
31 
32 /** \file
33 Illustrates a simple usage of the comparator module
34 */
35 
36 void configComparator(void) {
37  CMCON = CMP_IDLE_STOP | CMP1_ENABLE | CMP2_DISABLE |
38  CMP1_OUTPUT_DISABLE | CMP1_NORMAL_OUTPUT |
39  CMP1_NEG_IP_VIN_POS | CMP1_POS_IP_CV_REF;
40  CVRCON = CMP_VREF_ENABLE |
41  CMP_VREF_OUTPUT_DISABLE |
42  CMP_VRSRC_AVDD_AVSS |
43  CMP_0P50_CVRR_1 ;
44  DELAY_US(10) //wait for comparator to settle
45  _C1EVT = 0; //clear C1 event flag
46  _CMIF = 0; //clear interrupt flag
47 }
48 
49 int main (void) {
50  uint8_t u8_i;
51  configBasic(HELLO_MSG);
52  configComparator();
53  u8_i = _C1OUT; //intial read to set trigger
54  while (1) {
55  do {
56  doHeartbeat(); //wait for trigger
57  } while (!_CMIF);
58  _CMIF = 0;
59  u8_i = _C1OUT; //current value
60  outString("\n Comparator fired: ");
61  outUint8(u8_i);
62  DELAY_MS(100);
63  } //end while
64 }//end main