User Tools

Site Tools


Clock Lab


In this lab you will implement a clock. Every modern device seems to include a clock, so we want one too. Your clock will look like this:

The middle numerical display shows the time in 12-hour format. The green triangles (arrows) above and below indicate touch regions above and below the clock display that allow you to set the time. The arrows that are above the display will increment the hours, minutes, and seconds of the clock, when touched. The arrows that are below the clock will decrement the hours, minutes, and seconds of the clock when touched. These clock-setting functions also have an auto-update rate that engages after the user presses the arrow for at least 0.5 seconds. Once the auto-update engages, it updates the time at a rate of 10 changes per second. For example, say the user presses the arrow below the minutes field for 0.5 seconds. After the initial 0.5 second delay, the hours will decrement automatically at a rate of 10 decrements per second. You have probably seen similar functionality in the clocks that you have used.

The implementation of the lab will be separated into two major coding sections: display and control. This will provide good practice for just about any embedded-programming project as most embedded projects are usually best broken down this way, both for implementation and for debug/verification. We will follow this general strategy throughout the semester.

Display versus Control

The display code is responsible for the functions that render the various parts of the display. This includes things like:

  • drawing the triangles that indicate the up/down touch regions that are used to set the current time.
  • drawing the characters that indicate the time.
  • incrementing and decrementing the seconds, minutes, and hours.
  • updating the time when told that a second has passed.

Note that the display code has no “sense” of time. Its job is to perform computations and to draw things on the LCD but it has no sense of when these things should be done. The control code will tell the display code when it is time to add 1 second to the current time, when to update the screen when auto-update is occurring, etc.

The control code is responsible for handling the up/down touch regions and for keeping time. It has a sense of time and controls when the display code does things. The control code has no “sense” of how the display is updated, it just controls when these updates occur. It does things like:

  • keep track of when it is time to add 1 second to the current time.
  • when to increment or decrement the display according to the auto-update rate.
  • it times how long the user presses a region on the display so it knows whether to invoke auto-update rate or not.
  • keeps track of the settling time of the Analog-Digitial Converter (ADC) used by the controller chip that reads the resistive overlay on the LCD display.

New Stuff for this Lab

  1. Implement a synchronous state machine (you are given the initial state diagram).
  2. Learn how how to debug state machines.
  3. Program the interrupt timer on the ARM processor to set the appropriate period between interrupts.
  4. Ensure that you are not missing interrupts.
  5. use the isr_function() to invoke your clockControl_tick() function.


  1. Implement a synchronous state machine for the first time.
  2. Learn how to compute and set the period for the interrupt timer on the ARM processor.
  3. Use the interval timer package from the previous lab to measure execution time of your tick function.
  4. Learn how to identify and separate control and display functions.
  5. Use interrupts two different ways: (1) checking a flag set by the interrupt service routine, and (2) using the interrupt service routine to directly invoke clockControl_tick().


  1. You must create files: clockDisplay.c, clockControl.c, and clockMain.c. The clockDisplay files contain the code that performs all of the updates to the LCD. The clockControl files contain the controlling state machine that performs all timing-related functions. You will implement clock control using a single synchronous state machine as discussed in Chapter 5 of the text. clockMain.c will setup the interrupts and will start the clock running.
  2. The contents of the .h files must be as given below. Do not modify the .h code. Use it as given.
  3. You must demonstrate both methods of using interrupts to control your clock (see code examples below).
  4. You must use the coding strategy/standard for state machines. (I went over this in class in some depth but state it here explicitly for clarity.)
  5. You must write a debugStatePrint() function as outlined here (scroll to the bottom).


1. Implementing clockDisplay.c.
2. Implementing clockControl.c.
3. Working with the touch screen.
4. Using interrupts indirectly with the flag method.
5. Using interrupts directly.
6. Determining the interrupt period.

Pass Off

You will complete the clock lab in two milestones. Each will be passed off separately:

  • Milestone 1: clockDisplay.h and clockDisplay.c. You will completely implement the functionality for the display as described above. You must demonstrate that you can change the text size by changing a single #define. The TA should be able to say, for example, “please compile your code with a text size of 3”. You would then change the #define that controls text-size and demonstrate that your program can resize the clock display automatically. You must also implement the clockDisplay_runTest() routine described above. Goal: demonstrate that the display can be resized and that the increment/decrement functions work correctly. Note: You can achieve this milestone either with or without using display_isTouched(). If you do not use display_isTouched(), write test code that exercises the various increment/decrement functions in clockDisplay.c. The main thing is to demonstrate that clockDisplay.c works correctly and that you can resize the display.
  • Milestone 2A: clockControl.h, clockControl.c, and clockMain.c. You will completely implement these three files and your clock must run using the provided interrupt code (both versions). Demonstrate both versions of the interrupt code to the TAs and be able to explain the differences between the two approaches. Goal: demonstrate that your clock works correctly and that the increment/decrement functions work for setting time.
  • Milestone 2B: As a part of this milestone, you must show that the behavior of your state-machine matches its state-diagram. This is done by adapting the code shown in Coding and Debugging State Machines (see Debugging State Machines at the bottom of this page) to print out each state as it is entered. Compare this printed output and verify for the TA that the state-machine behavior matches the state diagram. Goal: demonstrate that your state-machine and the state-diagram behaviors match.

Submitting Source Code

Follow this procedure to submit your source code to learning-suite.

Overall Grading

  • Milestone 1: 30%.
  • Milestone 2A: 30%.
  • Milestone 2B: 10%.
  • Code Quality: 30%.
  • Subtract 10% if the isr invocation count differs from internal interrupt count by more than 1 (see the printfs near the bottom of the main() function that implements the flag method (above).
  • Subtract 10% if the clock is not very responsive, if it seems laggy, if it flashes the display when updating the time.
lab_4.txt · Last modified: 2019/05/07 11:24 by hutch