An Easy Timer in C Language
An Easy Timer in C Language
An Easy Timer in C Language
com/print/51711
BLOG
#include <stdio.h>
#include <dos.h>
#include <conio.h>
void timer(float cycle)
{
float count=0.0;
int goLoop=1;
while(goLoop)
{
if(kbhit())
{
// Keypressed, exit
goLoop=0;
}else{
delay(100);
count+=0.1;
if(count> cycle)
{
// Time expired, exit
goLoop=0;
}
}
clrscr();
printf(“ The timer is %06.1f seconds\n”, cycle);
} // end for
} // end module
As we can see there is one “while” loop using the (blocking) library function delay(), (e.g. delay(1000) = 1second ;
delay(10) = 0,01 second), we here use a 1 tenth of second delay (delay(100)).
For each loop the variable count is increment by 1 tenth then an end of interval control is done (if(count> cycle)).
We used as well function kbhit() to stop the timer on demand ( with just a keypress).
Variable “cycle” is obviously a float type to get a 1 tenth of second resolution.
Morover timer value is output to video using printf with a format specifier %06,1f (e.g. 0014.5).
#ifndef __TIMER_H__
#define __TIMER_H__
1 di 2 23/04/2008 12.13
An easy timer in C language http://dev.emcelettronica.com/print/51711
#endif
In the main program #include “timer.h” must be included among the other header files and the module could be called
with the following instruction : timer (x); where x has been declared as float type.
Trademarks
2 di 2 23/04/2008 12.13