Bird
Raised Fist0
Embedded Cprogramming~20 mins

GPIO port-wide operations in Embedded C - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
GPIO Port 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 GPIO port write operation?
Given a 8-bit GPIO port initially set to 0x00, what will be the port value after executing the following code?
Embedded C
uint8_t GPIO_PORT = 0x00;

// Set pins 0, 2, and 4 high
GPIO_PORT |= (1 << 0) | (1 << 2) | (1 << 4);

printf("0x%02X", GPIO_PORT);
A0x15
B0x1A
C0x14
D0x16
Attempts:
2 left
💡 Hint
Remember that (1 << n) sets bit n to 1. Combine bits with OR.
❓ Predict Output
intermediate
2:00remaining
What is the value of GPIO_PORT after clearing bits?
Starting with GPIO_PORT = 0xFF, what is the value after clearing bits 1, 3, and 5 using this code?
Embedded C
uint8_t GPIO_PORT = 0xFF;

// Clear bits 1, 3, and 5
GPIO_PORT &= ~((1 << 1) | (1 << 3) | (1 << 5));

printf("0x%02X", GPIO_PORT);
A0xAB
B0xD5
C0xBF
D0x7F
Attempts:
2 left
💡 Hint
Use bitwise AND with the negation of the bits to clear.
🔧 Debug
advanced
3:00remaining
Why does this GPIO port toggle code not work as expected?
This code is intended to toggle bits 0 and 7 of GPIO_PORT. What is the problem?
Embedded C
uint8_t GPIO_PORT = 0xAA; // 0b10101010

// Toggle bits 0 and 7
GPIO_PORT ^= 1 << 0 & 1 << 7;

printf("0x%02X", GPIO_PORT);
AThe code toggles all bits instead of just bits 0 and 7.
BThe bitwise AND has higher precedence than XOR, so only bit 0 is toggled incorrectly.
CThe code causes a syntax error due to missing parentheses.
DThe code toggles bits 0 and 7 correctly, output is 0x2A.
Attempts:
2 left
💡 Hint
Check operator precedence between ^ and &.
❓ Predict Output
advanced
2:30remaining
What is the output after setting and clearing multiple GPIO port bits?
Given GPIO_PORT initially 0x55, what is the value after this sequence?
Embedded C
uint8_t GPIO_PORT = 0x55; // 0b01010101

// Set bits 1 and 3
GPIO_PORT |= (1 << 1) | (1 << 3);

// Clear bits 0 and 4
GPIO_PORT &= ~((1 << 0) | (1 << 4));

printf("0x%02X", GPIO_PORT);
A0x6B
B0x4A
C0x5A
D0x7B
Attempts:
2 left
💡 Hint
Apply set bits first, then clear bits.
🧠 Conceptual
expert
3:00remaining
How many bits are set in GPIO_PORT after this operation?
If GPIO_PORT is 0xF0 and you execute GPIO_PORT ^= 0xFF;, how many bits are set (1) in GPIO_PORT afterwards?
Embedded C
uint8_t GPIO_PORT = 0xF0;
GPIO_PORT ^= 0xFF;
// Count bits set in GPIO_PORT
A6
B8
C0
D4
Attempts:
2 left
💡 Hint
XOR with 0xFF flips all bits.

Practice

(1/5)
1. What does writing a value to PORTB in embedded C typically do in GPIO port-wide operations?
easy
A. Configures the direction of pins in port B
B. Sets the output levels of all pins in port B at once
C. Reads the input levels of all pins in port B
D. Enables pull-up resistors on port B pins

Solution

  1. Step 1: Understand PORT register role

    The PORT register controls the output voltage levels on the pins of a GPIO port.
  2. Step 2: Effect of writing to PORTB

    Writing a value to PORTB sets the output state (high or low) of all pins in port B simultaneously.
  3. Final Answer:

    Sets the output levels of all pins in port B at once -> Option B
  4. Quick Check:

    PORTB write = set output levels [OK]
