fightforlife
Posts: 14
Joined: Thu May 30, 2013 11:02 am

Measure input in microseconds WiringPi

Sat Sep 27, 2014 3:41 pm

Hey there,

i'm a total beginner when it comes to using C.
I need to measure an Input as precisely as possible. Sadly C (in combination with the Wiring-Pi library) is the best choice for such an Project.

Does somebody have a simple C-Code which prints a timestamp (Microseconds) everytime the input changes from 0 to1 or vica versa.

I found some code that could help, but i'm not able to combine this into working code.
For the timestamp:

Code: Select all

#include <sys/time.h>
struct timeval tv;
gettimeofday(&tv,NULL);
tv.tv_sec // seconds
tv.tv_usec // microseconds
Some wiringpi code:

Code: Select all

#include <wiringPi.h>

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>

int main() {
  if (wiringPiSetup () == -1)
    exit (1) ;

  pinMode(1, INPUT);

  while(1) {
    digitalRead(1, 0);
  }

  return 0 ;
}
i also have some working python code, but its not precise enough:

Code: Select all

import wiringpi2 as wiringpi
import time
i=0
wiringpi.wiringPiSetupGpio()
wiringpi.pinMode(18, 0)
current_milli_time = lambda: int(round(time.time() * 1000))
while True:
        if wiringpi.digitalRead(18)==1:
                if i==0:
                        i=1
                        start=current_milli_time()
                current=current_milli_time()-start
                print current
Thanks in advance!
Hopefully someone else already did this!

User avatar
mad-hatter
Posts: 419
Joined: Sun Feb 26, 2012 2:58 pm
Location: By the UK seaside

Re: Measure input in microseconds WiringPi

Sat Sep 27, 2014 7:21 pm

Hello,

Have a look at Joan's pigpio library
http://abyz.co.uk/rpi/pigpio/

*** callbacks when any of gpios 0-31 change state (callbacks receive the time of the event accurate to a few microseconds).

Regards

fightforlife
Posts: 14
Joined: Thu May 30, 2013 11:02 am

Re: Measure input in microseconds WiringPi

Sat Sep 27, 2014 8:14 pm

This seems to be a good place to start.
The examples "frequency count 1 and 2" are working. But the code is way to complex for me to understand and to alter it..


Some background on this problem:
A long time ago i compiled a binary which controlls a 433Mhz Transmitter to switch my outlets. Now i want to port this to another system and cant remember the exact "waveform" of the signal. I know that it is send over GPIO17.
So i hope that its possible to just connect this pin to another pin on the board und retrieve the pulses.
Or is there a way to get the output of the pin before it leaves the board?

User avatar
joan
Posts: 14468
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Measure input in microseconds WiringPi

Sat Sep 27, 2014 8:35 pm

fightforlife wrote:This seems to be a good place to start.
The examples "frequency count 1 and 2" are working. But the code is way to complex for me to understand and to alter it..


Some background on this problem:
A long time ago i compiled a binary which controlls a 433Mhz Transmitter to switch my outlets. Now i want to port this to another system and cant remember the exact "waveform" of the signal. I know that it is send over GPIO17.
So i hope that its possible to just connect this pin to another pin on the board und retrieve the pulses.
Or is there a way to get the output of the pin before it leaves the board?
Have a look at piscope. It is a non-intrusive gpio monitor. You could capture the waveform with that and export it as a VCD or piscope file. It would take a bit of hand work but you should be able to pull out the waveform.

User avatar
gordon@drogon.net
Posts: 2020
Joined: Tue Feb 07, 2012 2:14 pm
Location: Devon, UK
Contact: Website

Re: Measure input in microseconds WiringPi

Mon Sep 29, 2014 9:38 am

[quote="fightforlife"]Hey there,

i'm a total beginner when it comes to using C.
I need to measure an Input as precisely as possible. Sadly C (in combination with the Wiring-Pi library) is the best choice for such an Project.

Does somebody have a simple C-Code which prints a timestamp (Microseconds) everytime the input changes from 0 to1 or vica versa.

I found some code that could help, but i'm not able to combine this into working code.
For the timestamp:

Code: Select all

#include <sys/time.h>
struct timeval tv;
gettimeofday(&tv,NULL);
tv.tv_sec // seconds
tv.tv_usec // microseconds
Some wiringpi code:

Code: Select all

#include <wiringPi.h>

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>

int main() {
  if (wiringPiSetup () == -1)
    exit (1) ;

  pinMode(1, INPUT);

  while(1) {
    digitalRead(1, 0);
  }

  return 0 ;
}
Your code isn't going to do anything. It's going to sit in an infinite loop. You're also calling digitalRead() wrongly too - please compile with the -Wall flag.

A somewhat simple approach might be to:

Code: Select all

  unsigned int start, finish ;
  wiringPiSetup () ;
  pinMode (1, INPUT) ;
  start = micros () ;
  while (digitalRead (1) == 0) // Wait for 0 -> 1 transition
    ;
  finish = micros () ;
  printf ("Time to go high was: %d microseconds\n", finish - start) ;
However you're at the mercy of Linux scheduling and DRAM refresh with all this.

Also note that the micros() function (which returns an unsigned 32-bit integer) wraps after 71 minutes.

-Gordon
--
Gordons projects: https://projects.drogon.net/

fightforlife
Posts: 14
Joined: Thu May 30, 2013 11:02 am

Re: Measure input in microseconds WiringPi

Tue Sep 30, 2014 3:38 pm

Thanks for all the replies!

I got it working with piscope und gtkwave!

Return to “C/C++”