⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main.c

📁 RTOS版本:uCOSII V2.83 OSView版本:uCOS_View 1.10 开发环境:KEIL 编译器:ADS1.2 开发板:EasyARM2200 内部FLASH运行
💻 C
字号:


#include "config.h"						 //包含LPC22xx 模板的配置头文件

#define TaskStartPrio                      1
#define TaskStartStkLen                    256
OS_STK  TaskStartStk[TaskStartStkLen];				   //操作系统起动任务堆栈

#define  TEST_TASK_STK_SIZE                 128       /* Size of each task's stacks (# of WORDs)       */

#define  TEST_N_TASKS                        10       /* Number of identical tasks                     */
char          TestTaskData[TEST_N_TASKS];             /* Parameters to pass to each task               */
/*
*********************************************************************************************************
*                                               VARIABLES
*********************************************************************************************************
*/

OS_STK        TestStartStk[TEST_TASK_STK_SIZE];       /* Tasks stacks                                  */

OS_STK        TestTaskStk[TEST_N_TASKS][TEST_TASK_STK_SIZE];

INT16U        TestDly;
INT16U        TestLoops;
INT8U         MsgBoxData;

OS_EVENT     *TestTerminalMbox;
OS_EVENT     *TestRandomSem;
static  void  TestTerminalCallback(INT8U data);
static  void  TestTerminalTask(void *pdata);

OS_STK        TestTerminalTaskStk[TEST_TASK_STK_SIZE];
static  void  TestCreateTasks (void);
INT16U        TestTerminalMsgCtr = 0;
static  void  TestTask(void *data);
//----------------------------------------------------------------

//----------------------------------------------------------------


