[PIC] Timer2 for PIC32MX

Status
Not open for further replies.

Felix889

Newbie
Joined
Jun 10, 2021
Messages
1
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
33
Hello,

Could someone please explain what is the proper way to use the Timer2 interrupt?
I found a lot of examples online on the official MICROCHIP website, but every example for an extra line of code.
I am trying to use Timer2 interrupt to enable LED when it is triggered, however, my code does not work for some reason.

My code:
Code:
#include<p32xxxx.h>
#include<plib.h>

// timer2 initialization
void timer2_init(void)
{
    T2CONbits.TON = 0;                /* turn off Timer 2 */
    T2CONbits.TCKPS = 7;              /* pre-scale = 1:256
    PR2 = 11000;
    TMR2 = 0;
}

void __ISR(_TIMER_2_VECTOR, ipl7) T2InterruptHandler(void)
{
    
    // toggle RA1 pin
    LATAbits.LATA1 ^= 1;
    // reset the overflow interrupt flag
    IFS0bits.T2IF = 0;
}

main()
{
    // init I/Os
    DDPCONbits.JTAGEN = 0;
    TRISA = 0xff00;     // PORTA LSB as outputs which are connected to LEDs
    PORTA = 0;

    // initialize timer
    timer2_init();

    IPC2bits.T2IP = 7;
    IFS0bits.T2IF = 0;
    IEC0bits.T2IE = 1;
    T2CONbits.TON = 1;
  
    while(1);
 
}// main

I would appreciate it if someone could help me with that.
Thank you.
 
Last edited by a moderator:

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