comparator_example.c - Illustrates a simple use of the comparator moduleΒΆ

Illustrates a simple usage of the comparator module

 
#include "pic24_all.h"
#include <stdio.h>

#if defined(__dsPIC33E__) || defined(__PIC24E__)
# warning "This example does not work with the dsPIC33E/PIC24E families."
int main(void) {
  return 0;
}
#else

void configComparator(void) {
  CMCON = CMP_IDLE_STOP | CMP1_ENABLE | CMP2_DISABLE |
          CMP1_OUTPUT_DISABLE | CMP1_NORMAL_OUTPUT |
          CMP1_NEG_IP_VIN_POS | CMP1_POS_IP_CV_REF;
  CVRCON = CMP_VREF_ENABLE |
           CMP_VREF_OUTPUT_DISABLE |
           CMP_VRSRC_AVDD_AVSS |
           CMP_0P50_CVRR_1 ;
  DELAY_US(10) //wait for comparator to settle
  _C1EVT = 0; //clear C1 event flag
  _CMIF = 0;  //clear interrupt flag
}

int main (void) {
  uint8_t u8_i;
  configBasic(HELLO_MSG);
  configComparator();
  u8_i = _C1OUT;  //intial read to set trigger
  while (1) {
    do {
      doHeartbeat(); //wait for trigger
    } while (!_CMIF);
    _CMIF = 0;
    u8_i = _C1OUT;  //current value
    outString("\n Comparator fired: ");
    outUint8(u8_i);
    DELAY_MS(100);
  } //end while
}//end main
#endif