1. vTaskDelay()
函数原型:void vTaskDelay(portTickType xTicksToDelay);
xTicksToDelay: 指定延时间,即指定心跳个数。调用该函数的任务将进入阻塞态,当延时结束时任务进入就绪态。
2. vTaskDelayUntil
函数原型:void vTaskDelayUntil(portTickType* pxPreviousWakeTime,
portTickType xtimeIncrement);
pxPreviousWakeTime: 用来保存任务上一次离开阻塞态进入就绪态的时刻,每次执行完该函数,将结束时的时刻赋给pxPreviousWakeTime指向的变量,这个时刻做为参考点用来计算下一次离开阻塞态的时刻。
xtimeIncrement:用来指定延时的时间长度,以心跳为单位。
例程:
-
#include "includes.h"
-
#include <stdio.h>
-
#include "usart.h"
-
-
-
void xDelay(void);
-
void xtask(void *pvParameter);
-
const char *pcTask1Name = "Task1 is running\n";
-
const char *pcTask2Name = "Task2 is running\n";
-
-
int main()
-
{
-
int ret = 0;
-
uart_init(9600); //????????????9600
-
NVIC_Configuration();// ?è????????????·?×é
-
-
/*
-
*???¨??????????
-
*/
-
xTaskCreate(xtask, //??????????
-
"task1", //????????±???×??????????÷???±????
-
512, //??????·????????????ó??
-
(void*)pcTask1Name, //??????????????????
-
1, //±?????????????
-
NULL); //????????????±ú
-
xTaskCreate(xtask, "task2", 512, (void*)pcTask2Name, 2, NULL); //??????1?????¨????2
-
/*
-
*?????????÷???÷??????????????
-
*/
-
-
vTaskStartScheduler();
-
/*
-
*??°????????á????????????·??????????ò??????????????×?????·¨??????????·???
-
*????
-
*/
-
while(1);
-
}
-
/***************************************
-
* ?????ò???????±????
-
**************************************/
-
void xDelay()
-
{
-
int i;
-
for(i = 200; i--; i >= 0);
-
}
-
/****************************************
-
* ??????????????
-
***************************************/
-
void xtask(void *pvParameter)
-
{
-
int i;
-
-
-
//volatile unsigned long u1 = 0;
-
portTickType xLastWakeTime;
-
xLastWakeTime = xTaskGetTickCount();
-
-
while(1)
-
{
-
printf("%s\n", (char*)pvParameter);
-
vTaskDelayUntil(&xLastWakeTime, 10);
-
}
-
}
-
-
执行结果为:
例程2
(1)创建两个任务,任务的有先级都为1,两个任务持续运行。
(2)创建第三个任务,任务的优先级为2,周期性的执行。
-
#include "includes.h"
-
#include <stdio.h>
-
#include "usart.h"
-
-
-
void xDelay(void);
-
void xtask(void *pvParameter);
-
void xtask3(void *pvParameter);
-
const char *pcTask1Name = "Task1 is running\n";
-
const char *pcTask2Name = "Task2 is running\n";
-
const char *pcTask3Name = "Task3 is running\n";
-
-
-
int main()
-
{
-
int ret = 0;
-
uart_init(9600); //????????????9600
-
NVIC_Configuration();// ?è????????????·?×é
-
-
/*
-
*???¨??????????
-
*/
-
xTaskCreate(xtask, //??????????
-
"task1", //????????±???×??????????÷???±????
-
512, //??????·????????????ó??
-
(void*)pcTask1Name, //??????????????????
-
1, //±?????????????
-
NULL); //????????????±ú
-
xTaskCreate(xtask, "task2", 512, (void*)pcTask2Name, 1, NULL); //??????1?????¨????2
-
xTaskCreate(xtask3, "task3", 512, (void*)pcTask3Name, 2, NULL); //??????1?????¨????2
-
/*
-
*?????????÷???÷??????????????
-
*/
-
-
vTaskStartScheduler();
-
/*
-
*??°????????á????????????·??????????ò??????????????×?????·¨??????????·???
-
*????
-
*/
-
while(1);
-
}
-
-
/****************************************
-
* ??????????????
-
***************************************/
-
void xtask(void *pvParameter)
-
{
-
int i;
-
-
-
//volatile unsigned long u1 = 0;
-
-
while(1)
-
{
-
printf("%s", (char*)pvParameter);
-
}
-
}
-
/****************************************
-
* ??????????????
-
***************************************/
-
void xtask3(void *pvParameter)
-
{
-
int i;
-
-
-
//volatile unsigned long u1 = 0;
-
portTickType xLastWakeTime;
-
xLastWakeTime = xTaskGetTickCount();
-
-
while(1)
-
{
-
printf("%s", (char*)pvParameter);
-
vTaskDelayUntil(&xLastWakeTime, 10);
-
}
-
}
阅读(1697) | 评论(0) | 转发(0) |