User`s manual

Dynamic C Users Manual digi.com 85
application tasks will each have a 1024 byte stack, ten will each have a 2048 byte stack, and an extra stack
is declared for the statistics task.
5.10.4.3 Examples
The following sample programs demonstrate the use of the default configuration supplied in UCOS2.LIB
and a custom configuration which overrides the defaults.
Example 1
In this application, ten tasks are created and one semaphore is created. Each task pends on the semaphore,
gets a random number, posts to the semaphore, displays its random number, and finally delays itself for
three seconds.
Looking at the code for this short application, there are several things to note. First, since µC/OS-II and
slice statements are mutually exclusive (both rely on the periodic interrupt for a “heartbeat”), #use
“ucos2.lib” must be included in every µC/OS-II application (1). In order for each of the tasks to have
access to the random number generator semaphore, it is declared as a global variable (2). In most cases, all
mailboxes, queues, and semaphores will be declared with global scope. Next, OSInit() must be called
before any other µC/OS-II function to ensure that the operating system is properly initialized (3). Before
µC/OS-II can begin running, at least one application task must be created. In this application, all tasks are
created before the operating system begins running (4). It is perfectly acceptable for tasks to create other
tasks. Next, the semaphore each task uses is created (5). Once all of the initialization is done,
OSStart() is called to start µC/OS-II running (6). In the code that each of the tasks run, it is important
to note the variable declarations. Each task runs as an infinite loop and once this application is started,
µC/OS-II will run indefinitely.