CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

interrupt TIMER0 and real time

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
rakto
Guest







interrupt TIMER0 and real time
PostPosted: Thu Nov 16, 2006 8:43 am     Reply with quote

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

View user's profile Send private message

PostPosted: Thu Nov 16, 2006 9:32 am     Reply with quote

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








PostPosted: Thu Nov 16, 2006 12:21 pm     Reply with quote

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.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group