How to set a TO interrupt ?

pjrebordao

Senior Member
i want to set a timer overflow interrupt for a few mS in the future.
What should be the correct / best order for the folowing instructions:

Let timer = 65535
Settimer XXX
Setintflags 10000000, 10000000

My concern is which of those instruction sets the time "running" ?
From where do the X mS start counting ?
 

inglewoodpete

Senior Member
The following code is taken from a working 40X2-based project of mine. The register and RAM allocations are taken directly from my code and can be redefined to suit your needs. I use b0 for bit variables (my program's flags), since the bits are easily addressable.

To answer your question, the timer starts with the "SetTimer" command. Interrupts will occur as soon as the currently executing command is completed. So, depending on the execution speed you select for your project, the interrupt should happen <1mS of the timer expiry. However, you must take care to avoid blocking commands which can delay interrupts from occurring. Due to the PICAXE using interpreted code, I prefer not to use short timer periods (<50mS), otherwise the processor spends most of its time servicing the interrupts.
Code:
[color=Navy]#PICAXE [/color][color=Black]40X2[/color]
[color=Navy]#Terminal 38400                        [/color][color=Green]'For 32MHz operation
'
' **** Register Definitions ****
'
   [/color][color=Blue]Symbol   [/color][color=Purple]bTimerStatus   [/color][color=DarkCyan]= [/color][color=Purple]b0
   [/color][color=Blue]Symbol   [/color][color=Purple]wGlobalTick    [/color][color=DarkCyan]= [/color][color=Purple]w19       [/color][color=Green]'=b38&39
'
' **** RAM and Misc Definitions****
'
   [/color][color=Blue]Symbol   rIntStack      [/color][color=DarkCyan]= [/color][color=Navy]$6E       [/color][color=Green]'Word used during interrupts etc to determine key
   [/color][color=Blue]Symbol   rTimersStatus  [/color][color=DarkCyan]= [/color][color=Navy]$85       [/color][color=Green]'Contains bit flags for software timers
   [/color][color=Blue]Symbol   False          [/color][color=DarkCyan]= [/color][color=Navy]0
   [/color][color=Blue]Symbol   True           [/color][color=DarkCyan]= [/color][color=Navy]1[/color]
[color=Green]'
' **** Timer Definitions ****
'
   [/color][color=Blue]Symbol   mskBGTimerOnly [/color][color=DarkCyan]= [/color][color=Navy]%10000000 [/color][color=Green]'When only the timer is running
   [/color][color=Blue]Symbol   flgBGTimerOnly [/color][color=DarkCyan]= [/color][color=Navy]%10000000 [/color][color=Green]'When only the timer is running
   [/color][color=Blue]Symbol   tmrIntOn1stTick[/color][color=DarkCyan]= [/color][color=Navy]65535     [/color][color=Green]'Interrupt to be caused by roll over on first major tick
   [/color][color=Blue]Symbol   tmr100mS_8x4   [/color][color=DarkCyan]= [/color][color=Navy]53036     [/color][color=Green]'= 65536 - (Treq * 1,000,000 / Clk / 256)
                                       ' Where: Treq = Required Time in microseconds; Clk = 32,000,000.
'[/color]
[color=Black]Init:       [/color][color=Blue]Pause [/color][color=Navy]1000                       [/color][color=Green]'Required to allow PE's Terminal to open and log all events
            [/color][color=Blue]SetFreq em32

            [/color][color=Green]'The following code starts the background timer that runs continuously
            [/color][color=Purple]TOFlag [/color][color=DarkCyan]= [/color][color=Blue]False
            [/color][color=Purple]Timer [/color][color=DarkCyan]= [/color][color=Blue]tmrIntOn1stTick          [/color][color=Green]'
            [/color][color=Blue]SetTimer tmr100mS_8x4            [/color][color=Green]'Expires after 100 milliseconds
            [/color][color=Blue]SetIntFlags flgBGTimerOnly[/color][color=Black], [/color][color=Blue]mskBGTimerOnly [/color][color=Green]'Set timer 0 to interrupt
            
            'Main Loop
            [/color][color=Blue]Do
               [/color][color=Green]'<insert your code here>
            [/color][color=Blue]Loop

Interrupt:If [/color][color=Purple]TOFlag [/color][color=DarkCyan]= [/color][color=Blue]True then
            [/color][color=Green]'Timer event has occurred
            [/color][color=Blue]Inc [/color][color=Purple]wGlobalTick                     [/color][color=Green]'Runs continually (100mS counter)
            '
            [/color][color=Blue]Poke rIntStack[/color][color=Black], [/color][color=Purple]bTimerStatus        [/color][color=Green]'Preserve variable - whatever happened to be in the reg at the time
            [/color][color=Blue]Peek rTimersStatus[/color][color=Black], [/color][color=Purple]bTimerStatus    [/color][color=Green]'Fetch status for all software timers
            '
            '<insert your code here>
            '
            [/color][color=Blue]Poke rTimersStatus[/color][color=Black], [/color][color=Purple]bTimerStatus    [/color][color=Green]'Save status for all software timers
            [/color][color=Blue]Peek rIntStack[/color][color=Black], [/color][color=Purple]bTimerStatus        [/color][color=Green]'Preserve variable(s)
            '
            [/color][color=Purple]Timer [/color][color=DarkCyan]= [/color][color=Blue]tmrIntOn1stTick             [/color][color=Green]'Sequence is important!
            [/color][color=Purple]TOFlag [/color][color=DarkCyan]= [/color][color=Blue]False
            SetIntFlags flgBGTimerOnly[/color][color=Black], [/color][color=Blue]mskBGTimerOnly [/color][color=Green]'Set timer 0 to interrupt
          [/color][color=Blue]EndIf
          Return[/color]
 
Top