Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

A Clock in C

Status
Not open for further replies.

WernerSchwarz

Programmer
Jun 30, 2003
11
CH
Does anyone know how to make a for-loop that goes 1 Second no more, no less?

an idea:
for (x=0;x<10000;x++)
{
}

This should make the program wait a little while and then continue but i don't know what for a number i need for one Second.

 
Well depending on your Operating system and compiler, try one of

// in unix and other POSIX operating systems
sleep( 1 );

// in win32
Sleep( 1000 );

 
You can't do it using a for loop - because then it would depend on the speed on the PC - hence in the early 90s all the games that no-one could play as PC speeds took off.
You will need to use a while loop - and check the system time - waiting till 1second is over.

#include <time.h>

time_t tmTimeNow;
time(&tmTimeNow);

time_t tmTimeLater;

do
{
time(&tmTimeLater);
} while (tmTimeLater <= tmTimeNow + 1);

M.





Hollingside Technologies, Making Technology work for you.
 
Then I would suggest that you look inside whatever header files you have since it seems that you have neither an ANSI nor POSIX interface.

Does the GBA have any kind of operating system? A delay function of some sort is usually provided.

> for (x=0;x<10000;x++);
Will work, but you have to figure out the limit by trial and error. Also, its a good idea to make 'volatile int x;' just in case the optimiser decides that the loop does nothing and removes it.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top