Configuring pin as input or output in Embedded C - Performance & Efficiency
When configuring a pin as input or output, the code runs a few simple steps.
We want to see how the time it takes changes if we do this for many pins.
Analyze the time complexity of the following code snippet.
void configure_pins(int pins[], int n) {
for (int i = 0; i < n; i++) {
if (pins[i] % 2 == 0) {
// Set pin as output
set_pin_output(pins[i]);
} else {
// Set pin as input
set_pin_input(pins[i]);
}
}
}
This code sets each pin in the array as input or output based on whether the pin number is even or odd.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Looping through each pin in the array.
- How many times: Exactly once for each pin, so n times.
As the number of pins increases, the time to configure them grows in a straight line.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 times setting pins |
| 100 | 100 times setting pins |
| 1000 | 1000 times setting pins |
Pattern observation: Doubling the pins doubles the work needed.
Time Complexity: O(n)
This means the time to configure pins grows directly with the number of pins.
[X] Wrong: "Configuring pins is always constant time no matter how many pins."
[OK] Correct: Each pin needs its own setup step, so more pins mean more time.
Understanding how loops affect time helps you explain how embedded code scales with hardware changes.
"What if we configure pins in pairs instead of one by one? How would the time complexity change?"
Practice
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.Final Answer:
DDR (Data Direction Register) -> Option DQuick Check:
DDR sets pin direction [OK]
- Confusing PORT with DDR
- Using PIN register to set direction
- Thinking TIMER controls pin mode
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 AQuick Check:
Use |= with DDR to set output [OK]
- Using &= ~ instead of |= to set output
- Modifying PORT or PIN instead of DDR
- Shifting wrong bit position
DDRC |= (1 << 2); // Line 1 DDRC &= ~(1 << 2); // Line 2
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 AQuick Check:
Set then clear bit changes pin direction [OK]
- Thinking both lines set output
- Ignoring second line effect
- Confusing PORTC with DDRC
DDRD |= (0 << 5);
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 CQuick Check:
Use &= ~ to clear bit for input [OK]
- Using |= with 0 does not clear bit
- Confusing bitwise OR and AND
- Not using bitwise NOT (~) when clearing
Solution
Step 1: Set pins 0,1,2 as outputs
Use bitwise OR to set bits 0,1,2 without affecting others: DDRA |= (1<<0)|(1<<1)|(1<<2);Step 2: Set pins 3,4 as inputs
Clear bits 3 and 4 using bitwise AND with NOT: DDRA &= ~((1<<3)|(1<<4));Final Answer:
DDRA |= (1 << 0) | (1 << 1) | (1 << 2); DDRA &= ~((1 << 3) | (1 << 4)); -> Option BQuick Check:
Use |= to set output bits, &=~ to clear input bits [OK]
- Overwriting DDRA instead of modifying bits
- Using & instead of | for combining bits
- Setting input pins as output by mistake
Ready to prove it?
Pass the test and this pattern moves unknown
