UC/OS II多任務(wù)切換例程
#include “config.h”
#define TaskStkLengh 64
OS_STK TaskStk [TaskStkLengh];
OS_STK TaskStk1 [TaskStkLengh];
OS_STK TaskStk2 [TaskStkLengh];
OS_EVENT *TASK1;
OS_EVENT *TASK2;
OS_EVENT *TASK3;
void Task0(void *pdata); //Task0 ÈÎÎñ0
void Task1(void *pdata);
void Task2(void *pdata);
uint8 err;
void UART0_Ini(void)
{ uint16 Fdiv;
U0LCR = 0x83;
Fdiv = (Fpclk / 16) / UART_BPS;
U0DLM = Fdiv / 256;
U0DLL = Fdiv % 256;
U0LCR = 0x03;
}
void UART0_SendByte(uint8 data)
{ U0THR = data; // ·¢ËÍÊý¾Ý
while( (U0LSR&0x40)==0 ); // µÈ´ýÊý¾Ý
}
int main (void)
{
OSInit
();
OSTaskCreate (Task0,(void *)0, &TaskStk[TaskStkLengh - 1], 0);
TASK1= OSSemCreate(0);
TASK2= OSSemCreate(0);
TASK3= OSSemCreate(0);
TargetInit();
OSStart ();
return 0;
}
/*******************************************************************************************
** Task0 ÈÎÎñ0
*******************************************************************************************/
void Task0 (void *pdata)
{
UART0_Ini();
OSTaskCreate(Task1, (void *)0, &TaskStk1[TaskStkLengh-1], 9);
OSTaskCreate(Task2, (void *)0, &TaskStk2[TaskStkLengh-1], 10);
OSSemPost(TASK1);
for (;;)
{
OSSemPend(TASK1,10,&err
if(err==OS_NO_ERR)
{
UART0_SendByte(0x01);
OSSemPost(TASK2);
}
}
}
/******************************************************************************************* Task1 ÈÎÎñ1
*******************************************************************************************/
void Task1 (void *pdata)
{
UART0_Ini();
for (;;)
{
OSSemPend(TASK2,10,&err);
if(err==OS_NO_ERR)
{
UART0_SendByte(0x02);
OSSemPost(TASK3);
}
}
}
void Task2 (void *pdata)
{
UART0_Ini();
for (;;)
{
OSSemPend(TASK3,10,&err);//»&
if(err==OS_NO_ERR)
{
UART0_SendByte(0x03);
OSSemPost(TASK1);
}
}
}
三個任務(wù)分別通過UART0輸出0X01,0X02,0X03,可以學(xué)習(xí)理解信號量的基本用法及多任務(wù)的調(diào)度方法.