​ ​ ​ ​ processing events using interrupts in a fixed time interval ("uninterruptible PAUSE")
Results 1 to 2 of 2

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

  1. #1
    Senior Member
    Join Date
    Jan 1970
    Groningen, The Netherlands

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


    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

    REM interrupt (active low) input on C.0
    SYMBOL TimeBase = W13
    SETINT %00000000, %00000001
    GOSUB Init_1S
    	GOSUB Sync_1S
    	PULSOUT LEDpin, 5000
    Init_1S:     'define the current time as the time base to compare the running timer with
    	TimeBase = Time
    Sync_1S:     'Waits (but will still service interrupts) and returns when time variable gets updated after 1 second
    	LOOP UNTIL Time <> TimeBase
    	TimeBase = Time
    	PULSOUT LEDpin, 500         ' or do some other useful things here
    	SETINT %00000000, %00000001
    Best regards,

    PS: Simplified Init_S since it only needs to initialize the TimeBase variable to the current Time value
    Last edited by kranenborg; 24-09-2012 at 11:56.

  2. #2


    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.


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts