Scuola Superiore S. Anna

Research Teaching Personal Contact


Quick links


Courses 2012







Embedded Systems - Model-Based Design

Stopwatch application (hand coded)

The first step is the definition of a possible architecture for our application. Several options are possible. In this example, a simple structure with a single task is used. You are encouraged to find your own.

The stopatch state machine reacts to a set of events that include pressing buttons and a timer event with a period of 1/10 of a second. Hence, the program consists of a task activated with a period of 100ms, which invokes and some simple code that detects edges in the button presses and forwards the events to the state machine dispatcher. The OSEK-related parts are in gold, the code to be written in blue.

Stage 1: Button presses and events

The first step is to build a very simple program that is simply going to detect the button press events and forward them to a task (activated every 100ms) that is doing nothing else but lighting a led on the board whenever one of the four buttons is pressed.

The code consists of four files:
  • an OIL file: conf.oil that defines the application as consisting of two code files (a main file and a Button.c file that contains the code for edge detection), a single task (TaskSW), triggered by an alarm (AlarmSW) connected to a counter (myCounter).
  • a main file: main.c performing all initializations, creating the task and then entering an infinite loop.
  • two files: Button.c and Button.h that provide the functions and the data structure for managing the buttons.
The code can be found here

Stage 2: Managing the time

The second step is add a module that manages the time using the (implicit) 100ms event to count tenths of a second, seconds, minutes and hours and display them on the LCD screen. The OIL file conf.oil now needs to be modified to add the new code files. Similarly, the main file main.c is changed to add the necessary initialization code.

The code consists of additional files:
  • two files Watch.c and Watch.c that provide the functions and the data structure for managing the buttons.
  • three files LCDdisplay.c, LCDdisplay.h (public functionss and data for the LCD module code) and LCDdisplay_local.h (private functions and data for the LCD module code)that provide the functions and the data structure for managing the output on the LCD display.
The code can be found here

Stage 3: The first simple state machine

The next step is to build our first version of the stopwatch state machine. The first state machine will cycle through the four main modes: Time display, Time set, Alarm set and Chrono.

The code consists of two additional files:
  • two files: SWatchFSM.c and SWatchFSM.h that provide the code for managing the four main modes of the stopwatch.
The code can be found here
and an extended version here

Stage 4: The full stopwatch application

This part is now up to you, remember that the code for the stopwatch application must be brought to the exam and discussed.