In this lesson, you will learn how to calibrate your system timer by adjusting the prescaler to translate a high-speed system clock into a precise, usable timing interval.
The Role of the Prescaler
Your microcontroller’s system clock often runs at high frequencies (like 80MHz or 100MHz). If you tried to count these pulses directly using a 16-bit timer, the timer would overflow in less than a millisecond. To solve this, we use a prescaler.
Think of a prescaler as a "clock divider." If your system clock is 80MHz and you set your prescaler to 80, the timer will only increment once every 80 cycles. This effectively slows the timer clock down to 1MHz, creating a base interval of 1 microsecond per tick. By dividing the frequency, you gain the ability to time much longer events without the counter resetting constantly.
Calculating the Target Frequency
The formula for your timer clock frequency () is:
We add 1 because most hardware prescaler registers are zero-indexed; writing a 0 actually results in a division of 1 (no division), and writing a 79 results in a division of 80.
Practical Implementation
To achieve a 1-microsecond tick from an 80MHz system clock, you would configure the register like this:
// Configure Prescaler for 1MHz Timer Clock (80MHz / 80)
// Prescaler register = 80 - 1 = 79
TIMER_PRESCALER_REG = 79;
// Now, every increment of the timer's counter register
// corresponds exactly to 1 microsecond.
By adjusting this value, you can calibrate your hardware to match specific timing requirements, such as generating 1ms system ticks for real-time task scheduling or precise microsecond-scale pulse widths for motors. Always remember that the prescaler acts as the foundation; if your base frequency is calibrated incorrectly, every delay or PWM signal derived from that timer will be inaccurate.
Key Takeaway: The prescaler divides the high-speed system clock into a manageable frequency, allowing you to establish the base timing interval required for your specific application.