' Keep track of time using an interrupt and display
' it on a terminal connected to the serial port
Include <libs\18c452.bas> ' For chip specific definitions
Include <libs\hardware.bas> ' include hardware functions
Option Banka ' Variables following this are stored in access ram
' Global variables, These can be accessed throughout the entire program
Dim time As Fixed
Dim displayTime As Boolean
Dim seconds As uByte, minutes As uByte, hours As uByte
' Every program MUST have a main procedure, this is
' where program execution begins
Sub main()
Dim s(20) As String ' <- local variable s can only be accessed from process "main"
Call init()
Call puts("Running"): putcrlf() ' display "Running" on the terminal followed by a CR and LF character
While 1
If displayTime=1 Then
displayTime=0
Call puts("Time= ")
Call str(hours,s): Call puts(s): Call putc(":")
Call str(minutes,s): Call puts(s): Call putc(":")
Call str(seconds,s): Call puts(s): putcrlf()
End If
Wend
End Sub
Sub init()
' init variables
seconds=0: minutes=0: hours=0
time=0: displayTime=1
' init hardware
SPBRG=12: TXSTA=0x24: RCSTA=0x90 ' 19.2KBaud @ 4MHz
' Init Timer1 1:1 Interrupt enabled
TMR1H=0: TMR1L=0: TMR1IF=0
T1CON=1 ' 1:1
TMR1IE=1: GIE=1: PEIE=1
End Sub
' The interrupt service routine
' WREG,BSR, and STATUS registers are automatically saved and
' restored for both high and low priority interrupts
Sub isr() IntHigh
' Timer 1 Interrupt
If TMR1IF=1 Then
TMR1IF=0: time=time+0.065536
If time>=1.0 Then
time=time-1.0
seconds=seconds+1
displayTime=1
If seconds>59 Then
seconds=0: minutes=minutes+1
If minutes>59 Then
minutes=0: hours=hours+1
End If
End If
End If
End If
End Sub