Bird
Raised Fist0
Embedded Cprogramming~20 mins

GPIO register configuration in Embedded C - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
GPIO Register 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 configuration code?

Consider the following embedded C code snippet configuring a GPIO port. What will be the value of GPIOA->MODER after execution?

Embedded C
typedef struct {
    volatile unsigned int MODER;
} GPIO_TypeDef;

GPIO_TypeDef GPIOA_instance = {0};
GPIO_TypeDef *GPIOA = &GPIOA_instance;

int main() {
    GPIOA->MODER = 0;
    GPIOA->MODER |= (1 << (2 * 3)); // Set pin 3 to output mode (01)
    GPIOA->MODER |= (2 << (2 * 4)); // Set pin 4 to alternate function mode (10)
    return GPIOA->MODER;
}
A0x00000240
B0x00000140
C0x00000400
D0x00000340
Attempts:
2 left
💡 Hint

Remember each pin uses 2 bits in MODER. Pin 3 bits are bits 6-7, pin 4 bits are bits 8-9.

🧠 Conceptual
intermediate
1:00remaining
How many bits control the mode of a single GPIO pin?

In a typical microcontroller GPIO port configuration register, how many bits are used to set the mode (input, output, alternate function, analog) of one pin?

A1 bit
B4 bits
C8 bits
D2 bits
Attempts:
2 left
💡 Hint

Think about how many modes are possible and how many bits are needed to represent them.

🔧 Debug
advanced
1:30remaining
Why does this GPIO output configuration fail to set pin 3 as output?

Examine the code below. It attempts to set GPIO pin 3 as output mode but does not work as expected. What is the cause?

Embedded C
GPIOA->MODER &= ~(0x3 << 3);
GPIOA->MODER |= (0x1 << 3);
ABit shifts are incorrect; should shift by 6 and 6 respectively
BMasking uses wrong bits; should use 0x1 instead of 0x3
CThe code sets pin 1 instead of pin 3
DNo error; code works correctly
Attempts:
2 left
💡 Hint

Remember each pin uses 2 bits in MODER. Check the shift amount carefully.

📝 Syntax
advanced
1:30remaining
What error does this GPIO register configuration code produce?

Consider this code snippet. What error will it produce?

Embedded C
GPIOA->MODER = 0;
GPIOA->MODER |= 1 << 2 * 5;
ANo error; sets pin 5 mode bit 2 correctly
BLogical error; shifts 1 by 2 then multiplies by 5, wrong bit set
CSyntaxError due to missing parentheses around shift expression
DRuntime error due to invalid pointer
Attempts:
2 left
💡 Hint

Operator precedence matters in bit shifts and multiplication.

🚀 Application
expert
3:00remaining
Calculate the final MODER register value after multiple pin configurations

Given the following code configuring GPIO pins, what is the final hexadecimal value of GPIOB->MODER?

Embedded C
typedef struct {
    volatile unsigned int MODER;
} GPIO_TypeDef;

GPIO_TypeDef GPIOB_instance = {0};
GPIO_TypeDef *GPIOB = &GPIOB_instance;

int main() {
    GPIOB->MODER = 0;
    // Set pin 0 to analog mode (11)
    GPIOB->MODER |= (3 << (2 * 0));
    // Set pin 1 to output mode (01)
    GPIOB->MODER |= (1 << (2 * 1));
    // Set pin 2 to alternate function mode (10)
    GPIOB->MODER |= (2 << (2 * 2));
    // Set pin 3 to input mode (00)
    GPIOB->MODER &= ~(3 << (2 * 3));
    return GPIOB->MODER;
}
A0x00000023
B0x0000002B
C0x00000027
D0x0000003B
Attempts:
2 left
💡 Hint

Calculate each pin's bits carefully and apply the clearing operations in order.

Practice

(1/5)
1. What is the primary purpose of configuring GPIO registers in embedded C?
easy
A. To handle interrupts from external devices
B. To increase the clock speed of the microcontroller
C. To allocate memory for variables
D. To set pins as input or output for hardware interaction

Solution

  1. Step 1: Understand GPIO register role

    GPIO registers control the mode and state of microcontroller pins.
  2. Step 2: Identify the main use

    Setting pins as input or output allows the microcontroller to read sensors or control devices.
  3. Final Answer:

    To set pins as input or output for hardware interaction -> Option D
  4. Quick Check:

    GPIO config = pin mode setup [OK]
Hint: GPIO controls pin direction and state [OK]
Common Mistakes:
  • Confusing GPIO with clock or memory settings
  • Thinking GPIO handles interrupts directly
  • Assuming GPIO sets processor speed
2. Which of the following is the correct way to set bit 3 of a GPIO register named GPIO_DIR to configure pin 3 as output?
easy
A. GPIO_DIR = (1 << 3);
B. GPIO_DIR = GPIO_DIR & (1 << 3);
C. GPIO_DIR = GPIO_DIR | (1 << 3);
D. GPIO_DIR = ~GPIO_DIR | (1 << 3);

