检查位是否改变

Check if bit is changed

我要传输位的状态。设置后,即使状态发生变化也要传送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;
  }
}

是否正确?

这是一个提议:

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();
  }
}