Hint: PORTx controls output levels of all pins together [OK]
Common Mistakes:
  • Confusing PORT with DDR (direction register)
  • Thinking PORT reads input levels
  • Assuming PORT configures pull-ups
2. Which of the following is the correct syntax to set all pins of port D as outputs in embedded C?
easy
A. PINB = 0xFF;
B. PORTD = 0xFF;
C. DDRD = 0xFF;
D. DDRB = 0x00;

Solution

  1. Step 1: Identify DDR register purpose

    DDRx registers set pin directions: 1 for output, 0 for input.
  2. Step 2: Set all pins of port D as output

    Writing 0xFF (all bits 1) to DDRD sets all pins as outputs.
  3. Final Answer:

    DDRD = 0xFF; -> Option C
  4. Quick Check:

    DDRD = 0xFF sets all outputs [OK]
Hint: Use DDRx = 0xFF to set all pins output [OK]
Common Mistakes:
  • Using PORTD instead of DDRD to set direction
  • Writing to PINB which is read-only
  • Setting DDRB instead of DDRD
3. Given the code snippet:
DDRC = 0x00;
PORTC = 0xFF;
uint8_t val = PINC;

What is the value of val assuming all pins on port C are externally connected to ground?
medium
A. 0x00
B. Undefined behavior
C. 0xFF with pull-ups disabled
D. 0xFF

Solution

  1. Step 1: Analyze DDRC and PORTC settings

    DDRC = 0x00 sets all pins as inputs. PORTC = 0xFF enables pull-up resistors on all pins.
  2. Step 2: Effect of external ground on input pins

    Even with pull-ups enabled, external ground forces pins low, so PINC reads 0x00.
  3. Final Answer:

    0x00 -> Option A
  4. Quick Check:

    Input pins grounded read 0x00 [OK]
Hint: Input pins grounded read zero despite pull-ups [OK]
Common Mistakes:
  • Assuming pull-ups force input high regardless
  • Confusing PORTC write with output level
  • Ignoring DDRC input configuration
4. Identify the error in the following code snippet for setting port A pins as outputs and turning them all high:
DDRA = 0xFF;
PINA = 0xFF;
medium
A. PORTA should be used instead of PINA to set outputs
B. DDRA should be set to 0x00
C. PINA is not a valid register name
D. No error, code is correct

Solution

  1. Step 1: Check register purpose

    PINA reads input levels or toggles outputs, but does not directly set output levels.
  2. Step 2: Correct register to set output levels

    To set output pins high, write to PORTA, not PINx.
  3. Final Answer:

    PORTA should be used instead of PINA to set outputs -> Option A
  4. Quick Check:

    Use PORTA to write outputs, not PINx [OK]
Hint: Use PORTx to write outputs, PINx is for reading inputs [OK]
Common Mistakes:
  • Using PINx registers instead of PORTx for outputs
  • Writing outputs to PIN register
  • Confusing DDR and PORT registers
5. You want to configure port C pins 0-3 as outputs and pins 4-7 as inputs with pull-ups enabled. Which code correctly achieves this?
hard
A. DDRC = 0x00; PORTC = 0xFF;
B. DDRC = 0xF0; PORTC = 0x0F;
C. DDRC = 0xFF; PORTC = 0x00;
D. DDRC = 0x0F; PORTC = 0xF0;

Solution

  1. Step 1: Set pins 0-3 as outputs

    Bits 0-3 set to 1 in DDRC means DDRC = 0x0F (00001111 binary).
  2. Step 2: Enable pull-ups on pins 4-7 inputs

    Pins 4-7 inputs require DDRC bits 4-7 = 0, and PORTC bits 4-7 = 1 to enable pull-ups, so PORTC = 0xF0.
  3. Final Answer:

    DDRC = 0x0F; PORTC = 0xF0; -> Option D
  4. Quick Check:

    Outputs low nibble, pull-ups high nibble [OK]
Hint: DDRx bits 1=output, PORTx bits 1=pull-up on inputs [OK]
Common Mistakes:
  • Mixing up which bits control direction vs pull-ups
  • Setting pull-ups on output pins
  • Using wrong hex values for bit masks
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