0
0
Embedded Cprogramming~10 mins

Direction register vs data register in Embedded C - Visual Side-by-Side Comparison

Choose your learning style9 modes available
Concept Flow - Direction register vs data register
Start
Set Direction Register
Direction Register configures pins
Write to Data Register
Data Register sets pin output
Pins behave as input or output
Read from Data Register if input
End
First, the direction register sets each pin as input or output. Then, the data register controls the pin's output value or reads input.
Execution Sample
Embedded C
DDRB = 0b00001000;  // Set pin 3 as output
PORTB = 0b00001000; // Set pin 3 HIGH
uint8_t val = PINB & 0b00001000; // Read pin 3 input
This code sets pin 3 of port B as output, sets it HIGH, then reads the pin state.
Execution Table
StepRegisterValue Written/ReadEffectPin 3 State
1DDRB0b00001000Pin 3 set as output, others inputConfigured as output
2PORTB0b00001000Pin 3 output set HIGHPin 3 HIGH
3PINBRead 0b00001000Read pin 3 input statePin 3 reads HIGH
4End-No more instructions-
💡 Execution stops after reading pin 3 input state.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
DDRB0b000000000b000010000b000010000b000010000b00001000
PORTB0b000000000b000000000b000010000b000010000b00001000
PINBUnknownUnknownUnknown0b000010000b00001000
valUndefinedUndefinedUndefined0b000010000b00001000
Key Moments - 3 Insights
Why do we set the direction register before writing to the data register?
Because the direction register decides if a pin is input or output. Writing to data register only affects pins set as output (see Step 1 and 2 in execution_table).
Can we read the pin state from the data register?
No, we read the pin state from the PIN register, not the data register. Step 3 shows reading PINB to get pin input.
What happens if we write to the data register but the pin is set as input?
Writing to data register has no effect on pin output if the pin is input. The pin state depends on external signals (not shown in this trace).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of DDRB after Step 1?
A0b00000000
B0b00001000
C0b00001111
D0b11110000
💡 Hint
Check the 'Value Written/Read' column at Step 1 in execution_table.
At which step does the pin 3 output get set HIGH?
AStep 2
BStep 1
CStep 3
DStep 4
💡 Hint
Look at the 'Effect' column describing pin output changes.
If DDRB was set to 0b00000000 (all inputs), what would happen when writing 0b00001000 to PORTB?
APin 3 output goes HIGH
BPin 3 output goes LOW
CPin 3 remains input, output unchanged
DMicrocontroller resets
💡 Hint
Refer to key_moments about direction register effect on data register writes.
Concept Snapshot
Direction register (DDR) sets pin direction: 1=output, 0=input.
Data register (PORT) sets output value for pins set as output.
PIN register reads actual pin state.
Write DDR before PORT to control pin behavior.
Reading PIN gives input state regardless of DDR.
Writing PORT affects only output pins.
Full Transcript
This visual trace shows how the direction register and data register work together in embedded C. First, the direction register DDRB is set to 0b00001000, configuring pin 3 as output. Then, writing 0b00001000 to PORTB sets pin 3 output HIGH. Reading PINB returns the current state of pin 3, which is HIGH. The direction register controls whether pins act as input or output, and the data register controls output values only on pins set as output. Reading pin states uses the PIN register. This step-by-step helps beginners see how these registers interact to control microcontroller pins.