gbadev.org forum archive

This is a read-only mirror of the content originally found on forum.gbadev.org (now offline), salvaged from Wayback machine copies. A new forum can be found here.

DS development > help with timers

#148887 - darknut101 - Fri Jan 11, 2008 6:46 pm

Can someone please direct me to a tutorial or some information on timers with libnds. devkitpro doesn't have any useful examples and I can't find any tutorials that cover them in the slightest manner. Thanks.

#148889 - eKid - Fri Jan 11, 2008 6:59 pm

timers are rather simple :)
Code:

// example to set up a millisecond timer

// a millisecond counter
volatile int milliseconds=0;

void timer0_function( void )
{
    // increment milliseconds
    milliseconds++;
}

void main( void )
{
    // initialize interrupts
    irqInit();

    // setup timer0 interrupt
    irqSet( IRQ_TIMER0, timer0_function );

    // set frequency of timer 0 to 1000hz (1 second / 1000)
    TIMER0_DATA = TIMER_FREQ(1000);

/* TIMERx_DATA sets the frequency of timer X (X being 0->3). Writing to it changes the frequency.
    TIMER_FREQ is a definition in libnds that divides the clock frequency by the Hz value given. Keep in mind that there is only a 16-bit counter so larger values may overflow.
*/

    // start timer0 with interrupt enabled
    TIMER0_CR = TIMER_ENABLE | TIMER_IRQ_REQ;

/* TIMER_ENABLE starts the timer
    TIMER_IRQ_REQ is the bit that enables the interrupt.
*/
   
    // ....
    // do other things
}


gbatek has a good reference how to control the timers (and everything else) - http://nocash.emubase.de/gbatek.htm

#148893 - darknut101 - Fri Jan 11, 2008 7:12 pm

thanks a lot. that answered all my questions.