Become a subscriber (Free)

Join 29,000 other subscribers to receive subscriber sale discounts and other free resources.
Don't worry -- youre-mail address is totally secure. I promise to use it only to send you MicroZine.
12F675 Comparator use

12F675 comparator test circuts and code.

The PIC comparator is the least used peripheral but it is just as fast as the standard LM311...

Note: This information is  applicable to any PIC Microcontroller that has a built in comparator.

There is one comparator in the 12F675 which you can set up in many different ways and you can even supply a comparison voltage from an internal reference. This voltage can be set to any of 32 steps.

This is just a simple project designed to show simple operation so that you can use it.  It shows all you need to do to operate the comparator.

Response time

The comparator is high speed with a specification of between 150ns to 400ns (150ns typ , 400ns max).  

The typical response time is better than the standard response time of an LM311 which is 200ns - but you may get up to 400ns for the PIC comparator - thats still very good.

Note This is one of the strengths of the comparator module - the fact that it operates independently from the processor and has an extremely fast response time.

One "Gotcha"

One piece of information that is only implied by the documentation is that the comparator inputs must be set to analogue inputs - otherwise the comparator does nothing!

So you need to control ANSEL.





Comparator Modes.

Registers associated with the comparator.

CMCON Comparator control register.

VRCON Comparator voltage reference control.

Comparator Modes

The following table shows the eight different ways  of configuring the comparator:

12F675 comparator modes


These are the registers for controlling the Comparator and Voltage reference:

The main controls are:

  • CMCON for the Comparator and
  • VRCON for the Voltage reference.

...the rest are for interrupt control and I/O Direction (don't forget ANSEL not in table!)

12F675 comparator and voltage reference


12F675 Comparator control CMCON


12F675 Comparator voltage reference VRCON

Simple Circuit to test the comparator

12F675 comparator test circuit


Download code here : Click to download.

C Code for the comparator

The following code sets up the comparator as a fully external device i.e. the internal voltage reference is not used:

// File: 16F675_comparator.c
// Author: J F Main.
// Description:
//   Use comparator GP0,GP1,GP2
// Compiler : mikroC, mikroElektronika C compiler
//            for Microchip PIC microcontrollers
//            Version:
// Note Testing:
//   Tested on 12F675
// Requirements:
//   Target : 12F675
// Notes :
//   Uses internal oscillator.
// Version:
// 1.00 Initial release.
// Copyright : Copyright © John Main
//   Free for non commercial use as long as this entire
//   copyright notice
s included in source code
//   and any other documentation.

// Definitions
#define LED 4

void init_ports(void) {
   //GP0 & GP1 are inputs
   TRISIO = 0 | (1<<GP0) | (1<<GP1); // 0 - op, 1 - ip

   ANSEL = (1<<GP0) | (1<<GP1); ; // Ana. ip on GP0 GP1

void init_comparator(void) {
   // Comparator with external input and output.
   // Cout = 0 (comparator output), Cinv =0 (inversion)
   CMCON = 0x01;

// Start here
void main() {
int i;


   // Show device is active on power up.
   for (i=0;i<5;i++) {

      GPIO |= (1<<COMPARATOR_OP);

      GPIO &= ~(1<<COMPARATOR_OP);


   while(1) {;
      GPIO |= (1<<LED);

      GPIO &= ~(1<<LED);

Note: To run the above code you may need to remove the ICSP connections after programming as the comparator pins are on the PGD and PGC lines.

The code simply flashes the comparator output LED 5 times at start up and after this the comparator is turned on.  Then the second LED is flashed continuously.

By changing the POT setting you can see the comparator output turn on and off.

Jump from 12F675 comparator page
to Best Microcontroller Projects Home Page.

New! Comments

Have your say about what you just read! Leave me a comment in the box below.

Privacy Policy | Contact | About Me

Site Map | Terms of Use

Visit our Facebook Page:

   Click Here

Recent Articles

  1. PIC PWM: Easy Pulse width Modulation on the PIC Micro.

    PIC PWM : What its for and how to use it. Including an example showing typical setup of a PIC PWM module with register settings.

    Read more

  2. The Essential I2C Tutorial: All you need to know about I2C...

    How to use I2C. In this tutorial you will learn all about the 2 wire I2C serial protocol. Learn how easy it is to use, how it works and when to use it...

    Read more

  3. PIC LCD volt meter : How to use the ADC for voltage measurement.

    A 0-5V LCD volt meter project using an HD44870 display and one PIC micro ADC.

    Read more

  4. PIC Programming using ICSP

    PIC programming : How to use ICSP for programming a PIC microcontroller giving some ICSP connection diagrams and it also shows how to use MPLAB X and ICPROG.

    Read more

  5. PIC ICSP In Circuit Serial Programming

    How to program PIC ICSP, IN-Circuit, live and still have a working programmer and operational circuit!

    Read more

  6. An Ultrasonic Distance meter project: How to make one using a microcontroller.

    Understand how an Ultrasonic Distance meter works by building one your self – including Free software to drive the microcontroller and display.

    Read more

Sign up for MicroZine
''The'' Microcontroller Newsletter

Enter your first Name and primary email address in the form below:

And receive absolutely FREE a full project for:

"Measuring Analogue Voltages
Without An ADC"

(Using only one pin).

Instant Download:
You Can
Get It Right Now

Warning: This project could be  Removed 
at any time.  

It will  NOT be 
available indefinitely SO
To avoid 
disappointment  get it:


Don't worry -- your e-mail address is totally secure. I promise to use it only to send you MicroZine
Remember this is a project with full description and fully debugged C Source code - and it's not available from the main website.

You can only get it through this newsletter.

To get exclusive access Enter your first name Name and primary email address Now in the form above.:

But wait !

There's more...

You'll receive more
free and exclusive reports as well as site information and site product updates

Scroll up to the form above and sign up NOW. Don't forget it's FREE and if you don't like it, you can unsubscribe at any time.

Click Here Now to use the form above to get your Valuable information absolutely free.

Readers Comments

"I wanted to thank
you so so so much
for all the information
you have provided in
your site it's


- Ranish Pottath

"This site really is
the best and my favorite.
I find here many useful
projects and tips."

- Milan


"Awesome site,
very, very easy and nice
to navigate!"

- Matt

Learn Microcontrollers

"Interested in

Sign up for The
Free 7 day guide:


"I am a newbie to PIC
and I wanted to say
 how great your
site has been for me."

- Dave


"Your site is a great
and perfect work.

- Suresh


"I couldn't find the correct
words to define
yourweb site.

Very useful, uncovered,
honest and clear.

Thanks so much for
your time and works.

- Anon

Back to Top