• 一种定时更新一组状态的方法


    定时更新一组状态一般用于状态机运行的判定条件

    实现机制为:

      利用一硬件定时器作为时基,该时基选取适合状态更新频率,以该时基为累加周期,待累加周期变量值满足各状态更新周期时更新各状态。

      具体实现为:以各状态更新周期为对象,对累加周期变量取余,以结果0为依据更新各状态,累加周期变量清0依据为各状态更新周期的最小公倍数,不放任累加周期变量在其类型范围边界处自动清零,就是避免在范围边界处清0带来的绝对误差

    示例

        更新状态      更新周期

        status1       100ms

        status2       200ms

        status3       1s

        status4       5s

        status5       1min

        status6       1hour

    示意代码如下

     1 #define  T_PERIOD          10        /* 10ms */
     2 
     3 
     4 unsigned char status1=0, status2=0, status3=0, status4=0, status5=0, status6=0;
     5 
     6 unsigned int timer10ms_count =0;
     7 
     8 /* 设置定时器周期 */
     9 set_timer_period(T_PERIOD);
    10 
    11 /* 定时器计时到时的处理函数 */
    12 void timer_handler()
    13 {
    14     timer10ms_count ++;
    15 
    16     if(0 == (timer10ms_count  %10))
    17     {
    18         status1 =1;
    19     }
    20     
    21     if(0 == (timer10ms_count  %20))
    22     {
    23         status2 =1;
    24     }
    25     
    26     if(0 == (timer10ms_count  %100))
    27     {
    28         status3 =1;
    29     }
    30     
    31     if(0 == (timer10ms_count  %100*5))
    32     {
    33         status4 =1;
    34     }
    35     
    36     if(0 == (timer10ms_count  %100*60))
    37     {
    38         status5 =1;
    39     }
    40     
    41     if(0 == (timer10ms_count  %100*3600))
    42     {
    43         status6 =1;
    44     }
    45     
    46     /* 待满足最小公倍数归零 */
    47     if(3600 == timer10ms_count)
    48     {
    49         timer10ms_count =0;
    50     }
    51 }

    范围边界处清0带来的绝对误差:

      上边代码中累加周期变量 timer10ms_count 类型为 unsigned int 其取值范围 [0, 4_294_967_295], 取值范围内1hour计时的次数:4,294,967,295 / 3,600 =1,193,046.47083...,timer10ms_count 计数到边界时即 3,600 *1,193,046 =4,294,965,600距越界还有1695个计数,但这已不够1hour计时数3600,所以待 timer10ms_count 计数到边界值 4_294_967_295 处归0,由于这1695个计数不满足1hour计时数3600而造成绝对误差。

  • 相关阅读:
    [HNOI2004]L语言
    快速沃尔什变换FWT
    [BZOJ1486][HNOI2009]最小圈
    [BZOJ4819][SDOI2017]新生舞会
    [POJ2976]Dropping tests
    CTSC2018&APIO2018游记
    [Luogu3769][CH弱省胡策R2]TATT
    [BZOJ3489]A simple rmq problem
    [BZOJ4066]简单题
    [BZOJ2648]SJY摆棋子
  • 原文地址:https://www.cnblogs.com/skullboyer/p/9095830.html
Copyright © 2020-2023  润新知