User`s manual

Dynamic C Users Manual digi.com 87
Example 2
This application runs exactly the same code as Example 1, except that each of the tasks are created with
1024-byte stacks. The main difference between the two is the configuration of µC/OS-II.
First, each configuration constant that differs from the library default is defined. The configuration in this
example differs from the default in that it allows only two events (the minimum needed when using only
one semaphore), 20 tasks, no queues, no mailboxes, and the system tick rate is set to 32 ticks per second
(1). Next, since this application uses tasks with 1024 byte stacks, it is necessary to define the configuration
constants differently than the library default (2). Notice that one 512 byte stack is declared. Every
Dynamic C program starts with an initial stack, and defining STACK_CNT_512 is crucial to ensure that
the application has a stack to use during initialization and before multi-tasking begins. Finally
ucos2.lib is explicitly used (3). This ensures that the definitions in (1 and 2) are used rather than the
library defaults. The last step in initialization is to set the number of ticks per second via
OSSetTicksPerSec (4). The rest is identical to example 1 and is explained in the previous section.
// 1. Define necessary configuration constants for uC/OS-II
#define OS_MAX_EVENTS 2
#define OS_MAX_TASKS 20
#define OS_MAX_QS 0
#define OS_Q_EN 0
#define OS_MBOX_EN 0
#define OS_TICKS_PER_SEC 32
// 2. Define necessary stack configuration constants
#define STACK_CNT_512 1 // initial program stack
#define STACK_CNT_1K OS_MAX_TASKS // task stacks
// 3. This ensures that the above definitions are used
#use "ucos2.lib"
void RandomNumberTask(void *pdata);
// Declare semaphore global so all tasks have access
OS_EVENT* RandomSem;
void main(){
int i;
// Initialize OS internals
OSInit();
for(i = 0; i < OS_MAX_TASKS; i++){
// Create each of the system tasks
OSTaskCreate(RandomNumberTask, NULL, 1024, i);
}
// semaphore to control access to random number generator
RandomSem = OSSemCreate(1);
// 4. Set number of system ticks per second
OSSetTicksPerSec(OS_TICKS_PER_SEC);
// Begin multi-tasking
OSStart();
}