The comparator along with the operational amplifier are two of the most common analog devices that are included as peripherals in modern microcontrollers. The PIC32 is no exception. In this post we will look at using the on board op amp module of the PIC32MX270F256B microcontroller.

PIC32 Comparator Module

The PIC32 microcontroller contains three comparators. These comparators can use either the onboard internal voltage reference, an external reference can generate interrupts and can have an output that is inverted.

For those of you who may not know a comparator is a device that compares two voltage levels and then outputs either a HIGH or a LOW output signal if the voltage rises or falls below or above the set threshold voltage levels.

Since this output is HIGH or LOW this makes the comparator essentially having an output that can be treated as digital in nature and thus can  easily be interfaced with the microcontroller. In this example we will use the onboard voltage comparator of the PIC32MX270F256D microcontroller.


Connections

The circuit is connected as shown above. The reference of the comparator is connected to a voltage divider and the input signal voltage is connected to a 10k potentiometer. The output of the comparator is connected to an LED. Additionally information about the comparator state is sent via UART.


C Program

/*
 * File: Main.c
 * Author: Armstrong Subero
 * Processor: PIC32MX270F256D w/Ext OSC @ 4MHz, PLL to 48 MHz, 3.3v
 * Program: 12_Comparator
 * Compiler: XC32 (v1.44, MPLAX X v4.00)
 * Program Version: 1.0
 *                             
 * Program Description: This Program Allows PIC32MX270F256D to use the 
 *                      comparator module. The non-inverting input of the 
 *                      comparator is connected to a potentiometer and the
 *                      inverting input is connected to the output of a voltage
 *                      divider.
 * 
 * Hardware Description: A 10k potentiometer is connected to PINB3 and a 1k 
 *                       voltage divider is connected to PINB2. An LED is 
 *                       connected via a 1k resistor to PIN RC9 which is
 *                       comparator output.
 * 
 * Change History:
 *
 * Author             Rev     Date          Description
 * Armstrong Subero   1.0     03/04/2018    Initial Release.
 * 
 *                      
 * Created April 3nd, 2018, 9:07 PM
 */

/*******************************************************************************
 Includes and defines
*******************************************************************************/
#include <xc.h>
#include <stdio.h>
#include <stdlib.h>
#include "PIC32MX270_STD.h"
#include "Delay.h"
#include "uart1.h"

#define LED LATBbits.LATB14


/*******************************************************************************
* Function Name: void initMain (void)
*
* Specification: initializes main
*
*******************************************************************************/
void initMain(void)
{  
    uc_init();                // initialize microcontroller
    TRISBbits.TRISB14 = 0;    // set PINB14 as output
    
    // unlock PPS
    systemReg_Unlock();
    CFGCONbits.IOLOCK = 0;

    RPC5Rbits.RPC5R = 0x0001;  //RC5->UART1 TX
    RPC9Rbits.RPC9R = 0x0007;  //RC9->C1OUT;

    // lock PPS
    CFGCONbits.IOLOCK = 1; 
    systemReg_Lock();
    
    TRISCbits.TRISC9 = 0;     // set comparator output pin as output
   
    CMSTATbits.SIDL  = 0;     // comparators will continue to operate in IDLE
    CM1CONbits.COE   = 1;     // comparator output is driven to out pin
    CM1CONbits.CREF  = 0;     // comparator positive input connected to C1INA
    CM1CONbits.CCH0  = 0;     // comparator negative input connect to   C1INB
    CM1CONbits.CCH1  = 0;
    CM1CONbits.ON    = 1;     // turn the comparator on
    
    UART1_Initialize();       // initialize UART   
}


/*******************************************************************************
* Function Name: void main (void)
*
* Specification: main function
*
*******************************************************************************/
void main(void) {
    
    initMain();         // initializes main function
    
    while(1)
    {
      // configure printf to use UART1
      __XC_UART = 1;
      
      if (CM1CONbits.COUT)    // if output bit is high
      {
        printf("High\n");     // send High via UART
        delay_ms(1000);       // delay 1 second
      }
      
      else
      {
        printf("Low\n");      // send Low via UART
        delay_ms(1000);       // delay 1 second
      }
      
    }
}

The comparator is configured to have the output driven to a pin, and to have the input signal and reference voltages connected to external pins. The output bit is then read and the result is sent via UART.


Link to Project

You can download the entire project here!

Comparator Project

Leave a Reply

Your email address will not be published. Required fields are marked *