Page 437 - Programming Microcontrollers in C
P. 437
422 Chapter 8 MCORE, A RISC Machine
}
}
output_time();
}
}
Here we are starting to write a program. Therefore, it is smart to
do all things correctly. For example, we will attempt to avoid magic
numbers by defining mnemonics for the numbers used in the program.
Also, whenever a new function is written, its function prototype will
be immediately inserted into a function prototype list at the beginning
of the program. A series of external variables is used. It is usually
better to use local variables when working with parameters in several
different functions. In that case, the parameters can be passed as
arguments to the functions when they are called. This approach avoids
debug problems where it becomes difficult to determine where
variables are changed in different functions. In the case here, the
variables hours, minutes and seconds are changed in only one
function, keep_time(), and the variable count is changed only
in the interrupt service routine or the reset time function. Therefore,
there is no uncertainty as to where the variables are changed.
Note the structure of the if() statements in the function
keep_time(). The nesting of these statements
if()
{
.
if()
{
.
if()
{
.
}
}
}
causes the first test to be executed every time the function is executed.
The argument of the first test must be TRUE when the second test is
executed, and the argument of the second test must be TRUE when
the third test is executed, and so forth. Most of the time when the
function is executed, the total effort required by the above nested if