0
0
Embedded Cprogramming~20 mins

Configuring pin as input or output in Embedded C - Practice Exercises

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Pin Configuration 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 pin configuration code?
Consider a microcontroller where setting a bit in the DDR (Data Direction Register) configures the pin as output (1) or input (0). What will be the value of DDRB after running this code?
Embedded C
DDRB = 0x00;
DDRB |= (1 << 3);  // Set pin 3 as output
DDRB &= ~(1 << 1); // Set pin 1 as input
A0x0A
B0x08
C0x0C
D0x02
Attempts:
2 left
💡 Hint
Remember that (1 << 3) sets bit 3 and (1 << 1) sets bit 1. |= sets bits, &=~ clears bits.
🧠 Conceptual
intermediate
1:30remaining
Which statement correctly configures pin 5 as input?
Given a DDR register controlling pin directions, which code snippet correctly sets pin 5 as input without changing other pins?
ADDR &= ~(1 << 5);
BDDR = (1 << 5);
CDDR |= (1 << 5);
DDDR ^= (1 << 5);
Attempts:
2 left
💡 Hint
Input pins have DDR bit cleared (0). Use bitwise AND with NOT to clear a bit.
🔧 Debug
advanced
2:30remaining
Why does this code fail to configure pin 2 as output?
This code is intended to set pin 2 as output by setting bit 2 in DDR. Why does it not work as expected? DDRB = 0; DDRB & (1 << 2);
ADDRB is reset to 0 after setting the bit.
BThe bit shift is incorrect; it should be (1 << 3).
CThe code uses & instead of |=, so it does not set the bit.
DPin 2 cannot be configured as output on this microcontroller.
Attempts:
2 left
💡 Hint
Check the operator used to set bits.
📝 Syntax
advanced
1:30remaining
Which option contains a syntax error in configuring pin 0 as output?
Identify the code snippet that will cause a syntax error when trying to set pin 0 as output.
ADDRB |= (1 << 0);
BDDRB = DDRB | (1 << 0);
CDDRB |= 1 << 0;
DDDRB |= (1 << 0
Attempts:
2 left
💡 Hint
Look for missing parentheses or incomplete expressions.
🚀 Application
expert
3:00remaining
How many pins are configured as output after this code runs?
Given DDRC initially 0x00, what is the number of pins configured as output after running this code? DDRC |= (1 << 0); DDRC |= (1 << 2); DDRC &= ~(1 << 0); DDRC |= (1 << 7);
A2
B3
C1
D4
Attempts:
2 left
💡 Hint
Count bits set to 1 in DDRC after all operations.