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 configuration

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



Joined: 14 Oct 2010
Posts: 12

View user's profile Send private message

Timer0 configuration
PostPosted: Thu Apr 21, 2011 10:42 pm     Reply with quote

Hello everyone,

I'm using PIC18LF4550 with a 20MHz crystal. My #fuses are:

Code:
//configure a 20MHz crystal to operate at 48MHz
#fuses HSPLL,NOWDT,NOPROTECT,NOLVP,NODEBUG,USBDIV,PLL5,CPUDIV1,VREGEN
#use delay(clock=48M)


I need time interrupt every 1 millisecond and my question what is the difference between timer0 configure the following ways:

Code:
setup_timer_0(RTCC_INTERNAL | RTCC_DIV_32); // y
set_timer0(65161); // x
enable_interrupts(INT_TIMER0);


Code:
setup_timer_0(RTCC_INTERNAL | RTCC_DIV_1); // y
set_timer0(53536); // x
enable_interrupts(INT_TIMER0);


I made this setting based on the calculation:

Code:
Frequency(Hz) = 48Mhz/(4 * RTCC_DIV_y * (65536 - setup_timer0(x))) // 65536 -> 16bits


For the record, my ISR function is as follows:

Code:
#int_timer0
void timer0_isr()
{
   milsec++;
   set_timer0(65161);

   flag_1ms = TRUE;
   if(milsec >= 1000)
   {
      milsec = 0;
      flag_1s = TRUE;
   }   
}
Ttelmah



Joined: 11 Mar 2010
Posts: 19538

View user's profile Send private message

PostPosted: Fri Apr 22, 2011 5:08 am     Reply with quote

How much accuracy do you want?.
When you enter an ISR, it takes _time_.
The count will no longer be '0' when you reach your code.
When you load a timer value, the prescaler counter is reset to 0.
So if you actually ant '1mSec', the code won't work. You will lose a few counts every time the interrupt is called. How many will be changed if any other interrupt is occurring, so accuracy will not be very good.
The loss will change with the setup option. If you use /1, the counter will probably have advanced by perhaps 30 counts. You might actually consider correcting for this, by _adding_ your required preload to the value in the timer register. Potentially more accurate. Using /32, the clock might just have counted by 1, or not, if not,t the prescaler might have perhaps 31 'counts' in it at the moment of the load. So the clock will gain this amount.

The _best_ timer for an accurate count, is timer2. You want a count of 48000 clock cycles between interrupts. With a prescaler of '4', and the built in Fosc/4, you then want 3000 counts.
With timer2, set to use '199' as it's reset count, and a postscaler of 15, between interrupts, you will get:

4*4*(199+1)*15 = 48000 clock cycles between interrupts

an exact count, without having to reload the timer, and hence an accurate clock.

Best Wishes
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