Course contentsShow
← Embedded Systems & Computer Architecture
Lesson 240 of 9363. Microcontroller Fundamentals and Bare-Metal CFree lesson

Selecting Clock Multiplexers for Dynamic Switching

Program clock control registers to switch between different clock sources at runtime to balance high-performance processing with low-power idle states.

In this lesson, you will learn how to program clock multiplexers to switch your microcontroller between high-performance processing and energy-efficient idle states during runtime.

The Clock Multiplexer Concept

Think of a clock multiplexer (MUX) as a digital "train switch." Your microcontroller has several clock sources—such as a fast, power-hungry Phase-Locked Loop (PLL) for heavy computation and a slow, low-power internal RC oscillator for standby periods. The MUX acts as the junction where these paths meet, allowing you to route exactly one of these sources to the system bus at any given time.

By programmatically toggling the MUX select bits in your clock control registers, you can change your system clock frequency on the fly. This is the cornerstone of dynamic power management: you "dial up" the clock speed when performing complex data processing and "dial down" to a low-frequency oscillator the moment the work is finished.

Implementing a Runtime Switch

To switch clock sources, you typically write to a specific field in the Clock Configuration Register. However, you must be careful: the hardware often requires a "handshake" to ensure the new clock is stable before the switch completes.

// Example: Switching from a slow internal RC to a fast PLL
void switch_to_high_speed(void) {
    // 1. Ensure the target source (PLL) is enabled and locked
    while (!(CLOCK->STATUS & PLL_LOCKED_BIT));

    // 2. Update the MUX selection bits in the Clock Control Register
    // Bit field [3:2] typically controls the MUX source
    CLOCK->CFG &= ~CLOCK_MUX_MASK;        // Clear previous source
    CLOCK->CFG |= (PLL_SOURCE_VAL << 2);  // Set to PLL

    // 3. Wait for the hardware to acknowledge the clock switch
    while ((CLOCK->STATUS & CURRENT_CLOCK_MASK) != PLL_SOURCE_VAL);
}

Why This Matters

When you aren't actively processing data, running at 100MHz is wasteful. By selecting a lower-frequency source, you reduce the dynamic power consumption (P = CV²f), where frequency (f) is directly proportional to power draw. Mastering the MUX allows your firmware to adapt to the workload, ensuring your device lasts longer on battery power while still maintaining the performance necessary for heavy computational tasks.

Key Takeaway: Use the clock multiplexer to dynamically route your system clock to different oscillators, enabling you to trade off raw performance for significant power savings during runtime.