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

TIMER0 PROBLEM

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



Joined: 10 Jan 2019
Posts: 16
Location: Maracaibo, Venezuela

View user's profile Send private message Send e-mail Visit poster's website

TIMER0 PROBLEM
PostPosted: Wed Feb 13, 2019 6:25 pm     Reply with quote

Hello guys, i'm using this code:

Code:

#include <16F876A.h>
#FUSES NOWDT                   
#FUSES XT                       
#FUSES PUT                     
#FUSES NOPROTECT               
#FUSES NODEBUG                 
#FUSES BROWNOUT                 
#FUSES NOLVP                   
#FUSES NOCPD                   
#FUSES NOWRT                   
#FUSES RESERVED                 

#use delay(clock=4000000)
#use standard_io (B)

#define LED PIN_B6

#int_TIMER0
void  TIMER0_isr(void)
{
   output_toggle(LED);
   set_TIMER0(56);
}

void main() {
   set_tris_b(0b01111111);
   setup_timer_0(RTCC_INTERNAL|RTCC_DIV_2);
   enable_interrupts(INT_TIMER0);
   enable_interrupts(GLOBAL);
   set_TIMER0(56);
   while(TRUE)
   {
   }
}


It should overflow at exactly 400us, but i'm checking the signal on RB7 with an oscilloscope and it's about 447us WHY!? but if i change the values and try bigger overflow times like 32ms everything goes perfect, it's related to my relatively low 4Mhz Crystal?

P.D: I'm not trying to generate a square signal, i'm just checking if the timer is working ok.
Thanks in advance.
jeremiah



Joined: 20 Jul 2010
Posts: 1353

View user's profile Send private message

PostPosted: Wed Feb 13, 2019 7:44 pm     Reply with quote

Interrupts don't happen as instantaneously as you expect. There is a bit of latency before the ISR is triggered. You are most likely seeing this. Plus your output_toggle() will add a small bit of drift as well.
Ttelmah



Joined: 11 Mar 2010
Posts: 19537

View user's profile Send private message

PostPosted: Thu Feb 14, 2019 1:31 am     Reply with quote

It takes typically about 30uSec to get into an interrupt.
Your reset of the timer is then happening this long after the timer
interrupt triggers, plus the time to toggle the pin (five more instructions).
Then the actual timer update happens on the next tick of the timer.
Your time is a fraction slower than I'd have expected. It should be about 439uSec.
luisjoserod



Joined: 10 Jan 2019
Posts: 16
Location: Maracaibo, Venezuela

View user's profile Send private message Send e-mail Visit poster's website

PostPosted: Thu Feb 14, 2019 10:30 am     Reply with quote

Thanks guys!

I will avoid the use of Timers for my project,
following the recommendations on Ttelmah on
this other thread:
https://www.ccsinfo.com/forum/viewtopic.php?p=222513#222513
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