How to execute a single impulse depending on the state of machne in C?

I want to write c code to generate a pulse, but can not, it seems, envelops the logic for its implementation. I come from Verilog’s strong background and know how to do it in verilog (look for state changes with xor and use this momentum, stretch it over time, if necessary, registering it several times)

How do I do this in C? I want to do something like

while(1)
{
   switch(state)
   case 0: // generate single pulse
   case 1: // dont generate 
   case 2: // dont gererate
   case 3: // generate single pulse
   usleep(1000) // I want a 1ms pulse
}  
condition

It is modified by code running on the FPGA, therefore it changes depending on some logic.
This doesn't seem to be the way to do this. Some recommendations will be appreciated.

+4
source share
3 answers

statemachine, .

  • ,
  • state (, ISR)
  • - ( )

, state. . updatestate(), ; - , // enter protection ... // leave protection ....
, statemachine , usleep(1000);. , .

:

// somewhere before
volatile int state = 0; // somehow changing within the loop 

int statecopy = 0;  
int statebefore = state,

while(1)
{
    // updatestate(); // if that is necessary

    // Note that the state machine is blind for changes to state
    // between executions of these initial lines.
    // I.e. changes are noticed only when executing the update above
    // or the critical section below this comment. (depending on how
    // the variable state changes.

    // enter protection for critical section
    statebefore = statecopy;
    statecopy   = state;
    // leave protection for critical section

    switch(statecopy )
    {
        case 0: // generate single pulse
            if (statecopy != statebefore)
            {
                // switch high
                usleep(1000); // I want a 1ms pulse
                // switch low
            }
            break;
        case 1: // dont generate 
            break;
        case 2: // dont gererate
            break;
        case 3: // generate single pulse
            if (statecopy != statebefore)
            {
                // switch high
                usleep(1000); // I want a 1ms pulse
                // switch low
            }
            break;
        default:
            break;
    }
}
+3

, :

int state = 0;
while(1) {
  switch(state) {
    case 0: // generate single pulse
       start_pulse();
       usleep(1000);
       stop_pulse();
       break;
    case 1: // dont generate , wait 1ms?
       usleep(1000);
       break;
    case 2: // dont generate, wait 1ms?
       usleep(1000);
       break;
    case 3: // generate single pulse
       start_pulse();
       usleep(1000);
       stop_pulse();
       break;
  }
  state = (state+1)%3; // next state: 0, 1, 2, 3, 0, 1, 2,...
} 
+1

switch( state ) {
case 1:
case 2:
  usleep(1000);
  break;
case 0:
case 3:
  /* pulse high */
  usleep(1000);
  /* pulse low */
}

Perhaps, perhaps, but probably not worth it, let the compiler figure it out.

0
source

Source: https://habr.com/ru/post/1682028/


All Articles