Page 1 of 1
Trouble with timers.h (SOLVED)
Posted: Sun Oct 04, 2009 11:07 pm
by sand7000
I would like to create a timer that will tell me how many milliseconds have elapsed. This is apparently much more complicated than I expected as CLOCKS_PER_SEC and clock() from time.h do not function properly on my ds. From what I have scraped together in my internet search I wrote the following code, but it doesn't update the count. Any suggestions?
Code: Select all
#include <nds.h>
#include <stdlib.h>
#include <stdio.h>
using namespace std;
volatile int milliseconds=0;
void timer0_function( void )
{
milliseconds++;
}
int main()
{
consoleDemoInit();
irqInit();
irqSet( IRQ_TIMER0, timer0_function );
TIMER0_DATA = TIMER_FREQ(1000);
TIMER0_CR = TIMER_ENABLE | TIMER_IRQ_REQ;
while(true)
{
printf( "%i\n" , milliseconds );
}
}
Re: Trouble with timers.h
Posted: Mon Oct 05, 2009 1:20 pm
by Copper
Perhaps remove irqInit(); if you're using last versions (libnds 1.3.x)
Re: Trouble with timers.h
Posted: Tue Oct 06, 2009 2:09 am
by sand7000
I tried removing irqInit(); and it did not change anything. I fell like i need the timerStart() called at some point but I don't understand the docs at
http://libnds.devkitpro.org/a00102.html. What I would really like is an example of a millisecond timer if anyone can get one to compile.
Re: Trouble with timers.h
Posted: Tue Oct 06, 2009 8:20 am
by elhobbs
you are just missing a call to enable the irq after the timer is setup
you may want to take a look at the following thread as this setup has been known to cause issues
http://forum.gbadev.org/viewtopic.php?t ... highlight=
Re: Trouble with timers.h
Posted: Tue Oct 06, 2009 10:20 pm
by sand7000
Thanks again elhobbs! Adding irqEnable(IRQ_TIMER0); does fix my code.
However, I cannot get the method suggested in the link you provided to work. My attempt is below, and milliseconds doesn't get updated. I tried adding irqSet(IRQ_TIMER0); but it requires that the second argument be a function. If I give it a function then I am still calling interrupts like crazy so what is the point?
Code: Select all
#include <nds.h>
#include <stdlib.h>
#include <stdio.h>
using namespace std;
int main()
{
consoleDemoInit();
TIMER0_CR = TIMER_ENABLE|TIMER_DIV_1024;
TIMER1_CR = TIMER_ENABLE|TIMER_CASCADE;
irqEnable(IRQ_TIMER0);
irqEnable(IRQ_TIMER1);
double milliseconds;
while(true)
{
milliseconds = ((TIMER1_DATA*(1<<16))+TIMER0_DATA)/32728500;
printf("%E\n",milliseconds);
}
}
Re: Trouble with timers.h
Posted: Tue Oct 06, 2009 11:56 pm
by elhobbs
neither irqSet or irqEnable are required for the second approach. if you had waited 1000 seconds you would have seen milliseconds change... the original code was for seconds - so multiple by a 1000 not divide. I tested this code and it works
Code: Select all
#include <nds.h>
#include <stdlib.h>
#include <stdio.h>
//---------------------------------------------------------------------------------
int main(void) {
int i = 0;
double milliseconds;
//---------------------------------------------------------------------------------
consoleDemoInit();
TIMER0_CR = TIMER_ENABLE|TIMER_DIV_1024;
TIMER1_CR = TIMER_ENABLE|TIMER_CASCADE;
while(1) {
milliseconds = ((TIMER1_DATA*(1<<16))+TIMER0_DATA)/32.7285;
printf("%d %f\n",i*1000/60,(float)milliseconds);
swiWaitForVBlank();
i++;
}
}
Re: Trouble with timers.h (SOLVED)
Posted: Wed Oct 07, 2009 1:29 pm
by sand7000
(*slaps forehead) Argh, I hate when I broadcast my own silly math mistakes out to the world.
Thanks elhobbs!