【问题标题】:weird issue with time function in CC中时间函数的奇怪问题
【发布时间】:2018-05-11 19:48:52
【问题描述】:

我试图让我的 Beaglebone Green 从某个引脚读取输入,当该引脚看到 1 到 0 的转换(在 pulse() 中定义)时,它应该设置一个计时器。 我想让我的计时器是这样的:

在空闲时,计时器设置为TIMER_HOLD 值 (900000) 要启动计时器,我将其设置为低于 TIMER_HOLD 的值。 然后它应该递减(使用timerupdate() 函数)。 如果它达到 0 或更小(对于 unsigned long 而言它高于 TIMER_HOLD),则执行触发操作。

为了测试/调试这个,我添加了:printf("timer 1: %lld \n",timer[1]); 这很完美......我看到了这个输出:

  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

【问题讨论】:

  • 您在删除printf() 时是否尝试过添加sleep()usleep()
  • 不管你有什么问题——也许你应该把timer[1]改成timer[0]
  • 通常你的症状意味着未初始化的东西或内存损坏。尝试运行 valgrind。
  • 您的代码将 timer[1] 设置为 5000,但在 PULSE 之后立即显示为 1645677788 的 printf。除非另一个线程正在破坏内存,否则这是不可能的。
  • @JeremyP ;你是绝对正确的,我在输出中使用了错误的示例。我在帖子中更正了这一点。在脉冲之后')计时器值是 5000....

标签: c linux timer epoch beagleboard


【解决方案1】:

我通过读取 /proc/uptime 来修复它,然后将其乘以 10(十分之一秒的分辨率对我的程序来说就足够了)

我认为由于舍入错误或将浮点数转换为 int.... 不确定

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-05-26
    • 2016-02-19
    • 1970-01-01
    • 1970-01-01
    • 2012-04-12
    • 2018-01-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多