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

Accuracy problem using set_timer0 function

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



Joined: 11 Feb 2008
Posts: 5

View user's profile Send private message

Accuracy problem using set_timer0 function
PostPosted: Mon Feb 11, 2008 10:55 am     Reply with quote

Hello, I'm using the code below to get a 2,5 seconds account, but instead of 2,5s I get 3,40. I need nClocks as int16 to have enough accuracy in my program.

Where is the problem? The code seems good and easy.

Code:

#include <16LF84A.h>                               //pic a utilizar

#fuses XT
#fuses NOWDT
#fuses PROTECT
#fuses PUT

#use delay (clock=4000000)                         
#use fast_io(a)
#use fast_io(b)

#define LED PIN_A1                                 //LED de funcionamiento


int16 nClocks=0;


#INT_TIMER0
void Interrupcion_Timer0()
{
   set_timer0(156);
   nClocks++;                                     

   if(nClocks==25000)                                 
   {
      nClocks=0;
      output_toggle(LED);
   }
}


void main(void)
{
   disable_interrupts(GLOBAL);                     //deshabilitar interrupciones
   disable_interrupts(INT_EEPROM);                 //deshabilitar interrupcion EEPROM
   disable_interrupts(INT_RB);                     //deshabilitar interrupcion portb
   disable_interrupts(INT_EXT);                    //deshabilitar interrupcion externa
   
   ext_int_edge(H_TO_L);                           //interrupcion en flanco de bajada
   port_b_pullups(TRUE);                           //pullups portb activadas
   setup_TIMER_0(RTCC_INTERNAL | RTCC_DIV_1);     

   set_tris_a(0b00000000);                         //porta todo como salida
   set_tris_b(0b00000000);                         //portb todo como salida
   
   enable_interrupts(INT_TIMER0);                  //interrupcion del contador
   enable_interrupts(GLOBAL);                      //interrupciones activadas

   do
   {
     
   }while(TRUE);
}
Marc



Joined: 11 Feb 2008
Posts: 5

View user's profile Send private message

PostPosted: Mon Feb 11, 2008 10:58 am     Reply with quote

I forgot to say that if I delete the set_timer0(156); line I get the correct time:

T=256*25000*(4/4ยท10^6)=6,4 seconds
Ttelmah
Guest







PostPosted: Mon Feb 11, 2008 11:08 am     Reply with quote

How about searching the forum?....
Look for posts about the problems with setting a timer _to_ a value.
The problem is that timer0, is counting in instructions. By the time your code reaches the line to set the timer, typically something like _30_ instructions will have been executed since the last timeout. So instead of the 100 instructions to the 'next' interrupt, that you expect, the total, is more like 130.
There are a lot better approaches. What you are doing, results in a code, that will spend most of it's time in the interrupt. Have a look for code about RTC's, and in particular code posted (I think), by Neutone, which shows how to get accurate timings, without adjusting the timer value.

Best Wishes
Marc



Joined: 11 Feb 2008
Posts: 5

View user's profile Send private message

PostPosted: Mon Feb 11, 2008 11:26 am     Reply with quote

Thank you!! I'll look for for the Neutone's codes.
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