Solution

  1. Step 1: Understand bitwise OR for setting bits

    Using OR with (1 << 3) sets bit 3 without changing other bits.
  2. Step 2: Analyze each option

    GPIO_DIR = GPIO_DIR | (1 << 3); correctly sets bit 3. GPIO_DIR = GPIO_DIR & (1 << 3); clears other bits. GPIO_DIR = (1 << 3); overwrites all bits. GPIO_DIR = ~GPIO_DIR | (1 << 3); inverts bits incorrectly.
  3. Final Answer:

    GPIO_DIR = GPIO_DIR | (1 << 3); -> Option C
  4. Quick Check:

    Set bit with OR = correct [OK]
Hint: Use OR (|) to set bits without clearing others [OK]
Common Mistakes:
  • Using AND (&) instead of OR (|) to set bits
  • Overwriting register instead of modifying bits
  • Using NOT (~) incorrectly
3. Given the code snippet:
uint8_t GPIO_DATA = 0x00;
GPIO_DATA |= (1 << 2);
GPIO_DATA &= ~(1 << 0);
GPIO_DATA |= (1 << 0);

What is the final value of GPIO_DATA in hexadecimal?
medium
A. 0x04
B. 0x05
C. 0x01
D. 0x00

Solution

  1. Step 1: Apply each operation step-by-step

    Start: GPIO_DATA = 0x00 (binary 0000 0000).
    After GPIO_DATA |= (1 << 2): sets bit 2 -> 0000 0100 (0x04).
    After GPIO_DATA &= ~(1 << 0): clears bit 0 -> still 0000 0100 (0x04).
    After GPIO_DATA |= (1 << 0): sets bit 0 -> 0000 0101 (0x05).
  2. Step 2: Convert final binary to hex

    Binary 0000 0101 equals hex 0x05.
  3. Final Answer:

    0x05 -> Option B
  4. Quick Check:

    Bit 2 and 0 set = 0x05 [OK]
Hint: Track bits set/cleared stepwise [OK]
Common Mistakes:
  • Ignoring bit clear operation
  • Confusing bit positions
  • Not converting binary to hex correctly
4. Identify the error in this GPIO pin configuration code snippet:
GPIO_DIR &= ~(1 << 5); // Set pin 5 as input
GPIO_DIR |= (1 << 5); // Set pin 5 as output
medium
A. Pin 5 is configured as both input and output, causing conflict
B. Bitwise AND operator is used incorrectly
C. Shift operator should be << 4 instead of << 5
D. GPIO_DIR register cannot be modified directly

Solution

  1. Step 1: Analyze the two lines

    First line clears bit 5 to set pin 5 as input.
    Second line sets bit 5 to set pin 5 as output.
  2. Step 2: Understand the conflict

    Both lines contradict each other immediately, so pin 5 cannot be both input and output at the same time.
  3. Final Answer:

    Pin 5 is configured as both input and output, causing conflict -> Option A
  4. Quick Check:

    Conflicting pin modes = error [OK]
Hint: Avoid setting same pin input and output simultaneously [OK]
Common Mistakes:
  • Overwriting pin mode without delay or condition
  • Misunderstanding bitwise operators
  • Assuming GPIO_DIR is read-only
5. You want to configure pins 0, 2, and 4 as outputs and pins 1, 3, and 5 as inputs using a single 8-bit GPIO_DIR register. Which code snippet correctly sets this configuration without affecting other bits?
hard
A. GPIO_DIR = (GPIO_DIR & 0xAA) | 0x15;
B. GPIO_DIR = (GPIO_DIR & 0x55) | 0x2A;
C. GPIO_DIR = (GPIO_DIR & 0x3F) | 0xC0;
D. GPIO_DIR = (GPIO_DIR & 0xF0) | 0x0F;

Solution

  1. Step 1: Identify bits for outputs and inputs

    Outputs: pins 0,2,4 -> bits 0,2,4 set to 1 -> binary 0001 0101 (0x15).
    Inputs: pins 1,3,5 -> bits 1,3,5 preserved.
  2. Step 2: Use mask to clear bits 0,2,4 before setting

    Mask 0xAA (1010 1010) clears bits 0,2,4 when ANDed.
    Then OR with 0x15 sets output bits.
    So: GPIO_DIR = (GPIO_DIR & 0xAA) | 0x15;
  3. Final Answer:

    GPIO_DIR = (GPIO_DIR & 0xAA) | 0x15; -> Option A
  4. Quick Check:

    Mask inputs, set outputs correctly [OK]
Hint: Clear output bits with mask, then OR to set [OK]
Common Mistakes:
  • Using wrong mask to clear bits
  • Overwriting entire register losing other bits
  • Confusing input bits with output bits
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