Felix889
Newbie
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:
I would appreciate it if someone could help me with that.
Thank you.
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: