创建三个任务,
1.task1打印I’m task1!三遍,然后换行;延时两秒。
2.task2打印I’m taskk2!三遍,换行,延时两秒。
3.task3(task_led)实现流水灯,每亮一个灯时延两秒。
设定task3优先级为3,task1和task2优先级为5,task3优先级高于task1和task2
函数(OSTimeDlyHMSM)为时延函数
led.c
#include "led.h"
void led_init(void)
{
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOE|RCC_AHB1Periph_GPIOF, ENABLE);//外围设备编号,状态
GPIO_InitTypeDef P;
P.GPIO_Mode =GPIO_Mode_OUT;
P.GPIO_OType =GPIO_OType_PP;
P.GPIO_Pin =GPIO_Pin_9|GPIO_Pin_10;
P.GPIO_PuPd =GPIO_PuPd_NOPULL;
P.GPIO_Speed =GPIO_Speed_50MHz;
GPIO_Init(GPIOF,&P);
P.GPIO_Pin =GPIO_Pin_13|GPIO_Pin_14;
GPIO_Init(GPIOE,&P);
GPIO_SetBits(GPIOF, GPIO_Pin_9|GPIO_Pin_10);//灭
GPIO_SetBits(GPIOE, GPIO_Pin_13|GPIO_Pin_14);//灭
}
void led_one_on(LED_ID led_n){
switch(led_n){
case LED_D1:
{
GPIO_ResetBits( GPIOF, GPIO_Pin_9);//0亮,1灭
GPIO_SetBits(GPIOF, GPIO_Pin_10);
GPIO_SetBits(GPIOE, GPIO_Pin_13|GPIO_Pin_14);
break;
}
case LED_D2:
{
GPIO_ResetBits( GPIOF, GPIO_Pin_10);
GPIO_SetBits(GPIOF, GPIO_Pin_9);
GPIO_SetBits(GPIOE, GPIO_Pin_13|GPIO_Pin_14);
break;
}
case LED_D3:
{
GPIO_ResetBits( GPIOE, GPIO_Pin_13);
GPIO_SetBits(GPIOF, GPIO_Pin_14);
GPIO_SetBits(GPIOF, GPIO_Pin_10|GPIO_Pin_9);
break;
}
case LED_D4:
{
GPIO_ResetBits( GPIOE, GPIO_Pin_14);
GPIO_SetBits(GPIOE, GPIO_Pin_13);
GPIO_SetBits(GPIOF, GPIO_Pin_9|GPIO_Pin_10);
break;
}
default:
break;
}
}
void delay(int num){
while(num--);
}
//将led_loop改造成任务函数
void led_loop(void *p_arg){
OS_ERR err;
led_init();
LED_ID led_n =1;
for(led_n=1;led_n<5;led_n++){
led_one_on(led_n);
OSTimeDlyHMSM(0,0,2,0, OS_OPT_TIME_DLY|OS_OPT_TIME_HMSM_STRICT , &err);
}
}
app.c
#include <includes.h>
#include "led.h"
#include "usart.h"
OS_TCB LedTcb;
CPU_STK LedStk[100];
OS_TCB Task1TCB;
CPU_STK TaskStk[100];
OS_TCB Task2TCB;
CPU_STK Task2Stk[100];
OS_ERR err;
OS_MUTEX lock;
void Task1Func(void *p){
while(1){
OSMutexPend(&lock,0,OS_OPT_PEND_BLOCKING,0,&err);
printf("I'm task1!I'm task1!I'm task1! \r\n");
OSMutexPost(&lock,0,&err);
OSTimeDlyHMSM(0,0,2,0, OS_OPT_TIME_DLY|OS_OPT_TIME_HMSM_STRICT , &err);
}
}
void Task2Func(void *p){
while(1){
OSMutexPend(&lock,0,OS_OPT_PEND_BLOCKING,0,&err);
printf("I'm task2!I'm task2!I'm task2! \r\n");
OSMutexPost(&lock,0,&err);
OSTimeDlyHMSM(0,0,2,0, OS_OPT_TIME_DLY|OS_OPT_TIME_HMSM_STRICT , &err);
}
}
//main函数
int main(void)
{
BSP_IntDisAll(); //关闭所有的中断
CPU_Init(); /* Initialize the uC/CPU Services */
Mem_Init(); /* Initialize Memory Management Module */
Math_Init(); /* Initialize Mathematical Module */
BSP_Init();
BSP_Tick_Init();
usart_init();
OSInit(&err); /* Init uC/OS-III. */
// 使能分时特性
OSSchedRoundRobinCfg(ENABLE, 10,&err);
OSMutexCreate(&lock, "print_lock", &err);
OSTaskCreate((OS_TCB *)&Task1TCB, /* Create the start task */
(CPU_CHAR *)"Task1Func",
(OS_TASK_PTR )Task1Func, //任务函数名
(void *)0u,
(OS_PRIO )5,
(CPU_STK *)&TaskStk[0],
(CPU_STK_SIZE )10,
(CPU_STK_SIZE )100,
(OS_MSG_QTY )10u,
(OS_TICK )10u,
(void *)NULL,
(OS_OPT )(OS_OPT_TASK_STK_CHK | OS_OPT_TASK_STK_CLR),
(OS_ERR *)&err);
OSTaskCreate((OS_TCB *)&Task2TCB, /* Create the start task */
(CPU_CHAR *)"Task2Func",
(OS_TASK_PTR )Task2Func, //任务函数名
(void *)0u,
(OS_PRIO )5,
(CPU_STK *)&Task2Stk[0],
(CPU_STK_SIZE )10,
(CPU_STK_SIZE )100,
(OS_MSG_QTY )10u,
(OS_TICK )10u,
(void *)NULL,
(OS_OPT )(OS_OPT_TASK_STK_CHK | OS_OPT_TASK_STK_CLR),
(OS_ERR *)&err);
OSTaskCreate((OS_TCB *)&LedTcb, /* Create the start task */
(CPU_CHAR *)"led_loop",
(OS_TASK_PTR )led_loop, //任务函数名
(void *)0u,
(OS_PRIO )3,
(CPU_STK *)&LedStk[0],
(CPU_STK_SIZE )10,
(CPU_STK_SIZE )100,
(OS_MSG_QTY )10u,
(OS_TICK )10u,
(void *)NULL,
(OS_OPT )(OS_OPT_TASK_STK_CHK | OS_OPT_TASK_STK_CLR),
(OS_ERR *)&err);
if(err!=OS_ERR_NONE)
{
printf("TASK CREATE ERROR!\r\n");
}
OSStart(&err); /*启动任务,只写一个,从休眠状态转入就绪状态 Start multitasking (i.e. give control to uC/OS-III). */
(void)&err;
return (0u);
}
实验结果
task3因为优先级高,所以创建以后直接进入运行状态(灯就亮了),然后延时函数开始,task3转入等待状态;
CPU开始分时调度执行task2和task1.
延时结束,task3再次进入就绪状态,因为优先级高,所以被实时调度再次灯亮,循环这个过程。