|
|
View previous topic :: View next topic |
Author |
Message |
Guest
|
Timer2 calculate... |
Posted: Fri Mar 06, 2009 12:09 pm |
|
|
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
|
|
Posted: Fri Mar 06, 2009 2:34 pm |
|
|
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
|
|
Posted: Fri Mar 06, 2009 3:59 pm |
|
|
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
|
|
Posted: Fri Mar 06, 2009 5:21 pm |
|
|
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
|
|
Posted: Sat Mar 07, 2009 2:49 am |
|
|
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 |
|
|
|
|
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
|