我刚入门FreeRtos,自己写了一个任务切换,以及优先级改写的实验:
具体定义三个任务:
1.任务一,任务优先级默认定义为6,每taskDelayUntil(500)输出一次字符串并输出一次本函数运行次数。
2.任务二,任务优先级默认定义为7,每taskDelayUntil(10000)修改一次任务一的任务优先级,设置为6或者4(相当于任务一是否能得到运行的开关)
3.任务三,任务优先级默认定义为5,里面就一个while(1)空循环,为了使得任务一被设置为优先级为4的时候,不给任务一运行权限。
代码如下:
#include "sys.h"
#include "delay.h"
#include "usart.h"
#include "FreeRTOS.h"
#include "task.h"
#include "led.h"
#define StackDepth 120
TaskHandle_t LED_TASK_HANDLE;
TaskHandle_t UART_TASK_HANDLE;
TaskHandle_t TASK_HANDLE;
void LED_TASK(void *para)
{
TickType_t m;
u8 f=0;
int i=0;
m=xTaskGetTickCount();
while(1)
{
i++;
if(f==0)
{
printf("LED is on!\r\n");
LED0=!LED0;
f=1;
}
else
{
printf("LED is off!\r\n");
LED1=!LED1;
f=0;
}
printf("LED has cycled %d times!\r\n",i);
printf("LED'priority is %d!!\r\n",(u8)uxTaskPriorityGet(NULL));
vTaskDelayUntil(&m, pdMS_TO_TICKS(1000));
}
vTaskDelete(NULL);
}
void UART_TASK(void *para)
{
TickType_t m=xTaskGetTickCount();
u8 f=0;
while(1)
{
if(f==0)
{
f=1;
vTaskPrioritySet(LED_TASK_HANDLE,4);
printf("LED has been stopped!\r\n");
}
else if(f==1)
{
f=0;
vTaskPrioritySet(LED_TASK_HANDLE,6);
printf("LED has been started!\r\n");
}
vTaskDelayUntil(&m, pdMS_TO_TICKS(10000));
}
vTaskDelete(NULL);
}
void TASK(void *para)
{
u8 i=0;
while(1)
{
i=0;
}
vTaskDelete(NULL);
}
int main(void)
{
uart_init(115200);
LED_Init();
xTaskCreate( (TaskFunction_t) LED_TASK,
"LED_TASK",
StackDepth,
NULL,
6,
&LED_TASK_HANDLE );
xTaskCreate( (TaskFunction_t) UART_TASK,
"UART_TASK",
StackDepth,
NULL,
7,
&UART_TASK_HANDLE );
xTaskCreate( (TaskFunction_t) TASK,
"TASK",
StackDepth,
NULL,
5,
&TASK_HANDLE );
vTaskStartScheduler();
}
然而,运行结果如下:
LED has been stopped![2017-12-1611:40:25.518] LED has been started![2017-12-1611:40:35.542] LED is on![2017-12-16 11:40:35.542] LED has cycled 1 times![2017-12-1611:40:35.542] LED'priority is 6!![2017-12-1611:40:35.542] LED is off![2017-12-16 11:40:36.520] LED has cycled 2 times![2017-12-1611:40:36.520] LED'priority is 6!![2017-12-1611:40:36.524] LED is on![2017-12-16 11:40:37.554] LED has cycled 3 times![2017-12-1611:40:37.554] LED'priority is 6!![2017-12-16 11:40:37.554] LED is off![2017-12-16 11:40:38.543] LED has cycled 4 times![2017-12-1611:40:38.543] LED'priority is 6!![2017-12-1611:40:38.543] LED is on![2017-12-16 11:40:39.521] LED has cycled 5 times![2017-12-1611:40:39.521] LED'priority is 6!![2017-12-16 11:40:39.521] LED is off![2017-12-16 11:40:40.524] LED has cycled 6 times![2017-12-1611:40:40.524] LED'priority is 6!![2017-12-1611:40:40.524] LED is on![2017-12-16 11:40:41.522] LED has cycled 7 times![2017-12-1611:40:41.522] LED'priority is 6!![2017-12-1611:40:41.522] LED is off![2017-12-16 11:40:42.521] LED has cycled 8 times![2017-12-1611:40:42.521] LED'priority is 6!![2017-12-1611:40:42.521] LED is on![2017-12-16 11:40:43.522] LED has cycled 9 times![2017-12-1611:40:43.522] LED'priority is 6!![2017-12-1611:40:43.522] LED is off![2017-12-16 11:40:44.526] LED has cycled 10 times![2017-12-1611:40:44.526] LED'priority is 6!![2017-12-1611:40:44.526] LED has been stopped![2017-12-1611:40:45.520] LED has been started![2017-12-1611:40:55.520]
LED is on![2017-12-16 11:40:55.520] LED has cycled 11 times![2017-12-1611:40:55.520] LED'priority is 6!![2017-12-1611:40:55.520] LED is off![2017-12-16 11:40:55.532] LED has cycled 12 times![2017-12-1611:40:55.532] LED'priority is 6!![2017-12-16 11:40:55.532] LED is on![2017-12-16 11:40:55.532] LED has cycled 13 times![2017-12-1611:40:55.532] LED'priority is 6!![2017-12-1611:40:55.532] LED is off![2017-12-16 11:40:55.542] LED has cycled 14 times![2017-12-1611:40:55.542] LED'priority is 6!![2017-12-16 11:40:55.542] LED is on![2017-12-16 11:40:55.542] LED has cycled 15 times![2017-12-1611:40:55.542] LED'priority is 6!![2017-12-1611:40:55.551] LED is off![2017-12-16 11:40:55.551] LED has cycled 16 times![2017-12-1611:40:55.551] LED'priority is 6!![2017-12-1611:40:55.551] LED is on![2017-12-16 11:40:55.551] LED has cycled 17 times![2017-12-1611:40:55.551] LED'priority is 6!![2017-12-1611:40:55.557] LED is off![2017-12-16 11:40:55.557] LED has cycled 18 times![2017-12-1611:40:55.564] LED'priority is 6!![2017-12-1611:40:55.564] LED is on![2017-12-16 11:40:55.564] LED has cycled 19 times![2017-12-1611:40:55.564] LED'priority is 6!![2017-12-1611:40:55.572] LED is off![2017-12-16 11:40:55.572] LED has cycled 20 times![2017-12-1611:40:55.572] LED'priority is 6!![2017-12-1611:40:55.572]
LED is on![2017-12-16 11:40:55.578] LED has cycled 21 times![2017-12-1611:40:55.578] LED'priority is 6!![2017-12-1611:40:55.578] LED is off![2017-12-16 11:40:56.553] LED has cycled 22 times![2017-12-16 11:40:56.553] LED'priority is 6!![2017-12-1611:40:56.553] LED is on![2017-12-16 11:40:57.525] LED has cycled 23 times![2017-12-1611:40:57.525] LED'priority is 6!![2017-12-1611:40:57.525]
请看红色标注部分,相当于瞬间,任务一瞬间运行了10次,与我设置的逻辑出错, 出错部分,1.应该是间隔500ms 2.应该优先级为4,低于任务三的优先级5,不该被执行 3.而且瞬间十次的优先级检测为都为6
求各位大佬指点,,我询问过附近的人,也是无解。请各位帮帮忙
|