📖 Scenario: In embedded systems, a watchdog timer helps keep the system safe by resetting it if the program stops working properly. To prevent this reset, the program must regularly 'feed' or 'kick' the watchdog timer.Imagine you are programming a simple embedded device that needs to feed the watchdog every few seconds to keep running smoothly.
🎯 Goal: You will write a small embedded C program that sets up a watchdog timer and regularly feeds it inside a loop to prevent the system reset.
📋 What You'll Learn
Create a variable called watchdog_counter initialized to 0
Create a constant WATCHDOG_THRESHOLD set to 5
Use a while loop to simulate the program running
Inside the loop, increment watchdog_counter and feed the watchdog when it reaches the threshold
Print "Watchdog fed" each time the watchdog is fed
💡 Why This Matters
🌍 Real World
Watchdog timers are used in embedded devices like home appliances, cars, and medical devices to keep them running safely by resetting if the software hangs.
💼 Career
Understanding how to feed the watchdog is essential for embedded software engineers to ensure system reliability and prevent crashes.
Progress0 / 4 steps
1
Create the watchdog counter variable
Create an integer variable called watchdog_counter and set it to 0.
Embedded C
Hint
Use int watchdog_counter = 0; to create the variable.
2
Define the watchdog threshold
Create a constant integer called WATCHDOG_THRESHOLD and set it to 5.
Embedded C
Hint
Use const int WATCHDOG_THRESHOLD = 5; to define the threshold.
3
Implement the main loop and feeding logic
Write a while(1) loop. Inside it, increment watchdog_counter by 1. When watchdog_counter equals WATCHDOG_THRESHOLD, reset watchdog_counter to 0 and print "Watchdog fed".
Embedded C
Hint
Use while(1) for an infinite loop. Increment the counter and check if it reached the threshold to feed the watchdog.
4
Run the program and observe output
Run the program and observe that it prints "Watchdog fed" repeatedly every 5 increments.
Embedded C
Hint
The program prints "Watchdog fed" every 5 increments. You may need to stop the infinite loop manually after seeing several lines.
Practice
(1/5)
1. What is the main purpose of feeding (kicking) the watchdog in embedded C systems?
easy
A. To start a new program execution
B. To increase the system clock speed
C. To save power by turning off peripherals
D. To reset the watchdog timer and prevent system reset
Solution
Step 1: Understand watchdog function
The watchdog timer resets the system if it is not fed in time, preventing hangs.
Step 2: Purpose of feeding watchdog
Feeding the watchdog resets its timer, showing the system is running fine.
Final Answer:
To reset the watchdog timer and prevent system reset -> Option D
Quick Check:
Feeding watchdog = reset timer [OK]
Hint: Feeding watchdog means resetting its timer regularly [OK]
Common Mistakes:
Thinking feeding increases clock speed
Confusing feeding with power saving
Assuming feeding starts program execution
2. Which of the following is the correct syntax to feed (kick) the watchdog in embedded C if the function is named FeedWatchdog()?
easy
A. FeedWatchdog();
B. feed_watchdog();
C. FeedWatchdog;
D. FeedWatchdog[];
Solution
Step 1: Identify correct function call syntax
In C, calling a function requires parentheses and a semicolon.
Step 2: Check each option
FeedWatchdog(); uses correct capitalization and parentheses; others are incorrect syntax.
Final Answer:
FeedWatchdog(); -> Option A
Quick Check:
Function call syntax = functionName(); [OK]
Hint: Function calls need parentheses and semicolon [OK]
Common Mistakes:
Omitting parentheses when calling function
Using wrong capitalization
Using brackets instead of parentheses
3. What will be the output of the following code snippet if the watchdog is not fed within 5 seconds?
void main() {
init_watchdog(5); // 5 seconds timeout
while(1) {
// no FeedWatchdog() call here
}
}
medium
A. System runs normally without reset
B. System resets after 5 seconds
C. Compilation error due to missing FeedWatchdog()
D. Watchdog timer disables automatically
Solution
Step 1: Understand watchdog timeout behavior
If the watchdog is not fed within the timeout, it triggers a system reset.
Step 2: Analyze code behavior
No FeedWatchdog() call means timer is not reset, so system resets after 5 seconds.
Final Answer:
System resets after 5 seconds -> Option B
Quick Check:
Not feeding watchdog = system reset [OK]
Hint: No feeding within timeout causes reset [OK]
Common Mistakes:
Assuming system runs normally without feeding
Expecting compilation error for missing feed
Thinking watchdog disables automatically
4. Identify the error in this code snippet that feeds the watchdog incorrectly:
FeedWatchdog is a function and must be called with parentheses.
Step 2: Analyze code snippet
FeedWatchdog; is missing parentheses, so it does not call the function.
Final Answer:
Missing parentheses in FeedWatchdog call -> Option A
Quick Check:
Function call needs () [OK]
Hint: Always use () to call functions [OK]
Common Mistakes:
Forgetting parentheses on function calls
Assuming delay is incorrect without context
Thinking timeout value causes error
5. You have a system with a watchdog timeout of 3 seconds. Your main loop takes 5 seconds to complete one iteration. How should you modify your code to prevent the watchdog from resetting the system?
hard
A. Remove the FeedWatchdog() calls entirely
B. Increase the watchdog timeout to 10 seconds
C. Call FeedWatchdog() multiple times inside the loop before 3 seconds pass
D. Call FeedWatchdog() only once after the loop finishes
Solution
Step 1: Understand watchdog timeout vs loop time
The loop takes longer than the watchdog timeout, so feeding once per loop is too late.
Step 2: Feeding watchdog multiple times inside loop
Calling FeedWatchdog() multiple times before 3 seconds resets timer and prevents reset.
Final Answer:
Call FeedWatchdog() multiple times inside the loop before 3 seconds pass -> Option C
Quick Check:
Feed watchdog before timeout expires [OK]
Hint: Feed watchdog often enough within timeout [OK]
Common Mistakes:
Assuming increasing timeout is always possible
Feeding watchdog only once after long loop
Removing feeding calls thinking it helps
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.