Timers count clock pulses to measure time. A prescaler slows down the timer clock so it counts slower, letting you measure longer times easily.
Timer prescaler and clock division in Embedded C
TIMx->PSC = prescaler_value; // Set prescaler register
TIMx->ARR = auto_reload_value; // Set timer max count
TIMx->CR1 |= TIM_CR1_CEN; // Enable timerThe prescaler divides the main clock by (prescaler_value + 1).
Setting prescaler to 0 means no division (timer runs at full clock speed).
TIM2->PSC = 7999; // Divide clock by 8000 TIM2->ARR = 999; // Count up to 1000 TIM2->CR1 |= TIM_CR1_CEN; // Start timer
TIM3->PSC = 0; // No prescaler, timer runs at full clock speed TIM3->ARR = 65535; // Max count for 16-bit timer TIM3->CR1 |= TIM_CR1_CEN; // Start timer
This program sets up a timer to count 1 second intervals by using a prescaler and auto-reload value. The timer clock is slowed down by the prescaler to make counting easier.
#include <stdint.h> #include "stm32f4xx.h" // Example MCU header void timer_init(void) { RCC->APB1ENR |= RCC_APB1ENR_TIM2EN; // Enable clock for TIM2 TIM2->PSC = 7999; // Prescaler: divide clock by 8000 TIM2->ARR = 999; // Auto-reload: count to 1000 TIM2->CNT = 0; // Reset counter TIM2->CR1 |= TIM_CR1_CEN; // Start timer } int main(void) { timer_init(); while(1) { if (TIM2->SR & TIM_SR_UIF) { // Check update interrupt flag TIM2->SR &= ~TIM_SR_UIF; // Clear flag // Timer reached 1 second (assuming 8 MHz clock) } } }
Prescaler value is always one less than the division factor.
Changing prescaler affects timer speed and resolution.
Always clear timer interrupt flags after handling to avoid repeated triggers.
Prescaler slows down the timer clock by dividing the main clock.
Use prescaler to measure longer times or slower events.
Timer count speed = main clock / (prescaler + 1).
Practice
Solution
Step 1: Understand the role of a prescaler
A prescaler divides the main clock frequency to slow down the timer's counting speed.Step 2: Compare options with prescaler function
Only To divide the main clock frequency and slow down the timer counting correctly describes this function; others describe unrelated timer features.Final Answer:
To divide the main clock frequency and slow down the timer counting -> Option CQuick Check:
Prescaler divides clock = To slow timer [OK]
- Confusing prescaler with timer reset
- Thinking prescaler speeds up the clock
- Assuming prescaler triggers interrupts
Solution
Step 1: Recall prescaler bits for timer0
For prescaler 8, only bit CS01 must be set.Step 2: Match bits to options
TCCR0 |= (1 << CS01); sets CS01 bit, correctly configuring prescaler 8.Final Answer:
TCCR0 |= (1 << CS01); -> Option AQuick Check:
Prescaler 8 = CS01 set only [OK]
- Setting multiple bits for prescaler 8
- Confusing bit positions for prescaler values
- Using wrong register for timer control
Solution
Step 1: Calculate timer tick frequency
Timer tick frequency = CPU clock / prescaler = 16,000,000 / 64 = 250,000 Hz.Step 2: Compare with options
250,000 Hz matches the calculated frequency exactly.Final Answer:
250,000 Hz -> Option BQuick Check:
16MHz/64 = 250kHz [OK]
- Multiplying instead of dividing clock by prescaler
- Using wrong prescaler value
- Confusing frequency units
TCCR1B = (1 << CS12) | (1 << CS10);
Solution
Step 1: Check prescaler bits for 256 on Timer1
Prescaler 256 requires only CS12 bit set; CS10 must be cleared.Step 2: Analyze given code
Code sets both CS12 and CS10 bits, which corresponds to prescaler 1024, not 256.Final Answer:
CS10 should not be set for prescaler 256 -> Option DQuick Check:
Prescaler 256 = only CS12 set [OK]
- Setting extra bits causing wrong prescaler
- Confusing prescaler bit combinations
- Assuming code is correct without checking datasheet
Solution
Step 1: Calculate timer tick frequency for each prescaler
Tick frequency = 16,000,000 / prescaler. For 1024: 16,000,000 / 1024 ≈ 15,625 Hz.Step 2: Calculate max timer duration
Max duration = max count / tick frequency = 65535 / 15,625 ≈ 4.19 seconds, which covers 1 second.Step 3: Check smaller prescalers
Prescaler 128 gives 125,000 ticks per second, 64 gives 250,000, and 8 gives 2,000,000--all exceeding 65,535, causing overflow before 1 second.Final Answer:
Prescaler 1024 -> Option AQuick Check:
Prescaler 1024 allows 1s count without overflow [OK]
- Ignoring timer max count limit
- Choosing prescaler too small causing overflow
- Not calculating ticks per second correctly
Ready to prove it?
Pass the test and this pattern moves unknown
