0
0
Embedded Cprogramming~10 mins

Configuring pin as input or output in Embedded C - Visual Walkthrough

Choose your learning style9 modes available
Concept Flow - Configuring pin as input or output
Start
Select Pin
Set Pin Mode
If Output
YesConfigure as Output
Pin Ready
If Output
NoConfigure as Input
Pin Ready
End
Choose a pin, decide if it is input or output, then configure it accordingly.
Execution Sample
Embedded C
DDRB |= (1 << 2);  // Set pin 2 as output
DDRB &= ~(1 << 3); // Set pin 3 as input
This code sets pin 2 as output and pin 3 as input on port B.
Execution Table
StepRegisterOperationBitResulting Register ValueExplanation
1DDRBDDRB |= (1 << 2)20b00000100Set bit 2 to 1 to configure pin 2 as output
2DDRBDDRB &= ~(1 << 3)30b00000100Clear bit 3 to 0 to configure pin 3 as input
3----Configuration complete, pins ready
💡 All pins configured; DDRB and PORTB registers updated accordingly
Variable Tracker
RegisterStartAfter Step 1After Step 2Final
DDRB0b000000000b000001000b000001000b00000100
PORTB0b000000000b000000000b000000000b00000000
Key Moments - 2 Insights
Why do we use |= to set a pin as output but &=~ to set a pin as input?
Using |= with (1 << bit) sets that bit to 1 without changing others (output mode). Using &=~ with (1 << bit) clears that bit to 0 without changing others (input mode). See execution_table steps 1 and 2.
What happens if we set a pin as output but forget to configure PORT register?
The pin direction is output, but the output level might be undefined or default. Configuring PORT controls the output voltage level or pull-up resistor for input pins.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the DDRB value after step 1?
A0b00000100
B0b00000010
C0b00001000
D0b00000000
💡 Hint
Check the 'Resulting Register Value' column for DDRB at step 1
At which step is pin 3 configured as input?
AStep 1
BStep 2
CStep 3
DPin 3 is never configured
💡 Hint
Look at the 'Operation' and 'Explanation' columns for DDRB changes
If we change DDRB |= (1 << 2) to DDRB &= ~(1 << 2), what happens to pin 2?
APin 2 becomes output
BPin 2 state does not change
CPin 2 becomes input
DPin 2 is disabled
💡 Hint
Clearing a bit in DDR register sets pin as input; see how &=~ works in step 2
Concept Snapshot
Configuring pin direction:
- Use DDRx register (Data Direction Register)
- Set bit to 1 (DDRx |= 1<<pin) for output
- Clear bit to 0 (DDRx &= ~(1<<pin)) for input
- Use PORTx to set output level or enable pull-up
- Always modify bits carefully to avoid changing other pins
Full Transcript
This visual trace shows how to configure microcontroller pins as input or output by changing bits in DDR and PORT registers. First, select the pin number. Then, set the corresponding bit in DDR register to 1 to make it output or clear it to 0 to make it input. The example sets pin 2 as output by setting bit 2 in DDRB and pin 3 as input by clearing bit 3 in DDRB. The execution table tracks each step and register value changes. Key moments clarify why |= sets bits and &=~ clears bits. The quiz tests understanding of register values and bit operations. Remember, DDR controls direction, PORT controls output level or pull-up resistor.