Bird
Raised Fist0
Embedded Cprogramming~20 mins

Configuring watchdog timeout in Embedded C - Practice Exercises

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Watchdog Timeout Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Watchdog Timer Timeout Value Calculation
Given a watchdog timer with a clock frequency of 1 MHz and a prescaler of 64, what is the timeout period in milliseconds if the watchdog counter is set to 250?
Embedded C
/* Timeout (ms) = (Counter * Prescaler) / (Clock Frequency in kHz) */
// Clock Frequency = 1 MHz = 1000 kHz
// Prescaler = 64
// Counter = 250

unsigned int timeout_ms = (250 * 64) / 1000;
A64 ms
B250 ms
C16 ms
D4 ms
Attempts:
2 left
💡 Hint
Calculate the timeout by multiplying the counter by the prescaler, then divide by the clock frequency in kHz.
❓ Predict Output
intermediate
2:00remaining
Watchdog Configuration Register Value
If a watchdog timer register uses bits 0-3 to set the prescaler value as a power of two (e.g., 0 means divide by 1, 1 means divide by 2, 2 means divide by 4, etc.), what value should be written to set a prescaler of 128?
Embedded C
/* Prescaler = 2^n, find n for prescaler 128 */
// 2^7 = 128

unsigned char prescaler_setting = 7;
A6
B7
C8
D128
Attempts:
2 left
💡 Hint
Find the power n such that 2^n equals the prescaler value.
🔧 Debug
advanced
2:00remaining
Identify the Bug in Watchdog Timeout Setup
What error does the following code produce when configuring the watchdog timeout? unsigned int timeout = 5000; void setup_watchdog() { WDT_CTRL = timeout << 2; } Assume WDT_CTRL is an 8-bit register and timeout is in milliseconds.
Embedded C
unsigned int timeout = 5000;
void setup_watchdog() {
    WDT_CTRL = timeout << 2;
}
ANo error, code runs correctly
BSyntaxError due to missing semicolon
CTypeError because timeout is unsigned int
DValue overflow causing incorrect timeout
Attempts:
2 left
💡 Hint
Consider the size of WDT_CTRL and the value assigned to it.
📝 Syntax
advanced
2:00remaining
Correct Syntax for Watchdog Timeout Macro
Which option correctly defines a macro to calculate watchdog timeout in milliseconds given clock frequency (in Hz), prescaler, and counter value?
Embedded C
#define WDT_TIMEOUT_MS(clock, prescaler, counter) ???
A#define WDT_TIMEOUT_MS(clock, prescaler, counter) ((counter) * (prescaler) * 1000 / (clock))
B#define WDT_TIMEOUT_MS(clock, prescaler, counter) (counter * prescaler / clock * 1000)
C#define WDT_TIMEOUT_MS(clock, prescaler, counter) ((counter * prescaler) / (clock / 1000))
D#define WDT_TIMEOUT_MS(clock, prescaler, counter) ((counter + prescaler + 1000) / clock)
Attempts:
2 left
💡 Hint
Use parentheses to ensure correct order of operations and multiply by 1000 to convert seconds to milliseconds.
🚀 Application
expert
3:00remaining
Calculate Maximum Watchdog Timeout
An embedded system uses an 8-bit watchdog timer counter and a prescaler selectable as powers of two from 1 to 256. The clock frequency is 2 MHz. What is the maximum watchdog timeout in milliseconds achievable?
Embedded C
/* Max counter = 255 (8-bit)
   Max prescaler = 256
   Clock frequency = 2 MHz = 2,000,000 Hz
   Timeout (s) = (counter * prescaler) / clock
   Timeout (ms) = Timeout (s) * 1000
*/
A32.64 ms
B326.4 ms
C652.8 ms
D65.28 ms
Attempts:
2 left
💡 Hint
Calculate timeout in seconds first, then convert to milliseconds.

Practice

(1/5)
1.

What is the main purpose of configuring a watchdog timer's timeout in embedded C?

easy
A. To enable debugging messages
B. To increase the CPU clock speed
C. To set how long the system waits before resetting if it hangs
D. To allocate memory for variables

Solution

  1. Step 1: Understand watchdog timer role

    The watchdog timer resets the system if it stops responding.
  2. Step 2: Identify timeout configuration purpose

    Timeout sets the wait time before reset triggers.
  3. Final Answer:

    To set how long the system waits before resetting if it hangs -> Option C
  4. Quick Check:

    Watchdog timeout = reset delay [OK]
