Bird
Raised Fist0
Embedded Cprogramming~20 mins

Generating precise delays with timers in Embedded C - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Timer Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of timer delay calculation
What is the output of this code snippet that calculates timer counts for a 1ms delay on a 16 MHz clock with a prescaler of 64?
Embedded C
#include <stdio.h>

int main() {
    unsigned long clock_freq = 16000000; // 16 MHz
    unsigned int prescaler = 64;
    unsigned int delay_ms = 1;
    unsigned int timer_counts = (clock_freq / prescaler) / 1000 * delay_ms;
    printf("Timer counts: %u\n", timer_counts);
    return 0;
}
ATimer counts: 250
BTimer counts: 25000
CTimer counts: 1000
DTimer counts: 16000
Attempts:
2 left
💡 Hint
Remember to divide the clock frequency by the prescaler, then by 1000 for milliseconds.
🧠 Conceptual
intermediate
2:00remaining
Understanding timer overflow for delay generation
Which statement correctly explains why timer overflow is used to generate precise delays in embedded systems?
ATimer overflow triggers an interrupt exactly when the timer reaches zero, allowing precise delay control.
BTimer overflow occurs when the timer count exceeds its maximum value, signaling that the desired delay time has passed.
CTimer overflow resets the CPU clock to synchronize delay timing with the system clock.
DTimer overflow disables the timer to save power during delay periods.
Attempts:
2 left
💡 Hint
Think about what happens when a timer reaches its maximum count value.
🔧 Debug
advanced
2:00remaining
Identify the error in delay timer setup
This code aims to configure a timer for a 500 microsecond delay on a 8 MHz clock with prescaler 10. What error causes the delay to be incorrect?
Embedded C
void setup_timer() {
    unsigned long clock_freq = 8000000;
    unsigned int prescaler = 10;
    unsigned int delay_us = 500;
    unsigned int timer_counts = (clock_freq / prescaler) / 1000000 * delay_us;
    // Timer register setup omitted
    // Assume timer_counts loaded into timer register
}
AThe delay_us variable should be in milliseconds, not microseconds.
BThe prescaler value is too high for 500 microseconds delay.
CThe calculation uses integer division causing loss of precision before multiplication.
DThe clock frequency should be divided by 1000, not 1000000.
Attempts:
2 left
💡 Hint
Check the order of operations and data types in the calculation.
📝 Syntax
advanced
2:00remaining
Identify the syntax error in timer initialization code
Which option contains the syntax error that prevents this timer initialization code from compiling?
Embedded C
void init_timer() {
    unsigned int prescaler = 64;
    unsigned int timer_value = 25000;
    TCCR0A = 0x00;
    TCCR0B = (1 << CS01) | (1 << CS00); // prescaler 64
    TCNT0 = timer_value;
}
ATCNT0 = timer_value;
Bunsigned int timer_value = 25000;
Cvoid init_timer() {
DTCCR0B = (1 << CS01) | (1 << CS00); // prescaler 64
Attempts:
2 left
💡 Hint
Check if the prescaler bits set in TCCR0B match the prescaler value 64.
🚀 Application
expert
3:00remaining
Calculate timer reload value for 1ms delay with 12-bit timer
You have a 12-bit timer (max count 4095) running at 1 MHz clock with prescaler 1. To generate a 1ms delay using timer overflow, what reload value should you load into the timer register at start?
A3095
B3995
C3595
D3905
Attempts:
2 left
💡 Hint
Calculate total counts needed for 1ms, then subtract from max count to find reload value.

Practice

(1/5)
1. What is the main purpose of using timers in embedded C programming for delays?
easy
A. To create precise and accurate wait times without blocking the CPU
B. To increase the CPU clock speed automatically
C. To store large amounts of data temporarily
D. To handle user input events directly

Solution

  1. Step 1: Understand timer functionality

    Timers count clock pulses to measure time intervals precisely without busy-waiting.
  2. Step 2: Identify timer use for delays

    Using timers allows the program to wait exact times while freeing CPU for other tasks.
  3. Final Answer:

    To create precise and accurate wait times without blocking the CPU -> Option A
  4. Quick Check:

    Timers = precise delays [OK]
Hint: Timers count clock pulses for exact delays [OK]
Common Mistakes:
  • Thinking timers speed up the CPU
  • Confusing timers with memory storage
  • Assuming timers handle user input
2. Which of the following is the correct way to start a hardware timer in embedded C?
easy
A. beginTimer();
B. start_timer();
C. timer_start();
D. initTimer();

Solution

  1. Step 1: Recognize common timer function names

    Standard embedded C libraries often use timer_start() to begin timers.
  2. Step 2: Check syntax correctness

    timer_start(); is a valid function call matching typical timer APIs.
  3. Final Answer:

    timer_start(); -> Option C
  4. Quick Check:

    Correct timer start syntax = timer_start() [OK]
Hint: Look for standard function names like timer_start() [OK]
Common Mistakes:
  • Using incorrect function names like start_timer()
  • Confusing initialization with starting
  • Missing parentheses in function calls
3. Given the code snippet:
timer_set(1000); // Set timer for 1000 ms
timer_start();
while(!timer_expired()) {
  // wait
}
LED_toggle();

What happens after this code runs?
medium
A. LED toggles immediately without delay
B. LED toggles after approximately 1 second delay
C. LED never toggles because timer_expired() is always false
D. LED toggles after 1000 CPU cycles, not milliseconds

Solution

  1. Step 1: Understand timer_set and timer_start

    The timer is set for 1000 milliseconds and then started to count down.
  2. Step 2: Analyze the while loop waiting for timer expiration

    The loop waits until timer_expired() returns true, which happens after ~1 second.
  3. Final Answer:

    LED toggles after approximately 1 second delay -> Option B
  4. Quick Check:

    timer_set(1000) = 1 second delay [OK]
Hint: timer_set(ms) delays for ms milliseconds [OK]
Common Mistakes:
  • Assuming timer_expired() never becomes true
  • Confusing CPU cycles with milliseconds
  • Thinking LED toggles immediately
4. Identify the error in this delay code:
timer_set(500);
while(timer_expired()) {
  // wait
}
LED_on();
medium
A. timer_expired() function does not exist
B. timer_set() should be called after the while loop
C. LED_on() should be called before timer_set()
D. The while condition should be !timer_expired() to wait correctly

Solution

  1. Step 1: Analyze the while loop condition

    The loop waits while timer_expired() is true, but timer just set, so expired is false initially.
  2. Step 2: Correct the condition to wait until timer expires

    It should wait while timer is NOT expired, so condition must be negated: !timer_expired().
  3. Final Answer:

    The while condition should be !timer_expired() to wait correctly -> Option D
  4. Quick Check:

    Wait while not expired = !timer_expired() [OK]
Hint: Wait while timer not expired: use !timer_expired() [OK]
Common Mistakes:
  • Using timer_expired() without negation in wait loop
  • Calling timer_set() after waiting
  • Assuming LED_on() order affects delay
5. You want to toggle an LED every 250 ms precisely using a timer interrupt. Which approach is best?
hard
A. Use a timer interrupt set for 250 ms to toggle LED inside ISR
B. Use a busy-wait loop with delay function for 250 ms repeatedly
C. Toggle LED once and never use timer
D. Use a timer interrupt but toggle LED outside ISR only

Solution

  1. Step 1: Understand precise timing with interrupts

    Using timer interrupts allows exact timing without blocking CPU.
  2. Step 2: Identify best place to toggle LED

    Toggling LED inside the timer ISR ensures it happens exactly every 250 ms.
  3. Final Answer:

    Use a timer interrupt set for 250 ms to toggle LED inside ISR -> Option A
  4. Quick Check:

    Timer ISR toggles LED precisely every 250 ms [OK]
Hint: Toggle LED inside timer ISR for precise periodic delay [OK]
Common Mistakes:
  • Using busy-wait loops causing CPU blocking
  • Toggling LED outside ISR losing timing precision
  • Not using timer interrupts for periodic tasks
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