lbenson
Senior Member
The following code loops every 500ms based on "setfreq M8", "settimer t1s_4" and a check for a change in the value of "timer". The two commands function so that the system variable, "timer" increments every half second.
The first statement in the main do-loop, "do while wLastTimer=timer : loop" idles until an increment occurs. "inc bSecondFlag" causes a bit variable to toggle on once a second. The test for that variable going to "1" enables you to do things which are supposed to happen every second. "inc b2SecondFlag" toggles another flag every two seconds. Similar additional code could toggle a bit flag every 4,8,16, etc. seconds.
A second way to mark a lapsed time with a bit variable is to divide the "timer" variable by the number of seconds you want to look at times 2, and view the remainder. Thus when, as in this case, nLoopsPerSecond is equal to 2, "ticksRemainder = timer / nLoopsPerSecond // 60" will give a value of 0 for ticksRemainder once every minute. "if ticksRemainder = 0 then : bMinuteFlag = 1 : else : bMinuteFlag = 0 : endif" sets the bit flag, bMinuteFlag, to 1 when this occurs--otherwise its value will be 0 (for 119 out of 120 passes through the main loop).
When bMinuteFlag=1, a wLapsedMinutes WORD variable could be incremented. This would allow 65,536 minutes to be counted before rollover--1,092 hours, over 45 days.
The timing requires that any code that you call from this main loop executes in less than 500ms. That means no long PAUSE statements, and no READTEMP statements (these may take up to 750ms to complete), among others. (READTEMP can be replaced with the one-wire statements, OWIN and OWOUT--there are examples in Manual 2.) At maybe 4000 statements per second with "setfreq M8", you should be able to accomplish a lot within these constraints.
Code:
' 20timertest tests 500ms timing
#picaxe 20x2
#terminal 9600
#no_data
#no_table
symbol nLoopsPerSecond = 2
symbol bSecondFlag = bit2
symbol b2SecondFlag = bit3
symbol bMinuteFlag = bit4
symbol ticksRemainder = b4 ' used for minutes division
symbol wLapsedMin = w26
symbol wLastTimer = w27
setfreq M8
settimer t1s_4 ' timer variable ticks every half second (at M8)
wLapsedMin=0
wLastTimer=timer
main: ' loops every 500ms
do ' depends on code called during loop taking less than 500ms
do while wLastTimer=timer : loop ' wait until timer is incremented--M8, tls_4
wLastTimer=timer ' reset
inc bSecondFlag ' toggles between 0 and 1
if bSecondFlag = 1 then ' one second has elapsed
inc b2SecondFlag ' toggles between 0 and 1 every 2 seconds
if b2SecondFlag = 1 then ' two seconds have elapsed
' do something
high B.5
endif
high B.1
else ' do things on the half-second (e.g. turn off camera)
low B.1
low B.5
endif
ticksRemainder = timer / nLoopsPerSecond // 60 ' every 60 seconds equals 0
if ticksRemainder = 0 then : bMinuteFlag = 1 : else : bMinuteFlag = 0 : endif
if bMinuteFlag = 1 then
inc wLapsedMin ' minutes counter
high B.7
else
low B.7 ' redundant most of the time
endif
loop
A second way to mark a lapsed time with a bit variable is to divide the "timer" variable by the number of seconds you want to look at times 2, and view the remainder. Thus when, as in this case, nLoopsPerSecond is equal to 2, "ticksRemainder = timer / nLoopsPerSecond // 60" will give a value of 0 for ticksRemainder once every minute. "if ticksRemainder = 0 then : bMinuteFlag = 1 : else : bMinuteFlag = 0 : endif" sets the bit flag, bMinuteFlag, to 1 when this occurs--otherwise its value will be 0 (for 119 out of 120 passes through the main loop).
When bMinuteFlag=1, a wLapsedMinutes WORD variable could be incremented. This would allow 65,536 minutes to be counted before rollover--1,092 hours, over 45 days.
The timing requires that any code that you call from this main loop executes in less than 500ms. That means no long PAUSE statements, and no READTEMP statements (these may take up to 750ms to complete), among others. (READTEMP can be replaced with the one-wire statements, OWIN and OWOUT--there are examples in Manual 2.) At maybe 4000 statements per second with "setfreq M8", you should be able to accomplish a lot within these constraints.
Last edited: