Hi there i was wondering if any of you could help me out here . Basically, i need a Timer to count down a time limit which is used to turn off my device after the specified time limit. My pic18f46k20 is running at 1MHz and it is a 16 bit counter . (65536x(1/250KHz) = approx 1 second. I was wondering if this is a correct approach . The period_counter is to increase the second after the timer finish counting from 0-65536 . However to get 1 second , i have to set the period counter to 4000 instead of just 1 . I thought that the timer takes approx 1 second to finish counting.
INTCONbits.TMR0IF = 0; // clear roll-over interrupt flag
T0CON = 0b00000001; // pre scale 1:4
TMR0H = 0x00; // clear timer
TMR0L = 0x00;
T0CONbits.TMR0ON = 1;
void timer (void)
{
if ( INTCONbits.TMR0IF == 1 )
{
TMR0H= 0x00;
TMR0L= 0x00;
period_counter++;
}
}
if ( period_counter > 4000 ) // 1 second
{
LATDbits.LATD7 = 1;
period_counter = 0;
counter_second ++;
}
INTCONbits.TMR0IF = 0; // clear roll-over interrupt flag
T0CON = 0b00000001; // pre scale 1:4
TMR0H = 0x00; // clear timer
TMR0L = 0x00;
T0CONbits.TMR0ON = 1;
void timer (void)
{
if ( INTCONbits.TMR0IF == 1 )
{
TMR0H= 0x00;
TMR0L= 0x00;
period_counter++;
}
}
if ( period_counter > 4000 ) // 1 second
{
LATDbits.LATD7 = 1;
period_counter = 0;
counter_second ++;
}