Lecture 4 Digital Input

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 16

CMP3006

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

DDRxi PORTxi Function


0 0 Input - Floating
0 1 Input – internal Pullup connected
1 0 Output LOW
1 1 Output HIGH
DDRx = 0

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

int state =0;


loop() { 000000000000101010111111111111111111111
b = digitalRead(0);
state= (state<<1) | b ;
if(State==b01111111){
// PRESSED
}

0111111111111

You might also like