Bird
Raised Fist0
Embedded Cprogramming~10 mins

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

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

Practice

(1/5)
1. What is the primary role of a direction register in embedded C programming?
easy
A. It resets the microcontroller.
B. It stores the data value to be sent through the pin.
C. It sets whether a pin is an input or an output.
D. It controls the clock speed of the microcontroller.

Solution

  1. Step 1: Understand the function of direction register

    The direction register configures each pin as either input or output.
  2. Step 2: Differentiate from data register

    The data register holds the actual value sent or received, not the pin mode.
  3. Final Answer:

    It sets whether a pin is an input or an output. -> Option C
  4. Quick Check:

    Direction register = Pin mode control [OK]
Hint: Direction register controls pin mode, not data value [OK]
Common Mistakes:
  • Confusing direction register with data register
  • Thinking direction register stores data values
  • Mixing up pin configuration with clock control
2. Which of the following is the correct way to set pin 3 as output using a direction register named DIR_REG in embedded C?
easy
A. DIR_REG = 1 << 3;
B. DIR_REG |= (1 << 3);
C. DIR_REG &= ~(1 << 3);
D. DIR_REG ^= (1 << 3);

Solution

  1. Step 1: Understand setting a bit without affecting others

    Using |= with (1 << 3) sets bit 3 to 1, keeping other bits unchanged.
  2. Step 2: Analyze other options

    DIR_REG = 1 << 3; overwrites all bits, DIR_REG &= ~(1 << 3); clears bit 3, DIR_REG ^= (1 << 3); toggles bit 3.
  3. Final Answer:

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

    Use |= to set bits safely [OK]
Hint: Use |= to set a bit without changing others [OK]
Common Mistakes:
  • Using = instead of |= which overwrites all bits
  • Using &=~ which clears the bit instead of setting
  • Using ^= which toggles the bit unpredictably
3. Given the following code snippet, what is the final value of DATA_REG if initially it was 0x00?
DIR_REG |= (1 << 2);  // Set pin 2 as output
DATA_REG |= (1 << 2); // Set pin 2 high
DATA_REG &= ~(1 << 1); // Clear pin 1
DATA_REG ^= (1 << 2);  // Toggle pin 2
medium
A. 0x04
B. 0x06
C. 0x02
D. 0x00

Solution

  1. Step 1: Trace DATA_REG changes step-by-step

    Initially DATA_REG = 0x00.
    After DATA_REG |= (1 << 2); DATA_REG = 0x04.
    After DATA_REG &= ~(1 << 1); bit 1 cleared, no change, DATA_REG = 0x04.
    After DATA_REG ^= (1 << 2); toggles bit 2, DATA_REG = 0x00.
  2. Step 2: Confirm final value

    Final DATA_REG value is 0x00 after toggling bit 2 off.
  3. Final Answer:

    0x00 -> Option D
  4. Quick Check:

    Toggle bit 2 twice returns to 0 [OK]
Hint: Toggle a bit twice returns it to original state [OK]
Common Mistakes:
  • Ignoring toggle effect and assuming bit stays set
  • Confusing bit positions (bit 1 vs bit 2)
  • Not tracking bit clearing correctly
4. Identify the error in the following code that tries to set pin 0 as output and set it high:
DIR_REG = DIR_REG & (1 << 0);
DATA_REG = DATA_REG | (1 << 0);
medium
A. Using & instead of | to set direction bit clears other bits.
B. DATA_REG should use &= instead of | to set pin high.
C. DIR_REG should be assigned 0, not bitwise operation.
D. No error, code works correctly.

Solution

  1. Step 1: Analyze DIR_REG operation

    DIR_REG & (1 << 0) clears all bits except bit 0, overwriting other pin settings.
  2. Step 2: Correct way to set bit without clearing others

    Use DIR_REG |= (1 << 0) to set bit 0 without affecting others.
  3. Final Answer:

    Using & instead of | to set direction bit clears other bits. -> Option A
  4. Quick Check:

    Use |= to set bits, not & [OK]
Hint: Use |= to set bits, & clears bits [OK]
Common Mistakes:
  • Using & instead of |= to set bits
  • Thinking |= clears bits
  • Assuming DATA_REG needs &= to set bits
5. You want to configure pins 0, 2, and 4 as outputs and set pins 0 and 4 high without changing other pins. Which sequence of operations correctly achieves this?
// Initial values: DIR_REG = 0x08, DATA_REG = 0x02
hard
A. DIR_REG |= (1 << 0) | (1 << 2) | (1 << 4); DATA_REG |= (1 << 0) | (1 << 4);
B. DIR_REG = (1 << 0) | (1 << 2) | (1 << 4); DATA_REG = (1 << 0) | (1 << 4);
C. DIR_REG &= ~((1 << 1) | (1 << 3)); DATA_REG |= (1 << 0) | (1 << 4);
D. DIR_REG |= (1 << 1) | (1 << 3); DATA_REG |= (1 << 2);

Solution

  1. Step 1: Set pins 0, 2, 4 as outputs without clearing others

    Use DIR_REG |= (1 << 0) | (1 << 2) | (1 << 4); to set these bits safely.
  2. Step 2: Set pins 0 and 4 high without affecting others

    Use DATA_REG |= (1 << 0) | (1 << 4); to set those pins high.
  3. Step 3: Analyze other options

    DIR_REG = (1 << 0) | (1 << 2) | (1 << 4); DATA_REG = (1 << 0) | (1 << 4); overwrites all bits, clearing others.
    DIR_REG &= ~((1 << 1) | (1 << 3)); DATA_REG |= (1 << 0) | (1 << 4); clears pins 1 and 3 but does not set required pins.
    DIR_REG |= (1 << 1) | (1 << 3); DATA_REG |= (1 << 2); sets wrong pins.
  4. Final Answer:

    DIR_REG |= (1 << 0) | (1 << 2) | (1 << 4); DATA_REG |= (1 << 0) | (1 << 4); -> Option A
  5. Quick Check:

    Use |= to set multiple bits safely [OK]
Hint: Use |= with combined bits to set multiple pins safely [OK]
Common Mistakes:
  • Using = which overwrites other pins
  • Setting wrong pins by mistake
  • Clearing pins unintentionally
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