zoukankan      html  css  js  c++  java
  • 【uTenux实验】时间管理(系统时间/周期性处理/警报处理)

    1、系统时间管理

    系统时间管理函数用来对系统时间进行操作,是OS的一个基础性的东西。个人认为,设置系统时间和获取系统时间对OS来说基本是可有可无的。

    uTenux提供了三个系统时间相关API。分别用于设置系统时间、获取系统时间和获取系统工作时间。其中,前两个比较蛋疼。时间的表示是从1985年1月1日0:00:00(GMT)开始以ms为单位的累加。要想获得可读的时分秒,还得手动转换。我最讨厌这个转换了,所以不想细做这个实验。

    tk_get_otm获取的系统工作时间也是一ms表示的,只是这个数据比较直观,不需要再去转换了。

    【实验说明】

    我懒的去转换时间,本实验使用验证三个函数,用ms表示时间。

    首先设置系统时间,然后读出十次otm,最后读出系统时间。看看读出的系统时间与写入的时间的差别。其实这时,板子上RTC功能没有打开,设置系统时钟这个功能基本等于没用。

    我比较懒,这就就在例程的基础上添加几行代码。没有手动输入各个函数。

    【代码及输出】

    #include "TimeSample.h"
    
    EXPORT ER TimeSample( void )
    {
        B ostr[10],hstr[3],mstr[3],sstr[3];
        UW i,hour,minute,second;
        SYSTIM otm,systime;
        ER ercd;
      
        //设置系统时间
        systime.hi = 1000;
        systime.lo = 600;
        tk_set_tim(&systime);
    
        tm_putstring((UB*)"Time sample is now starting!
    ");
        for(i=0;i<20;i++) {
            ercd=tk_get_otm(&otm);
            if(E_OK != ercd){
                tm_putstring((UB*)"Time can't be got;
    ");
                PutErcd(ercd);
            }
            tm_putstring((UB*)"Now working up time is ");
            second=otm.lo/1000;
            minute=second/60;
            second=second%60;
            hour=minute/60;
            minute=minute%60;
            ltostr(otm.lo,ostr,10,10);          /*put otm string */
            tm_putstring((UB*)ostr);
            tm_putstring((UB*)",");
            ltostr(hour,hstr,10,3);             /*put hour string */
            tm_putstring((UB*)hstr);
            tm_putstring((UB*)":");
            ltostr(minute,mstr,10,3);           /*put minute string */
            tm_putstring((UB*)mstr);
            tm_putstring((UB*)":");
            ltostr(second,sstr,10,3);           /*put second string */
            tm_putstring((UB*)sstr);
            tm_putstring((UB*)"
    ");
    
            Delay(0x1700000);
        }
        //获取系统时间并输出
        tk_get_tim(&systime);
        tm_putstring((UB*)"Now system time is ");
        second=systime.lo/1000;
        minute=second/60;
        second=second%60;
        hour=minute/60;
        minute=minute%60;
        ltostr(systime.lo,ostr,10,10);          /*put otm string */
        tm_putstring((UB*)ostr);
        tm_putstring((UB*)",");
        ltostr(hour,hstr,10,3);             /*put hour string */
        tm_putstring((UB*)hstr);
        tm_putstring((UB*)":");
        ltostr(minute,mstr,10,3);           /*put minute string */
        tm_putstring((UB*)mstr);
        tm_putstring((UB*)":");
        ltostr(second,sstr,10,3);           /*put second string */
        tm_putstring((UB*)sstr);
        tm_putstring((UB*)"
    ");
    
        return E_OK;
    }

    输出:


    ----------------------------------------------------
            micro Tenux Version 1.6.00(build 0180)     
                Supported MCU is ST STM32F407VG        
      Copyright(c) 2008-2013 by Dalian uLoong Co.,Ltd. 
    ----------------------------------------------------

    Time sample is now starting!
    Now working up time is 0,0:0:0
    Now working up time is 570,0:0:0
    Now working up time is 1150,0:0:1
    Now working up time is 1730,0:0:1
    。。。。。。。。。。。。。。
    Now working up time is 9230,0:0:9
    Now working up time is 9810,0:0:9
    Now working up time is 10390,0:0:10
    Now working up time is 10960,0:0:10
    Now system time is 12140,0:0:12
    Push any key to shutdown the micro Tenux.

    2、周期性处理

         周期性处理程序是一个在规则的时间间隔内启动的时间事件处理程序。与任务不同,周期性处理函数不是死循环。执行一次就退出,但是不能单独存在。

         建立好周期性处理函数之后,原任务就不用管了。OS会周期性地执行这个任务。这个周期性的函数,不受优先级限制。只要到期就会运行一次。不管多高优先级的任务在使用MCU。

         在建立每个周期性处理程序时,会为它们设定程序启动时的时间间隔(周期时间)和周期相位。周期时间t就是每隔t时间会启动一次处理函数,周期相位m,是指每个周期的第m时间启动这个处理函数。

    SVC描述

    1、创建周期性处理程序

    ID cycid=tk_cre_cyc(T_CCYC* pk_ccyc);

    其中T_CCYC是创建结构体,定义如下

    typedef struct t_ccyc {
        VP                  exinf;                     /* Extended information */
        ATR                 cycatr;                     /* Cycle handler attribute */
        FP                  cychdr;                     /*处理程序入口地址*/
        RELTIM              cyctim;                     /* 处理周期*/
        RELTIM              cycphs;                     /* 相位*/
        UB                  dsname[8];               /* Object name */
    } T_CCYC;

    2、创建之后就实验tk_sta_cyc让周期函数可以运行了

    3、停止周期运行tk_stp_cyc,删除周期处理tk_del_cyc,只要提供一个ID即可。

    【实验描述】

    1、创建一个周期运行程序并启动。

    2、创建一个优先级较高的任务TaskA,启动后进入死循环。即不给初始任务再次运行的机会

    3、周期处理函数,处理20次之后,自动停止并删除自己。

    【代码及输出】

    例子太简单了,自己加了点内容。

    #include "CycSample.h"
    
    void CycHandler(void);
    void CycTaskSampseA(W stacd,VP exinf);
    void CycTaskSampseB(W stacd,VP exinf);
    static B count;
    static ID CycID;
    static ID TaskA_ID;
    
    
    EXPORT ER CycSample( void )
    {
      ER ercd;
      T_CCYC ccyc;
      T_CTSK ctsk;
      
      ctsk.bufptr = NULL;
      ctsk.exinf = (VP)NULL;;
      ctsk.itskpri = 15;
      ctsk.stksz = 512;
      ctsk.task = CycTaskSampseA;
      ctsk.tskatr = TA_HLNG | TA_RNG0;
      TaskA_ID = tk_cre_tsk(&ctsk);
      
     
      //创建周期处理
      ccyc.cycatr = TA_HLNG | TA_STA;
      ccyc.cychdr = CycHandler;
      ccyc.cycphs = 100;
      ccyc.cyctim = 200;
      ccyc.exinf = (VP)NULL;
      CycID = tk_cre_cyc(&ccyc);
      
      if(E_OK<= CycID)
      {
        tm_putstring((UB*)"成功创建周期性处理
    ");
      }
      else
      {
      }
      if(E_OK <= tk_sta_cyc(CycID))
      {
        tm_putstring((UB*)"成功启动周期性处理
    ");
      }
      
      tk_sta_tsk(TaskA_ID,5);
      
    
      return E_OK;
    }
    
    //验证周期处理与任务优先级无关
    void CycTaskSampseA(W stacd,VP exinf)
    {
      tm_putstring((UB*)"TaskA启动成功
    ");
       while(1);
    }
    
    
    void CycHandler(void)
    {
        count++;
        if(count == 10)
        {
          tm_putstring((UB*)"i will stop my cyc
    ");
          if(E_OK <= tk_stp_cyc(CycID))
          {
            tm_putstring((UB*)"sucess stop cyc
    ");
          }
          tm_putstring((UB*)"i will delete my cyc
    ");
          if(E_OK <= tk_del_cyc(CycID))
          {
            tm_putstring((UB*)"sucess delete cyc
    ");
          }
          return;
        }
        tm_putstring((UB*)"The times of cycle is: ");
        tm_putchar(count+48);
        tm_putstring((UB*)" !
    ");
    }

    输出:


    ----------------------------------------------------
            micro Tenux Version 1.6.00(build 0180)     
                Supported MCU is ST STM32F407VG        
      Copyright(c) 2008-2013 by Dalian uLoong Co.,Ltd. 
    ----------------------------------------------------

    成功创建周期性处理
    成功启动周期性处理
    TaskA启动成功
    The times of cycle is: 1 !
    The times of cycle is: 2 !
    The times of cycle is: 3 !
    The times of cycle is: 4 !
    The times of cycle is: 5 !
    The times of cycle is: 6 !
    The times of cycle is: 7 !
    The times of cycle is: 8 !
    The times of cycle is: 9 !
    i will stop my cyc
    sucess stop cyc
    i will delete my cyc
    sucess delete cyc

    3、警报处理

    警报处理程序是一个在特定时间启动的时间事件处理程序。警报处理也是有OS直接管理的。创建警报处理后,并不能立即激活警报,只有当调用了tk_sta_alm之后才能激活这个警报。

    激活警报时,需要为警报处理提供警报持续时间。警报持续时间结束,或者手动调用tk_stp_alm停止警报时,警报停止。

    这个就像大楼里边的火警,发生火情(事件)激活警报,警报开始工作。警报持续一段时间,该知道发生火情的人都知道了,警报就停止了。也可能是虚惊一场,然后大楼管理员按了停止按钮,警报就停止了。

    uTenux提供的警报处理API:

    1、创建警报:

    ID almid=tk_cre_alm(T_CALM* pk_calm);

    创建结构体如下:

    typedef struct t_calm { 
    
    VP exinf; /* Extended information */ 
    
    ATR almatr; /* Alarm handler attribute */ 
    
    FP almhdr; /* Alarm handler address */ 
    
    UB dsname[8]; /* Object name */ 
    
    }T_CALM; 
    

    即只需要提供警报处理的函数入口和处理程序属性即可。

    2、激活警报

    ER ercd= tk_sta_alm(ID almid,RELTIM almtim);

    提供警报处理的ID和警报持续时间。即可

    3、停止警报

    ER ercd= tk_stp_alm(ID almid);

    将警报almid停止掉。

    【实验描述】

    1、创建一个警报,并启动

    2、在警报处理函数中,输出一段信息

    3、等待警报时间到,自动结束

    4、删除警报,返回

    【代码及输出】

    #include "CycSample.h"
    
    void CycHandler(void);
    void CycTaskSampseA(W stacd,VP exinf);
    void CycTaskSampseB(W stacd,VP exinf);
    static B count;
    static ID CycID;
    static ID TaskA_ID;
    
    EXPORT ER CycSample( void )
    {
      ER ercd;
      T_CCYC ccyc;
      T_CTSK ctsk;
      
      ctsk.bufptr = NULL;
      ctsk.exinf = (VP)NULL;;
      ctsk.itskpri = 15;
      ctsk.stksz = 512;
      ctsk.task = CycTaskSampseA;
      ctsk.tskatr = TA_HLNG | TA_RNG0;
      TaskA_ID = tk_cre_tsk(&ctsk);
       
      //创建周期处理
      ccyc.cycatr = TA_HLNG | TA_STA;
      ccyc.cychdr = CycHandler;
      ccyc.cycphs = 100;
      ccyc.cyctim = 200;
      ccyc.exinf = (VP)NULL;
      CycID = tk_cre_cyc(&ccyc);
      
      if(E_OK<= CycID)
      {
        tm_putstring((UB*)"成功创建周期性处理
    ");
      }
      else
      {
      }
      if(E_OK <= tk_sta_cyc(CycID))
      {
        tm_putstring((UB*)"成功启动周期性处理
    ");
      }
       tk_sta_tsk(TaskA_ID,5);
     
      return E_OK;
    }
    
    //验证周期处理与任务优先级无关
    void CycTaskSampseA(W stacd,VP exinf)
    {
      tm_putstring((UB*)"TaskA启动成功
    ");
       while(1);
    }
    
    void CycHandler(void)
    {
        count++;
        if(count == 10)
        {
          tm_putstring((UB*)"i will stop my cyc
    ");
          if(E_OK <= tk_stp_cyc(CycID))
          {
            tm_putstring((UB*)"sucess stop cyc
    ");
          }
          tm_putstring((UB*)"i will delete my cyc
    ");
          if(E_OK <= tk_del_cyc(CycID))
          {
            tm_putstring((UB*)"sucess delete cyc
    ");
          }
          return;
        }
        tm_putstring((UB*)"The times of cycle is: ");
        tm_putchar(count+48);
        tm_putstring((UB*)" !
    ");
    }

    输出:


    ----------------------------------------------------
            micro Tenux Version 1.6.00(build 0180)     
                Supported MCU is ST STM32F407VG        
      Copyright(c) 2008-2013 by Dalian uLoong Co.,Ltd. 
    ----------------------------------------------------

    Start alarm sucess
    Wait alarm stop
    Wait alarm stop
    Wait alarm stop
    Wait alarm stop
    Wait alarm stop
    Wait alarm stop
    Now alarm will delete
    Delete alarm sucess
    Push any key to shutdown the micro Tenux.

  • 相关阅读:
    git stash
    vim 使用
    git 分支管理
    git 日常使用
    js createElement
    git checkout
    2.2链表 链表中倒数第k个结点
    1.8字符串及分析 翻转子串
    全排列
    1.7数组 清除行列
  • 原文地址:https://www.cnblogs.com/zyqgold/p/3169270.html
Copyright © 2011-2022 走看看