Thanks
What happens when you try to use it?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
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;
}
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.joan wrote:I wrote a version when I was decoding ir pulses.
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.gordon@drogon.net wrote:What happens when you try to use it?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
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