|
|
View previous topic :: View next topic |
Author |
Message |
Codehunter Guest
|
timer0 |
Posted: Sun Nov 19, 2006 2:42 pm |
|
|
Hi all,
Just starded pic programming so forgive me when i tell something stupid.
i try to get timers working for the pic18f452
i use this to test my timer1
Code: |
#use delay(clock=20000000 )
...
setup_timer_1 ( T1_INTERNAL|T1_DIV_BY_1);
while(true)
{
counter = 0;
enable_interrupts(int_TIMER1);
delay_ms(1000);
disable_interrupts(int_TIMER1);
printf("timer1: %Lu \n \r",counter);
}
#INT_TIMER1
void timer1() // +- 76 times called per sec
{
++counter;
}
|
This print +- 76. I think this is correct because:
1 / (20/4) * 65535 => overflow after 13.107 ms
this is 76 overflows per sec.
To test timer0 i use the following:
Code: |
#use delay(clock=20000000 )
...
setup_timer_0 ( RTCC_INTERNAL| RTCC_8_BIT | RTCC_DIV_1 );
while(true)
{
counter = 0;
enable_interrupts(int_TIMER0);
delay_ms(1000);
disable_interrupts(int_TIMER0);
printf("timer0: %Lu \n \r",counter);
}
#INT_TIMER0
void timer1()
{
++counter;
}
|
this prints +- 29001
So it overflows 29001 per sec but if i do the same calculations as before, it has to overflow +- 19531 per second
1 / (20/4) * 256=> overflow after 51.2us
this is 19531 overflows per sec.
Someone who knows why i get this result?
thx in advance,
codehunter |
|
|
Ttelmah Guest
|
|
Posted: Sun Nov 19, 2006 3:31 pm |
|
|
The key is to remember that 'delay_ms', does no such thing.
It _counts, till the total machine cycles executed in the main program loop, correspond to those needed for 1 second of execution. 4million instruction cycles.
There is a significant overhead with interrupt handling (typically about 60 instructions for a single interrupt). Add the call to the handler, and the maths, and I'd guess a total of perhaps 80 to 85 instructions. This is happening every 256 processor instructions with the faster call, so the processor is spending perhaps 85/256ths of it's time handling the interrupt. The 'one second' timer, will then run for 1/(1-85/256) seconds. About 1.5 seconds. In 1.5 seconds, the interrupt will execute about 29000 times...
Your calculation for the first code, should use 65536 at the bottom (it resets at 65535, then counts on the next cycle, so there are 65536 actual counts). Here the error from the code execution is tiny. About 1.5mSec.
Best Wishes |
|
|
Codehunter Guest
|
|
Posted: Sun Nov 19, 2006 4:02 pm |
|
|
ok thx |
|
|
|
|
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
|