We can control the counting of the stopwatch by "reading" the value of PORTB which has been stored for us by our cunning keypad reader. The code on the right is the main portion from Exercise 4.7. It sets up the hardware and then loops checking the settings in PORTB. Remember that we are reading the variable which is being set by the interrupt hander, not the actual port values themselves.
This is not a proper state machine solution as I created for the player piano, but it does work. Load up Program 4.7 and run it in the PICmicro. Note that you can press LB0 to stop the watch, LB1 to start it again and LB2 to reset the counter to 0.
void main ( void )
{
setup_hardware () ;
while (1)
{
/* use the bit 0 of PORTB */
/* to stop the watch */
if ( PORTBinputs & 0x01 )
{
state = PAUSED ;
}
/* use the bit 1 of PORTB */
/* to start the watch */
if ( PORTBinputs & 0x02 )
{
state = RUNNING ;
}
/* use the bit 2 of PORTB */
/* to reset the watch */
if ( PORTBinputs & 0x04 )
{
count = 0 ;
}
display_value ( count ) ;
}
}