Embedded Systems Development and Labs; The English Edition
7.2.6 Exercises
Improve the program by implementing inter-task communication and synchronization such that every time when the 8-SEG LED displays a character the serial port also outputs the same character.
7.3 uC/OS Application Lab
7.3.1 Content of the Lab
Write a start-stop watch program that uses the uC/OS-II kernel. The program is a simple one-button stopwatch that displays minutes, seconds, and tenths of seconds in the following format: 99:59.9
The stopwatch has a single button that cycles the watch through three modes: CLEAR -> COUNT -> STOP -> CLEAR …
7.3.2 Stopwatch Tasks
There are five tasks for the complete program, including the start-up task. The priorities assigned to each task follow the rate monotonic scheduling rule. Following are the task execution rates and the assigned priorities:
Task |
Task Period |
Priority |
StartTask() |
One time only |
4* |
UpdateTimeTsk() |
1ms |
6 |
ScanSwTsk() |
10ms |
8 |
DispTimeTsk() |
100ms |
10 |
TimerModeTsk |
1/keypress |
12 |
* Required to be the highest priority.
The following describes briefly the tasks functions:
(1)StartTask(): This task starts by initializing the kernel timer with OSTTickInit(). It then initializes the LCD and creates the rest of the tasks. Once the rest of the tasks are complete, the start-up task suspends itself indefinitely.
(2)UpdateTimeTsk(): This is the primary time keeping task. It has the highest priority to keep the stopwatch accuracy within 1ms. The task increments a global variable called msCntr every millisecond.
(3)ScanSw(): This is the switch-scanning and debouncing task. The main requirement is that it has to run with a period that is at least one-half the switch bounce time. Since the task period is 10ms, it is designed for switch bounce times less than 20ms. It also rejects noise pulses up to 10ms wide. Notice that the task period does not have to be exactly 10ms. It can vary as much as 20% without causing significant errors. When a valid keypress is accepted, ScanSw() signals a semaphore event flag, SwFlag. This flag can than be used by other tasks to service a keypress. In this application the timer mode task changes the mode each time the key is pressed.
(4)TimerModeTsk(). This task is a simple state machine that controls the mode of the stopwatch. Each time a key is pressed, the SwFlag semaphore is signaled by the switch-scanning task. When SwFlag is