Is it possible to set an interrupt on a Tristate port? I’m trying to catch a short pulse (no more than 38ms) on a Tristate pin, and I’m having difficulty measuring the width accurately in a busy-waiting loop.
Why tristate port? Using interrupts? Code example?
Why not use the PinCapture function?
Here’s my code at the moment:
public double GetPulse()
{
long t1, t2;
// Set it to an putput
_port.Active = true;
//Send a quick HIGH pulse
_port.Write(true);
_port.Write(false);
// Set it as an input
_port.Active = false;
//Wait till port is high
while (!_port.Read());
t1 = System.DateTime.Now.Ticks;
//Wait till port is low
while (_port.Read());
t2 = System.DateTime.Now.Ticks;
return t2 - t1;
}
At present, it busy waits, instead of using interrupts. However, this is sub-optimal, as I need the processing time elsewhere. I need to rewrite the code using interrupts. Currently, my two choices seem to be wire up an interrupt pin in parallel with the TristatePort, or drop down into native code.
Basically, you are choking the processor in a dead loop. The right way to do this is through interrupt events, where the handler will be automatically provided by the tick of when the pin changed state.
Or use PinCapture.
I know that! The problem is I need the pin to be configured as a Tristate port, and they don’t support interrupts! If I try and instantiate a PinCapture on the same port, I’m pretty sure an Exception is thrown.
This was also a problem for the DHT11. Nicolas had to resort to using two pins connected together, one tri-state to issue the start pulse, and another pin capture pin for reading the pulses. Very annoying to sacrifice an extra pin, and very un-neat…
See http://code.tinyclr.com/project/289/dht11---temperature-and-humidity-sensor/