Electrical – PIC24 Timer 2/3 Interrupt Flag does not gets set

microchippictimer

I am working with a PIC24FJ1024.

I want to test Timer 2/3 (32 bit timer), by writing a C code, where I toggle a pin (PORTAbits.RA7) every second.

That is part of the code:

while(!IFS0bits.T2IF);
LATAbits.LATA7 = ~LATAbits.LATA7;
IFS0bits.T2IF = 0;  

When I compile and download it onto my Development Tool (Explorer 16/32), I don't see my Pin (which is connected to an LED) Toggle.

I debug program, and it turns out that IFS0bits.T2IF doesn't get set, the program just sits on while(!IFS0bits.T2IF)

Can someone explain to me why this happens?

Thank you in advance.

EDIT:
The Code

#include <xc.h>

#pragma config FWDTEN = OFF
#pragma config FNOSC = FRC //8MHz 
#pragma config ICS = PGD2

void Timer23_Init (void)
{
    /*
     Initialize T2 & 3
     from FRC
     * 1 second
     * on and off

     */

    T2CONbits.T32 = 1;//32 bit timer

    T2CONbits.TSIDL = 0;//continues through Idle Mode
    T2CONbits.TGATE = 0;//no TGATE Operationss
    T2CONbits.TCS = 0;//internal CLK SRC
    T2CONbits.TECS = 0x00;

    T2CONbits.TCKPS = 3;//1:256
    /*
      PRESCALAR = 256
     * t = 1 sec = 1000 ms = 1,000,000 us
     * Fosc = 8MHz Fosc/2 = 4MHz Tcy = 0.25 us
     * 
     * t = N*PRE*Tcy
     * N = t/(PRE*Tcy) = 1,000,000/(256*0.25) = 15,625
     * PR2 = 15625 (0x3D09)    
     */

    TMR2 = 0x00000000;
    PR2 = 15625;   

    TRISAbits.TRISA7 = 0;//output
    ANSAbits.ANSA7 = 0;//Digital
    LATAbits.LATA7 = 0;

    T2CONbits.TON = 1;
}

int main (void)
{   


    Timer23_Init ();

    while(1)
    {
        IFS0bits.T2IF = 0;

        while(!IFS0bits.T2IF);
        LATAbits.LATA7 = ~LATAbits.LATA7;
        IFS0bits.T2IF = 0;  
    }

    return 0;
}

Best Answer

Can someone explain to me why this happens?

The flag is almost surely firing and your led pin flipping. Except that the pin is not in GPIO mode.