#174258 - relminator - Wed May 26, 2010 4:13 am
Can anyone tell me why this code does not work?
Thanks!!!
Code: |
#include <nds.h>
#include <stdio.h>
int main(void)
{
//initialize the DS Dos-like functionality
consoleDemoInit();
//set frame buffer mode 0
videoSetMode(MODE_0_2D);
timerStart(0, ClockDivider_1, 65535, NULL);
timerStart(1, ClockDivider_64, 65535, NULL);
timerStart(2, ClockDivider_256, 65535, NULL);
timerStart(3, ClockDivider_1024, 65535, NULL);
while(1)
{
swiWaitForVBlank();
consoleClear();
iprintf("\x1b[1;1HTimer 0 = %i", timerElapsed(0));
iprintf("\x1b[3;1HTimer 1 = %i", timerElapsed(1));
iprintf("\x1b[5;1HTimer 2 = %i", timerElapsed(2));
iprintf("\x1b[7;1HTimer 3 = %i", timerElapsed(3));
}
return 0;
}
|
_________________
http://rel.betterwebber.com
#174260 - elhobbs - Wed May 26, 2010 5:52 am
You cannot run TimerStart 4 times. It actually uses 2 timers each time. The one you requested as the parameter plus the next one. If you are using wifi then you can only one instance as wifi uses timer 3.
#174261 - relminator - Wed May 26, 2010 6:05 am
Thanks but....
Code: |
#include <nds.h>
#include <stdio.h>
int main(void)
{
//initialize the DS Dos-like functionality
consoleDemoInit();
//set frame buffer mode 0
videoSetMode(MODE_0_2D);
timerStart(0, ClockDivider_256, 65535, NULL);
while(1)
{
swiWaitForVBlank();
consoleClear();
iprintf("\x1b[1;1HTimer 0 = %i", timerElapsed(0));
}
return 0;
}
|
Doesn't work either.
_________________
http://rel.betterwebber.com
#174263 - elhobbs - Wed May 26, 2010 6:54 am
my bad, I was thinking of cpuStartTiming.
I am not sure what 65535 will do as the tick parameter. I think you may want to use timerFreqToTicks_256 (or one of the other divider variants) to generate this value from a frequency.
#174265 - relminator - Wed May 26, 2010 9:26 am
Thanks!!! I'll try that tomorrow when I get some coding time.
BTW, do you have an example of a timer usage that counts every second or so? I just want to profile my code.
_________________
http://rel.betterwebber.com
#174266 - Pete_Lockwood - Wed May 26, 2010 10:42 am
timerStart(1, ClockDivider_1024, 32768, fn_RunsEverySecond);
_________________
It's not an illusion, it just looks like one.
#174270 - relminator - Wed May 26, 2010 1:49 pm
Pete_Lockwood wrote: |
timerStart(1, ClockDivider_1024, 32768, fn_RunsEverySecond); |
So I have to use a callback function to get values from the timer?
_________________
http://rel.betterwebber.com
#174274 - elhobbs - Wed May 26, 2010 4:12 pm
no, you do not need so use the callback. it looks like cpuStartTiming and cpuEndTiming may already do what you want.
timers work in 2 different modes - the normal mode is count down and other mode is count up. timerStart puts the timer in count down mode and that is really more suitable for scheduling a callback at a certain frequency rather than measuring time - though it is not impossible. count up mode requires two timers. take a look at the source code - the functions to look at (cpuStartTiming and cpuEndTiming) are at the bottom of this file http://devkitpro.svn.sourceforge.net/viewvc/devkitpro/trunk/libnds/source/common/timers.c?revision=3412&view=markup
cpuStartTiming sets up a high precision timer running at 33.513982 MHz
#174288 - Rajveer - Wed May 26, 2010 10:01 pm
How exactly would you use timers for code profiling? I've never used timers so as a test I included timers.h and wrapped my main while loop with the following code:
Beginning
Code: |
u32 timePassed;
cpuStartTiming(0); |
Ending
Code: |
timePassed = cpuEndTiming();
printf("\nTime %u", timePassed);
while(1){} |
and I get values ranging from 450781 to 553608 whenever I start the app and it finishes 1 cycle of the loop. Is this the number of instructions that have passed or something, and is it expected that you'd get a range of values?
#174289 - elhobbs - Wed May 26, 2010 11:06 pm
cpuEndTiming returns the number of timer ticks that have elapsed since the timer was started. since the timer is running at 33.513982 MHz if you divide by 33514 you will get the number of milliseconds that have elapsed. so for instance 450781/33514 = ~13ms. the other value is ~16ms. these values sound about right as at the ds display rate of 60Hz each frame is 16.66ms long.
#174290 - Rajveer - Wed May 26, 2010 11:48 pm
Aha excellent thanks for that, about time I started profiling some of my code too. Just for testing I tried timing 100 multiplications and I got between 99 and 130. I guess I can't always expect the same value as during program execution some things aren't constant, such as memory access times e.t.c?
#174291 - elhobbs - Thu May 27, 2010 12:37 am
no, you probably will not get the same exact value every time, cache hits etc.
#174295 - relminator - Thu May 27, 2010 1:08 am
Thanks guys!
_________________
http://rel.betterwebber.com