C中时间函数的奇怪问题
weird issue with time function in C
我正在尝试让我的 Beaglebone Green 从某个引脚读取输入,当该引脚看到 1 到 0 的转换(在 pulse() 中定义)时,它应该设置一个计时器。
我想像这样制作我的计时器:
空闲时,计时器设置为 TIMER_HOLD
值 (900000)
为了启动计时器,我将其设置为低于 TIMER_HOLD 的值。
然后它应该递减(使用 timerupdate()
函数)。
如果它达到 0 或更小 - 对于 unsigned long 高于 TIMER_HOLD - 然后执行触发操作。
为此,我添加了:printf("timer 1: %lld \n",timer[1]);
test/debug
这很完美......我看到了这个输出:
timer 1: 900000
timer 1: 900000
PULSE
timer 1: 5000
timer 1: 4990
timer 1: 4975
..........<truncated>
timer 1: 1
timer 1: 1
timer 1: 1
timer 1: 1
timer 1: 1
timer 1: 0
timer 1: 0
timer 1: 0
timer 1: 0
timer 1: 0
timer 1: 0
timer 1: -1
TIMER TRIGGER
timer 1: 900000
timer 1: 900000
timer 1: 900000
timer 1: 900000
这是我的 C 代码:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/timeb.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <assert.h>
#include <string.h>
#include "../../BBBio_lib/BBBiolib.h"
#include <linux/kernel.h>
#include <sys/sysinfo.h>
#define INPUT_LEN 65
#define TIMER_HOLD 900000 // ms
void pin2array();
void timerupdate(void);
unsigned char pulse(unsigned char chkin);
unsigned char x,y;
unsigned char input[6][INPUT_LEN];
unsigned long long timer[10];
unsigned long long skew, prevtime, currtime;
int main(void)
{
for (x=0;x<10;x++) timer[x]=TIMER_HOLD;
iolib_init();
while(1)
{
pin2array();
timerupdate();
if (pulse(0))
{
printf("PULSE\n");
timer[1]=5000;
}
printf("timer 1: %lld \n",timer[1]); //THIS IS THE DEBUG PRINTF !!!
if (timer[1]>TIMER_HOLD)
{
printf("\nTIMER TRIGGER\n");
timer[1]=TIMER_HOLD;
// usleep(50000);
}
usleep(1); // CPU savings
}
iolib_free();
return(0);
}
void timerupdate(void)
{
struct timeval tv;
gettimeofday(&tv, NULL);
unsigned long long millisecondsSinceEpoch=
(unsigned long long)(tv.tv_sec) * 1000 +
(unsigned long long)(tv.tv_usec) / 1000;
currtime=millisecondsSinceEpoch;
skew=currtime-prevtime;
prevtime=currtime;
for (x=0;x<10;x++)
{
if (timer[x]<TIMER_HOLD) timer[x]-=skew;
}
}
unsigned char pulse(unsigned char chkin)
{
if (input[0][chkin]==0 && input[1][chkin]==1 && input[2][chkin]==1 && input[3][chkin]==1 && input[4][chkin]==1 && input[5][chkin]==1) return 1;
else return 0;
}
void pin2array()
{
for (x=0;x<INPUT_LEN;x++) input[5][x]=input[4][x];
for (x=0;x<INPUT_LEN;x++) input[4][x]=input[3][x];
for (x=0;x<INPUT_LEN;x++) input[3][x]=input[2][x];
for (x=0;x<INPUT_LEN;x++) input[2][x]=input[1][x];
for (x=0;x<INPUT_LEN;x++) input[1][x]=input[0][x];
input[0][0]=(is_high(8,7));
input[0][1]=(is_high(8,8));
input[0][2]=(is_high(8,9));
input[0][3]=(is_high(8,10));
}
现在奇怪的是:
当我删除 printf("timer 1: %lld \n",timer[1]);
那我只看到
PULSE
关于输出...计时器永远不会触发...
知道发生了什么事吗?
系统信息:
root@beaglebone:/# uname -na
Linux beaglebone 4.4.9-ti-r25 #1 SMP Thu May 5 23:08:13 UTC 2016 armv7l GNU/Linux
root@beaglebone:/# gcc --version
gcc (Debian 4.9.2-10) 4.9.2
我通过阅读 /proc/uptime 来修复它,然后将其乘以 10(十分之一秒的分辨率对我的程序来说足够了)
我认为原来的东西因为舍入错误或浮点数到整数的转换而无法工作....不过不确定
我正在尝试让我的 Beaglebone Green 从某个引脚读取输入,当该引脚看到 1 到 0 的转换(在 pulse() 中定义)时,它应该设置一个计时器。 我想像这样制作我的计时器:
空闲时,计时器设置为 TIMER_HOLD
值 (900000)
为了启动计时器,我将其设置为低于 TIMER_HOLD 的值。
然后它应该递减(使用 timerupdate()
函数)。
如果它达到 0 或更小 - 对于 unsigned long 高于 TIMER_HOLD - 然后执行触发操作。
为此,我添加了:printf("timer 1: %lld \n",timer[1]);
test/debug
这很完美......我看到了这个输出:
timer 1: 900000
timer 1: 900000
PULSE
timer 1: 5000
timer 1: 4990
timer 1: 4975
..........<truncated>
timer 1: 1
timer 1: 1
timer 1: 1
timer 1: 1
timer 1: 1
timer 1: 0
timer 1: 0
timer 1: 0
timer 1: 0
timer 1: 0
timer 1: 0
timer 1: -1
TIMER TRIGGER
timer 1: 900000
timer 1: 900000
timer 1: 900000
timer 1: 900000
这是我的 C 代码:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/timeb.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <assert.h>
#include <string.h>
#include "../../BBBio_lib/BBBiolib.h"
#include <linux/kernel.h>
#include <sys/sysinfo.h>
#define INPUT_LEN 65
#define TIMER_HOLD 900000 // ms
void pin2array();
void timerupdate(void);
unsigned char pulse(unsigned char chkin);
unsigned char x,y;
unsigned char input[6][INPUT_LEN];
unsigned long long timer[10];
unsigned long long skew, prevtime, currtime;
int main(void)
{
for (x=0;x<10;x++) timer[x]=TIMER_HOLD;
iolib_init();
while(1)
{
pin2array();
timerupdate();
if (pulse(0))
{
printf("PULSE\n");
timer[1]=5000;
}
printf("timer 1: %lld \n",timer[1]); //THIS IS THE DEBUG PRINTF !!!
if (timer[1]>TIMER_HOLD)
{
printf("\nTIMER TRIGGER\n");
timer[1]=TIMER_HOLD;
// usleep(50000);
}
usleep(1); // CPU savings
}
iolib_free();
return(0);
}
void timerupdate(void)
{
struct timeval tv;
gettimeofday(&tv, NULL);
unsigned long long millisecondsSinceEpoch=
(unsigned long long)(tv.tv_sec) * 1000 +
(unsigned long long)(tv.tv_usec) / 1000;
currtime=millisecondsSinceEpoch;
skew=currtime-prevtime;
prevtime=currtime;
for (x=0;x<10;x++)
{
if (timer[x]<TIMER_HOLD) timer[x]-=skew;
}
}
unsigned char pulse(unsigned char chkin)
{
if (input[0][chkin]==0 && input[1][chkin]==1 && input[2][chkin]==1 && input[3][chkin]==1 && input[4][chkin]==1 && input[5][chkin]==1) return 1;
else return 0;
}
void pin2array()
{
for (x=0;x<INPUT_LEN;x++) input[5][x]=input[4][x];
for (x=0;x<INPUT_LEN;x++) input[4][x]=input[3][x];
for (x=0;x<INPUT_LEN;x++) input[3][x]=input[2][x];
for (x=0;x<INPUT_LEN;x++) input[2][x]=input[1][x];
for (x=0;x<INPUT_LEN;x++) input[1][x]=input[0][x];
input[0][0]=(is_high(8,7));
input[0][1]=(is_high(8,8));
input[0][2]=(is_high(8,9));
input[0][3]=(is_high(8,10));
}
现在奇怪的是:
当我删除 printf("timer 1: %lld \n",timer[1]);
那我只看到
PULSE
关于输出...计时器永远不会触发...
知道发生了什么事吗?
系统信息:
root@beaglebone:/# uname -na
Linux beaglebone 4.4.9-ti-r25 #1 SMP Thu May 5 23:08:13 UTC 2016 armv7l GNU/Linux
root@beaglebone:/# gcc --version
gcc (Debian 4.9.2-10) 4.9.2
我通过阅读 /proc/uptime 来修复它,然后将其乘以 10(十分之一秒的分辨率对我的程序来说足够了)
我认为原来的东西因为舍入错误或浮点数到整数的转换而无法工作....不过不确定