Software set - reset flip flop

RobertN

Member
What’s the best way to do a dual input software flip flop? Can't wait around to do pulsin or handle interrupts. Input low must be detected as the program cycles through and polls the input pins, then toggles a bit.

Came up with this, it seems rather cumbersome. It depends on successive cycles through the software to capture the input change. It works since the input stays low long enough for it to be polled by several cycles.


if pin1 = 0 and bit0 = 1 then let bit1 = 1 'first input, capture low input
and set bit 1

bit0 = pin1 update bit 0


if pin2 = 0 and bit2 = 1 then let bit1 = 0 '2nd input, capture low input
and reset bit 1

bit2 = pin2 update bit 2


Is there a better way to do this without affecting the program loop time?
 

hippy

Ex-Staff (retired)
Congratulations, you're the first person to give me cause to use the &/ ( ANDNOT ) operator. This may be what you are after ...

#Picaxe 18
Do
bit0 = bit1 &/ pin0 ^ 1
bit1 = bit0 &/ pin1 ^ 1
pin1 = bit1
Loop

Run in the simulator. Press Pin 1 In, Pin 1 Out goes high, stays high no matter how Pin 1 In is toggled. Press Pin 0 In to clear the Pin 1 Out.

Basically an R-S Flip-flop. Pin 0 In = R, Pin 1 In = S, Pin 1 Out = Q, derived from the hardware which would be used to create a NAND gate R-S Fip-Flop ...


Image from http://wearcam.org/ece385/lectureflipflops/flipflops

Pin 0 Out is a Q\ output but doesn't exactly track inverse of Q in the same cycle. Use either bit0 or bit1 but not both and you should be okay.

If your input is active low, change the &/ to &
 
Last edited:

hippy

Ex-Staff (retired)
If you don't need true S-R Flip-Flop but S to set until R to clear, even easier ...

#Picaxe 18
Do
bit1 = bit1 | pin1 &/ pin0
pin1 = bit1
Loop
 

RobertN

Member
Thanks for the response. There are some powerful operators in there.

Will try | and ^. Might be able to use them else where.

Forgot to mention this is for a 08M.
 
Top