division is the odd number of seconds less than one minute,
but we can find that value separately by dividing and
looking for the remainder only with the “//” math function.
We do this same method again to get the hours and
minutes aligned properly.
‘ Add the accumulated minutes
minutes = minutes + (seconds / 60)
‘ Retain the left-over seconds
seconds = seconds // 60
‘ Add the accumulated hours
hour = hour + (minutes/60)
‘ Retain the left -over minutes
minutes = minutes//60
I wanted 12 hour time, not 24 hour, so I added an
IF-Then statement to make that happen.
if hour > 12 then
hour = hour - 12
endif
Because this was a subroutine, we need to add the
RETURN command.
Return
‘ Return to the main program
Finally, after all that, we get to the main loop of code
and it’s very short. We start with the “init:” label and just
clear the LCD.
init:
‘ *******************************************************
‘ Begin program code here. The time will be updated
‘ when you call the get_time routine.
‘ *******************************************************
LCDOut $fe,1
‘ Clear LCD
The main loop of code is at the “main” label. The
program jumps to the “get_time” subroutine to get the
latest seconds, minutes, and hour values and then displays
them on the LCD.
main:
GoSub get_time
‘ Update minutes and seconds
‘**** Display the elapsed time ******
LCDOut $fe, 2, DEC2 hour, “:”, DEC2 minutes, “:”,
DEC2 seconds
The program loops back to the main label and does it
over and over again.
GoTo main
End
‘ Repeat main loop
That’s all there is to making a real time clock with
PICBasic Pro and some assembly thrown in for accuracy.
NEXT STEPS
A great place to improve this program is to add
switches for presetting the seconds, minutes, and hour
variables. This way, the clock can be set to any time
you desire. Another great action would be to add some
kind of compare routine to make an alarm clock. When
the hour and minutes variables match a preset value, a
buzzer can be activated or a relay driven. There are a lot
of options you can choose from once you have the basic
clock working.
SUMMARY
I hope the addition of a little assembly wasn’t too much
for the beginners out there. I wanted to show that assembly
now and then is very helpful. Learning PIC assembly is
a good thing and you will find it’s not that tough to
understand once you’ve written a few successful Basic
programs for the PIC.
Let me know how you liked this article and what else
you’d like to see in future articles. I’m getting a lot of
feedback that helps me determine what readers want. If you
don’t tell me, I won’t know what you want to see. Email me
at chuck@elproducts.com. NV
94
September 2006