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

How to setup timer 0 for...

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



Joined: 30 Mar 2004
Posts: 8

View user's profile Send private message Visit poster's website

How to setup timer 0 for...
PostPosted: Mon Aug 16, 2004 12:38 am     Reply with quote

How to setup timer 0 for interrupt ever 2.8 mS at crystal 4 MHz?


Thanks
chingB



Joined: 29 Dec 2003
Posts: 81

View user's profile Send private message

Re: How to setup timer 0 for...
PostPosted: Mon Aug 16, 2004 1:56 am     Reply with quote

Somkiat wrote:
How to setup timer 0 for interrupt ever 2.8 mS at crystal 4 MHz?

Thanks


Try dis code below....

Code:


#include <18F452.H>

#fuses HS,NOPROTECT,NOWDT,NOLVP,PUT
#use delay(clock=4000000)


#int_timer0
void timer0_isr()
{
   < add your routine here... >

   set_timer0(62736);
   clear_interrupt(int_timer0);   
}


void init_mcu()
{
   // 4M/(4*1*65536) = 65.536mSec.
   setup_timer_0(RTCC_INTERNAL | RTCC_DIV_1);

   set_timer0(62736);
   clear_interrupt(int_timer0);
   enable_interrupts(int_timer0);
   
   enable_interrupts(global);
}


void main()
{
   init_mcu();
   // < add your routine here... >

   while (TRUE);
}



Hope this give you a startup....
Ttelmah
Guest







Re: How to setup timer 0 for...
PostPosted: Mon Aug 16, 2004 4:27 am     Reply with quote

Somkiat wrote:
How to setup timer 0 for interrupt ever 2.8 mS at crystal 4 MHz?


Thanks

You don't say what chip...
This matters, since the 16family, only have an 8bit timer0. You also need to give an idea of the accuracy needed. The problem is that setting the counter 'forward', as suggested in the code posted, has a large error, because the counter wll have allready incremented by perhaps 50 instruction times, by the time the interrupt handler is reached.
If accuracy really is important, it'd be better to use a slightly different clock frequency, or if your chip supports it, one of the CTC channels, and it's associated timer.
A count at 2.048mSec, is easy with the 8bit timer module, using a prescaler of 8.
Switching to using a prescaler of 16, and then in the interrupt, setting the counter 'forward', by 81, should be close.
So, specify the 'criteria' (accuracy needed, and the chip involved), and some better solutions may be available.

Best Wishes
Somkiat



Joined: 30 Mar 2004
Posts: 8

View user's profile Send private message Visit poster's website

PostPosted: Wed Aug 18, 2004 11:15 am     Reply with quote

I use PIC16F877-04/P

Thanks
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