Subscribe to the MicroZine Newsletter and collect your free microcontroller Ebooks, download project code and more...

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.

Links

Circuit.

Download.

Code.

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

Registers

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


CMCON:

12F675 Comparator control CMCON

VRCON:

12F675 Comparator voltage reference VRCON

Simple Circuit to test the comparator

12F675 comparator test circuit

Download

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: 6.2.0.0
//
// Note Testing:
//
//   Tested on 12F675
//
// Requirements:
//
//   Target : 12F675
//
// Notes :
//
//   Uses internal oscillator.
//
// Version:
// 1.00 Initial release.
//
// Copyright : Copyright © John Main
//   https://www.best-microcontroller-projects.com
//   Free for non commercial use as long as this entire
//   copyright notice
s included in source code
//   and any other documentation.
//
///////////////////////////////////////////////////////

///////////////////////////////////////////////////////
// Definitions
#define COMPARATOR_OP 2
#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;

   init_ports();

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

      GPIO |= (1<<COMPARATOR_OP);
      delay_ms(100);

      GPIO &= ~(1<<COMPARATOR_OP);
      delay_ms(100);
   }

   init_comparator();

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

      GPIO &= ~(1<<LED);
      delay_ms(100);
   }
}



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. The TCS230 Color Sensing Chip: How it works and how to use it.

    How to use the TCS230 (/TCS3200) Color detector chip and easily add it to any of your projects.

    Read more

  2. Arduino millis

    How to use Arduino millis() for delays but still make the processor do work - Stop using delay()

    Read more

  3. [Arduino Tutorial] : How to use the 74HC595 shift register with shiftOut().

    The Essential Guide to the 74HC595; What it is and how you can easily use one in any of your projects.

    Read more

  4. Pic Programmer Types

    Which pic programmer do you need? This page discusses PIC programmers and gives some essential information on choosing or building your own programmer.

    Read more

  5. Arduino Pulsein: an easy way to measure pulse periods in microseconds.

    learn how to use Arduino pulseIn and pulseInLong to get the most accurate pulse measurement on an Arduino.

    Read more

  6. Multiple MCP23017 Interrupt tutorial for operating multiple interrupts from multiple MCP23017s.

    This MCP23017 Interrupt tutorial shows you how to connect interrupt outputs from several MCP23017s to a single microcontroller interrupt pin.

    Read more

Readers Comments

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

SUPERB and FANTASTIC."

- Ranish Pottath

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

- Milan

bursach<at>gmail.com<

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


- Matt
matt_tr<at>
wolf359.cjb.net


Learn Microcontrollers

"Interested in
Microcontrollers?"

Sign up for The
Free 7 day guide:

FREE GUIDE : CLICK HERE


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


- Dave

de_scott<at>bellsouth.net

"Your site is a great
and perfect work.
congratulations."


- Suresh

integratredinfosys<at>
yahoo.com

"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.
Regards."


- Anon

Back to Top