[En-Nut-Discussion] Timers running slow

William Baker bbaker at priefert.com
Fri Apr 28 19:22:24 CEST 2006


Here is a section of code that does what you want, and I believe this is 
what Harald Kipp was referring to.  This appears to get RT precision in 
my threads.  This is mostly working pseudo-code.  Comments are welcome, 
especially if anyone sees any bad practices.

bbaker
-------------------------------------------------

HANDLE       laser_event;

THREAD(LaserService, arg) {
    for (;;) {      
        NutEventWait( &laser_event, NUT_WAIT_INFINITE );
        .. process thread logic
    }
}

SIGNAL( SIG_OVERFLOW1 ) {
        NutEventPostAsync( &laser_event );
}

main() {
    TCCR1B = 1;       // Clock frequency, prescaler is 1

    //timer_enable_int( _BV(TOIE1));      -- another way to do the same 
thing
    sbi(TIMSK, TOIE1);

        NutEventPost( &laser_event );  // initialize the semaphore
        NutThreadCreate( thname, LaserService, (void *) 
(uptr_t)&laser_event, 512 ); // NUT_THREAD_MAINSTACK);

}

>Hello everyone,
>
>I'm having some trouble getting a simple timer to run on in my
>application (a modified version of httpserv).  The timer does run, but
>much slower than it should (approx 2-3 times too slow).  My timer
>callback function is this:
>
>void TimerCallback(HANDLE timer, void *arg)
>{
>    PORTB^=0x08;
>}
>
>this timer is started as follows:
>
>timer1 = NutTimerStart(500, TimerCallback, NULL, 0);
>
>Am I doing something totally stupid here?  I've also got another thread
>running:
>
>THREAD (blinky, arg)
>{
> DDRB|=0x0F;
> char  i;
> for(;;)
> {
>  for(i=0;i<blinks;i++)
>   {
>   PORTB|=0x01;
>   NutSleep(125);
>   PORTB&=~0x01;
>   NutSleep(125);
>   }
>   NutSleep(1000);
> }
>}
>
>blinks is a global variable set by other functions.  The important part
>here is that I have noticed that the time is off in this thread as well.
>The nutsleeps take ~2-3x longer than they should.  What might be causing
>these problems?
>
>Greg Wood
>
>  
>



More information about the En-Nut-Discussion mailing list