Stack Over
Stack Over

Reputation: 425

Check if bit is changed

I want transmit the state of a bit. When it is set, it should be transmetted for 10 secondes even if its status changes.

here is my solution:

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

Is it correct?

Upvotes: 0

Views: 462

Answers (1)

Valeri Atamaniouk
Valeri Atamaniouk

Reputation: 5163

Here is a proposal:

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

Upvotes: 1

Related Questions