Challenge - 5 Problems
Timer Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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; }
Attempts:
2 left
💡 Hint
Remember to divide the clock frequency by the prescaler, then by 1000 for milliseconds.
✗ Incorrect
The timer counts needed for 1ms delay is (16,000,000 / 64) / 1000 = 250,000 / 1000 = 250 counts.
🧠 Conceptual
intermediate2:00remaining
Understanding timer overflow for delay generation
Which statement correctly explains why timer overflow is used to generate precise delays in embedded systems?
Attempts:
2 left
💡 Hint
Think about what happens when a timer reaches its maximum count value.
✗ Incorrect
Timer overflow happens when the timer count exceeds its max value and rolls over, which can be used to signal that a set delay period has elapsed.
🔧 Debug
advanced2: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
}Attempts:
2 left
💡 Hint
Check the order of operations and data types in the calculation.
✗ Incorrect
Integer division (clock_freq / prescaler) / 1000000 truncates to zero before multiplying by delay_us, causing timer_counts to be zero or incorrect.
📝 Syntax
advanced2: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;
}Attempts:
2 left
💡 Hint
Check if the prescaler bits set in TCCR0B match the prescaler value 64.
✗ Incorrect
The bits (1 << CS01) | (1 << CS00) set prescaler to 64 is correct for AVR, but CS01 and CS00 are not defined constants in this snippet without proper includes, causing a compile error.
🚀 Application
expert3: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?
Attempts:
2 left
💡 Hint
Calculate total counts needed for 1ms, then subtract from max count to find reload value.
✗ Incorrect
At 1 MHz, 1ms = 1000 counts. Reload value = 4096 - 1001 = 3095 (accounting for the full cycle from reload to overflow).