ady18ro2007
Posts: 4
Joined: Mon Feb 04, 2013 8:34 am

wiringpi pulsein function

Mon Feb 04, 2013 8:40 am

Does anyone know how to implement pulseIn function in C? I've looked it up on google and only found it on wiringpi drogon site but it doesn't seem to work at all...
Thanks :)

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

Re: wiringpi pulsein function

Mon Feb 04, 2013 9:36 am

ady18ro2007 wrote:Does anyone know how to implement pulseIn function in C? I've looked it up on google and only found it on wiringpi drogon site but it doesn't seem to work at all...
Thanks :)
What happens when you try to use it?

I admit that I only tested it once - briefly and with nothing more than 2 wires connected to the Pi, but it's based on the same one I write for my Arduino library which I have used on the Arduino...

Let me know more about what you're trying to achieve (and what sort of frequencies!)

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

ady18ro2007
Posts: 4
Joined: Mon Feb 04, 2013 8:34 am

Re: wiringpi pulsein function

Mon Feb 04, 2013 9:49 am

When I call pulseIn the compiler tells me that the function doesn't exist. I've already checked other functions from the wiring.h and they work.

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

Re: wiringpi pulsein function

Mon Feb 04, 2013 9:53 am

I wrote a version when I was decoding ir pulses.

Code: Select all

int pulseIn(int pin, int level, int timeout)
{
   struct timeval tn, t0, t1;

   long micros;

   gettimeofday(&t0, NULL);

   micros = 0;

   while (digitalRead(pin) != level)
   {
      gettimeofday(&tn, NULL);

      if (tn.tv_sec > t0.tv_sec) micros = 1000000L; else micros = 0;
      micros += (tn.tv_usec - t0.tv_usec);

      if (micros > timeout) return 0;
   }

   gettimeofday(&t1, NULL);

   while (digitalRead(pin) == level)
   {
      gettimeofday(&tn, NULL);

      if (tn.tv_sec > t0.tv_sec) micros = 1000000L; else micros = 0;
      micros = micros + (tn.tv_usec - t0.tv_usec);

      if (micros > timeout) return 0;
   }

   if (tn.tv_sec > t1.tv_sec) micros = 1000000L; else micros = 0;
   micros = micros + (tn.tv_usec - t1.tv_usec);

   return micros;
}

ady18ro2007
Posts: 4
Joined: Mon Feb 04, 2013 8:34 am

Re: wiringpi pulsein function

Mon Feb 04, 2013 10:18 am

joan wrote:I wrote a version when I was decoding ir pulses.
I've already tested this function and it seemed to not work properly, anyway I'll give it a try if you say me that it works.

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

Re: wiringpi pulsein function

Mon Feb 04, 2013 10:28 am

I used the same code when timing a LDR charge/discharge cycle. I'd say 99% of problems are the mapping from gpios to pins.

I'm a bit confused. The code was used with wiringPi but I didn't think there was a similar wiringPi function at the time (I'd have used it if I knew about it).

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

Re: wiringpi pulsein function

Mon Feb 04, 2013 11:49 am

gordon@drogon.net wrote:
ady18ro2007 wrote:Does anyone know how to implement pulseIn function in C? I've looked it up on google and only found it on wiringpi drogon site but it doesn't seem to work at all...
Thanks :)
What happens when you try to use it?

I admit that I only tested it once - briefly and with nothing more than 2 wires connected to the Pi, but it's based on the same one I write for my Arduino library which I have used on the Arduino...

Let me know more about what you're trying to achieve (and what sort of frequencies!)

-Gordon
Doh - it's shiftIn that I'm thinking of here - there is no pulseIn in wiringPi! (yet?) It's certianly do-able though, however the jitter imposed by the rest of the system might make it unreliable on very short timings (say under 10uS). Also to measure timings under about 150uS would require a busy poll-loop unless you dive into kernel space, so that would suck all CPU capacity.

What sort of timing ranges are you after?

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

Return to “C/C++”