Digital counter 2026-10-25
A digital counter is a finite-state circuit that increments its stored value on each qualifying clock or event. A binary -bit implementation naturally has modulus . Dividing counted ticks by a known clock frequency gives a time interval, subject to wrap ambiguity and sampling accuracy.
Primary source: Released examination archive, member 4224.pdf. The original nine-page cover identifies the Preliminary Examination in Computer Science, Trinity Term 2014, Friday 20 June. Questions 1–3 cover digital counters, the MIPS architecture and file systems; Questions 4–6 cover linear algebra; Questions 7–8 use the supplied natural deduction rules in Figures 1–3 on printed page 9.
Gray-code counter 2026-10-31
A digital counter following a Gray code, so adjacent output states differ in exactly one bit. The two-bit cycle is implemented by D flip-flops with next-state functions , .
Hardware timer Created 2026-10-25 Updated 2026-10-31
A hardware timer uses a clock signal to measure intervals, commonly by a digital counter with capture or comparison facilities. A comparison event can request an interrupt or change an output directly. Counter resolution, wraparound, interrupt latency and the hardware's comparison semantics determine the timing guarantees available to software.
Modulo-counter elapsed-time identity Created 2026-10-24 Updated 2026-10-31
A free-running digital counter with modulus shows only the residue of elapsed ticks. If two observations differ by a true interval , their modular difference is congruent to and is the unique representative in , proving the displayed identity. It handles a wrap between observations without a conditional branch. A complete elapsed modulus is indistinguishable from zero, so the interval bound is essential. For a -bit timer at frequency , the wrap period is , and unsigned integer arithmetic in C can implement the difference directly on an appropriate type.
Modulo-six counter 2026-10-31
A digital counter cycling through six stored values. With ordinary binary representation, the valid three-bit states are . The unused encodings require either an initialization assumption or specified recovery transitions.
Use a synchronous counter cascade, with the three-bit modulo-six counter advancing on every active clock edge and the Gray-code counter advancing only when the old binary state is . Its enable can therefore be
On the six valid binary states, only at . Both digital counters sample the same clock: at an edge leaving , the binary output becomes and simultaneously advances to the next Gray code. At all other edges holds. Starting from , this gives exactly the listed 24-state sequence. Use the shared clock and an enable, rather than making a combinational terminal-state signal into a new clock.[ref]
For the output , every state with has . For , the only extra high states are with binary state , and with binary state or . On valid states these conditions are and , respectively. Hence
The lower circuit in the figure implements this with an AND for , an exclusive or on , an AND with , and an OR with . The resulting Morse code output blocks, in Gray code order , are
Across the two middle blocks these join to three three-clock highs separated by one-clock lows; the first and last blocks provide the three single-clock highs of each S. The specified table is a repeating SOS symbol pattern; its wraparound has a one-clock low, rather than an added inter-message pause. The output is a Moore machine decoding of current stored bits; its value changes after those bits settle.
A short interrupt handler should acknowledge the detector and count work; a timed finite-state machine should generate the flashes. A Boolean “someone arrived” variable cannot preserve several queued detections. This is an interrupt-driven pulse sequencer. Initialise the light off, the pending-event counter to zero and the sequencer to IDLE before enabling interrupts.
In the following pseudocode, interrupt acknowledgement uses the source-specified operation of writing zero. The atomic operation and timer calls name abstract services, not undocumented device registers of a particular microcontroller.
eye_handler():
    if EYE_DETECTED != 0:
        EYE_DETECTED = 0
        pending = pending + 1

try_take_event():
    briefly mask the detector interrupt
    if pending == 0: taken = false
    else: pending = pending - 1; taken = true
    restore the previous interrupt mask
    return taken

service(now):
    if state == ON and elapsed(now, started) >= one_second:
        LIGHT = 0
        state = GAP
        started = now
    if state == GAP and elapsed(now, started) >= one_second:
        state = IDLE
    if state == IDLE and try_take_event():
        LIGHT = 1
        state = ON
        started = now
The main program repeatedly handles a new event or the next timer deadline and calls service. The ON state lasts one second; every completed flash enters GAP, even if the queue is empty at that instant. An arrival during that gap must wait for it to expire. Otherwise turning the light off and immediately treating the device as idle could allow a new flash too soon. Once a gap has expired, a queued event starts a new flash; if no work is pending the device remains idle. Removing a pending event only when a flash starts gives one completed flash per consumed event.
An interrupt can arrive during the main program's decrement. Without a critical section, its increment can be overwritten by a race condition. Merely declaring the counter volatile, as in volatile-qualified objects in C, does not make the decrement an atomic operation. Short interrupt masking, or an appropriate atomic operation, protects it; interrupts must remain enabled during the one-second intervals so that detections continue to be recorded.
A calibrated busy-wait loop is simple, but occupies the CPU and its duration depends on instruction timing, compiler transformations and time spent in interrupts. Polling a hardware timer avoids calibration dependence but still consumes CPU while polling. A timer interrupt or timer comparison deadline permits useful work or safe sleeping between transitions and is preferable for precise timing. If sleeping, the test for queued work and entry into sleep must avoid a lost wake-up. By the modulo-counter elapsed-time identity, a wrapping digital counter is usable if elapsed-time subtraction is unsigned and the real observation interval is less than its modulus; a wide enough counter or suitable bounded intervals make this explicit. Software dispatch adds bounded latency, so the specification is achieved to timer/interrupt-service resolution; stricter physical pulse-width requirements call for hardware output comparison or a suitably prioritised timer handler.
Finally, the device register is only a single-bit event latch. Two physical detections before acknowledgement may coalesce, so no software can recover their multiplicity from that bit alone. The event counter must also have enough capacity for the allowed backlog; indefinitely many arrivals faster than one flash every two seconds cannot be guaranteed with finite storage. These are explicit hardware/rate assumptions, not missing input files. Keeping the handler short minimises the rearming interval but cannot abolish those limitations.
Coupled digital counters sharing one clock, with a slower stage enabled by a terminal-state decode of a faster stage. Both stages sample the old state at the same clock edge. An enable changes whether a stage advances; it does not create a separate clock from a combinational decode.
Timer interrupt 2026-10-24
An interrupt requested by a hardware timer, for example when a free-running digital counter reaches a programmed comparison value. Scheduling the next deadline permits other work or sleep between timed state transitions.