OpenEdv-开源电子网

 找回密码
 立即注册

扫一扫,访问微社区

正点原子新作:阿波罗STM32F767&F429&探索者STM32F4开发板&赶快来下载资料哦。

查看: 1229|回复: 4

taskDelayUntil使用问题

[复制链接]

  离线 

1

主题

5

帖子

0

精华

初级会员

Rank: 2

积分
52
金钱
52
注册时间
2016-10-28
在线时间
6 小时
发表于 2017-12-16 23:46:53 | 显示全部楼层 |阅读模式
5金钱
我刚入门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

求各位大佬指点,,我询问过附近的人,也是无解。请各位帮帮忙

最佳答案

查看完整内容[请看2#楼]

已解决,将xTaskDelayUtil中的xTimeToWake = *pxPreviousWakeTime + xTimeIncrement; 替换为xTimeToWake = xTaskGetTickCount() + xTimeIncrement;就ok, 可是这样会影响其他调用本函数的任务,修改只为找到答案,以及原因,请慎重
回复

使用道具 举报

  离线 

1

主题

5

帖子

0

精华

初级会员

Rank: 2

积分
52
金钱
52
注册时间
2016-10-28
在线时间
6 小时
 楼主| 发表于 2017-12-16 23:46:54 | 显示全部楼层
已解决,将xTaskDelayUtil中的xTimeToWake = *pxPreviousWakeTime + xTimeIncrement;
替换为xTimeToWake = xTaskGetTickCount() + xTimeIncrement;就ok,
可是这样会影响其他调用本函数的任务,修改只为找到答案,以及原因,请慎重
回复

使用道具 举报

  离线 

489

主题

9万

帖子

30

精华

管理员

Rank: 12Rank: 12Rank: 12

积分
124984
金钱
124984
注册时间
2010-12-1
在线时间
1053 小时
发表于 2017-12-18 01:07:17 | 显示全部楼层
仿真试过么?
我是开源电子网www.openedv.com站长,有关站务问题请与我联系。
正点原子STM32开发板购买店铺http://openedv.taobao.com
微信公众平台:正点原子   点击扫码添加
回复

使用道具 举报

  离线 

1

主题

5

帖子

0

精华

初级会员

Rank: 2

积分
52
金钱
52
注册时间
2016-10-28
在线时间
6 小时
 楼主| 发表于 2017-12-18 22:54:07 | 显示全部楼层

我仿真试过了,在任务二将任务一调低优先级的10s中,程序的确是在任务三中跑的,可是任务二一旦将任务一优先级调高,任务一就瞬间输出了十次,那个延时1s都不起作用
回复

使用道具 举报

  离线 

1

主题

5

帖子

0

精华

初级会员

Rank: 2

积分
52
金钱
52
注册时间
2016-10-28
在线时间
6 小时
 楼主| 发表于 2017-12-18 22:56:06 | 显示全部楼层
正点原子公众号

我把任务一的taskdelayuntil替换成taskdelay输出就没问题,可是我还是想知道为什么会出问题。原子哥帮忙给看一下
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则




QQ|联系我们|手机版|官方淘宝店|新浪微博|微信公众平台|OpenEdv-开源电子网 ( 粤ICP备12000418号-1 )

GMT+8, 2018-4-25 00:17

Powered by OpenEdv-开源电子网

© 2001-2030 OpenEdv-开源电子网

快速回复 返回顶部 返回列表
/* */