Page 1 of 1
wiringpi pulsein function
Posted: Mon Feb 04, 2013 8:40 am
by ady18ro2007
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

Re: wiringpi pulsein function
Posted: Mon Feb 04, 2013 9:36 am
by gordon@drogon.net
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
Re: wiringpi pulsein function
Posted: Mon Feb 04, 2013 9:49 am
by ady18ro2007
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.
Re: wiringpi pulsein function
Posted: Mon Feb 04, 2013 9:53 am
by joan
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;
}
Re: wiringpi pulsein function
Posted: Mon Feb 04, 2013 10:18 am
by ady18ro2007
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.
Re: wiringpi pulsein function
Posted: Mon Feb 04, 2013 10:28 am
by joan
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).
Re: wiringpi pulsein function
Posted: Mon Feb 04, 2013 11:49 am
by gordon@drogon.net
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