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

Settings timer1 for have 20µs delay

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



Joined: 21 Dec 2011
Posts: 42

View user's profile Send private message

Settings timer1 for have 20µs delay
PostPosted: Fri Jan 27, 2012 3:47 pm     Reply with quote

Hi,

I have a question about the timer utilisation....
Timer1 16 bits, 2^16=65536.
20Mhz-> 1 /20Mhz=50ns....x4=200ns.

>>So 65536-64536=100.....100*200ns=20µs.... in theory...
In fact, with a scope, i have 30.40µs...
>> (With timer0, i have the same problem)

If someone could say me why ???

Code:

#include <18F4523.h>
#fuses NOWDT, HS
#use delay(clock=20000000)

//-------------------------------------------------------------
#int_timer1
void interruption_20us()
{
   set_timer1(65436);
   
   output_high(pin_a0);
   output_low(pin_a0);
}
//-------------------------------------------------------------
void main(void)
{
   set_timer1(65436);
   setup_timer_1(T1_internal|T1_DIV_BY_1);

   enable_interrupts(int_timer1);
   enable_interrupts(global);

   for(;;)
   {
   }
}
//-------------------------------------------------------------
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Fri Jan 27, 2012 4:04 pm     Reply with quote

Read this thread:
http://www.ccsinfo.com/forum/viewtopic.php?t=43399
Mike Walne



Joined: 19 Feb 2004
Posts: 1785
Location: Boston Spa UK

View user's profile Send private message

Timer error
PostPosted: Sat Jan 28, 2012 4:13 am     Reply with quote

As PCM programmer points out, most of your timing error is due to the time taken to get into the interrupt routine.

I tested your program with MPLAB SIM and got a repeat rate of 150 instruction cycles, which is near enough your value for me.

I then replaced your interrupt line
Code:

   set_timer1(65436);


with the lines
Code:

signed int16 x;
 
   x = get_timer1();
   x-=100;
   set_timer1(x);


The repeat rate is now 113 instruction cycles. The error is now essentially the time taken to process the timer1 register. I haven't had time to test it with a 'scope, but I usually find that the MPLAB SIM is out by a few cycles so have to adjust empirically on the real hardware.

Mike
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