0
0
Embedded Cprogramming~20 mins

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

Choose your learning style9 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.