C语言定时1分钟程序,C语言实现毫秒级定时

由于手机电视项目接收数据出现问题(每帧数据后半部分有丢失,并且每隔一帧就会有几帧丢失),无法在手机上正常播放,原因很可能与SPI接口的速率(与接口驱动相关)与硬件速率不匹配造成的。为此,需要测量接收每个复用子帧所需要的时间。我在www.cplusplus.com网站上找到了一个可用的例子,例子在附录部分附上。另外,在网上搜到了如下的代码,可以实现毫秒级的定时,用于测试程序执行所需要的时间。

#include

#include

#include

int main( void )

{

long i = 10000000L;

clock_t start, finish;

double duration;

/* 测量一个事件持续的时间*/

printf( "Time to do %ld empty loops is ", i );

start = clock();

while( i-- )

finish = clock();

duration = (double)(finish - start) / CLOCKS_PER_SEC;

printf( "%f seconds/n", duration );

system("pause");

}

程序执行结果:

Time to do 10000000 empty loops is 0.421000 seconds

请按任意键继续...

基于上面的例子,我把附录中的程序进行了简单的修改,可以实现毫秒级的延时。

程序代码如下:

#include

#include

#include

//实现毫秒级的延时函数

void wait(float seconds)

{

clock_t endwait;

endwait = clock() + seconds * CLOCKS_PER_SEC;

while (clock() < endwait)

{

}

}

int main()

{

int n;

long i = 5000000L;

double time_elapsed;

clock_t begin_time;

clock_t end_time;

begin_time = clock();

printf("Starting countdown.../n");

for (n = 10; n > 0; n--)

{

printf("%d/n", n);

wait(0.5);

}

printf("FIRE!!!/n");

while( i-- )

{

if (i % 1000000 == 0)

{

printf("fire.../n");

wait(0.3);

}

}

end_time = clock();

time_elapsed = (double)(end_time - begin_time) / CLOCKS_PER_SEC;

printf(" Time elapsed: %f./n Clock ticks: %d/n", time_elapsed, end_time - begin_time);

//system("notepad note.txt");    //execute notepad program.

system("pause");

return 0;

}

程序执行结果:

Starting countdown...

10

9

8

7

6

5

4

3

2

1

FIRE!!!

fire...

fire...

fire...

fire...

fire...

Time elapsed: 11.562000.

Clock ticks: 11562

请按任意键继续. . .

======================附录====================

clock

function

clock_t clock ( void );

Clock program

Returns the number of clock ticks elapsed since the program was launched.

The macro constant expression CLOCKS_PER_SEC specifies the relation between a clock tick and a second (clock ticks per second).

The initial moment of reference used by clock as the beginning of the program execution may vary between platforms. To calculate the actual processing times of a program, the value returned by clock should be compared to a value returned by an initial call to clock.

Parameters

(none)

Return Value

The number of clock ticks elapsed since the program start.

On failure, the function returns a value of -1.

clock_tis a type defined in to some type capable of representing clock tick counts and support arithmetical operations (generally a long integer).

Example

/* clock example: countdown */ #include #include void wait ( int seconds ) { clock_t endwait; endwait = clock () + seconds * CLOCKS_PER_SEC ; while (clock() < endwait) {} } int main () { int n; printf ("Starting countdown.../n"); for (n=10; n>0; n--) { printf ("%d/n",n); wait (1); } printf ("FIRE!!!/n"); return 0; }

Output:

Starting countdown...

10

9

8

7

6

5

4

3

2

1

FIRE!!!

See also

Get current time (function)

Return difference between two times (function)

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值