Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

How to write interrupt routine

Status
Not open for further replies.

electronicsman

Full Member level 5
Joined
May 4, 2012
Messages
291
Helped
11
Reputation
22
Reaction score
12
Trophy points
1,298
Activity points
3,737
I am bit confused with ISRs of microchip. These are the two versions i can write which is correct and why? Please help.
Code:
version 1
void __attribute__((__interrupt__,__no_auto_psv__)) _T1Interrupt(void)
{      

    C instructions;
    IFS0bits.T1IF = 0; 
}

version2
void __attribute__((__interrupt__,__no_auto_psv__)) _T1Interrupt(void)
{      
    IFS0bits.T1IF = 0; 
    C instructions;
    
}
I mean to say interrupt flag should be cleared at the beginning or end?
 

It depends on whether you want your ISR to be interruptible or not.
 

Ok thank you. Basically i am facing issue with comparator interrupt code for dspic33ev family controllers. I have two versions of the software can you please tell me which is better or correct. At some times one code seems to be working and some other times the other code is working.
Code:
uchar8_t g_dummy_u8;
uchar8_t hall_a;
uchar8_t hall_b;
uchar8_t hall_c;

void __attribute__((__interrupt__,__no_auto_psv__)) _CMP1Interrupt(void)
{
    if(CM2CONbits.CEVT == 1)
    {
        CM2CONbits.CEVT = 0;
        g_dummy_u8 = CM2CONbits.COUT;
        hall_a = CMSTATbits.C2OUT;
    }
    if( CM3CONbits.CEVT == 1) // Clear comparator 3 event bit)
    {
         CM3CONbits.CEVT = 0; // Clear comparator 3 event bit
         g_dummy_u8 = CM3CONbits.COUT;
         hall_b = CMSTATbits.C3OUT;
    }
    if(CM1CONbits.CEVT == 1) // Clear comparator 3 event bit)
    {
        CM1CONbits.CEVT = 0; // Clear comparator 3 event bit
        g_dummy_u8 = CM1CONbits.COUT;
        hall_c = CMSTATbits.C1OUT;
    }
    
    IFS1bits.CMPIF = 0; // clear the interrupt flag
}

Code:
uchar8_t g_dummy_u8;
uchar8_t hall_a;
uchar8_t hall_b;
uchar8_t hall_c;

void __attribute__((__interrupt__,__no_auto_psv__)) _CMP1Interrupt(void)
{
    IFS1bits.CMPIF = 0; // clear the interrupt flag
    if(CMSTATbits.C1EVT== 1) // Clear comparator 3 event bit)
    {
        CM1CONbits.CEVT = 0; // Clear comparator 3 event bit
        hall_a = CMSTATbits.C1OUT;
        g_dummy_u16 = CM1CONbits.COUT;
    }
    if(CMSTATbits.C2EVT == 1)
    {
        CM2CONbits.CEVT = 0;
        hall_b = CMSTATbits.C2OUT;
        g_dummy_u16 = CM2CONbits.COUT;
        
    }
    if( CMSTATbits.C3EVT == 1) // Clear comparator 3 event bit)
    {
         CM3CONbits.CEVT = 0; // Clear comparator 3 event bit
         hall_c = CMSTATbits.C3OUT;
         g_dummy_u16 = CM3CONbits.COUT;
    }
}
because same data is available in both control register and status register.


I am mainly confused with the statement
"The Comparator Interrupt Flag (CMPIF) bit (IFS1<2>) is set when the synchronized output value of any of the comparators changes, with respect to the last read value". What is this last read value? I am also attaching the comparator data sheet. Please help.
 

Attachments

  • comparator_70000357e.pdf
    278.7 KB · Views: 76

A previous dsPIC33 family reference manual illustrates the comparator signal processing as below

comparator.png

Reading CMCON latches the present state, I guess also reading CMSTAT if present in the respective processor. Means operation is similar to input change notification of digital input ports.
 

So which version of the interrupts i can use or both are same. The issue i am facing is i should get a single interrupt when there is cross over but what i observe is a kind of continuous toggling during the cross over time. I observe this by toggling a port and i observe this in only one comparator. Not sure what is the issue.
 

Hi,

maybe a bad schematic or a bad PCB layout.
Show us both.

Klaus
 

I verified the analog signals on the oscilloscope to the inputs of the comparator and they seem to be ok.
 

That's why I will never return back to PIC!
void __attribute__((__interrupt__,__no_auto_psv__)) _CMP1Interrupt(void) -> this will blow my mind
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top