One of my projects requires a simple time display, so I've been experimenting with an X1 chip and the settimer function. I noticed there is no code for this on the forum yet, so here is a starter using an LCD to display elapsed time in the format mm:ss (maximum of 99:59).
It would be a simple matter to use the code to show elapsed hours as well by using the same logic applied to the minutes display.
Thanks to previous posters who have helped explain the workings of settimer.
It would be a simple matter to use the code to show elapsed hours as well by using the same logic applied to the minutes display.
Thanks to previous posters who have helped explain the workings of settimer.
Code:
; Elapsed timer code snippet
; for use on picaxe 28X1 and 40X1
'initialise
symbol bMins=b0 'count of minutes (calculated from wTicks)
symbol bSecs=b1 'count of seconds (remainder ie. wTicks - bMins)
symbol wTicks=w1 'wTicks is the number of seconds counted
symbol wMinSecs=w2 'is the total number of seconds represented by minutes (used to calculate the seconds value)
'outputs
symbol oLCD=0 'serial output to LCD
startmsg:
serout oLCD, N2400, (254,1) 'clear display
serout oLCD, N2400, ("LCD timer", 254,192)
serout oLCD, N2400, ("using X1s")
pause 1000
timerScreenInit:
'sets up the initial info on the timer screen
serout oLCD, N2400, (254,1) 'clear display
serout oLCD, N2400, ("Time 00:00", 254,192)
serout oLCD, N2400, (" mm:ss")
settimer t1s_4 'settimer to 1 second ticks at 4MHz
Timer=$ffff 'set timer to generate interrupt on next timer overflow
setintflags $80,$80 'enable timer interrupt
main:
'put in anything you want the code to do here
'the code will 'interrupt' every second
goto main
Interrupt:
'interrupt is triggered by toflag
'toflag triggers when timer excedes $ffff
'process and update timer display
'wTicks is used to keep a count of seconds
'if timer was not being used to trigger an interrupt,
'the value of timer could be used to record seconds count
inc wTicks 'add another tick
if wTicks>5999 then
'we have reached the max of 99:59 - reset to 00:00
wTicks=0
endif
bMins=wTicks/60 'divide by total seconds by 60 to get minute value
wMinSecs=bMins*60 'this is the total number of seconds represented by the minutes value
bSecs=wTicks-wMinSecs 'the remainder of the two gives the seconds count
'move to position for writing to LCD
serout oLCD, N2400, (254,133) 'move to start of mins write position
if bMins<10 then
'add a leading 0
serout oLCD, N2400, ("0")
endif
serout oLCD, N2400, (#bMins, ":") 'write the minutes value to LCD
if bSecs<10 then
'add a leading 0
serout oLCD, N2400, ("0")
endif
serout oLCD, N2400, (#bSecs) 'write the seconds value to LCD
toflag = 0 'clear the timer overflow flag
timer = $ffff 'initialise timer to interupt on next count
setintflags $80,$80 'enable the timer interrupt
return ‘return