D flip flop question - how to make a latch

Go To Last Post
11 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hello,

I am trying to toggle a small motor on and off with a single push button.

Button press = motor ON, next button press = motor OFF.

So essentially I need a signal to change from high to low with each button press - a change of state.

A friend told me you can make a latch from a positive edge triggered D flip flop by connecting the NOT Q output to the D input and using the clock input as the input from the button's pull up resistor.

Has anyone ever seen this done before? It looks odd to me because you are connecting the NOT Q output to the D input.

Thanks in advance on any opinions on if this is a good and reliable design idea.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Yes, that is how you would do it.

HOWEVER, you will need to debounce the switch, otherwise, it will make many toggles every time the switch is pressed and every time it is released.

Jim

 

Until Black Lives Matter, we do not have "All Lives Matter"!

 

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

What has been described is the basis of sequential digital logic. Draw a table of D before clock and D after clock. It will show what happens.

Regarding bounce - most D f/f come in a package of two (4013 or 74x74). If your pushbutton has changeover contacts, you can use the other f/f as a R/S f/f to debounce the switch. If your switch is not change over, then you need to resort to a resistor/capacitor filter to remove the bounce.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

All electronics specs can be implemented with all hw or all sw or a mix of both. Since this forum discusses how to program avrs in c, I will show you how I program a 'software toggle' Lets say the input is in pb0 and the output is on pd0.

unsigned char but;   //button state 0/in or 1 out
unsigned char butos; //oneshot of button going on
unsigned char butl;  //button state last pass
unsigned char led;   //output state to be displayed led or relay etc
.
.
.
but   = (PINB & 0x01) = 0; //read button state
butos =  but && !butl;     //set one shot iff but on this pass an off last pass
butl  =  but;              //remember but state for next pass
if(butos){
  led = !led;              //toggle led on button oneshot
  if(led) PORTD |= 0x01; else PORTD &= ~0x01; //copy led var state to output 
}
.
.
.
//assume the rest of the main program takes longer than the button bounce time.
//button bounce might be 1 ms, so make program loop time 10 ms

Imagecraft compiler user

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

cnewbie wrote:

Has anyone ever seen this done before? It looks odd to me because you are connecting the NOT Q output to the D input.

Reg.D = !Reg.Q
that can look strange, until you grasp that transfer occurs on the active Clock edge, and the NOT of Q before the clock appears on Q one Tpd after the clock.

To do this with a PB, as mentioned you need to de-bounce the PB - for that you are probably best to find a D-FF with schmitt/hysteresis on the Clock signal, and use a simple RC on the PB.

Examples could be HEF4013B, or 1G80/ 2G80 in smaller SMD choices.

Some Power-Toggle designs use a Dual FF, and two time constants. One to debounce the button and a longer 5~10s one for deep power off choices.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

What is the D FF going to drive, to drive the motor?

Its been so long since I used one I don't recall its current output.

A Tiny uC might have fewer pins, (smaller chip), than the D FF, and might be able to power a small motor without a driver transistor.

JC

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

It would be a very small motor to direct drive from any Logic device, so an external Transistor/MOSFET will be needed.
The HEF4013B I mentioned above has 3-15V Vcc, so may not need a complex regulator, just a logic level MOSFET.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Microprocessors were invented as 'logic replacement' to make things cheaper/faster/smaller. Adding external parts/logic to a microprocessor is sort of retro, but it is in line with my assertion about all electronic specs can be met by all hw or all sw.

Imagecraft compiler user

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thanks for the example Bob,

I always find them easy to read and understand.

JC

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Your best bet is to use a SPDT momentary switch in conjunction with a 74xx74 dual D-Flop (14 pin pkg). Use one flop for debouncing, the other flop to implement the toggle latch just as you described.

To make the debouncer connect the common switch terminal to ground and the other two switch terminals to the RST/ and SET/ inputs of the D-Flop. The order (i.e. NO and NC ) is not important as you will get one polarity of switch state from the flop's Q output and the inverse from the Q/ ouput. Choose which one you need to drive the latch you made from the other flop. You will also need two pull-up resistors on the RST/ & SET/ inputs which are connected to the NO & NC switch terminals. Theses pull-up are tied to +Vcc ( i.e. 5 or 3.3 VDC . 1K resistors should be fine.

That's it, one 14-pin SOIC or DIP and two resistors!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

That software oneshot trick was the first thing I learned from Chris Lea when I started at ECC in '74. So I've been passing it on for 40 years or so... Hope Chris googles his name sometime....

Imagecraft compiler user