EE 109 - Spring 2026 Introduction to Embedded Systems

Lab 5

Digital Stopwatch

Honor Statement

This is an individual assignment. The code you write, use for the demo and submit should be wholly your own and not produced by working in teams or using, in part or in whole, code written by someone else (found online, from a fellow student, acquaintance, etc.). You will be asked to verify this when you submit your code on Vocareum by signing your name in the honor.txt file on Vocareum. Penalties for violation include a 0 on the assignment and a potential further deduction or referral to Student Judicial Affairs.

Introduction

In this lab exercise you will use your Arduino board and the LCD shield to implement the functionality of a digital stopwatch. This lab will incorporate embedded coding concepts from multiple I/O modules including timers and digital I/O in a single project. You may utilize code from other labs or in class demos as modules/functions for this project.

To see a short video demonstrating the operation of lab, click here.

For a copy of the Lab 5 grading sheet, click here.

Getting Started

To get started, create a “lab5” folder, download the lab5.zip file from the class web site and put it in the “lab5” folder. The zip files contains a lab5.c file and a Makefile to use for the program and the Lab5_Answers.txt file for the review questions. Copy your lcd.c and lcd.h files from the lab4 folder to the new lab5 folder.

The lab5.c template file should be used as a starting point for the program. Add code to implement the stopwatch as described below in Task 2.

Before developing the code to implement the stopwatch we want to expand our ability to write strings of characters on the LCD. We will need to be able to move to any location on the LCD and output a string of characters starting at that position.

Displaying Text Strings on the LCD

In many of the EE109 labs we will have to display text of some type on the screen of the LCD. In the previous lab we used the lcd_writedata function to display a single character on the LCD, but it would be better if we could display an entire string of charaction with just one function call, and had the ability to display the strings anywhere on the screen. In this section we will learn how to do that.

Character strings in C

The lcd.c file contains a function, lcd_stringout, that will write to the LCD a string of multiple ASCII characters starting at wherever the LCD cursor is located. In the C language these are often called “C strings” and are simply a “char” array containing the characters to be displayed.

It is very important that this array be a proper and correct collection of characters or garbage is likely to end up being displayed on the LCD. A array that holds a string of characters in C has to:

  1. Contain any number of printable ASCII character codes. The code for the first character in the string is in array location 0, the next in location 1, etc.

  2. Contain a zero byte (also called a “NULL” byte) in the next array location after the last ASCII character to be display. This is just a byte is containing the value zero. The zero byte marks the end of the characters in the string.

There are many ways to create a C string. For example to make a string containing “USC” we could do this.

char school[4] = "USC";

The compiler puts the ASCII code for ‘U’ (0x55) in school[0], the code for ‘S’ (0x53) in school[1], the code for ‘C’ (0x43) in school[2] and puts a zero byte in school[3]. It’s very important that the array was declared as size 4 even though we only had 3 characters to put in it since the compiler needs the extra place in the array for the zero byte.

We could have also assigned the array values individually.

char school[4];

school[0] = 'U';
school[1] = 'S';
school[2] = 'C';
school[3] = 0;

The last line could also have been written as

school[3] = '\0';

but it would NOT be correct to write the last line as

school[3] = '0';

since this assigns school[3] to be the ASCII value of 0 character (0x30).

No matter how the C string is created, it is very important that it is a correctly formatted array with the zero byte at the end. If the array elements do not contain proper ASCII codes, or the zero byte is missing, all sorts of strange things will appear on the LCD when it is sent to the LCD, and the Arduino’s program will probably die at that point.

Displaying strings of characters

To write a string of characters to the LCD, the “lcd.c” file contains the the function lcd_stringout. This routine writes a string of multiple characters, as described above, to the LCD starting at the current cursor position. The argument to lcd_stringout must be pointer to a standard C string, an array of “char” variables each containing an ASCII character code, terminated by a zero byte. Normally in programs we just write the name of the array containing the C string as the function argument and the compiler knows to pass a pointer to that string when the function is called.

Open up the “lcd.c” file and look at how the lcd_stringout function works. It contains a loop that does the following:

We can pass to the lcd_stringout function an array of ASCII codes either by our first filling an array in one of the ways shown above, or by just putting the string in the function call as the argument. For example to print our name we just need to do

lcd_stringout("Tommy Trojan");

