2017-11-28 6 views
0

Ich versuche, mein Beaglebone Green eine Eingabe von einem bestimmten Pin zu lesen, und wenn dieser Pin einen Übergang von 1 zu 0 sieht (definiert in pulse()), dann sollte es gesetzt werden ein Timer. Ich möchte meine Timer wie diese machen:seltsames Problem mit Zeitfunktion in C

Im Bereitschaftsmodus der Timer eingestellt wird auf einen Wert TIMER_HOLD (900000) den Timer zu starten, habe ich es zu etwas niedriger als TIMER_HOLD. Dann sollte es dekrementieren (mit der timerupdate() Funktion). Wenn es 0 oder weniger erreicht - was für eine vorzeichenlose Länge höher ist als TIMER_HOLD - dann wird eine Auslöseaktion ausgeführt.

Um zu testen/debug dies, fügte ich hinzu: printf("timer 1: %lld \n",timer[1]); Und das funktioniert perfekt ... Ich sehe diese Ausgabe:

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 

Das ist mein 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("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)); 
    } 

jetzt das seltsame sache: Wenn ich die printf("timer 1: %lld \n",timer[1]); entferne dann sehe ich nur PULSE

am Ausgang ... der Timer löst nie aus ....

Irgendeine Idee, was passiert?

Systeminfo:

[email protected]:/# uname -na 
Linux beaglebone 4.4.9-ti-r25 #1 SMP Thu May 5 23:08:13 UTC 2016 armv7l GNU/Linux 

[email protected]:/# gcc --version 
gcc (Debian 4.9.2-10) 4.9.2 
+0

Haben Sie versucht, ein 'sleep()' oder 'usleep()' zu setzen, als Sie 'printf entfernten() '? – Gaurav

+0

Unabhängig von Ihrem Problem - Vielleicht sollten Sie diesen 'timer [1]' in 'timer [0]' ändern? – goodvibration

+1

Normalerweise bedeutet Ihr Symptom nicht initialisiertes oder Speicherbeschädigungen. Versuche, Valgrind auszuführen. – Serge

Antwort

0

Ich reparierte sie durch das Lesen/proc/uptime statt, dann ist es um 10 (Zehntelsekunde Auflösung für mein Programm ist ausreichend) multipliziert

Ich denke, das ursprüngliche Ding funktionierte nicht wegen eines Rundungsfehlers oder eines Umwandlungsflosses zum int .... nicht sicher obwohl