main.c
上传用户:dongxin
上传日期:2022-06-22
资源大小:370k
文件大小:6k
- /*
- * File: main.c
- * Purpose: sample program
- *
- */
- //#include "support_common.h" /* include peripheral declarations and more */
- #include "includes.h"
- #include <stdio.h>
- #define TASK_STK_SIZE 512
- //定义一些全局变量
- OS_STK MAIN_TASK_STK[TASK_STK_SIZE];
- OS_STK TASK1_STK[TASK_STK_SIZE];
- OS_STK TASK2_STK[TASK_STK_SIZE];
- OS_STK TASK3_STK[TASK_STK_SIZE];
- OS_STK DEL_TASK_STK[TASK_STK_SIZE];
- OS_STK LED_TASK_STK[TASK_STK_SIZE];
- OS_STK SETTIME_TASK_STK[TASK_STK_SIZE];
- OS_STK SHOWTIME_TASK_STK[TASK_STK_SIZE];
- char reStr[8];
- INT8U delPrio;
- //定义信号量和邮箱
- OS_EVENT *delSEM;
- OS_EVENT *allMBOX;
- OS_EVENT *task1MBOX;
- OS_EVENT *task2MBOX;
- OS_EVENT *task3MBOX;
- OS_EVENT *sw2SEM;
- OS_EVENT *sw1SEM;
- //任务函数原型
- void mainTask(void *pdata);
- void task1(void *pdata);
- void task2(void *pdata);
- void task3(void *pdata);
- void delTask(void *pdata);
- void ledTask(void *pdata);
- void showTimeTask(void *pdata);
- void main(void)
- {
- DisableInterrupts();
- // printf("hellonr");
- //系统再一次初始化
- PITInit(); //初始化时钟中断
- IRQInit(); //初始化按键中断
- LEDInit(); //初始化LED灯
- UartInit(0,60,19200,1); //初始化串口
-
- OSInit();
- //创建信号量及消息邮箱
- delSEM = OSSemCreate(0);
- sw1SEM = OSSemCreate(0);
- sw2SEM = OSSemCreate(0);
- allMBOX = OSMboxCreate((void *)0);
- task1MBOX = OSMboxCreate((void *)0);
- task2MBOX = OSMboxCreate((void *)0);
- task3MBOX = OSMboxCreate((void *)0);
-
- EnableInterrupts();
-
- printf("rn The uc/os II is running ! rn");
- printf("rn----------------------------------------rn");
-
- //创建任务
- OSTaskCreate(mainTask, (void *)0, &MAIN_TASK_STK[TASK_STK_SIZE-1],1);
- OSTaskCreate(task1, (void *)0, &TASK1_STK[TASK_STK_SIZE-1], 9);
- OSTaskCreate(task2, (void *)0, &TASK2_STK[TASK_STK_SIZE-1], 10);
- OSTaskCreate(task3, (void *)0, &TASK3_STK[TASK_STK_SIZE-1], 11);
- OSTaskCreate(ledTask, (void *)0, &LED_TASK_STK[TASK_STK_SIZE-1], 12);
- OSTaskCreate(delTask, (void *)0, &DEL_TASK_STK[TASK_STK_SIZE-1], 2);
- OSTaskCreate(showTimeTask,(void *)0, &SHOWTIME_TASK_STK[TASK_STK_SIZE-1],15);
-
- OSStart();
-
- }
- void mainTask(void *pdata)
- {
- char *msg = "running";
- INT8U *premsg1 = 0, *premsg2 = 0, *premsg3 = 0;
- pdata = 0;
-
- //第一个运行的任务开启时钟中断
- EnableTIN0Int();
- while (1)
- {
- printf("nrNow the created tasks number is :%4dnr",OSTaskCtr);
-
- //放弃运行,增加等待allMBOX的任务
- OSTimeDly(10);
-
- //释放信号量,task1.task2.task3运行
- OSMboxPostOpt(allMBOX,(void *)msg, OS_POST_OPT_BROADCAST);
- //给task1, task2, task3运行的机会
- OSTimeDly(10);
-
- //无等待的查看是否接收到消息
- premsg1 = OSMboxAccept(task1MBOX);
-
- //如果接受到消息,打印出接受到的消息
- if(premsg1 !=(void *) 0)
- {
- printf("nrReceive a message :%snr",(char *)premsg1);
- }
- premsg2 = OSMboxAccept(task2MBOX);
- if(premsg2 !=(void *) 0)
- {
- printf("nrReceive a message :%snr",(char *)premsg2);
- }
-
- premsg3 = OSMboxAccept(task3MBOX);
- if(premsg3 != (void *)0)
- {
- printf("nrReceive a message :%snr",(char *)premsg3);
- }
-
- //每10秒运行一次
- OSTimeDlyHMSM(0,0,10,0);
- }
- }
- void task1(void *pdata)
- {
- INT8U perr;
- char *msg = "I am task1"; //发送给主任务的消息
- INT32U runCtr = 1; //记录任务运行的次数
-
- pdata = pdata;
-
- while(1)
- {
- //等待运行信号
- OSMboxPend(allMBOX, 0, &perr);
- printf("nrmy name is task1, and my priority is %4d nr", OSTCBCur ->OSTCBPrio);
- printf("This is task1 %4dth runningnr",runCtr++);
-
- //向主任务发送信号
- OSMboxPost(task1MBOX,(void *)msg);
-
- //放弃运行
- OSTimeDly(1);
- }
- }
- void task2(void *pdata)
- {
- INT8U perr;
- char *msg = "I am task2";
- INT32U runCtr = 1;
- pdata = pdata;
- while(1)
- {
-
- OSMboxPend(allMBOX, 0, &perr);
-
- printf("nrmy name is task2, and my priority is %4d nr", OSTCBCur ->OSTCBPrio);
- printf("This is task2 %4dth runningnr",runCtr++);
-
- OSMboxPost(task2MBOX,(void *)msg);
-
- OSTimeDly(1);
- }
- }
- void task3(void *pdata)
- {
- INT8U perr;
- char *msg = "I am task3";
- INT32U runCtr = 1;
- pdata = pdata;
- while(1)
- {
-
- OSMboxPend(allMBOX, 0, &perr);
-
- printf("nrmy name is task3, and my priority is %4d nr", OSTCBCur ->OSTCBPrio);
- printf("This is task3 %4dth runningnr",runCtr++);
-
- OSMboxPost(task3MBOX,(void *)msg);
-
- OSTimeDly(1);
- }
- }
- void delTask(void *pdata)
- {
- INT8U err;
- INT8U result;
- pdata = pdata;
-
- while(1)
- {
- //等待删除信号
- OSSemPend(delSEM,0,&err);
-
- //将欲删除的任务挂起
- OSTaskSuspend(delPrio);
-
- result = OSTaskDel(delPrio);
-
- switch (result)
- {
- case OS_ERR_NONE:
- printf("nrThe task with prio %4d is deleted successnr",delPrio);
- break;
- case OS_ERR_TASK_DEL_IDLE:
- printf("nrYour want to delete the idle task ! Error!!!nr");
- break;
- case OS_ERR_PRIO_INVALID:
- printf("nrThe prio you want to delete is invalid! Please check!nr");
- break;
- case OS_ERR_TASK_DEL:
- printf("nrThe task can't be deleted nownr");
- break;
- case OS_ERR_TASK_NOT_EXIST:
- printf("nr The task with prio %4d don't existnr",delPrio);
- break;
- case OS_ERR_TASK_DEL_ISR:
- printf("nrYou can't delete a task in the ISR!!!nr");
- break;
- }
-
- //放弃运行
- OSTimeDly(1);
- }
- }
- void ledTask(void *pdata)
- {
- INT8U err;
- INT8U loop;
- pdata = pdata;
- while(1)
- {
- //等待信号sw1SEM
- OSSemPend(sw1SEM, 0, &err);
-
- //LED循环闪亮3次
- for(loop = 0; loop < 3; loop++)
- {
- MCF_GPIO_PORTTC |= 0x01; //led1 light
- OSTimeDly(10);
- MCF_GPIO_PORTTC &= 0x0E; //LED1 go off
- MCF_GPIO_PORTTC |= 0x02; //led2 light
- OSTimeDly(10);
- MCF_GPIO_PORTTC &= 0x0D; //led2 go off
- MCF_GPIO_PORTTC |= 0x04; //led3 light
- OSTimeDly(10);
- MCF_GPIO_PORTTC &= 0x0B; //led3 go off
- MCF_GPIO_PORTTC |= 0x08; //led4 light
- OSTimeDly(10);
- MCF_GPIO_PORTTC &= 0x07; //led4 go off
- }
-
- //放弃运行
- OSTimeDly(1);
- }
- }
- void showTimeTask(void *pdata)
- {
- INT32U hour,min,sec;
- INT8U err;
- pdata = pdata;
- while(1)
- {
- //等待信号sw2SEM
- OSSemPend(sw2SEM, 0, &err);
-
- hour = (OSTime / (OS_TICKS_PER_SEC * 3600)) % 24;
- min = (OSTime / (OS_TICKS_PER_SEC * 60)) % 60;
- sec = (OSTime / OS_TICKS_PER_SEC) % 60;
- printf("nr The OS runned time is %2d:%2d:%2dnr",hour,min,sec);
-
- //放弃运行
- OSTimeDly(1);
- }
-
- }