📄 example1.c
字号:
/* * File: example1.c * * uC/OS Real-time multitasking kernel for the ARM processor. * * Simple example of using multiple tasks and mailboxes. * * Created by Marco Graziano (marcog@crl.com). * */#include "ucos.h" /* uC/OS interface */#include "pid.h" /* PID specific include */extern void PutByte(byte); /* defined in pid.c *//* allocate memory for tasks' stacks */#define STACKSIZE 64uint Stack1[STACKSIZE];uint Stack2[STACKSIZE];uint Stack3[STACKSIZE];/* mailbox event control blocks */OS_EVENT *Mbox1;OS_EVENT *Mbox2;OS_EVENT *Mbox3;char PassMsg[] = "Example 1";/* * Task running at the lowest priority. * Wait for a message in the first mailbox and post * a messages to the third mailbox. */voidTask1(void *Id){ char *Msg; uint err; for (;;) { /* wait for a message from the input mailbox */ Msg = (char *)OSMboxPend(Mbox1, 0, &err); /* print task's id */ PutByte(*(char *)Id); /* post the input message to the output mailbox */ OSMboxPost(Mbox2, Msg); }}voidTask2(void *Id){ char *Msg; uint err; for (;;) { /* wait for a message from the input mailbox */ Msg = (char *)OSMboxPend(Mbox2, 0, &err); /* print task's id */ PutByte(*(char *)Id); /* post the input message to the output mailbox */ OSMboxPost(Mbox3, Msg); }}voidTask3(void *Id){ char *Msg; uint err; for (;;) { /* wait for a message from the input mailbox */ Msg = (char *)OSMboxPend(Mbox3, 0, &err); /* print task's id */ PutByte(*(char *)Id); /* post the input message to the output mailbox */ OSMboxPost(Mbox1, Msg); }}/* * Main function. */intmain(int argc, char **argv){ char Id1 = '1'; char Id2 = '2'; char Id3 = '3'; /* needed by uC/OS */ OSInit(); /* * create the first mailbox in the pipeline with a message * in it to get the first task started. */ Mbox1 = OSMboxCreate(PassMsg); /* create the remaining mailboxes empty */ Mbox2 = OSMboxCreate((void *)0); Mbox3 = OSMboxCreate((void *)0); /* * create the tasks in uC/OS and assign increasing * priorities to them so that Task3 at the end of * the pipeline has the highest priority. */ OSTaskCreate(Task1, (void *)&Id1, (void *)&Stack1[STACKSIZE - 1], 3); OSTaskCreate(Task2, (void *)&Id2, (void *)&Stack2[STACKSIZE - 1], 2); OSTaskCreate(Task3, (void *)&Id3, (void *)&Stack3[STACKSIZE - 1], 1); /* start the game */ OSStart(); /* never reached */} /* main */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -