Page 330 - Programming Microcontrollers in C
P. 330
Cosmic MC68HC16 Compiler 315
the EXTAL signal is 32768 Hz, not some number around 16 MHz.
The formula to calculate the periodic interrupt time is
5 (
P
T= 4 TM 11PTP 1 ) /F
+
I
pit extal
Here PITM is the 8-bit field with the same name found in the
PITR. PTP is a single-bit field in the PITR that can have a value of
either 0 or 1. As such, if PTP is 1, the prescaler value of 512 is used.
Otherwise, when there is no prescaler, the value in the parentheses
reduces to 1. With the values placed in these fields in the above code,
i.e., PTP of 1, and PITM of 64, and with a 32768-Hz external crys
tal, the periodic interrupt time should be one second.
Two additional blocks of code are added to the applications sec
tion of the code. This code is shown below:
if(sec>MAX_SEC)
{
sec=0;
if(++mts>MAX_MIN)
{
mts=0;
if(++hrs>MAX_HOURS)
hrs=MIN_HOURS;
}
}
if(been_here && !new_input)
{
been_here=OFF;
output_time();
}
The first eight lines of code here are taken directly from similar
clocking code found in Chapter 4. This code merely counts the time
in seconds, minutes, and hours. The second block of code determines
if a PIT has been serviced, and if it has, it resets the been_here
flag that indicated that the PIT service routine has been entered and
then sends the time out the serial port when output_time( ) is
executed.
Here is a case where several subroutines are used in the applica
tions portion of the program. output_time( ) calls functions
putch( ) and send_out( ). In turn send_out( ) calls