Keep in mind that the lcd_stringout function has no idea where it is writing characters on the screen. It just writes characters at whatever place on the screen that the cursor happens to be sitting. To be able to write anywhere on the screen we need to use another function to tell the LCD where the string should go.

Moving the Cursor on the LCD

In order to write characters to various places on the display, we need to ability to move the cursor to one of the 32 character positions prior to sending the ASCII data. Each character position on the display has a unique address in the same way that bytes in computer’s memory have unique addresses. The “lcd.c” file contains the function lcd_moveto that accepts as arguments a row (0 or 1) and column number (0 to 15) and translates that to the command to move the cursor to the position with the correct address. For example, to move the the fifth column (column 4) on the second row (row 1), we would use this command.

lcd_moveto(1, 4);

The lcd_moveto function is designed to hide from the user the ugly details about how the LCD actually addresses the characters in the display but instead just uses a row number (0 or 1) and a column number (0 to 15).

Once the cursor has been moved to a position, any subsequent data bytes sent will cause characters to appear starting at this position and extending to the right. Note: if you write more characters than will fit on a line, it doesn’t automatically wrap to the next line. It just keeps writing characters on that line even if some go past the right side and can’t be seen. If you want the additional characters to go on the second line, you have to use lcd_moveto to position the cursor there before writing the additional characters.

Task 1: Write the Splash Screen

All EE109 labs from now on should have a splash screen on the LCD that shows your name and the which lab it is.

  1. In the lab5.c file, add code to iniitialize the LCD module by calling the lcd_init function.

  2. In the section of the file that says to add the splash screen, use the lcd_moveto function to move to a position on the first line and then use lcd_stringout to display your name, or as much as will fit. If your name is less than 16 characters, try to center it on the line by adjusting the column argument to the lcd_moveto call.

  3. Move to the second line and display the name of the lab assignment.

  4. Add a call to the delay_ms function to delay the program for one second or more so people have time to read the splash screen.

  5. Call lcd_writecommand with an argument of 1 to clear the screen so parts of the splash screen are not left on the display while the rest of the program runs.

Image

Stopwatch Application

The Arduino and LCD shield will be used to implement a stopwatch application that counts upwards in increments of tenths of seconds from 0.0 to 59.9 seconds. It will provide the ability to start, stop, and reset the stopwatch back to 0.0. It should also implement a “lap” feature which freezes the displayed time at the instant the “lap” button is pressed while still keeping the internal watch time incrementing. When “lap” is pressed again (or “start” is pressed again) the internal watch time (which has been running) should be re-displayed and then continue as normal.

Note, while we can use delay functions to cause things to happen every 0.1 seconds, this approach will not give us good results in this lab for several reasons. Recall that when using delay functions the time is measured only while the function is executing and it will miss the time associated with the code checking for button presses or updating the LCD. From previous labs we know that the time we miss this way accounts for for several milliseconds each iteration of the loop which adds up to seconds when measuring durations close to a minute. The alternative approach we will be using in this lab is to use a hardware timer that runs in parallel to our while (1) loop code that continuously measures the time. To get updates from the hardware timer at precise time intervals, we will enable the timer interrupts that will call our interrupt routine, a special C function.

State Machine Approach

The stopwatch is implemented as a state machine with three states: PAUSE, RUN and LAPPED. Presses of the button will cause updates/transitions between states as shown below and may cause actions to be taken depending on the transition that occurred. What needs be done in each state (whether the time is incremented and what is displayed) should be fairly intuitive. The program will also need some sort of internal representation of the time (more on that below) and logic to decide when to update the display.

Image

With your text editor open the lab5.c file and look at how it is structured. The main loop of the program is performing three tasks each time around the loop:

While that loop is running, your timer module is also running generating interrupts every 0.1 second. The main loop and the ISR can be thought of as two separate programs, both running at the same time, that communicate about what needs to happen through the state variable and flag variables. For example, the code in the timer ISR can set a flag variable to indicate that the internal time has changed due to the ISR being invoked. The main program can check this variable, and the state variable, to determine whether the displayed time on the LCD needs to be updated.

Button Inputs

As input to your stopwatch you will use two push-buttons on your breadboard.

Below is a schematic diagram of the circuit for Lab 5.

Image

Task 2: Configure the Timer Module

