Write a program on IoT board to function as a chronograph (stopwatch). There are two displays, one is the present time, another one is the timer. There is one push button to start/stop the timer.
Here is how we write the program. We set a flag to represent the state of timer: start/stop. The time update for current time is continue every second by int0. The chronograph function, chrono time, is conditionally update when the state of timer is started. int1 is used to toggle this flag.
updatedisplay()
update current time
if (state == start) update chrono time
display current time and chrono time
startstop()
if (state == stop) state = start else state = stop
main()
initialize variables: current time, chrono time, state
while(1)
doze()
Use int0 to update the current time, and int1 as the push button. Assuming one clock period is 1/100 sec. (10 ms), then int0 is set to run at 1 second (100 ticks) and int1 at 4 seconds (400 ticks).
int0 binds to updatedisplay. int1 binds to startstop.
We need to enable int1 using: ei(1)
. int0 is enable
by default. #noframe
is used to improve the machine
code generation for interrupt service routine. We will discuss this fine
points later.
Here is the source code in full (tested).
last update 21 Jan 2018