2

I dont have good experience on c... i just want to learn some of the practical scenarios to be implemented in c.... for example how can i implement the following in C code...

y=1 when x=1
y=0 when x!=1

the main thing is that....

output y changes when input x changes and has to maintain its state for 1 second if there is any change in the input within 1 sec it has to maintain its previous state.

please any one help me on this..and kindly help me how to approch for this type of logics.. please

3
  • 1
    This sounds more like a VHDL/Verilog problem... Commented Dec 6, 2010 at 8:52
  • @Matt: I'd say temporal logic, but that shows my theory background. Commented Dec 6, 2010 at 8:56
  • 1
    Woods (1989) Temporal logic case study gives an idea of how temporal logic is used to model these kinds of constraints, with the obligatory elevator running example. Commented Dec 6, 2010 at 8:59

2 Answers 2

2

If you can afford busy waiting when x isn't changing, then

volatile int x;
int old_x, tmp = x;
while (1){
    y = ((old_x = tmp) == 1);
    Sleep(1000);
    while(old_x == (tmp = x));
}

if you have any event or interrupt when it's changed, it can be done without busy waiting.

Sign up to request clarification or add additional context in comments.

3 Comments

The qner was also after general thoughts on coding these kinds of example.
@Charles: it's almost all that C could do about VHDL's delay. Generic case should be tough..
+1 for using volatile. jack needs to develop on this. The part that's not mentioned in the question but is obvious is taking the input (which would require multithreading/multiprocessing).
0

Once you figure out how you want to handle IO and timing, these are possibilities for the relevant test:

y = (x == 1 ? 1 : 0);

or:

if (x == 1) 
    y = 1;
else 
    y = 0;

or:

y = 0;
if (x == 1)
    y = 1;

1 Comment

This doesn't treat the IO constraints in the qn at all.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.