What does configuring a pin as an input mean in embedded C?
Configuring a pin as input means setting it to read signals from external devices, like buttons or sensors, so the microcontroller can detect changes.
Click to reveal answer
beginner
How do you configure a pin as output in embedded C?
You set the pin direction register bit to 1 for that pin, which allows the microcontroller to send signals out, like turning on an LED.
Click to reveal answer
intermediate
Why is it important to configure pins correctly as input or output?
Correct configuration prevents damage and ensures the microcontroller communicates properly with other devices by either sending or receiving signals as intended.
Click to reveal answer
beginner
Example: What does this code do? DDRB |= (1 << 3);
This sets pin 3 of port B as an output by setting the third bit of the DDRB register to 1.
Click to reveal answer
intermediate
Example: How to configure pin 2 of port D as input with pull-up resistor enabled?
Clear bit 2 in DDRD to 0 (input), then set bit 2 in PORTD to 1 to enable the internal pull-up resistor.
Click to reveal answer
What does setting a pin's DDR register bit to 0 do?
ADisables the pin
BConfigures the pin as output
CEnables the pin's pull-up resistor
DConfigures the pin as input
✗ Incorrect
Setting the DDR bit to 0 configures the pin as input, allowing it to read signals.
Which register controls whether a pin is input or output?
APORT register
BPIN register
CDDR register
DADC register
✗ Incorrect
The DDR (Data Direction Register) controls pin direction: 0 for input, 1 for output.
How do you enable an internal pull-up resistor on an input pin?
ASet DDR bit to 1 and PORT bit to 0
BSet DDR bit to 0 and PORT bit to 1
CSet DDR bit to 1 and PORT bit to 1
DSet DDR bit to 0 and PORT bit to 0
✗ Incorrect
For input pins, setting PORT bit to 1 enables the internal pull-up resistor.
What happens if you configure a pin as output but connect it to an input device?
AThe pin will send signals to the device
BThe pin will read signals correctly
CThe pin will be damaged
DNothing happens
✗ Incorrect
An output pin sends signals; if connected to an input device, it controls that device.
Which bit operation sets pin 5 of PORTC as output?
ADDRC |= (1 << 5);
BPORTC |= (1 << 5);
CDDRC &= ~(1 << 5);
DPORTC &= ~(1 << 5);
✗ Incorrect
Setting bit 5 of DDRC to 1 configures pin 5 as output.
Explain how to configure a microcontroller pin as input and enable its internal pull-up resistor.
Think about which registers control direction and pull-up.
You got /4 concepts.
Describe the steps and register changes needed to set a pin as output and turn on an LED connected to it.
Consider how output pins send signals.
You got /4 concepts.
Practice
(1/5)
1. Which register is used in embedded C to configure a microcontroller pin as input or output?
easy
A. PORT (Data Register)
B. TIMER Register
C. PIN (Input Register)
D. DDR (Data Direction Register)
Solution
Step 1: Understand pin configuration registers
The DDR register controls whether a pin is input or output by setting bits to 0 or 1.
Step 2: Identify the correct register for direction
PORT controls output values, PIN reads input values, TIMER is unrelated to pin direction.
2. Which of the following lines correctly sets pin 3 of PORTB as output in embedded C?
easy
A. DDRB |= (1 << 3);
B. DDRB &= ~(1 << 3);
C. PORTB |= (1 << 3);
D. PINB |= (1 << 3);
Solution
Step 1: Understand setting pin as output
To set a pin as output, set its bit in DDR register to 1 using bitwise OR.
Step 2: Analyze each option
DDRB |= (1 << 3); uses DDRB |= (1 << 3) which sets bit 3 to 1 correctly. DDRB &= ~(1 << 3); clears bit 3 (input). Options C and D affect PORTB and PINB, not direction.
Final Answer:
DDRB |= (1 << 3); -> Option A
Quick Check:
Use |= with DDR to set output [OK]
Hint: Use |= with DDR to set output bit [OK]
Common Mistakes:
Using &= ~ instead of |= to set output
Modifying PORT or PIN instead of DDR
Shifting wrong bit position
3. What is the effect of the following code snippet?
DDRC |= (1 << 2); // Line 1
DDRC &= ~(1 << 2); // Line 2
medium
A. Pin 2 of PORTC is set as output then input
B. Pin 2 of PORTC remains output
C. Pin 2 of PORTC remains input
D. Syntax error in code
Solution
Step 1: Analyze Line 1
DDRC |= (1 << 2) sets bit 2 to 1, configuring pin 2 as output.
Step 2: Analyze Line 2
DDRC &= ~(1 << 2) clears bit 2 to 0, configuring pin 2 as input.
Final Answer:
Pin 2 of PORTC is set as output then input -> Option A
Quick Check:
Set then clear bit changes pin direction [OK]
Hint: |= sets output, &=~ clears to input [OK]
Common Mistakes:
Thinking both lines set output
Ignoring second line effect
Confusing PORTC with DDRC
4. Identify the error in this code that tries to set pin 5 of PORTD as input:
DDRD |= (0 << 5);
medium
A. DDRD cannot be modified directly
B. Bit shift is incorrect, should be (1 << 5)
C. Should use &= ~ instead of |= to set input
D. No error, code is correct
Solution
Step 1: Understand setting pin as input
To set a pin as input, clear its bit in DDR register using &= ~ (bitwise AND with NOT).
Step 2: Analyze the code
DDRD |= (0 << 5) does nothing because (0 << 5) is 0; it does not clear bit 5. Correct way is DDRD &= ~(1 << 5).
Final Answer:
Should use &= ~ instead of |= to set input -> Option C
Quick Check:
Use &= ~ to clear bit for input [OK]
Hint: Use &= ~ to clear bit for input, not |= [OK]
Common Mistakes:
Using |= with 0 does not clear bit
Confusing bitwise OR and AND
Not using bitwise NOT (~) when clearing
5. You want to configure pins 0, 1, and 2 of PORTA as outputs and pins 3 and 4 as inputs without changing other pins. Which code snippet correctly does this?