PIC16F877A Comparator interrupt always triggered

Status
Not open for further replies.

lloydi12345

Member
Hi, my program about comparator interrupt has a problem. Can you help me with this one? The circuit below is a battery low voltage indicator. I think ISR is activated but it is always triggered. In my program, portb.f0's state should be inverted as soon as the voltage CMCON.C1OUT state is changed. Can you check my program? Here's my code:

Code:
void interrupt(void){
     PORTB.F0 = ~PORTB.F0;   //invert the output on portb pin 0
     PIR2.CMIF = 0;
}

void main() {
     TRISA = 0XFF;
     TRISB = 0x00;
     PORTB = 0x00;
     CMCON = 0b00000011; //Use external voltage reference connected to RA3
                         //"-" input of C1 connected to RA0
                         //Mode 011

     PIE2.CMIE = 1;
     INTCON.PEIE = 1;
     INTCON.GIE = 1;
     
     while(1){}
}

I attached my schematic below. I hope you can help me.
 

Attachments

  • Capture.JPG
    104.8 KB · Views: 364
Last edited:
hi,
Check your CMCON settings.
 

Attachments

  • 000esp01.gif
    44 KB · Views: 202
You have both comparators enabled. The other comparator will be oscillating and causing the interrupt.

Mike.
 
Thanks for the reply ericgibbs and Pommie, I changed my mode from 011 to 001. It is still triggered.
 
Last edited:
Hi, I changed my code with this one. I think it is not anymore triggered. But can you double check my code please? I'm not so sure if I'm able to disable it.

Code:
void interrupt(void){
     CMCON.C2OUT = 0;
     PORTB.F0 = ~PORTB.F0;
     PIR2.CMIF = 0;
}

Is this one alright?
 
Last edited:
That should work. Even though CMCON.C2OUT is read only, any read or write to CMCON will clear the interrupt condition.

Mike.
 
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…