Garyl
Full Member level 5
Hey,
I've triied to setup the 32 bit timer interrupt, according to the datasheet, but it's not starting.
The blinking and uart works, but the counter is not changing (nor the PR2/TMR2/PR3/TMR3).
What's missing?
I've triied to setup the 32 bit timer interrupt, according to the datasheet, but it's not starting.
The blinking and uart works, but the counter is not changing (nor the PR2/TMR2/PR3/TMR3).
What's missing?
Code:
int counter = 0;
void main() {
char buff[64];
ADPCFG = 0xFFFF; // Configure AN pins as digital I/O
AD1PCFG = 0x1C3F; // Configure all pins as digital I/O
TRISB = 0; // Initialize PORTB as output
LATB = 0; // Set PORTB to zero
RPOR1bits.RP2R=3; //Assign U1TX To Pin RP2 (pin 6)
Delay_ms(100);
UART1_Init(9600); // Initialize UART module at 9600 bps
Delay_ms(100); // Wait for UART module to stabilize
UART1_Write_Text("Start");
UART1_Write(13);
UART1_Write(10);
// TIMER 32 BIT!
/*
To configure Timer2/3 or Timer4/5 for 32-bit operation:
1. Set the T32 bit (T2CON<3> or T4CON<3> = 1). */
// SETBIT(T2CON,3);
T2CON.T32 = 1;
/*
2. Select the prescaler ratio for Timer2 or Timer4
using the TCKPS<1:0> bits.
*/
T2CON.TCKPS_0 = 0;
T2CON.TCKPS_1 = 0;
/*
3. Set the Clock and Gating modes using the TCS
and TGATE bits. If TCS is set to the external
clock, RPINRx (TxCK) must be configured to an
available RPn pin. See Section 10.4 “Peripheral
Pin Select (PPS)” for more information.
*/
/*
bit 1 TCS: Timery Clock Source Select bit(1,2)
1 = External clock from pin, TyCK (on the rising edge)
0 = Internal clock (FOSC/2)
*/
T2CON.TCS = 0;
/*
bit 6 TGATE: Timer1 Gated Time Accumulation Enable bit
When TCS = 1:
This bit is ignored.
When TCS = 0:
1 = Gated time accumulation is enabled
0 = Gated time accumulation is disabled
*/
T2CON.TGATE = 0;
/*
4. Load the timer period value. PR3 (or PR5) will
contain the most significant word of the value
while PR2 (or PR4) contains the least significant
word. */
PR3 = 0x00ff;
PR2 = 0x00ff;
/*
5. If interrupts are required, set the Timer3/5 Interrupt
Enable bit, T3IE or T5IE; use the priority
bits, T3IP<2:0> or T5IP<2:0>, to set the interrupt
priority. Note that while Timer2 or Timer4
controls the timer, the interrupt appears as a
Timer3 or Timer5 interrupt.
*/
IEC0.T3IE = 1;
// TODO: set priority
IPC2.T3IP_0 = 1;
IPC2.T3IP_1 = 1;
IPC2.T3IP_2 = 1;
/*
6. Set the TON bit (= 1).
*/
T2CON.TON = 1;
while(1) {
sprintf(buff,"Cnt %i, PR3 %i, PR2 %i, TMR3 %i, TMR2 %i\n\r",counter, PR3, PR2, TMR3, TMR2);
UART1_Write_Text(buff);
LATB = ~LATB; // Invert PORTB value
Delay_ms(1000);
}
}
void interrupt()
{
counter++;
IFS0.T3IF = 0; //interrupt flag cleared
}
void Timer3Int() iv IVT_ADDR_T3INTERRUPT{// 0x1A Address in the interrupt vector table of timer1
counter++;
IFS0.T3IF = 0; //interrupt flag cleared
}