0
0
Embedded Cprogramming~20 mins

Pull-up and pull-down resistor configuration in Embedded C - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Pull-up and Pull-down Mastery
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 embedded C code configuring a pull-up resistor?

Consider the following code snippet configuring a microcontroller pin with a pull-up resistor enabled. What will be the value read from the pin if the external switch is open?

Embedded C
#define PIN_INPUT 0x01
#define PULLUP_ENABLE 0x02

unsigned char port_config = 0;
unsigned char pin_state = 0;

// Enable pull-up resistor on the pin
port_config |= PULLUP_ENABLE;

// Simulate reading the pin state
pin_state = (port_config & PULLUP_ENABLE) ? 1 : 0;

printf("Pin state: %d\n", pin_state);
APin state: 2
BPin state: 0
CPin state: 1
DCompilation error
Attempts:
2 left
💡 Hint

Think about what enabling a pull-up resistor does to the pin's voltage level when the switch is open.

Predict Output
intermediate
2:00remaining
What is the pin state when a pull-down resistor is configured and the switch is open?

Given this embedded C code snippet that configures a pull-down resistor on a pin, what will be the output when the external switch is open?

Embedded C
#define PULLDOWN_ENABLE 0x04
unsigned char port_config = 0;
unsigned char pin_state = 0;

// Enable pull-down resistor on the pin
port_config |= PULLDOWN_ENABLE;

// Simulate reading the pin state
pin_state = (port_config & PULLDOWN_ENABLE) ? 0 : 1;

printf("Pin state: %d\n", pin_state);
ARuntime error
BPin state: 0
CPin state: 4
DPin state: 1
Attempts:
2 left
💡 Hint

Recall what a pull-down resistor does to the pin voltage when the switch is open.

🧠 Conceptual
advanced
1:30remaining
Which statement correctly describes the behavior of pull-up and pull-down resistors?

Choose the correct statement about pull-up and pull-down resistors in microcontroller input pin configurations.

APull-up resistors connect the pin to ground, making the default state low when the switch is open.
BPull-down resistors disconnect the pin from any voltage, leaving it floating when the switch is open.
CPull-down resistors connect the pin to the supply voltage, making the default state high when the switch is open.
DPull-up resistors connect the pin to the supply voltage, making the default state high when the switch is open.
Attempts:
2 left
💡 Hint

Think about which resistor connects to supply voltage and which connects to ground.

🔧 Debug
advanced
2:00remaining
Identify the error in this pull-up resistor configuration code

Examine the following embedded C code snippet intended to enable a pull-up resistor on a pin. What is the error that will prevent it from working correctly?

Embedded C
unsigned char port_config = 0;
#define PULLUP_ENABLE 0x01

// Incorrectly clearing the pull-up bit instead of setting it
port_config &= ~PULLUP_ENABLE;

// Reading pin state
unsigned char pin_state = (port_config & PULLUP_ENABLE) ? 1 : 0;

printf("Pin state: %d\n", pin_state);
AThe code clears the pull-up bit instead of setting it, so the pull-up resistor is disabled.
BThe code uses the wrong bit mask for the pull-up resistor, causing a runtime error.
CThe pin_state variable is not initialized before use, causing undefined behavior.
DThe printf statement is missing a newline character, so output is not visible.
Attempts:
2 left
💡 Hint

Look carefully at the operation used to modify the port_config variable.

📝 Syntax
expert
1:30remaining
Which option will cause a syntax error in configuring pull-up resistor in embedded C?

Identify which code snippet will cause a syntax error when trying to enable a pull-up resistor on a pin.

Aport_config =| PULLUP_ENABLE;
Bport_config = port_config | PULLUP_ENABLE;
Cport_config |= PULLUP_ENABLE
Dport_config |= PULLUP_ENABLE;
Attempts:
2 left
💡 Hint

Check the operator syntax carefully.