开发者

Accuracy of Timer1 as real time clock with PIC Interrupts on 16F*

开发者 https://www.devze.com 2023-03-15 13:50 出处:网络
I\'m using C with the BoostC compiler. I\'m worried how accurate my code is. The configuration below ticks at more or less 1Hz, (tested with an LED to the naked eye). (It uses an external watch crysta

I'm using C with the BoostC compiler. I'm worried how accurate my code is. The configuration below ticks at more or less 1Hz, (tested with an LED to the naked eye). (It uses an external watch crystal 32kHz for Timer1 on the 16f74).

I'm hoping someone can tell me...

Does my code below need any adjustments? What would be the easiest way of measuring the accuracy to the nearest CPU clock period? Will I need to get dirty with assembly to reliably ensure accuracy of the 1Hz signal?

I'm hoping the time taken to execute the timer handler (and others) doesn't even come into the开发者_开发知识库 picture, since the timer is always counting. So long as the handlers never take longer to execute than 1/32kHz seconds, will the 1Hz signal have essentially the accuracy of the 32kHz Crystal?

Thanks

#define T1H_DEFAULT 0x80
#define T1L_DEFAULT 0
volatile char T1H = T1H_DEFAULT;
volatile char T1L = T1L_DEFAULT;

void main(void){

    // setup 
    tmr1h = T1H;
    tmr1l = T1L;
    t1con = 0b00001111; // — — T1CKPS1 T1CKPS0 T1OSCEN NOT_T1SYNC TMR1CS TMR1ON
    // ...

    // do nothing repeatedly while no interrupt
    while(1){}
}

interrupt(void) {

    // Handle Timer1
    if (test_bit(pir1, TMR1IF) & test_bit(pie1, TMR1IE)){

            // reset timer's 2x8 bit value
            tmr1h = T1H;
            tmr1l = T1L;

            // do things triggered by this time tick

            //reset T1 interrupt flag 
            clear_bit(pir1, TMR1IF);

    } else 

    ... handle other interrupts

}


I can see some improvements...

Your timer initiation inside interrupt isn't accurate. When you set the timer counter in interrupt...

    tmr1h = T1H;
    tmr1l = T1L;

... then you override the current value what isn't good for accuracy. ... just use:

tmr1h = T1H;  //tmr1h  must be still 0!

Or even better, just set the 7th bit of tmr1h register. The compiler must compile this order to single asm instruction like...

bsf    tmr1h, 7

...to avoid losing data in tmr1 register. Because if this is made with more than one instructions the hardware can increment the counter value between execution of: read-modify-write.

0

精彩评论

暂无评论...
验证码 换一张
取 消