Professional Documents
Culture Documents
Lecture 4 Digital Input
Lecture 4 Digital Input
Lecture 4 Digital Input
EMBEDDED SYSTEMS
PROGRAMMING
LECTURE 4 DIGITAL INPUT
Digital I/O Ports
•3 8-bit Ports (B, C, D)
•Each port controlled by 3 8-bit registers
• Each bit controls one I/O pin
• DDRx – Direction register
• Defines whether a pin is an input (0) or and output (1)
• PINx – Pin input value
• Reading this “register” returns value of pin
• PORTx – Pin output value
• Writing this register sets value of pin
Switches as Digital Input
Mechanical
Connecting Switches to a Digital System
Single-pole double-throw (SPDT) connection
1 (HIGH)
0 (LOW)
𝜇𝐶
Connecting Switches to a Digital System
Single-pole single-throw (SPST) connection
𝜇𝐶
𝜇𝐶
Connecting Switches to a Digital System
Single-pole single-throw (SPST) connection
Pullup &Pulldown
𝜇𝐶
𝜇𝐶
I/O Ports
•Internal Pullups
• If a pin is an input (DDRxi = 0):
• PORTxi = 0 – pin is floating
• PORTxi = 1 – connects a pullup to the pin
• Keeps pin from floating if no one driving
• Allows wired-OR bus
Pin Input
off
PORTx
PINx
Counting Event with Buttons (Switch)
Mechanical
loop()={0;
count
loop()
presses
b = digitalRead(0);
{= false;
loop()
if(b
b = digitalRead(0);
=={ 1){
if(b
b =
//digitalRead(0);
==
PRESSED
1){
if(b
digitalWrite(13,HIGH);
count++;
== 1 && !pressed){
} count++;
}else{
pressed = true;
} digitalWrite(13,LOW);
}
else if(b==0)
} pressed = false;
}
Bouncing
000000000000101010111111111111111111111
Debouncing
Debouncing
INTEGRATED CIRCUIT SOLUTIONS
Use switch debouncers like the MAX681
Software debouncing
Delay
Wait until input stabilizes
Counter
Count input states
Shift Register
Search for pattern (rising/falling edge)
Bouncing
Shift Register
0111111111111