Tuesday, March 18, 2025

Entry 5: Clock and D flip-flop.

Clock

Ny current goal is to get to "the register", which is based on "D flip-flops". Asking Grok about D flip-flops makes it clear that we need to start with "the clock".

So, "What is a clock in a CPU?": (curated answer)

In a CPU, the clock is a signal that acts like the heartbeat of the processor, synchronizing all its operations. It’s essentially a continuous, oscillating electrical pulse—typically a square wave—that alternates between high (1) and low (0) at a specific frequency. This frequency, measured in hertz (Hz), determines how fast the CPU can execute instructions, with modern CPUs running at gigahertz (GHz) speeds—billions of cycles per second.

The clock signal comes from a clock generator, often a crystal oscillator on the motherboard, which feeds into the CPU. The CPU’s internal circuitry then uses this signal to trigger registers, latches, and other components. In short, the clock keeps the CPU’s chaos in check, like a conductor in an orchestra.

"Can I generate a clock signal manually using a button?":

Yes, you can absolutely generate a clock signal manually using a button! This approach creates a manual clock where each press of the button toggles the signal between high and low, mimicking the clock pulses a CPU uses to step through operations.

The description is then the same as the pull-down resistor example in entry 2,

I'll generate a clock signal by "clicking the button". Later, I will have to design a clock that alternates between 0 and 1 automatically (Grok mentioned a chip called the 555). Grock also mentions this issue:

Issue: Switch Bounce

Real buttons aren’t perfect. When you press or release, the contacts can “bounce,” creating multiple quick high-low transitions instead of one clean pulse. Digital circuits (like a flip-flop) might see these as extra clock ticks.

Grok has some suggested solutions, like adding a capacitor, but I'll get back to this if needed.

Here is some additional terminology:

D flip-flop

"Tell me about the D flip-flop":

A D flip-flop, also known as a data or delay flip-flop, is a fundamental building block in digital electronics used to store a single bit of data. It’s a type of sequential logic circuit that captures the value of its input (D) at a specific moment and holds that value at its output (Q) until the next capture event. This makes it incredibly useful for synchronizing data in digital systems, such as registers, counters, and memory elements.

Here’s the gist of how it works: the D flip-flop has one data input (D), a clock input (CLK), and two outputs—Q (the stored value) and Q̅ (the complement of Q). When the clock signal triggers (usually on a rising or falling edge, depending on the design), the value at D is transferred to Q. Until the next clock trigger, Q remains stable, even if D changes. This edge-triggered behavior distinguishes it from simpler circuits like latches, which respond continuously to input changes when enabled.

The basic truth table for an edge-triggered D flip-flop looks like this:

  • CLK = ↑ (rising edge), D = 0: Q = 0, Q̅ = 1
  • CLK = ↑ (rising edge), D = 1: Q = 1, Q̅ = 0
  • CLK = no change: Q = previous Q, Q̅ = previous Q̅

In practice, D flip-flops can also have additional inputs like preset (to force Q to 1) and clear (to force Q to 0), though these are optional and depend on the specific implementation.

You’ll find D flip-flops everywhere in digital design—think CPUs, where they’re used in registers to hold data between processing stages, or in communication systems to align incoming signals. They’re typically built from logic gates (like NAND or NOR) or come packaged in integrated circuits (e.g., the 74LS74 chip, a dual D flip-flop).

"What is the difference between a D flip-flop and an SR latch" (see entry 4)

SR Latch (Set-Reset): Stores a single bit based on two inputs: S (Set) and R (Reset).
D Flip-Flop: Stores a single bit based on one data input (D) and a clock signal (CLK).

SR LatchLevel-sensitive: Responds continuously to S and R as long as they’re active. No clock is required—it’s asynchronous.
D Flip-FlopEdge-triggered: Only updates Q when the clock signal transitions (e.g., rising or falling edge). It’s synchronous.

Before getting into how the D flip-flop is designed, let's create a demonstration. I found out that 74HC74 has two D-flip-flops.

Demonstration

"How can I demonstrate the D flip flop using a 74HC74, buttons and LEDs"

