-
sand7000
- Posts: 7
- Joined: Fri Oct 02, 2009 9:52 pm
Post
by sand7000 » Sun Oct 04, 2009 11:07 pm
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 );
}
}
Last edited by
sand7000 on Wed Oct 07, 2009 1:26 pm, edited 1 time in total.
"For a variety of reasons, Fortran is the best and easiest programming language." ~Sooves (a physics professor who had never tried any programming language other than Fortran)
-
Copper
- Posts: 7
- Joined: Sun Dec 28, 2008 9:34 pm
Post
by Copper » Mon Oct 05, 2009 1:20 pm
Perhaps remove irqInit(); if you're using last versions (libnds 1.3.x)
-
sand7000
- Posts: 7
- Joined: Fri Oct 02, 2009 9:52 pm
Post
by sand7000 » Tue Oct 06, 2009 2:09 am
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.
"For a variety of reasons, Fortran is the best and easiest programming language." ~Sooves (a physics professor who had never tried any programming language other than Fortran)
-
sand7000
- Posts: 7
- Joined: Fri Oct 02, 2009 9:52 pm
Post
by sand7000 » Tue Oct 06, 2009 10:20 pm
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);
}
}
"For a variety of reasons, Fortran is the best and easiest programming language." ~Sooves (a physics professor who had never tried any programming language other than Fortran)
-
elhobbs
- Posts: 358
- Joined: Thu Jul 02, 2009 1:19 pm
Post
by elhobbs » Tue Oct 06, 2009 11:56 pm
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++;
}
}
-
sand7000
- Posts: 7
- Joined: Fri Oct 02, 2009 9:52 pm
Post
by sand7000 » Wed Oct 07, 2009 1:29 pm
(*slaps forehead) Argh, I hate when I broadcast my own silly math mistakes out to the world.
Thanks elhobbs!
"For a variety of reasons, Fortran is the best and easiest programming language." ~Sooves (a physics professor who had never tried any programming language other than Fortran)
Who is online
Users browsing this forum: Ahrefs [Bot] and 1 guest