I am using PIC18F4550
I am trying to generate an interrupt every 1 sec, and by trial and error(i.e. just writing and simulating on hardware ) I am getting interrupt approx to 1 sec. Code for this is:-
#pragma config PLLDIV = 5 , CPUDIV = OSC1_PLL2 , USBDIV = 2
#pragma config FOSC = INTOSCIO_EC
#pragma config FCMEN = OFF
#pragma config BORV = 3
#pragma config WDT = OFF
#pragma config CPB = OFF
#pragma config CPD = OFF
void intinter(void)
{
RCONbits.IPEN =1; //Priority Interrupt is enabled
INTCON = 0b11100000; //all high and low priority interrupt is enabled, timer 0 overflow interrupt enable
INTCON2bits.TMR0IP = 0; //set timer 0 interrupt low priority
}
void inittimer(void)
{
T0CON = 0b00000111; //prescaler of 256, 16 bit, internal clock
TMR0H = 0xFFFF; // Set TMRH and L Value
TMR0L = 0x0000;
}
void interrupt low_priority timerinterrupt (void)
{
if(TMR0IF == 1)
{
TMR0ON =0;//Timer 0 off
TMR0IF = 0; //Timer 0 IF disable
TMR0H = 0xFFFF;
TMR0L = 0x0000;
i++;
if(i==4)
{
//if i==4 I am getting approx 1 sec interrupt.
}
Now after reading the datasheet and about the procedure to calculate the value of the TMR0H and TMR0L register, I am unable to make sense of my code.
As I have set internal clock with default value by just using this,
#pragma config FOSC = INTOSCIO_EC
I think the clock is Fosc = 1Mhz, so the calculation is, Our FCPU=1MHz/4 (We are running from 1Mhz Internal Clock)
=0.25MHz
Time Period = 4uS
Prescaler Period = 4 x 256 = 1.024x10^-3 (Prescaler is set to divide frequency by 256)
Overflow Period = 1.024x10^-3 x 65535 = 67.10 s (Each over flow takes 65535 counts)
So it seems each overflow takes 67 seconds but it is taking 4 overflow for 1 sec timer, how is it possible? Is my calculation procedure wrong? Or I am taking wrong clock.
Please help me understand my error as I am new to PIC programming.
Thank You.
static int i=0;
\$\endgroup\$