processing events using interrupts in a fixed time interval ("uninterruptible PAUSE")

kranenborg

Senior Member
processing events using interrupts in a fixed time interval ("uninterruptible PAUSE")

Hello,

Sometimes you wish to record and process (possibly fast) asynchronous events using interrupts in a predefined time interval (doing more than just counting them, for which the Count command already provides for the proper functionality). Using a simple Pause statement for defining this interval does not work since the (interruptible) Pause command terminates directly after an interrupt has occurred, rendering the actual time interval length totally unpredictable.

The time variable on M2 processors can be used to easily create a "non-interruptible" pause whilst at the same time allowing the servicing of interrupts. The following example code (for a Picaxe-14M2) presents two subroutines that can be used to sycnhronize the interrupt handling with the free running 1-second timer in the M2 processors. In this example a low level signal input on C.0 triggers an interrupt. Subroutine Sync_1S synchronizes the waiting loop (here 1 second) with the free running M2 timer. The Subroutine Init_1S should be called just before the imterval begins in order to set the proper variable which remembers the state of the time variable; routine Synch_1S then returns as soon as the time variable is updated, and thus in this way implements a 1-second "uniterruptible Pause" command. Longer intervals may be created by multiple sequential calls to Synch_1S.

The example code below flashes a LED for 50ms each second, any interrupt will give a short 5ms flash as well but it is demonstrated that the interrupts do not disturb the main flashing process timing. Thusly, interrupts can be processed within a well-defined time interval

Code:
REM interrupt (active low) input on C.0
#picaxe_14M2
ENABLETIME
SYMBOL TimeBase = W13
SETINT %00000000, %00000001

Main:
GOSUB Init_1S
DO
	GOSUB Sync_1S
	PULSOUT LEDpin, 5000
LOOP
END



Init_1S:     'define the current time as the time base to compare the running timer with
	TimeBase = Time
	RETURN

Sync_1S:     'Waits (but will still service interrupts) and returns when time variable gets updated after 1 second
	DO 
	LOOP UNTIL Time <> TimeBase
	TimeBase = Time
	RETURN
	
INTERRUPT:
	PULSOUT LEDpin, 500         ' or do some other useful things here
	SETINT %00000000, %00000001
	RETURN
Best regards,
Jurjen
http://www.kranenborg.org/electronics

PS: Simplified Init_S since it only needs to initialize the TimeBase variable to the current Time value
 
Last edited:

Janne

Senior Member
Nice work. One thing worth remembering, is that some commands add to the delay to the time variable.. Can be a problem if the required delay must be accurate.
 
Top