• AVR Freaks

Hot!Comparator interrupt continually fires

Author
procrock
New Member
  • Total Posts : 7
  • Reward points : 0
  • Joined: 2015/10/10 21:06:05
  • Location: 0
  • Status: offline
2020/07/05 14:24:37 (permalink)
0

Comparator interrupt continually fires

Hi everybody,
 
I'm using a PIC18F2620 with a comparator that runs with an interrupt.  When the interrupt fires, I switch configuration of that same pin to ADC and run a short analytical routine (microphone/sound based). The issue is that I get thousands of CM interrupts in only a few seconds when the input signal does not warrant.  Could this be related to switching between CM and ADC?  Here are the relevant routines:
 
void initCVREF(void) {
CVRCONbits.CVR=COMPARATOR_SENSITIVITY;
CVRCONbits.CVREN=1; 
delayUs(10);
CVRCONbits.CVROE=0; 
CVRCONbits.CVRR=0;
CVRCONbits.CVRSS=0;
delayUs(10);
}
 
void deinitCVREF(void) {
CVRCONbits.CVREN=0; /* disable */
}
 
void initComparator(void) {
di();
CMCONbits.C1INV=1; //output inverted
delayUs(10);
CMCONbits.C2INV=0; //output not inverted
delayUs(10);
CMCONbits.CIS=0;//Vin- connects to AN0
delayUs(10);
CMCONbits.CM=0b110; 
PIE2bits.CMIE=1;
delayUs(10);
PIR2bits.CMIF=0;//clear flag
delayUs(10);
appdata.comparatorOn=1;
delayMs(1);
ei();
delayUs(10);
PIR2bits.CMIF=0;//clear flag
delayUs(10);
}
 
void deInitComparator(void) {
CMCONbits.CM=0b111; //111 comparators off
delayUs(10);
PIE2bits.CMIE=0;
delayUs(10);
PIR2bits.CMIF=0;//clear flag
appdata.comparatorOn=0;
}
 
void initADC(void) {
ADCON0 = 0b00000000; 
ADCON0bits.ADON=1; 
ADCON1 = 0b00001110;
ADCON2 = 0b10110100; 
ADIE=0;
delayUs(10);
};
 
void deInitADC(void) {
ADCON0 = 0b00000000; 
ADCON0bits.ADON=1; 
ADCON1 = 0b00001110;
delayUs(100);
ADCON2 = 0b10110100; 
delayUs(100);
ADIE=0;
delayUs(100);
};
#1

6 Replies Related Threads

    RISC
    Super Member
    • Total Posts : 5776
    • Reward points : 0
    • Status: offline
    Re: Comparator interrupt continually fires 2020/07/05 14:45:23 (permalink)
    +1 (1)
    Hi,
    Did you follow the method explained in section 20.6 a/ & b/ to clear the CM interrupt ?
    Regards
     

    For support make sure to check first here : http://microchipdeveloper.com
    There are hundreds of PIC, AVR, SAM...which one do YOU use ?
    #2
    PStechPaul
    Super Member
    • Total Posts : 2814
    • Reward points : 0
    • Joined: 2006/06/27 16:11:32
    • Location: Cockeysville, MD, USA
    • Status: offline
    Re: Comparator interrupt continually fires 2020/07/05 16:09:48 (permalink)
    +3 (3)
    Your initADC(void) and deInitADC(void) routines are identical except for delays, so ADC remains ON.

     
    #3
    procrock
    New Member
    • Total Posts : 7
    • Reward points : 0
    • Joined: 2015/10/10 21:06:05
    • Location: 0
    • Status: offline
    Re: Comparator interrupt continually fires 2020/07/05 18:58:11 (permalink)
    0
    Thank you and sorry for the stupid mistake...
    #4
    procrock
    New Member
    • Total Posts : 7
    • Reward points : 0
    • Joined: 2015/10/10 21:06:05
    • Location: 0
    • Status: offline
    Re: Comparator interrupt continually fires 2020/07/05 19:00:28 (permalink)
    +1 (1)
    For some reason it did not click that I needed to read CMCON in the ISR.  I have done that and things look much more promising.  You make it look easy but this has taken me days of head scratching.  Thanks very much!
    post edited by procrock - 2020/07/05 19:03:47
    #5
    1and0
    Access is Denied
    • Total Posts : 10999
    • Reward points : 0
    • Joined: 2007/05/06 12:03:20
    • Location: Harry's Gray Matter
    • Status: offline
    Re: Comparator interrupt continually fires 2020/07/05 22:04:23 (permalink)
    +3 (3)
    One suggestion -- take out all those delays. ;)
     
    #6
    procrock
    New Member
    • Total Posts : 7
    • Reward points : 0
    • Joined: 2015/10/10 21:06:05
    • Location: 0
    • Status: offline
    Re: Comparator interrupt continually fires 2020/07/05 22:45:38 (permalink)
    +1 (1)
    Yes, I will, thanks! (remnants of some random guessing...)
    #7
    Jump to:
    © 2020 APG vNext Commercial Version 4.5