Elapsed Timer Demo
Attached to this post is a Zip file that contains a "Drop-In" elapsed timer that uses Timer1 and interrupts. It's waaay down there at the bottom of all this dribble. This may get rather long winded, so my first suggestion is to scroll down to the bottom of this post and check out the file then come back and read this post later.
--------------------- ----------------------- -----------------------------
The files contained in the Zip are:
Test_Elapsed_SER.pbp ' Demo of Elapsed Timer using serout2 command and HyperTerminal
Test_Elapsed_LCD.pbp ' Demo of Elapsed Timer using an LCD
Elapsed.bas ' Elapsed Timer include file
ASM_INTS.bas ' Assembly language Interrupt Stubs
Note: These files are intended to be used on 14-bit core PICS (12F, 16C and 16F) that have a TIMER1 module.
Note2: They are written for use as the only interrupt in the program. If you need to use other interrupts as well, the program will have to be modified before it will work.
Note3: It will NEVER work in conjunction with PBP's ON INTERRUPT statement.
In it's simplest form, this is all it takes to use the Elapsed Timer:
Code:
Include "Elapsed.pbp"
Gosub ResetTime ' Reset Time to 0d-00:00:00.00
Gosub StartTimer ' Start the Elapsed Timer
This will create a Clock counting at 1/100 seconds. It runs in the background of PBP without any other program intervention required.
The time is kept in the variables:
Code:
Ticks var byte ' 1/100th of a second
Seconds var byte ' 0-59
Minutes var byte ' 0-59
Hours var byte ' 0-23
Days var word ' 0-65535
The time can be easily displayed with a single line:
Code:
LCDout $FE,2, dec Days,"d-",dec2 Hours,":",dec2 Minutes,":",dec2 Seconds
For each of the variables (Seconds, Minutes, Hours and Days) there is a flag that indicates when the value of that variable has changed.
The Flags are:
Code:
SecondsChanged var bit
MinutesChanged var bit
HoursChanged var bit
DaysChanged var bit
So, if you wanted to display the time like a clock, you could wait until SecondsChanged = 1, display the time, then reset the flag.
Code:
Loop1:
if SecondsChanged = 1 then
LCDout $FE,2, dec Days,"d-",dec2 Hours,":",dec2 Minutes,":",dec2 Seconds
SecondsChanged = 0
endif
Goto Loop1
If you only wanted to display the time each minute instead of every second just do the same thing using the MinutesChanged flag.
Code:
Loop1:
if MinutesChanged = 1 then
LCDout $FE,2, dec Days,"d-",dec2 Hours,":",dec2 Minutes<br> MinutesChanged = 0
endif
Goto Loop1
The timer can be Stopped and Started, like a stopwatch.
Code:
Gosub StopTimer
Gosub StartTimer
--------------------- ----------------------- -----------------------------
The Elapsed.bas include file also Includes another file, ASM_INTS.bas This file can also be used in your other programs as well. It handles the "Context Saving" that is required for any Assembly language interrupt.
It contains 2 macros:
INT_START
Saves the W, STATUS, PCLATH and FSR registers. This can be used at the beginning of your Interrupt routine.
INT_RETURN
Restores the W, STATUS, PCLATH and FSR registers after the Interrupt routine is finished and then returns from the Interrupt (RETFIE).
Using it in a normal Assembly language interrupt might look something like this:
Code:
Define INTHAND INT_CODE ' Tell PBP Where the code starts on an interrupt
ASM
INT_CODE
INT_START ' Save Context
... Your Interrupt routine goes here ...
INT_RETURN ' Restore Context
EndAsm
Well, I guess that covers most of it. if I've missed anything, or you still have questions, please don't hesitate to ask.
Happy holidays,
Bookmarks