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

timer0

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







timer0
PostPosted: Sun Nov 19, 2006 2:42 pm     Reply with quote

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







PostPosted: Sun Nov 19, 2006 3:31 pm     Reply with quote

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







PostPosted: Sun Nov 19, 2006 4:02 pm     Reply with quote

ok thx
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