I generated the code for a timer (timer2) using mplab code configurator. In the combo, I select the max time for the timer period. So Im using a postescaler of 1:16 in T2OUTPS (1111) and a prescaler of 16 in T2CKPS (1x) The period should be several seconds, but it triggers once every half second (aprox). I don't understand what's the problem, cause it doesn't matters what value I give to the pre and postscaler, the period is the same
Here is the relevant code. This is how I initialize the timer:
void TMR2_Initialize(void) {
// Set TMR2 to the options selected in the User Interface
T2CON = 0b01111011;
//T2CON = 0x3A;
//T2CON.T2OUTPS = 0b0000;
// PR2 255;
PR2 = 0xFF;
// TMR2 0x0;
TMR2 = 0x00;
// Clearing IF flag before enabling the interrupt.
PIR1bits.TMR2IF = 0;
// Enabling TMR2 interrupt.
PIE1bits.TMR2IE = 1;
// Start TMR2
TMR2_StartTimer();
}
void TMR2_StartTimer(void) {
// Start the Timer by writing to TMRxON bit
T2CONbits.TMR2ON = 1;
}
And this is how I handle the interrupt:
void interrupt SYS_InterruptHigh(void)
{
if (PIE1bits.TMR2IE == 1 && PIR1bits.TMR2IF == 1) {
TMR2_ISR();
}
......
void TMR2_ISR(void) {
// clear the TMR2 interrupt flag
PIR1bits.TMR2IF = 0;
if (colorUpdate%4 == 1)
{
LED_Color(0xFFFF,0x0000,0xFFFF);
}
else if (colorUpdate%4 == 2)
{
LED_Color(0x0000,0xFFFF,0xFFFF);
}
else if (colorUpdate%4 == 3)
{
LED_Color(0xFFFF,0xFFFF,0x0000);
}
else if (colorUpdate%4 == 0)
{
LED_Color(0x0000,0xFFFF,0x0000);
}
colorUpdate++;
if (colorUpdate>1000)
colorUpdate = 0;
LED_UpdateImage();
LATCbits.LATC6 = 1;
LATCbits.LATC6 = 0;
}