expression
Calculate a timer tick rate from a divider
A timer counts clock ticks. Some MCUs, including the STM32 timer example in the Discovery book, use a prescaler register value plus one as the divisor. This example calculates the integer tick rate for an illustrative exact division and returns None for zero clock or overflow. Clock tree and register width are device-specific. Expected host stdout (not executed): ticks/s: 1000000 Common mistake: Assuming the peripheral clock equals the CPU clock or applying this register formula universally.
Formal Syntax & Structure
rust syntaxrust
clock_hz / (prescaler + 1)Working Example
Example Snippetrust
#![no_std]pub fn tick_hz(clock_hz: u32, prescaler: u32) -> Option<u32> { if clock_hz == 0 { return None; } let divider = prescaler.checked_add(1)?; if clock_hz % divider != 0 { return None; } Some(clock_hz / divider)}