首页 > 编程知识 正文

linuxc语言获取时间函数,c语言一毫秒的延时程序

时间:2023-05-05 23:39:48 阅读:276082 作者:2847

该楼层疑似违规已被系统折叠 隐藏此楼查看此楼

#include

#include

#include

#include

#define B break

void de(int timee)

{

unsigned long int start,stop;

struct timeval str;

struct timeval stp;

gettimeofday(&str,NULL);

start=str.tv_usec;

while(1)

{ gettimeofday(&stp,NULL);

stop=stp.tv_usec;

if((stop-start)>timee)

B;

}

}

void tune(int tme)

{

int i;

for(i=0;i<200000/tme;i++)

{

digitalWrite(0,HIGH);de(tme);

digitalWrite(0,LOW);de(tme);

}

}

int main()

{

wiringPiSetup();

pinMode(0,OUTPUT);

char num;

while(1)

{

scanf("%c",&num);

printf("%c",num);

switch (num)

{

case '0' : delay(10);B;

case 'c' : tune(1465);B;

case 'd' : tune(1302);B;

case 'e' : tune(1172);B;

case 'f' : tune(1142);B;

case 'g' : tune(977);B;

case 'a' : tune(868);B;

case 'b' : tune(781);B;

}

}

return 0;

}

这些是源代码,小伙伴们帮忙看看怎样提高效率和精度,尤其是延时de()那块。这样好浪费cpu资源啊。运行时cpu的一个核都满载了。。

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。