Stumpyhuck29
Stumpyhuck29

Reputation: 63

How to emulate an Edge Triggered Flip-Flop?

I have a GPIO pin(GP4) on a microcontroller with a continuous digital pulse train coming into it. I am writing to it in C and do not have interrupts on this chip. I have been calling my input "#define inputA GP4". In C, I would like to be able to find the positive edge of a pulse and trigger the timer on it. I'm not sure if an XOR will accomplish what I need by storing inputA to another variable at some point. The other problem that I have is I need to be able to do this and not get caught in an infinite loop where I can still do other things sequentially with my code, if for instance, inputA goes to 0 and stays at 0 permanently. This means that my option of using while(inputA=0); is out of the question. Thanks in advance!

Upvotes: 0

Views: 5543

Answers (1)

glglgl
glglgl

Reputation: 91129

You should write the old value to some variable.

A positive edge is define by the old value being 0 and the new one 1. You should check exactly that:

char oldval, newval;
oldval = inputA;
while (1) {
    newval = inputA;
//    if (oldval == 0 && newval == 1) {
    if (!oldval && newval) {
        // positive edge
    }
    oldval = newval;
    // do other stuff
 }

Upvotes: 4

Related Questions