VAR (variable) directive, all the
variables are established, the first
being the speaker connection. It’s
easier to understand that “spkr” is
speaker rather than p20.
■ FIGURE 2
spkr con 20
‘Initialize Speaker Connection
individual digits to be displayed on an LCD.
Next, the variables required to store the time and
display values are reserved in RAM using the VAR directive.
This program only needs byte-size variables.
tns var byte
mns var byte
scs var byte
ths var byte
mn var byte
sc var byte
hscore var byte
vscore var byte
‘0x:xx 10’s digit display variable
‘x0:xx 1’s digit display variable
‘xx:0x 1/10’s digit display
‘xx:x0 1/100’s digit display
‘total minutes variable for clock
‘total seconds variable for clock
‘Home Score variable
‘Visitor Score variable
The LCD needs to be initialized as a 2x16 LCD with the
cursor block visible and blinking (scrblk). This command
also clears the display (clear) and puts the cursor at the
first position (home). We also pause 500 milliseconds
before this command to allow the LCD hardware to power
‘ Initialize LCD to 2x16 *********
Next the program confirms the display is working by
showing “ScoreBoard” on the first line and delaying it for
one second so we will see it.
‘ Initialize Section ***********
‘ Display Banner of “ScoreBoard” for 1 second ****
The program also has to initialize the clock and score.
This is done by initializing the variables.
‘ Initialize clock to 20:00 and Home 0 vs Visitor 0
mn = 20
sc = 0
hscore = 0
vscore = 0
The time is actually broken out from the two time
variables by math functions. We get the first digit by
dividing by 10, then use the remainder for the ones digit.
We do the same for the two digits beyond the decimal
point. This is a simple way to break up a number into
‘ Create timer values to be displayed ***
tns = mn/10 ‘ Ten’s digit is minutes divided by 10
mns = mn//10 ‘ One’s digit is the remainder
scs = sc/10 ‘ Tenths digit is seconds divided by 10
ths = sc//10 ‘ Hundreths is the remainder
The calculated data is displayed on the LCD. The
second line is controlled by the SCRRAM+$40 command.
The second line position starts at 40 hex on the LCD
character map inside the LCD display, as seen in Figure 2.
The position you want is then added to the value.
Notice I add “ 14” to position the visitor score and it is
treated as decimal 14, not a hex value. This is because the
Atom compiler doesn’t see a ‘$’ in front of it, so it converts
it and does the math. Nice little feature.
‘ Display the time on the LCD ***
lcdwrite 17\16,outc,[Clear,home,”HOME “,dec tns,
dec mns,”:”,dec scs,dec ths,” VIS”]
dec hscore,scram+$40+14,dec vscore]
The main loop is entered next. This just decrements the
clock and updates the display in a similar fashion to what we
did earlier. The only difference is the clock decrement calculation. Also notice the “pause 975” at the top of the loop. This
is a crude adjustment for accuracy to make the loop close to
one second. I just timed it next to a stopwatch via trial and
error to get that number. In fact, it’s probably a little off since
I removed some code from the original program for this article. You will have to adjust that by making it slightly larger.
‘ Main Loop of Code ***
if sc <> 0 then
sc =sc - 1
elseif sc = 0 and mn > 0
sc = 59
mn = mn - 1
‘ Adjustment for accuracy of
‘ Test if seconds is not zero
‘ Reduce seconds by one
‘ Seconds is zero so test
‘ minutes also
‘ Reset seconds to 59
‘ Reduce minutes by one
The Home score and Visitor score are next. All these
sections do is look for a switch to be pressed. If one of the
switches is pressed, the score is incremented and then it
waits for you to release the switch. This is actually lousy
code writing because holding the button stops the clock. It
also requires you to hold the switch for almost a second
February 2007 85