[C.CC USERS] Precision monitoring of interrupts?
Adam Sampson
ats at offog.org
Mon Oct 1 11:39:43 BST 2012
Matt Jadud <matt at jadud.com> writes:
> The specific concern is that I need to monitor the amount of time the
> sensor spends LOW during a 30 second window.
Hmm, I can probably enthuse about any hardware project where the test
procedure involves cooking bacon, ;-)
It's worth noting that even the XMOS folks decided to invent a special
input process syntax for doing this sort of thing in XC. To measure the
length of a low-going pulse on an input pin, you'd do something like (I
think!):
in port inputPin;
int x;
unsigned start, end;
inputPin when pinseq(0) :> x @ start;
// i.e. "wait until inputPin is 0, then save its value into x and
// the current time into start"
inputPin when pinseq(1) :> x @ end;
// now (end - start) is the time spent low
So I don't think adding a pulse-measuring input PROC or two to Plumbing
would necessarily be a bad idea -- if you need very accurate timing,
then it makes sense to be explicit about it... and this is unlikely to
be the only application that needs it (e.g. it'd also be handy for doing
CANbus or RC5 remote controls).
Other ways of doing it:
- Set up a pin change interrupt that grabs the state of the pin and the
current time, works out how long it was since it last changed, and
adds the elapsed time onto a "time on" or "time off" count. Your main
program then samples and resets these every 30s. This is probably what
I'd do as a real-world solution, rather than polling.
- Given a sufficiently fancy microcontroller, it may have a "gated
timer" mode where it can count how many clock ticks occurred while
one of the pins is high/low;
- ... but even if it doesn't, you could simulate this with an external
counter chip (feed the clock input from the microcontroller's clock,
connect the sensor to the gate, and read the counter's output with the
microcontroller every 30s).
- Similarly, you could stretch the pulses with some very simple external
hardware (just a resistor and capacitor may do), to make them easier
to detect in software -- then you may just be able to get away with
regular polling from occam...
Thanks,
--
Adam Sampson <ats at offog.org> <http://offog.org/>
More information about the users
mailing list