KilluaZoldyck 2020-02-02
摘要:
以ms为单位,获取系统时间、睡眠或延迟时间函数的使用方法。
#include<stdio.h>
#include <time.h>
#include <sys/time.h>
#include <unistd.h>
typedef unsigned int uint32_t;
#define csp_sleep_ms(time_ms) usleep(time_ms * 1000);
uint32_t csp_get_ms() {
struct timespec ts;
if (clock_gettime(CLOCK_MONOTONIC, &ts) == 0) {
return (ts.tv_sec*1000+ts.tv_nsec/1000000);
} else {
return 0;
}
}
int main()
{
uint32_t start = csp_get_ms();
csp_sleep_ms(1000);
uint32_t time = csp_get_ms()-start;
printf(" Reply in %u ms\r\n", time);
return 0;
}重要函数说明:
参加百度百科:https://baike.baidu.com/item/clock_gettime
%u:表示无符号十进制整数。