The counting action of the stopwatch is based on using the 16-bit TIMER1 module to generate an interrupt every 0.1 seconds. In order to make TIMER1 interrupt every 0.1 seconds several things have to be done to initialize it.

Step 1: Find the counter modulus and prescaler values

The counter need to be configured to count up to a specified value at some counting rate so it reaches that value after exactly the amount of time required has elapsed. Configuring the timer requires determing these two values: the counting rate and the value to count to.

To find these, first calculate the number of processor clock cycle that happen during this time period. The Arduino uses a 16mHZ clock, and for our stopwatch we want to have a 0.1 second elapsed time. If there are 16 million clock periods in one second, then in 0.1 second we will have 1.6 million clock periods. If our timer counts at the rate of 16mHz from 0 to 1.6 million, 0.1 seconds will have gone by. Next, determine if this number, 1,600,000, will work with TIMER1. TIMER1 is a 16-bit timer so the number to count up to must fit within 16-bits. The maximum 16-bit number is 65,535, so 1,600,000 is way too big. To fix this we need to slow TIMER1 down so it not counting as fast and we can then use a smaller number to count up to.

TIMER1 incorporates a “prescaler” which can be used to divide the incoming 16mHz clock down to a lower frequency. You can’t select any arbitrary divisor for the prescalar but have to use one of four possible divisors: 8, 64, 256 or 1024. Each of these divide the 16mHz clock down to a lower frequency which means TIMER1 is counting slower, and as a result the number it counts up to can be smaller for same amount of elapsed time.

For example, suppose our TIMER1 prescalar could divide by 4 (it can’t but we’ll use that number for this example). Using this prescalar value TIMER1 is now counting at a 16mHz/4 = 4mHz rate, and it would need to count to 1,600,000/4 = 400,000 to implement a 0.1 second elapased time.

To find a prescalar value that works for the stopwatch, try each of the possible prescalar values (8, 64,256,1024) to see which one results in counter modulus value that fits within a 16-bit number for implementing a 0.1 second counter interval. You are looking for a count modulus that

If there are more than one pair of prescalar and counter modulus that meet these two requirement, you can just pick any of the pairs to use.

Step 2: Determine the TIMER1 register contents

Assuming you found a prescalar and counter modulus value that result in a 0.1 second timer interrupt interval, you can now determine how the bits in the TIMER1 register must be set to implement the interval.

Once the prescalar bits are set to a value that selects one of these divisors, the timer starts counting. Conversely, if you want to stop the timer at any point, setting the three prescalar selection bits to 000 turns the prescaler off and this stops the counting of the timer. The timer is effectively turned on and off by changing the prescaler settings in register TCCR1B.

For more detailed information on using the timer, refer to the timer lecture notes that are linked in the Lab 5 section of the Labs web page.

Important: The information provided in the slides and video about setting up the timer were for generating interrupts every 0.25 seconds. That was just an example of configuring the timer and is not the timer interrupt interval you are using in this lab.

Step 3: Add code to lab5.c for TIMER1

In the lab5.c file, look for three functions near the top of the file: timer1_init, timer1_start, and timer1_stop. You will need to add few lines to these functions.

The timer1_init routine is called once at the start of the program and configures TIMER1 to produce interrupts every 0.1 seconds. There are already two lines in the function that set how the timer operates and enables it to generate interrupts. After these lines add code to store the value you determined above for the counter modules in the OCR1A register.

The timer1_start function is called to start the operation of the timer by setting the prescalar bits to the values you determined above. Add code to configure the CS12, CS11 and CS10 bits in the TCCR1B register to the setting you determined above. Note that this should be done using the symbolic names of the bits, not by using the number of the bit position. For example, if you want to set the CS12 bit to a one, the following code will work.

    TCCR1B |= (1 << CS12);

The timer1_stop function is used to stop the timer from counting and generating interrupts. As described above, the timer is stopped by clearing the CS12, CS11 and CS10 bits in the TCCR1B register all to zeros. Add code to the timer1_stop function to clear these bits.

Task 3: Test the Timer Module

In this task we want to have the program generate a signal every time the timer interrupts to confirm that you have configured it properly for interrupts every 0.1 seconds.

The code for testing the interrupts has already been added to the lab5.c file and can be compiled and downloaded with the command

make timer_test

