Skip to content
Advertisement

weird issue with time function in C

I’m trying to get my Beaglebone Green to read an input from a certain pin, and when that pin sees a 1 to 0 transition (defined in pulse()), then it should set a timer. I want to make my timer like this:

In idle, the timer is set to a TIMER_HOLD value (900000) To start the timer, I set it to something lower then TIMER_HOLD. Then it should decrement (with the timerupdate() function). If it reaches 0 or less -which for an unsigned long is higher than TIMER_HOLD – then a trigger action is executed.

To test/debug this, I added: printf("timer 1: %lld n",timer[1]); And this works perfect… I see this output:

  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

This is my c code:

#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("PULSEn");
                    timer[1]=5000;
            }
             printf("timer 1: %lld n",timer[1]); //THIS IS THE DEBUG PRINTF !!!
            if (timer[1]>TIMER_HOLD)
            {
                    printf("nTIMER TRIGGERn");
                    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));
   }

now the weird thing: When I remove the printf("timer 1: %lld n",timer[1]); then I only see PULSE

on the output… the timer never triggers….

Any idea what’s happening?

system info:

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

Advertisement

Answer

I fixed it by reading /proc/uptime instead, then multiplying it by 10 (tenth of a second resolution is sufficient for my program)

I think the original thing didn’t work because of a rounding error, or a conversion float to int…. not sure though

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement