Page 103 - Making PIC Microcontroller Instruments and Controllers
P. 103
llilEEt: IHE SECOND TIIER 93
In the preceding clock, the keyboard buttons are used as follows
Swl and Sw5 increment the hours.
SW2 and SW6 decrement the houls.
SW3 and SW? increment the minutes.
SW4 and SW8 decrement the minutes.
The seconds cannot be affected other than with the rcset switch.
Timer
Timerl: The Second
Tbe second timer, Timerl is the 16-bit timer/counter. This is the most powerful timer in
the MCU. As such, it is the hardest of the timers to understand and use but is also the
most flexjble of the three timers. It consists of two 8-bit registers and each register can
be rcad and wdtten to. The lmer can be sed either as a timer or as a counter depend
ing on how rhe Timerl clock selecr bir (TMR ICS), which is bit 1 in the Timerl coftol
register (IlCOl0, is set.
In Timerl, we can control the value that the timer starts its count with, and thus
change the frequency of the inte[upts. Here we are looking to se€ lhe effect of
changing the value preload into Timerl on the frequency of the interrrpts as
The higher the value ofthe pre-
reflected in a very rudimentary pseudo-stopwatch.
load, the sooner the counter will get to $FF and the faster the inteffupts willcome.
We will display the value of the prescalar loaded into the timer on t}le LCD so we
can seethe corelation between the values and the aclual operation of the interrupts,
As tlle intefupts get closer and closer together, the time left to do the main task
gets smaller and smaller and you can see this in the speed at which the stopwatch
In the following program:
SWl tums the stopwatch on.
SW2 stops the stopwatch.
SW3 resets the stopwatch.
POm, the filst potentiometer, is read and then written into TMR1 H. (TMRIL is ignor€d
in our case, but you may want to use it in a morc crjtical application.)
The (esults of the expedment are shown in the LCD display.
Let\ creep up oII the solution. We will develop the program segments and discuss
them as we go, putting the segments together later lbr a program we can run, as shown
in Program 6.4.