Demonstrating a D flip-flop using a 74HC74 IC with buttons and LEDs is a fantastic hands-on way to see how it works. The 74HC74 is a dual D flip-flop chip, meaning it has two independent D flip-flops inside, each with a data input (D), clock input (CLK), and outputs (Q and Q̅). We’ll set up a circuit to manually clock one flip-flop with a button, control the D input with another button, and use LEDs to show the Q and Q̅ outputs. Here’s how to do it on a breadboard.


What You’ll Need

  • 74HC74 IC (dual D flip-flop, CMOS logic).
  • 2 Push Buttons (momentary, normally open) - One for CLK, one for D.
  • 2 LEDs - One for Q, one for Q̅.
  • 2 Resistors - 330 Ω (for LEDs).
  • 2 Resistors - 10 kΩ (for pull-downs on buttons).
  • Power Supply - 5V (e.g., from a USB or Arduino).
  • Breadboard and Jumper Wires.
  • Optional: 0.1 µF capacitor (for CLK debounce).

Pinout of the 74HC74

  • Each flip-flop has:
    • D (data): Pin 2 (1D) or 12 (2D).
    • CLK (clock): Pin 3 (1CLK) or 11 (2CLK).
    • Q: Pin 5 (1Q) or 9 (2Q).
    • : Pin 6 (1Q̅) or 8 (2Q̅).
    • PRE̅ (preset, active-low): Pin 4 (1PRE̅) or 10 (2PRE̅).
    • CLR̅ (clear, active-low): Pin 1 (1CLR̅) or 13 (2CLR̅).
  • Power:
    • VCC (Pin 14): 5V.
    • GND (Pin 7): Ground.

We’ll use flip-flop 1 (pins 1–6) and disable PRE̅ and CLR̅ to simplify.




Circuit Setup

1. Power the IC

  • Connect Pin 14 (VCC) to 5V.
  • Connect Pin 7 (GND) to ground.

2. Disable Preset and Clear

  • Tie Pin 4 (1PRE̅) and Pin 1 (1CLR̅) to 5V (high) with jumper wires. This keeps them inactive (they’re active-low, so high = off).

3. D Input (Button-Controlled)

  • Connect Pin 2 (1D) to one side of the D button.
  • Connect the other side of the D button to 5V.
  • Add a 10 kΩ pull-down resistor from Pin 2 to ground.
    • Button released: D = 0 (low).
    • Button pressed: D = 1 (high).

4. Clock Input (Manual Clock with Button)

  • Connect Pin 3 (1CLK) to one side of the CLK button.
  • Connect the other side of the CLK button to 5V.
  • Add a 10 kΩ pull-down resistor from Pin 3 to ground.
    • Button released: CLK = 0.
    • Button pressed: CLK = 1.
  • Optional: Add a 0.1 µF capacitor from Pin 3 to ground to debounce the button (reduces false triggers).

The 74HC74 is rising-edge-triggered, so Q updates when CLK goes from 0 to 1 (button press).

5. Outputs (LEDs)

  • Connect Pin 5 (1Q) to one LED’s anode (+), then through a 330 Ω resistor to ground.
  • Connect Pin 6 (1Q̅) to the second LED’s anode (+), then through a 330 Ω resistor to ground.
    • Q = 1: First LED lights up.
    • Q̅ = 1: Second LED lights up.

How to Demonstrate It

  1. Power On:
    • Both LEDs might start in a random state (Q could be 0 or 1).
  2. Set D and Clock It:
    • Hold D button down (D = 1):
      • Press and release the CLK button. The Q LED lights up (Q = 1), Q̅ LED turns off (Q̅ = 0).
    • Release D button (D = 0):
      • Press and release the CLK button. The Q LED turns off (Q = 0), Q̅ LED lights up (Q̅ = 1).
  3. Test Holding State:
    • Change D (press or release the D button) without touching CLK. The LEDs won’t change—Q only updates on a rising CLK edge.
  4. Repeat:
    • Toggle D between 0 and 1, then press CLK each time to see Q follow D at the moment of the clock pulse.


I will dig deeper into the D flip-flop in later entries.