由于手机电视项目接收数据出现问题(每帧数据后半部分有丢失,并且每隔一帧就会有几帧丢失),无法在手机上正常播放,原因很可能与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)