A Direction Register controls whether a pin on a microcontroller is set as an input or an output. Setting a bit to 1 usually means output, and 0 means input.
Click to reveal answer
beginner
What is a Data Register in embedded C?
A Data Register holds the actual value sent to or read from a pin. For output pins, it sets the voltage level (high or low). For input pins, it reads the current voltage level.
Click to reveal answer
intermediate
How do Direction and Data Registers work together?
First, the Direction Register sets if a pin is input or output. Then, the Data Register either sends a signal out (if output) or reads a signal in (if input).
Click to reveal answer
beginner
Example: If you want to turn on an LED connected to a pin, which registers do you use and how?
Set the pin as output by writing 1 to the Direction Register bit. Then write 1 to the Data Register bit to turn the LED on (assuming active high).
Click to reveal answer
intermediate
Why can't you write to the Data Register if the pin is set as input?
Because the pin is configured to receive signals, not send them. Writing to the Data Register has no effect when the pin is input.
Click to reveal answer
What does setting a bit to 1 in the Direction Register usually mean?
APin is set as output
BPin is set as input
CPin is disabled
DPin is set to high impedance
✗ Incorrect
Setting a bit to 1 in the Direction Register usually configures the pin as an output.
Which register do you write to in order to send a high signal on an output pin?
ADirection Register
BControl Register
CStatus Register
DData Register
✗ Incorrect
The Data Register holds the value to send out on the pin, so writing 1 sets the pin high.
If a pin is set as input, what does reading the Data Register give you?
AThe current voltage level on the pin
BThe output value you set
CThe direction of the pin
DThe pin's configuration mode
✗ Incorrect
Reading the Data Register on an input pin gives the current voltage level (high or low) on that pin.
What happens if you write to the Data Register when the pin is set as input?
AThe pin outputs the value
BThe pin changes direction
CNothing happens to the pin output
DThe microcontroller resets
✗ Incorrect
Writing to the Data Register has no effect on the pin output if the pin is configured as input.
Which register do you configure first to use a pin as output?
AData Register
BDirection Register
CStatus Register
DInterrupt Register
✗ Incorrect
You must first set the pin as output by configuring the Direction Register before writing to the Data Register.
Explain the roles of the Direction Register and Data Register in controlling a microcontroller pin.
Think about how you tell a pin what to do and then what value it should have.
You got /4 concepts.
Describe a simple example of turning on an LED using Direction and Data Registers.
Remember: direction first, then data.
You got /3 concepts.
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
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 C
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
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 B
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
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 D
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:
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
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 A
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?