开发者

Timing problem in C

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 maint开发者_C百科ain 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


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.


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