Bird
Raised Fist0
Embedded Cprogramming~20 mins

Timer prescaler and clock division in Embedded C - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
๐ŸŽ–๏ธ
Timer Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
โ“ Predict Output
intermediate
2:00remaining
Calculate Timer Frequency with Prescaler
Given a microcontroller clock frequency of 16 MHz and a timer prescaler set to 64, what is the timer's input clock frequency?
Embedded C
unsigned long clock_freq = 16000000;
unsigned int prescaler = 64;
unsigned long timer_freq = clock_freq / prescaler;
printf("%lu\n", timer_freq);
A250000
B1000000
C64000000
D16000000
Attempts:
2 left
๐Ÿ’ก Hint
Divide the main clock frequency by the prescaler value.
โ“ Predict Output
intermediate
2:00remaining
Timer Overflow Period Calculation
If a timer counts from 0 to 255 (8-bit timer) with a clock frequency of 1 MHz after prescaling, what is the overflow period in microseconds?
Embedded C
unsigned long timer_clock = 1000000;
unsigned int max_count = 256;
unsigned long overflow_period = max_count * (1000000 / timer_clock);
printf("%lu\n", overflow_period);
A1000000
B255
C1
D256
Attempts:
2 left
๐Ÿ’ก Hint
Overflow period = max count * timer clock period.
๐Ÿ”ง Debug
advanced
2:00remaining
Identify the Error in Prescaler Setting Code
What error will occur when compiling this code snippet that sets a timer prescaler?
Embedded C
unsigned int prescaler = 8;
TCCR0B = (1 << CS01) | (1 << CS00);
ASyntaxError: missing semicolon after prescaler declaration
BTypeError: invalid bitwise operation
CNo error, code compiles successfully
DRuntimeError: invalid prescaler value
Attempts:
2 left
๐Ÿ’ก Hint
Check punctuation at the end of statements.
โ“ Predict Output
advanced
2:00remaining
Effect of Clock Division on Timer Frequency
If the main clock is 8 MHz and the timer uses a clock division factor of 256, what is the timer frequency?
Embedded C
unsigned long main_clock = 8000000;
unsigned int division_factor = 256;
unsigned long timer_freq = main_clock / division_factor;
printf("%lu\n", timer_freq);
A256
B31250
C8000000
D32768
Attempts:
2 left
๐Ÿ’ก Hint
Divide the main clock by the division factor.
๐Ÿง  Conceptual
expert
3:00remaining
Understanding Combined Prescaler and Timer Resolution
A 16-bit timer runs with a 20 MHz clock and a prescaler of 128. What is the maximum timer overflow period in milliseconds?
A655.36 ms
B1310.72 ms
C419.43 ms
D1638.4 ms
Attempts:
2 left
๐Ÿ’ก Hint
Calculate timer clock frequency, then multiply by max count (65536) and convert to milliseconds.

Practice

(1/5)
1. What is the main purpose of a timer prescaler in embedded C programming?
easy
A. To generate an interrupt on every clock cycle
B. To increase the CPU clock speed
C. To divide the main clock frequency and slow down the timer counting
D. To reset the timer counter automatically

Solution

  1. Step 1: Understand the role of a prescaler

    A prescaler divides the main clock frequency to slow down the timer's counting speed.
  2. 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.
  3. Final Answer:

    To divide the main clock frequency and slow down the timer counting -> Option C
  4. Quick Check:

    Prescaler divides clock = To slow timer [OK]
Hint: Prescaler always divides clock to slow timer [OK]
Common Mistakes:
  • Confusing prescaler with timer reset
  • Thinking prescaler speeds up the clock
  • Assuming prescaler triggers interrupts
2. Which of the following is the correct way to set a prescaler value of 8 for a timer in embedded C?
easy
A. TCCR0 |= (1 << CS01);
B. TCCR0 |= (1 << CS00);
C. TCCR0 |= (1 << CS02);
D. TCCR0 |= (1 << CS01) | (1 << CS00);

Solution

  1. Step 1: Recall prescaler bits for timer0

    For prescaler 8, only bit CS01 must be set.
  2. Step 2: Match bits to options

    TCCR0 |= (1 << CS01); sets CS01 bit, correctly configuring prescaler 8.
  3. Final Answer:

    TCCR0 |= (1 << CS01); -> Option A
  4. Quick Check:

    Prescaler 8 = CS01 set only [OK]
Hint: Check datasheet bits for prescaler values [OK]
Common Mistakes:
  • Setting multiple bits for prescaler 8
  • Confusing bit positions for prescaler values
  • Using wrong register for timer control
3. Given a microcontroller clock of 16 MHz and a timer prescaler set to 64, what is the timer tick frequency?
medium
A. 1,000,000 Hz
B. 250,000 Hz
C. 16,000,000 Hz
D. 64 Hz

Solution

  1. Step 1: Calculate timer tick frequency

    Timer tick frequency = CPU clock / prescaler = 16,000,000 / 64 = 250,000 Hz.
  2. Step 2: Compare with options

    250,000 Hz matches the calculated frequency exactly.
  3. Final Answer:

    250,000 Hz -> Option B
  4. Quick Check:

    16MHz/64 = 250kHz [OK]
Hint: Divide clock by prescaler for tick frequency [OK]
Common Mistakes:
  • Multiplying instead of dividing clock by prescaler
  • Using wrong prescaler value
  • Confusing frequency units
4. Identify the error in this code snippet setting a prescaler of 256 for Timer1:
  TCCR1B = (1 << CS12) | (1 << CS10);
medium
A. CS12 bit is missing
B. No error, code is correct
C. TCCR1B register is incorrect for prescaler
D. CS10 should not be set for prescaler 256

Solution

  1. Step 1: Check prescaler bits for 256 on Timer1

    Prescaler 256 requires only CS12 bit set; CS10 must be cleared.
  2. Step 2: Analyze given code

    Code sets both CS12 and CS10 bits, which corresponds to prescaler 1024, not 256.
  3. Final Answer:

    CS10 should not be set for prescaler 256 -> Option D
  4. Quick Check:

    Prescaler 256 = only CS12 set [OK]
Hint: Only set bits matching prescaler exactly [OK]
Common Mistakes:
  • Setting extra bits causing wrong prescaler
  • Confusing prescaler bit combinations
  • Assuming code is correct without checking datasheet
5. You want to measure a 1-second delay using a 16-bit timer with a 16 MHz clock. Which prescaler value should you choose to make the timer count up to 1 second without overflow? (Timer max count = 65535)
hard
A. Prescaler 1024
B. Prescaler 64
C. Prescaler 128
D. Prescaler 8

Solution

  1. Step 1: Calculate timer tick frequency for each prescaler

    Tick frequency = 16,000,000 / prescaler. For 1024: 16,000,000 / 1024 โ‰ˆ 15,625 Hz.
  2. Step 2: Calculate max timer duration

    Max duration = max count / tick frequency = 65535 / 15,625 โ‰ˆ 4.19 seconds, which covers 1 second.
  3. 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.
  4. Final Answer:

    Prescaler 1024 -> Option A
  5. Quick Check:

    Prescaler 1024 allows 1s count without overflow [OK]
Hint: Choose prescaler so max count โ‰ฅ ticks in 1 second [OK]
Common Mistakes:
  • Ignoring timer max count limit
  • Choosing prescaler too small causing overflow
  • Not calculating ticks per second correctly
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard