Hi, I'm trying to use Timer0 to interrupt every 1ms.
I'm using PIC18F4550 running on 8MHz. The code is written in C18.
Below is the code.
What i was excepting to get is an interval of 1ms between interrupts. However, what I get is 1.024ms. 1.024 is equivalent to TMR0L = 0. Even if I set TMR0L = 255, it still give me 1.024ms. Something might have gone wrong somewhere but I'm not sure what it is.
Another things i observed is that the timer interval is not consistent. For a time of 1.024ms, the number of instructions is 2048 for FOSC = 8MHz. However, sometimes i get the interval of 2049 or 2047 instructions.
I'm not really sure whats going on there.
Can somebody please advise on these 2 difficulties.. Thanks.
I'm using PIC18F4550 running on 8MHz. The code is written in C18.
Below is the code.
Code:
#pragma code low_vector=0x18 //setup the ISR vector
void low_interrupt (){
_asm GOTO LowISR _endasm //jump to interrupt handler
}
#pragma code
#pragma interruptlow LowISR
void LowISR()
{
if (INTCONbits.TMR0IF == 1)
{
INTCONbits.TMR0IF=0; // Put breakpoint here and monitor the time interval using stopwatch (in MPLAB SIM).
}
}
void main()
{
// Counting from 6 to 255 should give 250 counts
// Prescaler = 1:8
// 250 * 8 * 500ns = 1ms
TMR0L = 6;
T0CON = 0b11000010;
INTCONbits.GIE = 1;
INTCONbits.PEIE = 1;
INTCONbits.TMR0IE = 1;
while(1);
}
What i was excepting to get is an interval of 1ms between interrupts. However, what I get is 1.024ms. 1.024 is equivalent to TMR0L = 0. Even if I set TMR0L = 255, it still give me 1.024ms. Something might have gone wrong somewhere but I'm not sure what it is.
Another things i observed is that the timer interval is not consistent. For a time of 1.024ms, the number of instructions is 2048 for FOSC = 8MHz. However, sometimes i get the interval of 2049 or 2047 instructions.
I'm not really sure whats going on there.
Can somebody please advise on these 2 difficulties.. Thanks.