This compiles the code to have the TIMER1 ISR do an XOR operation on the PC5 bit that can be observed with the oscilloscope. The XOR operation will flip the PC5 bit the other state (0→1 or 1→0) each time the ISR is executed. If the scope shows this bit changing every 0.1 seconds then we know your code has correctly configured TIMER1.

Download the code to your Arduino with the make timer_test command and connect a scope probe to the PC5 pin to observe the signal. If the ISR is running every 0.1 seconds, this should create a squarewave signal on PC5 with a rising or falling edge every 0.1 seconds resulting in a signal frequency of 5 Hz.

Image

Show one of the teaching staff the 5 Hz test signal from PC5 on the scope to receive credit for completing this part of the assignment.

Task 4: Improving the Stopwatch

If the stopwatch is implemented as described above with three states (PAUSE, RUN and LAPPED) and debouncing used on all four transitions, it works but has a problem. When the stopwatch is paused, if you press and hold down the Start_Stop button button, the stopwatch will start timing as soon as the button is pressed, but the display is not updated with the advancing time until the button is released (Try it!).

If you examine the code in lab5.c you can see this is because the program is stuck in the debouncing routine as long as the button is held down. When the button is pressed the program starts the timer and sets the state to RUN so TIMER1 is interrupting and the ISR is causing the time to advance, but as long as the button is held down the program is in the debouncing routine and it can’t update the display. We would prefer to see the time start to advance immediately when the button is pressed, but still incorporate the debouncing.

For this task we ask that you find a solution to this problem.

Hint: We can see the problem is caused by the while loop in the debouncing function that locks the program in there until the button is released. Rather than having that loop handle the debouncing, can the program transition to a special state that handles the debouncing using the main while loop instead of a special one just for debouncing? Can this new state do the debouncing, but still allow the main loop of the program to run and the display can be updated?

Whatever states you add to allow the timer and LCD displayed time to be updating while you hold down the START button for a long duration, consider where else that solution is needed. Is a similar state needed for transitioning back from the RUN to STOP? From RUN to LAPPED? From LAPPED to RUN? Add states in all the necessary places.

When you think you have a solution to this problem add it to your lab5.c code. You should be able to show that pressing Start_Stop when in the PAUSE state immediately starts the display updating even if you are holding the button down.

Show this improved version of the stopwatch to an instructor to receive credit for this part of the lab assignment.

Results

When your program is running you should be able to confirm the following

The answers to the review questions below should be edited into the Lab5_Answers.txt file. The Lab5_Answers.txt file and all source code (lab5.c, lcd.c, lcd.h and Makefile) must be uploaded to the Vocareum web site by the due date. See the Assignments page of the class web site for a link for uploading.

Please make sure to save all the parts used in this lab in your project box. These will be needed for labs throughout the rest of the semester. We suggest also saving all the pieces of wire you cut and stripped since those will be useful in later labs.

Review Questions

Answer the following questions and submit them in a separate text file (“Lab5_Answers.txt”) along with your source code.

  1. Review the conceptual operation of a timer interrupt.

    1. For a 16-bit timer with clock frequency of 16MHz (like the Arduino) and prescalar of 64, how much time corresponds to a timer value of 2000?
    2. With a clock frequency of 16MHz and prescalar of 8, what is the longest amount of time we can track with an 8-bit hardware timer.
  2. The Timer modules we have used also have the ability to make an output pin turn ON (set), OFF (clear), or Toggle when the timer reaches the OCR1A or OCR1B values (i.e. the hardware can automatically control the output value of a pin). By searching the data sheet (ATmega328P datasheet is linked on our website from the Resources page) answer the following question:

    1. TIMER1 (the 16-bit timer) can control the pins that are associated with OC1A and OC1B signals. Find to what pins these signals are wired by looking at Figure 1.1 ("28 PDIP" package diagram) on page 12 of the data sheet, or alternatively, Tables 14-3, 14-6, or 14-9.
    2. In this lab we use TIMER1 in the ``Clear Timer on Compare'' or CTC mode. In this mode when the counter reaches the value in OCR1A register it generates an interrupt and starts counting again from zero. Using the information in section 16.11.1 and table 16-1, describe what the OC1A and OC1B pins would do when the timer reaches the OCR1A value if during initialization we used the statement `TCCR1A = 0x60;`