Page 443 - Programming Microcontrollers in C
P. 443
428 Chapter 8 MCORE, A RISC Machine
The function reset_time() follows. Recall from earlier
discussions that the function getch() was written specifically to
read in a character from the serial port after the function kbhit()
indicates that the keyboard has received an input. This function,
getch(), is used here. The character read in is saved in the location
c and then a switch/case statement is set up to determine what to do
with the input. It is assumed that the letters m, M, h, and H are the
valid inputs. When either an m or an M is received, the minutes will
be incremented. Detection of either an h or an H will cause the hours
to be incremented. Any other input will merely be discarded, so there
is no need for a default statement in the switch/case sequence. If an
m, either upper or lower case, is detected, the minutes will be
incremented. If the incremented value exceeds MAX_MINUTES,
minutes will be reset to 0. Similarly if an h is detected, the hours will
be incremented and if the incremented value exceeds MAX_HOURS,
hours is reset to MIN_HOURS. After these transactions are completed,
the function output_time() is executed. Without this final
function call, the setting of the time will be very jumpy, definitely
not smooth.
void reset_time(void)
{
int c;
c=getch();
switch(c)
{
case ‘m’:
case ‘M’: if(++minutes>MAX_MINUTES)
minutes=0;
break;
case ‘h’:
case ‘H’: if(++hours>MAX_HOURS)
hours=MIN_HOURS;
break;
}
output_time(); /* send out a new time after each change */
}
That is the end of the program. The program seems quite simple,
and it has been tested thoroughly. Shown below is the whole program

