Jump to content

Making Comparator Work On 16f877

Recommended Posts

I am having a problem getting the comparator C2 to work on a 16F877 and I believe that there is some kind of 'trick' I dont remember ...or maybe just not reading manual right. ...or maybe just dumb.


I have set all of port A to input except for the comparator output pin with trisa = 1110 1111


I am using two separate comparators with CM2:CM0 = 010 with C2OUT = C1OUT =1


The internal reference is turned off with CVRCON set to 0x00;


The external reference is half supply into RA2


Analog input is RA1




cvrcon = 0x00; // reference off

cmcon = 0b0011010; //two independent comparators

trisa = 0b11011111; //all but port a are inputs except porta.5


I can "see" the input on RA1 and RA2 shows the correct Vref, but nothing on porta.5.


Do I have to do something with adcon1?


I'm sure somebody out there has fought this battle before, but I couldnt find anything on Google..


It's making me crazy....




Link to post
Share on other sites

EEEEEErrrrrr........ The PIC16F877 doesn't HAVE any comparitors! According to Microchip they were introduced in the PIC16F877A.


Now Microchip doesn't always tell us if they've goofed and put in the silicon for some feature that dosn't work - so the PIC16F877 may actually secretly HAVE comparitors that DON'T WORK!


Please see Microchip's DS39591A 'PIC16F87X to PIC16F87XA Migration' for comfirmation of this.






Link to post
Share on other sites

Got it! Just a matter of spending some time with the manual and a bit of messing around with the hardware.


This setup works to give two separate comparators as shown in figure 12-1 of the manual:


//comparator setup for two independent comparators using external reference

cvrcon = 0x00; // voltage reference off


cmcon = 0b00011011; //two independent comparators with outputs

// ---- -011 two independent comparators with outputs

// ---- x--- dont care

// --11 ---- both outputs inverted

// 00-- ---- set output polarity


trisa = 0b00001111; //set up port configs

// ---- 1111 RA0, RA1, RA2, RA3 all inputs

// 0000 ---- RA4, RA5, RA6, RA7 all outputs (RA4 needs a pullup)

My biggest problem was selecting the cmcon setup. I had accidentally set it to be "two independent comparators" rather than "two independent comparators with outputs".


Live and learn.

Link to post
Share on other sites

Join the conversation

You are posting as a guest. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...