Direction registers tell the microcontroller if a pin is an input or output. Data registers hold the actual values sent to or read from those pins.
Direction register vs data register in Embedded C
Introduction
Syntax
Embedded C
/* Set pin direction */ DIR_REG = 0x01; // Set pin 0 as output, others as input /* Write data to pin */ DATA_REG = 0x01; // Set pin 0 high /* Read data from pin */ input_value = DATA_REG & 0x01; // Read pin 0 value
Direction register bits: 1 means output, 0 means input.
Data register bits: writing sets output level; reading gets input level.
Examples
Embedded C
DIR_REG = 0xFF; // All pins set as output DATA_REG = 0xAA; // Output pattern 10101010
Embedded C
DIR_REG = 0x00; // All pins set as input input_val = DATA_REG; // Read all pins
Embedded C
DIR_REG |= 0x04; // Set pin 2 as output without changing others DATA_REG &= ~0x04; // Set pin 2 low
Sample Program
This program shows how to set pin 0 as output and turn it on, then switch to input and read its value.
Embedded C
#include <stdint.h> #include <stdio.h> // Simulated registers uint8_t DIR_REG = 0x00; // Direction register uint8_t DATA_REG = 0x00; // Data register int main() { // Set pin 0 as output DIR_REG = 0x01; // Turn pin 0 ON DATA_REG = 0x01; // Print direction and data register values printf("DIR_REG = 0x%02X\n", DIR_REG); printf("DATA_REG = 0x%02X\n", DATA_REG); // Now set pin 0 as input DIR_REG = 0x00; // Simulate reading pin 0 as HIGH DATA_REG = 0x01; // Read pin 0 value uint8_t pin0_value = DATA_REG & 0x01; printf("Pin 0 input value = %d\n", pin0_value); return 0; }
Important Notes
Always set the direction register before writing to the data register to avoid unexpected behavior.
Reading the data register on an input pin gives the current voltage level on that pin.
Summary
Direction register controls if a pin is input or output.
Data register holds the value sent to or read from the pin.
Use bitwise operations to change specific pins without affecting others.
Practice
1. What is the primary role of a
direction register in embedded C programming?easy
Solution
Step 1: Understand the function of direction register
The direction register configures each pin as either input or output.Step 2: Differentiate from data register
The data register holds the actual value sent or received, not the pin mode.Final Answer:
It sets whether a pin is an input or an output. -> Option CQuick 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
Solution
Step 1: Understand setting a bit without affecting others
Using |= with (1 << 3) sets bit 3 to 1, keeping other bits unchanged.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.Final Answer:
DIR_REG |= (1 << 3); -> Option BQuick 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
Solution
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.Step 2: Confirm final value
Final DATA_REG value is 0x00 after toggling bit 2 off.Final Answer:
0x00 -> Option DQuick 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
Solution
Step 1: Analyze DIR_REG operation
DIR_REG & (1 << 0) clears all bits except bit 0, overwriting other pin settings.Step 2: Correct way to set bit without clearing others
Use DIR_REG |= (1 << 0) to set bit 0 without affecting others.Final Answer:
Using & instead of | to set direction bit clears other bits. -> Option AQuick 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
Solution
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.Step 2: Set pins 0 and 4 high without affecting others
Use DATA_REG |= (1 << 0) | (1 << 4); to set those pins high.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.Final Answer:
DIR_REG |= (1 << 0) | (1 << 2) | (1 << 4); DATA_REG |= (1 << 0) | (1 << 4); -> Option AQuick 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
10 questions ~5 mineasy hard
