micrometal
New Member
I am trying to measure the elapsed time between two events, in the region of 20 to 30 minutes apart, to a roughly 1msec precision. I have a clock crystal oscillator (32768kHz) and a 7-stage binary divider delivering clock pulses to a 20X2 at nominally 4msec intervals and I am counting the pulses using the following code (some code omitted for clarity) ...
As you can see other tasks have to be performed, such as processing Bluetooth commands received in the background and counting and debouncing a second (interrupt) input, and I am wondering how much processing I can do before the main counting loop is impacted; I cannot afford to miss any timing pulses. I know that "how much processing" is difficult to quantify, so an alternative question could be "How can I detect that a count has been missed?"
Code:
setfreq m64 ' Run at 64MHz
hsersetup B9600_64, 1 ' 9600 baud, background mode
hintsetup %00000010 ' Interrupt on falling edge on B.1
setintflags OR %00000010, %00000010
state = 0 ' Initial condition
low clk_gate ' Inhibit timer count
main: if hserflag > 0 then goto BT_in ' Check for background Bluetooth msg
if state = 2 then ' Counting clock pulses
if clk_in = 1 and latch = 0 then
inc clock0 ' Counter uses three bytes
if clock0 = 0 then : inc clock1 : endif
if clock1 = 0 then : inc clock2 : endif
latch = 1 ' Prevent double counting
endif
if clk_in = 0 and latch = 1 then
latch = 0 ' Unlatch for next pulse
endif
goto main
endif
if state = 0 then ' Set up initial conditions
endif
if state = 1 then ' Prepare to start
endif
if state = 3 then ' Prepare to finish
endif
if state = 4 then ' Hold data for retrieval
endif
goto main ' Main loop