khldragon

摘要:

以ms为单位,获取系统时间、睡眠或延迟时间函数的使用方法。

 1 #include<stdio.h>
 2 #include <time.h>
 3 #include <sys/time.h>
 4 #include <unistd.h>
 5 
 6 typedef unsigned int uint32_t;
 7 #define csp_sleep_ms(time_ms) usleep(time_ms * 1000);
 8 
 9 
10 uint32_t csp_get_ms() {
11     struct timespec ts;
12 
13     if (clock_gettime(CLOCK_MONOTONIC, &ts) == 0) {
14         return (ts.tv_sec*1000+ts.tv_nsec/1000000);
15     } else {
16         return 0;
17     }
18 }
19 
20 int main()
21 {
22     uint32_t start = csp_get_ms();
23     csp_sleep_ms(1000);
24     uint32_t time = csp_get_ms()-start;
25     printf(" Reply in %u ms\r\n", time);
26     return 0;
27 }
View Code

重要函数说明:

参加百度百科:https://baike.baidu.com/item/clock_gettime

 

%u:表示无符号十进制整数。

分类:

技术点:

相关文章: