#include "sys.h"
#include "delay.h"
#include "usart.h"
#include "led.h"
#include "includes.h"
#include "lcd.h"
#include "key.h"
#include "string.h"
#define START_TASK_PRIO 3
#define START_STK_SIZE 128
OS_TCB StartTaskTCB;
CPU_STK START_TASK_STK[START_STK_SIZE];
void start_task(void *p_arg);
#define TASK1_TASK_PRIO 4
#define TASK1_STK_SIZE 64
OS_TCB Task1_TaskTCB;
CPU_STK TASK1_TASK_STK[TASK1_STK_SIZE];
void task1_task(void *p_arg);
#define TASK2_TASK_PRIO 5
#define TASK2_STK_SIZE 64
OS_TCB Task2_TaskTCB;
CPU_STK TASK2_TASK_STK[TASK2_STK_SIZE];
void task2_task(void *p_arg);
u8 share_resource[30];
OS_SEM MY_SEM;
int main(void)
{
OS_ERR err;
CPU_SR_ALLOC();
delay_init();
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
uart_init(115200);
led_Init();
LCD_Init();
key_Init();
POINT_COLOR = RED;
LCD_ShowString(30,10,200,16,16,"ALIENTEK STM32F1");
LCD_ShowString(30,30,200,16,16,"UCOSIII Examp 10-1");
LCD_ShowString(30,50,200,16,16,"Visit Share Resource");
LCD_ShowString(30,70,200,16,16,"ATOM@ALIENTEK");
LCD_ShowString(30,90,200,16,16,"2016/1/21");
OSInit(&err);
OS_CRITICAL_ENTER();
OSTaskCreate((OS_TCB*)&StartTaskTCB,
(CPU_CHAR*)"start task",
(OS_TASK_PTR)start_task,
(void*)0,
(OS_PRIO)START_TASK_PRIO,
(CPU_STK*)&START_TASK_STK[0],
(CPU_STK_SIZE)START_STK_SIZE/10,
(CPU_STK_SIZE)START_STK_SIZE,
(OS_MSG_QTY)0,
(OS_TICK)0,
(void*)0,
(OS_OPT)OS_OPT_TASK_STK_CHK|OS_OPT_TASK_STK_CLR,
(OS_ERR*)&err);
OS_CRITICAL_EXIT();
OSStart(&err);
}
void start_task(void *p_arg)
{
OS_ERR err;
CPU_SR_ALLOC();
p_arg=p_arg;
CPU_Init();
#if OS_CFG_STAT_TASK_EN>0u
OSStatTaskCPUUsageInit(&err);
#endif
OS_CRITICAL_ENTER();
OSTaskCreate((OS_TCB * )&Task1_TaskTCB,
(CPU_CHAR * )"Task1 task",
(OS_TASK_PTR )task1_task,
(void * )0,
(OS_PRIO )TASK1_TASK_PRIO,
(CPU_STK * )&TASK1_TASK_STK[0],
(CPU_STK_SIZE)TASK1_STK_SIZE/10,
(CPU_STK_SIZE)TASK1_STK_SIZE,
(OS_MSG_QTY )0,
(OS_TICK )0,
(void * )0,
(OS_OPT )OS_OPT_TASK_STK_CHK|OS_OPT_TASK_STK_CLR|OS_OPT_TASK_SAVE_FP,
(OS_ERR * )&err);
OSTaskCreate((OS_TCB * )&Task2_TaskTCB,
(CPU_CHAR * )"Task2 task",
(OS_TASK_PTR )task2_task,
(void * )0,
(OS_PRIO )TASK2_TASK_PRIO,
(CPU_STK * )&TASK2_TASK_STK[0],
(CPU_STK_SIZE)TASK2_STK_SIZE/10,
(CPU_STK_SIZE)TASK2_STK_SIZE,
(OS_MSG_QTY )0,
(OS_TICK )0,
(void * )0,
(OS_OPT )OS_OPT_TASK_STK_CHK|OS_OPT_TASK_STK_CLR|OS_OPT_TASK_SAVE_FP,
(OS_ERR * )&err);
OSSemCreate((OS_SEM*)&MY_SEM,
(CPU_CHAR*)"MY_SEM",
(OS_SEM_CTR)1,
(OS_ERR*)&err);
OS_CRITICAL_EXIT();
OSTaskDel((OS_TCB*)0,&err);
}
void task1_task(void *p_arg)
{
OS_ERR err;
u8 task1_str[]="First task Running";
while(1)
{
printf("
任务 1:
");
LCD_Fill(0,110,239,319,CYAN);
OSSemPend(&MY_SEM,0,OS_OPT_PEND_BLOCKING,0,&err);
memcpy(share_resource,task1_str,sizeof(task1_str));
delay_ms(200);
printf("%s
",share_resource);
OSSemPost(&MY_SEM,OS_OPT_POST_1,&err);
PBout(5)=!PBout(5);
OSTimeDlyHMSM(0,0,1,0,OS_OPT_TIME_PERIODIC,&err);
}
}
void task2_task(void *p_arg)
{
//u8 i=0;
OS_ERR err;
u8 task2_str[]="Second task Running";
while(1)
{
printf("
任务 2:
");
LCD_Fill(0,110,239,319,BROWN);
OSSemPend(&MY_SEM,0,OS_OPT_PEND_BLOCKING,0,&err);
memcpy(share_resource,task2_str,sizeof(task2_str));
delay_ms(200);
printf("%s
",share_resource);
OSSemPost(&MY_SEM,OS_OPT_POST_1,&err);
PEout(5)=!PEout(5);
OSTimeDlyHMSM(0,0,1,0,OS_OPT_TIME_PERIODIC,&err);
}
}
请问为什么不是先输出任务 1和任务 2再输出"First task Running"?delay_ms(200)不是发生了一次任务调度吗?
LCD ID:ffff
任务1:
任务2:
First task Running!
Second task Running!
任务1:
First task Running!
任务2:
Second task Running!
任务1:
First task Running!
任务2:
Second task Running!
任务1:
First task Running!
任务2:
Second task Running!
delay_ms() 延时引起任务切换,代码运行结果串口打印不应该是:
任务1:
任务2:
First task Running!
Second task Running!
一周热门 更多>