检查位是否已更改

时间:2015-02-13 23:11:08

标签: c bit

我希望传输一点状态。设置后,即使状态发生变化,也应该进行10次转发。

这是我的解决方案:

 unsigned long Time;
 unsigned char State;
 unsigned char Flag;/*It is set by an other function*/
 unsigned char Bit;     
 #define BITDETECTION 1
 #define COUNT        2

void My_Function ()
{
  Bit = (Flag == 0)?0:1;
 switch(State)
  {
    case BITDETECTION:
    if(Bit == 0) Transmitte(Bit);
    else {State = COUNT; time = GetTime();/*Get the current time*/}
    break;
    case COUNT:
    if( GetTime() - time) <= 10 ) Transmitte(Bit);
    else State = BITDETECTION;
    break;
    default:break;
  }
}

这是对的吗?

1 个答案:

答案 0 :(得分:1)

以下是提案:

void My_Function ()
{
  Bit = (Flag == 0)?0:1;
  switch(State)
  {
  case COUNT:
    if( GetTime() - time) <= 10 )
    {
      Transmitte(Bit);
      break;
    }
    else
    {
      State = BITDETECTION;
      /* fall through to next case */
    }
  case BITDETECTION:
    if(Bit != 0)
    {
       State = COUNT;
       time = GetTime();/*Get the current time*/
    }
    Transmitte(Bit);
    break;
  default: abort();
  }
}