Picaxe 20X2 Interrpt Despatching

PhilHornby

Senior Member
I have some Picaxe 20X2 code that I'm in the process of tidying - it has been running 'correctly' for the past year or so...

It uses interrupts for Hint1,Hint2,Timer Overflow and Serial Background Receive.

My question is: how are overlapping interrupts best handled?


For example - say the 'Interrupt' service routine is called for Hint1 and some time later, while it is still processing, the timer overflows as well. Presumably, with 'toflag' now being set, the ISR will be re-invoked, once the 'SetIntFlags' and 'return' statements are executed.

It struck me, that I could loop back to my 'Interrupt' routine's despatcher and deal with any new interrupts, before issuing 'SetIntFlags' and leaving.

I've not seen anyone else adopt this technique(!), so are there any dis-advantages to it? (Presumably it is slightly more efficient in terms of execution speed...)
 

PhilHornby

Senior Member
I raked this thread up by referring to it here: , so I thought I'd post an example of what I had it mind:-
Rich (BB code):
interrupt:
      ; *******************************************
      ; Interrupt despatcher - in priority order...
      ; *******************************************


      ;
      ;     Save variables we need to use at Interrupt time
      ;
      ;     NOTE: PTR is NOT currently saved. It IS currently USED
      ;     Do not use it at non-interrupt state
      ;
      Pushram                                         ;;;save W0 - W7
      Push bptr                                       ;;;save bptr (on a different stack)
      ;
      ; Despatch to correct handler
      ;
Despatcher:
      If hint1flag = 1 Then Goto Switch_Pressed       ;;;user interaction is most important
      If hserflag = 1  Then Goto Serial_In            ;;;web server comms.
      If hint2flag = 1 Then Goto HC12                 ;;;inbound temperature reading
      If toflag = 1    Then Goto Timer_Overflow       ;;;time to toggle the LCD display
      Goto DISMISS                                    ;;;unknown interrupt - dismiss
      


Serial_In:
;;;   **********************************************************
;;;   Interrupt handler for Serial In - i.e. data from webserver
;;;   **********************************************************
      #Include "ISR Serial_In.basinc"
      Goto Dismiss                                    ;;;END-OF-INTERRUPT



Timer_Overflow:
;;;   **********************************
;;;   Interrupt handler for Timer Expiry
;;;   **********************************
      #Include "ISR Timer_Overflow.basinc"
      Goto Dismiss                                    ;;;END-OF-INTERRUPT                      
      
Switch_Pressed:
;;;   ****************************************
;;;   Interrupt handler for main switch press.
;;;   ****************************************
      #Include "ISR Switch_Pressed.basinc"
      Goto Dismiss                                    ;;;END-OF-INTERRUPT


HC12:
;     **********************************************************************
;     Interrupt handler for HC-12 458MHz Radio module receiving a character.
;     **********************************************************************
      #Include "ISR HC12.basinc"
      ;Goto Dismiss                                   ;;;END-OF-INTERRUPT (Fall-through)


;;;   *************************************************************************
;;;   END OF INTERRUPT - COMMON EXIT. Also used to initially set up interrupts.
;;;   *************************************************************************
Dismiss:                                              ;;;KEEP
Reset_Interrupts:                                     ;;;TOGETHER
;;;
;;; Each ISR tidies after itself. But what if another Interrupt has occurred while we were processing?
;;; Rather than restoring all the context, re-setting the interrupts, returning and immediately getting called again,
;;; let's check *now* for any other interrupts that need servicing.
;;;            
      If hint1flag = 1 or hserflag = 1 or hint2flag = 1 or toflag = 1  Then Goto Despatcher
      ;b0 = hint1flag + hserflag + hint2flag + toflag
      ;if b0 <> 0 then goto Despatcher                ;;;fancy 'efficient' code is actually 5 bytes longer ;-)


;HC-05 replacement by HC-12 change - Falling edge of "STATE" (Actually a START bit of a received serial byte)
      Hintsetup %00000110                             ;;;set hINT1 (B.0) (switch) to trigger Interrupt at falling edge
                                                      ;;;set hINT2 (B.1) (HC-12) to trigger on falling edge


            ;Bit 7 - reserved
            ;Bit 6 - Interrupt 2 Trigger (1 = rising edge, 0 = falling edge)
            ;Bit 5 - Interrupt 1 Trigger (1 = rising edge, 0 = falling edge)
            ;Bit 4 - Interrupt 0 Trigger (1 = rising edge, 0 = falling edge)
            ;Bit 3 - reserved
            ;Bit 2 - Interrupt 2 Enable
            ;Bit 1 - Interrupt 1 Enable
            ;Bit 0 - Interrupt 0 Enable (not 20X2)


                                                      ;;;set interrupt on Timer overflow OR Hint1 (switch) OR Hint2 (HC-12) or serial char from webserver.
      Setintflags Or %10100110,%10100110              ;;;The condition is the flag ... not the actual hardware condition.
                                                      ;;;The flag is set to "1", by the pin going LOW (makes sense ... ;-)


            ;Bit 7 toflag Timer overflow flag see settimer
            ;Bit 6 hi2cflag hi2c write has occurred (slave mode) see hi2csetup
            ;Bit 5 hserflag hserial backgrounmd receive (see hsersetup)
            ;Bit 4 compflag (see compsetup)
            ;Bit 3 hintflag INT0 or INT1 or INT2
            ;Bit 2 hint2flag INT2 see hintsetup
            ;Bit 1 hint1flag INT1 see hintsetup
            ;Bit 0 hint0flag INT0 (not on this processor)
     
      Pop bptr                                        ;;;restore
      Popram                                          ;;;saved locations
      return                                          ;;;END-OF-INTERRUPT
 
Last edited:

PhilHornby

Senior Member
Thanks, I came across that thread a while back, and noted its contents...

...and then I forgot all about it again!
 
Top