Using SetTimer preload values

hippy

Technical Support
Staff member
One technique to generate interrupts on a timed basis is to use SETTIMER with a preload value, and TIMER=$FFFF ( TIMER=65535 ); as soon as the preload expires, 'timer' gets incremented, overflows to zero, and an interrupt will be generated. For example this generates an interrupt every one second -

#Picaxe 20X2

SetIntFlags $80, $80
timer = $FFFF
SetTimer t1s_8
Do: Loop

Interrupt:
toflag = 0
timer = $FFFF
PulsOut B.7, 10
SetIntFlags $80, $80
Return

One problem comes in calculating a value to replace 't1s_8' for other time periods. One way to do it is this way. At the top of the program add -

Symbol TIME_PERIOD = 500000 ' Number of microseconds between interrupts
Symbol FREQUENCY = 8 ' Operating frequency of PICAXE in MHz

Symbol TICK_TIME = 256 / FREQUENCY
Symbol PRELOAD_TICKS = TIME_PERIOD / TICK_TIME
Symbol PRELOAD_VALUE = 0 - PRELOAD_TICKS
Symbol PRELOAD = PRELOAD_VALUE & $FFFF

Then use -

SetTimer PRELOAD

Voila; an interrupt every 500ms. Change to "TIME_PERIOD = 100000" and an interrupt every 100ms etc.
 
Top