View previous topic :: View next topic |
Author |
Message |
rakto Guest
|
interrupt TIMER0 and real time |
Posted: Thu Nov 16, 2006 8:43 am |
|
|
hello,
Maybe this problem was already solved on an other topic but i didn't find it. I try to use the interruption with timer to generate a fixed frequency but the frequency is not the same than theory. I can't find where the error.
the code is:
#include "16F877A.h"
#use delay(clock=20000000)
#fuses HS,NOWDT,NOPROTECT,NOLVP
char val_init_timer;
#INT_TIMER0
void clock_isr() //fonction appelée lors du depassement timer0(256->0)
{ set_timer0(val_init_timer);
output_high(PIN_A2);
output_low(PIN_A2);
}
void main(void) {
val_init_timer=156;
set_tris_A(0x00);
setup_timer_0(RTCC_INTERNAL|RTCC_div_1);
enable_interrupts(INT_TIMER0);
enable_interrupts(GLOBAL);
while(TRUE);
}
for example
if val_init_timer=156:
experimentaly =>the frequency on RA2 is 36.77kHz
in theory =>20MHz/(4*(256-156))=50kHz
if val_init_timer=0:
experimentaly =>the frequency on RA2 is 17.13kHz
in theory =>20MHz/(4*(256-0))=19.53kHz |
|
|
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Thu Nov 16, 2006 9:32 am |
|
|
The problem is that it takes some time before your interrupt routine is handled, search this forum for the keywords 'interrupt latency' for more info.
Depending on the processor, compiler version and number of enabled interrupts it takes anywhere from 20 to 50 instructions for saving all registers before the processor jumps to your interrupt routine.
One easy way to improve accuracy is to change the line Code: | set_timer0(val_init_timer); | to Code: | set_timer0( get_timer0() + val_init_timer ); |
Although more accurate this still has a small error when the hardware increments the counter in between reading and setting the new value. And when using a prescaler different from RTCC_DIV_1 the error will even be larger. For the most accurate timings use Timer2 which does the same thing with it's period register but in hardware, or use the CCP module. |
|
|
Guest
|
|
Posted: Thu Nov 16, 2006 12:21 pm |
|
|
thanks it's effectively better with your change, in fact if i affect
val_init_..=0 I can't have the frequency of 19.53kHz because get_timer()>0. |
|
|
|