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 support@ccsinfo.com

Timer2 calculate...

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








Timer2 calculate...
PostPosted: Fri Mar 06, 2009 12:09 pm     Reply with quote

Hi

Is this right?
Ask because the timer make interrupt every 5,7ms and i expect 5ms.
The measurement is done with a logic analyzer.

CPU is running 8mhz.
PIC is 18xx2455.

Config timer:
Code:
setup_timer_2(T2_DIV_BY_16,78,8); //8 bit 1/8mhz*4 * 16*78*8=5ms


Reload at the interrupt.
Code:
set_timer2(0xff-78);
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Fri Mar 06, 2009 2:34 pm     Reply with quote

This test program gives me an interrupt every 4.99 ms.
Code:

#include <18F452.h>
#fuses HS,NOWDT,PUT,BROWNOUT,NOLVP
#use delay(clock=8000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)

#int_timer2
void timer2_isr(void)
{
output_toggle(PIN_B0);
}

//======================================
void main(void)
{
output_low(PIN_B0);

setup_timer_2(T2_DIV_BY_16, 77, 8);

clear_interrupt(INT_TIMER2);
enable_interrupts(INT_TIMER2);
enable_interrupts(GLOBAL);

while(1);
}
Ttelmah
Guest







PostPosted: Fri Mar 06, 2009 3:59 pm     Reply with quote

Two things apply.
First, the timer counts to the loaded value +1. For 78 counts, you need to load 77 as PCM programmer shows.
Second, is that the whole point of timer2, is that it resets _itself_ when it gets to the preset count. What you are doing, is that it gets to 78, resets itself to zero, and calls the interrupt handler. At this point, about 20uSec latter, inside the interrupt routine, you are loading it again, and by yet another count to much (value would want to be 0-77).
So your timings, will be long by two extra counts, and the time taken to get into the interrupt handler....
PCM, shows how to use this.

Best Wishes
Guest








PostPosted: Fri Mar 06, 2009 5:21 pm     Reply with quote

Hi

1000*thanks:-)

I misunderstand timer2. I use timer2 as timer1, and make the reload myself. And I used it as rolling round at ff+1 as timer1!
Sydney



Joined: 13 Feb 2009
Posts: 71

View user's profile Send private message

PostPosted: Sat Mar 07, 2009 2:49 am     Reply with quote

Code:
setup_timer_2(T2_DIV_BY_4, 249, 10); // 1/8000000*4*4*250*10=5.000ms


Not much in it but is slightly closer than yours if timing is an issue Smile
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