0
0
Embedded Cprogramming~20 mins

Start and stop conditions in Embedded C - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
I2C Start and Stop Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this I2C start condition code snippet?
Consider the following embedded C code that attempts to generate an I2C start condition. What will be the value of the variable status after execution?
Embedded C
volatile int SDA = 1; // Data line
volatile int SCL = 1; // Clock line
int status = 0;

// Generate start condition
SDA = 1;
SCL = 1;
if (SDA == 1 && SCL == 1) {
    SDA = 0; // Pull SDA low while SCL high
    if (SDA == 0 && SCL == 1) {
        status = 1; // Start condition successful
    } else {
        status = -1; // Failed
    }
} else {
    status = -2; // Bus not idle
}
Astatus = 1
Bstatus = -1
Cstatus = -2
Dstatus = 0
Attempts:
2 left
💡 Hint
Remember, the I2C start condition is when SDA goes low while SCL is high.
Predict Output
intermediate
2:00remaining
What does this code print when generating an I2C stop condition?
This embedded C code tries to generate an I2C stop condition. What will be printed?
Embedded C
#include <stdio.h>

volatile int SDA = 0; // Data line
volatile int SCL = 1; // Clock line

int main() {
    // Generate stop condition
    SDA = 0;
    SCL = 1;
    SDA = 1; // Release SDA while SCL high

    if (SDA == 1 && SCL == 1) {
        printf("STOP_OK\n");
    } else {
        printf("STOP_FAIL\n");
    }
    return 0;
}
ACompilation error
BSTOP_FAIL
CNo output
DSTOP_OK
Attempts:
2 left
💡 Hint
The stop condition is when SDA goes from low to high while SCL is high.
🔧 Debug
advanced
2:30remaining
Why does this I2C start condition code fail to detect the start?
This code is intended to detect an I2C start condition but incorrectly sets start_detected to 1. What is the bug?
Embedded C
volatile int SDA = 1;
volatile int SCL = 1;
int start_detected = 0;

void detect_start() {
    if (SDA == 1 && SCL == 1) {
        SDA = 0; // Pull SDA low
    }
    if (SDA == 0 && SCL == 1) {
        start_detected = 1;
    } else {
        start_detected = 0;
    }
}

int main() {
    detect_start();
    return start_detected;
}
Astart_detected should be initialized to 1, not 0.
BThe code changes SDA before checking the condition, so the first if is always true and start_detected is never set correctly.
CSCL should be pulled low before SDA to detect start condition.
DThe function detect_start() never returns a value, causing undefined behavior.
Attempts:
2 left
💡 Hint
Think about the order of operations and when the condition is checked.
📝 Syntax
advanced
2:00remaining
Which option causes a syntax error in this I2C stop condition code?
Given the following code snippet, which option will cause a syntax error when compiling?
Embedded C
volatile int SDA = 0;
volatile int SCL = 1;

void generate_stop() {
    SDA = 0;
    SCL = 1;
    SDA = 1; // Release SDA while SCL high
}
Avoid generate_stop() { SDA = 0; SCL = 1 SDA = 1; }
Bvoid generate_stop() { SDA = 0; SCL = 1; SDA = 1; }
Cvoid generate_stop() { SDA = 0; SCL = 1; SDA = 1; return; }
D} ;1 = ADS ;1 = LCS ;0 = ADS { )(pots_etareneg diov
Attempts:
2 left
💡 Hint
Look carefully for missing semicolons.
🚀 Application
expert
3:00remaining
How many times does the start condition get detected in this signal sequence?
Given the following signal changes over time, how many start conditions are detected? A start condition is when SDA goes from high to low while SCL is high. Time steps (SDA, SCL): 1: (1,1) 2: (0,1) 3: (0,0) 4: (1,0) 5: (1,1) 6: (0,1) 7: (0,1) 8: (1,1) Assume the detection code checks transitions at each time step.
A1
B3
C2
D0
Attempts:
2 left
💡 Hint
Look for moments when SDA changes from 1 to 0 while SCL is 1.