Hint: Timeout controls reset delay after hang [OK]
Common Mistakes:
  • Confusing timeout with CPU speed
  • Thinking timeout allocates memory
  • Assuming timeout enables debugging
2.

Which of the following is the correct syntax to set a watchdog timeout to 5000 milliseconds in embedded C?

void setup_watchdog() {
    // Set timeout here
}
easy
A. WDT_SetTimeout(5000);
B. WDT_SetTimeoutMs(5000);
C. watchdog.timeout = 5000;
D. set_watchdog(5000);

Solution

  1. Step 1: Identify common watchdog API

    Many embedded C libraries use WDT_SetTimeoutMs() to set timeout in ms.
  2. Step 2: Check syntax correctness

    WDT_SetTimeoutMs(5000); correctly sets 5000 ms timeout.
  3. Final Answer:

    WDT_SetTimeoutMs(5000); -> Option B
  4. Quick Check:

    Correct function name with ms unit [OK]
Hint: Look for function with 'TimeoutMs' for milliseconds [OK]
Common Mistakes:
  • Using incorrect function names
  • Assigning timeout to struct without function
  • Using generic function names not in API
3.

Given the code below, what will be the watchdog timeout value after execution?

typedef struct {
    unsigned int timeout_ms;
} Watchdog;

Watchdog wdt;

void configure_watchdog(unsigned int t) {
    wdt.timeout_ms = t;
}

int main() {
    configure_watchdog(3000);
    configure_watchdog(7000);
    return wdt.timeout_ms;
}
medium
A. 7000
B. 3000
C. 0
D. Compilation error

Solution

  1. Step 1: Trace function calls

    First call sets timeout_ms to 3000, second call overwrites it to 7000.
  2. Step 2: Check return value

    Main returns wdt.timeout_ms, which is 7000 after second call.
  3. Final Answer:

    7000 -> Option A
  4. Quick Check:

    Last set timeout = 7000 [OK]
Hint: Last assignment overwrites previous timeout [OK]
Common Mistakes:
  • Assuming first call value remains
  • Confusing return value with function return
  • Thinking timeout resets automatically
4.

Identify the error in the following watchdog timeout configuration code:

void setup_watchdog() {
    int timeout = 2000;
    WDT_SetTimeout(timeout);
    timeout = 5000;
}
medium
A. Timeout value is changed after setting watchdog, ineffective
B. WDT_SetTimeout requires unsigned int, not int
C. Missing return statement in function
D. Timeout variable should be global

Solution

  1. Step 1: Analyze timeout variable usage

    Timeout is set to 2000 and passed to WDT_SetTimeout, then changed to 5000 but not applied.
  2. Step 2: Understand effect of changing timeout after setting

    Changing timeout variable after setting watchdog has no effect on watchdog timeout.
  3. Final Answer:

    Timeout value is changed after setting watchdog, ineffective -> Option A
  4. Quick Check:

    Changing variable after call doesn't update watchdog [OK]
Hint: Set timeout once before calling watchdog function [OK]
Common Mistakes:
  • Thinking variable change updates watchdog automatically
  • Confusing variable type with function parameter
  • Expecting return in void function
5.

You want to configure a watchdog timer that resets the system if it hangs for more than 10 seconds, but also want to avoid false resets during long processing tasks. Which approach is best?

hard
A. Set timeout to 20 seconds and never reset watchdog
B. Set timeout to 1 second to catch hangs quickly
C. Disable watchdog during long tasks
D. Set timeout to 10 seconds and reset watchdog regularly during long tasks

Solution

  1. Step 1: Understand watchdog timeout role

    Timeout defines max hang time before reset; too short causes false resets.
  2. Step 2: Balance timeout and reset calls

    Setting 10 seconds timeout and resetting watchdog during long tasks avoids false resets.
  3. Final Answer:

    Set timeout to 10 seconds and reset watchdog regularly during long tasks -> Option D
  4. Quick Check:

    Timeout + regular reset prevents false resets [OK]
Hint: Use longer timeout + reset watchdog in long tasks [OK]
Common Mistakes:
  • Setting too short timeout causing resets
  • Disabling watchdog risking no recovery
  • Never resetting watchdog causing reset
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