Event Timestamping with Counters
Maybe this is well known technique, but I found no sign of it here, and its not in the Counter PE Lab. Or I'm nuts and this has holes the size of, well something big (Possible since I have not actually tested it yet!)
From vague memory, some other MCU's are able to use the counters to record the system clock when a transition occurs. At first I thought it couldn't be done with the prop, without resorting to a dedicated cog doing polling, but I think I have figured out how to do it.
Why would you want that... when you can record accurately the transitions of a tacho pulse type signal monitor the health of the sensor, not only record the raw count.
In my case I want to monitor the duty cycle and detect missed pulses when the input falls outside the bounds of reasonable. (e.g. improbably high acceleration)
The key is to initialize the counter phase with the system clock, and set the counter to level detection, toggling the level to detect after each detection.
From vague memory, some other MCU's are able to use the counters to record the system clock when a transition occurs. At first I thought it couldn't be done with the prop, without resorting to a dedicated cog doing polling, but I think I have figured out how to do it.
Why would you want that... when you can record accurately the transitions of a tacho pulse type signal monitor the health of the sensor, not only record the raw count.
In my case I want to monitor the duty cycle and detect missed pulses when the input falls outside the bounds of reasonable. (e.g. improbably high acceleration)
The key is to initialize the counter phase with the system clock, and set the counter to level detection, toggling the level to detect after each detection.
PUB example(Pin) | time
' kind of assumes starting state is POS.... watch handling of initial cycle, might need to
' be smarter first time though.
frqa := 1
ctra := (%01000 << 26 ) | (Pin) ' POS detector
phsa := cnt
repeat
' now can do stuff...
' you've got almost a input full cycle...
' wait for the NEG. edge, if not already gone
waitpne( |<Pin, |<Pin, 0)
' grab timestamp and quickly set up counter to record the rising edge
time := phsa
ctra := (%01100 << 26 ) | (Pin) ' NEG detector
phsa := cnt ' catch up
' now can do stuff... falling edge time stamp time
' you've got almost a input full cycle...
' wait for the POS. edge, if not already gone
waitpeq( |<Pin, |<Pin, 0)
' record timestamp and quickly set up counter to record the falling edge
time := phsa
ctra := (%01000 << 26 ) | (Pin) ' POS detector
phsa := cnt ' catch up