void  TaskStart (void *pdata)
{
#if OS_CRITICAL_METHOD == 3          		//Allocate storage for CPU status register 
    OS_CPU_SR  cpu_sr;	
#endif
#if ((OS_VERSION > 276 ) && (OS_TASK_NAME_SIZE > 0))
    //INT8U err;
#endif
    INT32U x, y;
//---------------------------------------------------------------------------------------  	   
 	TargetInit();        //初始化硬件
	//#if EN_USE_OS_VIEW_COMM > 0
    OSStatInit();                                          /* Initialize uC/OS-II's statistics         */
                                             /* Initialize uC/OS-View             */
    //#endif

            //这个初始化里面有延时函数,不能在任务上锁以后调用
    TestDly   = 100;
	TestLoops = 1000;

	TestRandomSem = OSSemCreate(1);
    TestTerminalMbox = OSMboxCreate((void *)0);
    OSView_TerminalRxSetCallback(TestTerminalCallback);    /* Setup Terminal callback function         */
    TestCreateTasks();   

	while (1)
	{
	    
		for (x = 0; x < 100; x++)
		{
		    for (y = 0; y < 1000; y++)
			{
			}
		}
	    OSTimeDly(200);
	}

 }
 static  void  TestTerminalCallback (INT8U data)
{    
	MsgBoxData = data;
    //OSMboxPost(TestTerminalMbox, (char *)data);
	OSMboxPost(TestTerminalMbox, (void *)(&MsgBoxData));
}
static  void  TestCreateTasks (void)
{
	INT8U  prio;
    INT8U  err;
    INT8U  i;
    char   s[20];


    prio = 2;
    OSTaskCreateExt(TestTerminalTask,                      /* uC/OS-View Terminal task                 */
                    (void *)0, 
                    &TestTerminalTaskStk[TEST_TASK_STK_SIZE - 1], 
                    prio,
                    prio,
                    &TestTerminalTaskStk[0],
				    TEST_TASK_STK_SIZE,
                    (void *)0,
                    OS_TASK_OPT_STK_CLR + OS_TASK_OPT_STK_CHK);
    OSTaskNameSet(prio, "Terminal", &err);
    #if 1
    for (i = 0; i < TEST_N_TASKS; i++) {
        sprintf(s, "Task #%2d", i + 2);
        TestTaskData[i] = '0' + i;                         /* Each task will display its own letter    */
        prio            = i + 3;
        OSTaskCreateExt(TestTask, 
                        (void *)&TestTaskData[i], 
                        &TestTaskStk[i][TEST_TASK_STK_SIZE - 1], 
                        prio,
                        prio,
                        &TestTaskStk[i][0],
					    TEST_TASK_STK_SIZE,
                        (void *)0,
                        OS_TASK_OPT_STK_CLR + OS_TASK_OPT_STK_CHK);
        OSTaskNameSet(prio, s, &err);	
    }
	#endif
}
static  void  TestTerminalTask (void *pdata)
{
#if OS_CRITICAL_METHOD == 3                                /* Allocate storage for CPU status register */
    OS_CPU_SR  cpu_sr;
#endif
    char       s[100];
    char       key;
    INT8U      err;
	char       *pMbox;

    pdata = pdata;                                         /* Prevent compiler warning                 */
    while (TRUE) 
	{
        pMbox  = (char *)OSMboxPend(TestTerminalMbox, 0, &err);
		key    = *pMbox;
        //key  = (char)OSMboxPend(TestTerminalMbox, 0, &err);

        TestTerminalMsgCtr++;
        sprintf(s, "\nTerminalMsgCtr = %05u\n", TestTerminalMsgCtr);
		OSView_TxStr(s, 1);        
        switch (key) {
            case '1':
                 sprintf(s, "\nCPU Usage = %3u%%\n", OSCPUUsage);
                 OSView_TxStr(s, 1);
                 break;

            case '2':
                 sprintf(s, "\n#Tasks    = %3u\n", OSTaskCtr);
                 OSView_TxStr(s, 1);
                 break;

            default:
                 OSView_TxStr("\n\nMicrium, Inc.",       1);
                 OSView_TxStr("\n1: CPU Usage (%)",      1);
                 OSView_TxStr("\n2: #Tasks",             1);
                 OSView_TxStr("\n?: Help (This menu)\n", 1);
                 break;
        }
    }
}
static  void  TestTask (void *pdata)
{
#if OS_CRITICAL_METHOD == 3                      /* Allocate storage for CPU status register           */
    OS_CPU_SR  cpu_sr;
#endif
    INT8U      x;
    INT8U      y;
    INT8U      err;
	INT16U     i;

        

    pdata = pdata;                               /* Prevent compiler warning                           */
    while (TRUE) {
#if 1
        OSSemPend(TestRandomSem, 0, &err);       /* Acquire semaphore to perform random numbers        */
        if (OSTCBCur->OSTCBPrio == 11) {          /* Consume more processing time for task prio. #2     */
		    for (i = 0; i < TestLoops ; i++) 
			{
                x = rand();                  /* Find X position where task number will appear      */
                y = rand();                  /* Find Y position where task number will appear      */
		    }
			TestDly = 50;
        } else if (OSTCBCur->OSTCBPrio == 10) { 
		    for (i = 0; i < TestLoops * 5; i++) 
			{
                x = rand();                  /* Find X position where task number will appear      */
                y = rand();                  /* Find Y position where task number will appear      */
		    }
			TestDly = 60;
		} else if (OSTCBCur->OSTCBPrio == 9) { 
		    for (i = 0; i < TestLoops * 10; i++) 
			{
                x = rand();                  /* Find X position where task number will appear      */
                y = rand();                  /* Find Y position where task number will appear      */
		    }
			TestDly = 70;
		} else if (OSTCBCur->OSTCBPrio == 8) {
		    for (i = 0; i < TestLoops * 20; i++) 
			{
                x = rand();                  /* Find X position where task number will appear      */
                y = rand();                  /* Find Y position where task number will appear      */
		    }
			TestDly = 80;		 
		} else if (OSTCBCur->OSTCBPrio == 7) { 
		    for (i = 0; i < TestLoops * 30; i++) 
			{
                x = rand();                  /* Find X position where task number will appear      */
                y = rand();                  /* Find Y position where task number will appear      */
		    }
			TestDly = 90;
		} else if (OSTCBCur->OSTCBPrio == 6) {
		    for (i = 0; i < TestLoops * 35; i++) 
			{
                x = rand();                  /* Find X position where task number will appear      */
                y = rand();                  /* Find Y position where task number will appear      */
		    }		 
			TestDly = 100;
		} else if (OSTCBCur->OSTCBPrio == 5) { 
		    for (i = 0; i < TestLoops * 40; i++) 
			{
                x = rand();                  /* Find X position where task number will appear      */
                y = rand();                  /* Find Y position where task number will appear      */
		    }
			TestDly = 110;
		} else if (OSTCBCur->OSTCBPrio == 4) {
		    for (i = 0; i < TestLoops * 45; i++) 
			{
                x = rand();                  /* Find X position where task number will appear      */
                y = rand();                  /* Find Y position where task number will appear      */
		    }
			TestDly = 120;		 
		} else if (OSTCBCur->OSTCBPrio == 3) { 				
		    for (i = 0; i < TestLoops * 50; i++) 
			{
                x = rand();                  /* Find X position where task number will appear      */
                y = rand();                  /* Find Y position where task number will appear      */
		    }
			TestDly = 130;
		} else {    
		    for (i = 0; i < TestLoops; i++) 
			{
                x = rand();                  /* Find X position where task number will appear      */
                y = rand();                  /* Find Y position where task number will appear      */
		    }
			TestDly = 50;
        }
        //PC_DispChar(x, y + 2, *(char *)pdata, DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY);
        OSSemPost(TestRandomSem);                /* Release semaphore                                  */
                                                 /* Display the task number on the screen              */
        OSTimeDly(TestDly);
#else
        x = random(80);                          /* Find X position where task number will appear      */
        y = random(10);                          /* Find Y position where task number will appear      */
        PC_DispChar(x, y + 2, *(char *)pdata, DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY);
        OSTimeDly(TestDly);
#endif
    }
}
/************************************************************
			主  函  数
*************************************************************/
int main (void)
{
    #if OS_VERSION > 276                    
    #if OS_TASK_NAME_SIZE > 0
	INT8U err;
	#endif
	#endif



    OSInit();                               //初始化 uC/OS-II
    OSTaskCreateExt(TaskStart, 
                    (void *)0, 
                    &TaskStartStk[TaskStartStkLen - 1], 
                    TaskStartPrio,
                    TaskStartPrio,
                    &TaskStartStk[0],
                    TaskStartStkLen,
                    (void *)0,
                    OS_TASK_OPT_STK_CHK+OS_TASK_OPT_STK_CLR );

    #if OS_VERSION > 276                    
    #if OS_TASK_NAME_SIZE > 14
    OSTaskNameSet(TaskStartPrio, (INT8U *)"Start Task", &err);
    #else
    #if OS_TASK_NAME_SIZE > 7
    OSTaskNameSet(TaskStartPrio, (INT8U *)"Start", &err);
    #endif                    
    #endif
	#endif 
    							            //创建起动任务
    OSStart();
    return 0;                              //开始多任务
}

/************************************************************
*         起动任务, 优先级 3
*************************************************************/



⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -