0
0
Embedded Cprogramming~10 mins

GPIO register configuration in Embedded C - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - GPIO register configuration
Start
Set GPIO Mode Register
Set GPIO Output Type Register
Set GPIO Speed Register
Set GPIO Pull-up/Pull-down Register
Write to GPIO Output Data Register
End
The flow shows configuring GPIO registers step-by-step to set pin mode, output type, speed, pull-up/down, and output value.
Execution Sample
Embedded C
GPIOA->MODER = 0x01;
GPIOA->OTYPER = 0x00;
GPIOA->OSPEEDR = 0x03;
GPIOA->PUPDR = 0x00;
GPIOA->ODR = 0x01;
This code configures GPIOA pin 0 as output, push-pull, high speed, no pull-up/down, and sets output high.
Execution Table
StepRegisterValue WrittenEffect on GPIO Pin 0
1MODER0x01Pin 0 set as General Purpose Output
2OTYPER0x00Pin 0 output type set to Push-Pull
3OSPEEDR0x03Pin 0 speed set to High Speed
4PUPDR0x00Pin 0 no pull-up or pull-down resistor
5ODR0x01Pin 0 output set to High (logic 1)
6--Configuration complete, GPIO pin ready
💡 All GPIO registers configured; pin 0 is output, push-pull, high speed, no pull resistors, output high.
Variable Tracker
RegisterInitialAfter Step 1After Step 2After Step 3After Step 4After Step 5
MODER0x000000000x000000010x000000010x000000010x000000010x00000001
OTYPER0x000000000x000000000x000000000x000000000x000000000x00000000
OSPEEDR0x000000000x000000000x000000000x000000030x000000030x00000003
PUPDR0x000000000x000000000x000000000x000000000x000000000x00000000
ODR0x000000000x000000000x000000000x000000000x000000000x00000001
Key Moments - 3 Insights
Why do we write 0x01 to MODER to set pin 0 as output?
MODER uses 2 bits per pin; 0x01 sets bits 0 and 1 to '01', which means pin 0 is output mode (see execution_table step 1).
What does setting OTYPER to 0x00 do for pin 0?
OTYPER bit 0 controls output type; 0 means push-pull, which is standard output (see execution_table step 2).
Why is ODR set last?
ODR controls output level; setting it last ensures pin is configured before outputting high or low (see execution_table step 5).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what value is written to OSPEEDR at step 3?
A0x01
B0x03
C0x00
D0xFF
💡 Hint
Check the 'Value Written' column for OSPEEDR at step 3 in execution_table.
At which step is the GPIO pin output set to high?
AStep 1
BStep 3
CStep 5
DStep 4
💡 Hint
Look for when ODR register is written in execution_table.
If we change OTYPER to 0x01, what changes for pin 0?
APin 0 becomes open-drain output
BPin 0 becomes input
CPin 0 speed changes
DPin 0 pull-up enabled
💡 Hint
OTYPER bit 0 controls output type; 1 means open-drain (see key_moments about OTYPER).
Concept Snapshot
GPIO Register Configuration:
- MODER sets pin mode (2 bits per pin)
- OTYPER sets output type (push-pull/open-drain)
- OSPEEDR sets pin speed
- PUPDR sets pull-up/pull-down resistors
- ODR sets output data level
Write registers in order to configure pin safely.
Full Transcript
This visual execution shows how to configure a GPIO pin by writing to its registers step-by-step. First, MODER sets the pin mode to output by writing 0x01. Then OTYPER sets output type to push-pull with 0x00. Next, OSPEEDR sets high speed with 0x03. PUPDR disables pull resistors with 0x00. Finally, ODR sets the output level high with 0x01. The variable tracker shows register values after each step. Key moments clarify why specific values are used. The quiz tests understanding of register values and effects. This helps beginners see exactly how GPIO configuration works in embedded C.