← Writing

MCU Clock System

2025-07-15 embedded · basic · clock


🟠 Clock is the basis for timers and interrupts The clock system sets the pace of all digital components of the microcontroller. Timers use its signals to generate interrupts and perform tasks with precise time control.


⏱ What is a clock in an MCU?

A clock is not primarily about “time” - it is a system that generates clock signals to synchronize the CPU and peripheral modules (timers, UART, ADC, etc.). The frequency of the clock signal determines how fast the MCU executes instructions and processes events.
MCU components usually operate at different frequencies and need to be synchronized to perform operations at the same time.

A simple digital signal switches between high and low levels at a certain frequency (in Hertz).
Frequency is the number of events that occur per unit of time, usually per second.

Important: High frequency provides performance, but increases power consumption and heat generation. Therefore, peripheral devices often operate at a lower frequency.

⚙️ How do clocks work?

The most common clock generator is the quartz crystal.
It is used because it can vibrate at a stable frequency (the resonant frequency) when voltage is applied to it.

One of the disadvantages of a quartz crystal is that it is usually limited to generating a single frequency in the megahertz range. On complex boards, this can be a problem, since different components often operate at different frequencies and at much higher speeds. To solve the problem of having to use multiple frequencies on a single board, designers often use a PLL and prescalers.

To obtain different frequencies, use:

🔧 Clock system components

⌚ Clock types

Clocks are also used for their usual function - keeping time. There are 2 types of clocks for this purpose:

🛠 Example of clocks setup in Rust (RP2040)

use rp2040_hal as hal;
const XTAL_FREQ_HZ: u32 = 12_000_000;

let mut watchdog = hal::Watchdog::new(pac.WATCHDOG);

let clocks = hal::clocks::init_clocks_and_plls(
    XTAL_FREQ_HZ,
    pac.XOSC,
    pac.CLOCKS,
    pac.PLL_SYS,
    pac.PLL_USB,
    &mut pac.RESETS,
    &mut watchdog,
).unwrap();

⚡ Communicating with interrupts