You are on page 1of 25

College of Engineering

IENF641: Microcontrollers

I/O Ports in AVR Microcontrollers


Chapter 3

Using Digital Input (Switches) with


Microcontrollers

Edited by:
Dr. Essam Alnatsheh
efalnatsheh@amaiu.edu.bh
Presentation Index
 Pushbuttons
 Connecting a Pushbutton/LED Circuit
 Reading a Pushbutton
 Pushbutton Controlled LED
 2 Pushbuttons, 2 LEDs
 Chapter 3 Review
I/O unit in AVR

RAM EEPROM Timers

PROGRAM
ROM

Program
Bus Bus
CPU

Interrupt Other
OSC I/O Port
Unit Peripherals

I/O
PINS
Connecting a Pushbutton/LED Circuit
Pushbuttons
Pushbuttons are virtually everywhere interactions with an
electronics device are required.

In Chapter #2, the AVR microcontroller was used for


output control of a device – an LED.
In this chapter the AVR microcontroller will be used to
read the state of an input from a simple device – the
pushbutton.
Connecting a Pushbutton/LED Circuit
The pushbuttons are normally-open. That is, the switch
does not make contact until the button is pressed. Once
released, it returns to the open position.
Pushbutton Test Circuit
This circuit demonstrates how the push-buttons
switch allows current to flow when closed.

Not pressed -
Open: No
current flow, LED
is not-lit.

Pressed –
Open: Current
flows lighting the
LED.
Pushbutton Test Circuit
This circuit demonstrates how the switch can create a
short-circuit around the LED. Current will take the
easiest path and not flow through the LED.

Describe how this circuit will work?

Shorts are usually


not desirable. Note
that resistor is still
in the path either
way to ensure
excessive current is
not drawn.
Reading a Pushbutton
Reading a Pushbutton
 Connecting the circuit to the microcontroller.
Describe how this circuit will work considering that P3 is in
the input mode?
Reading a Pushbutton
When the switch is Vdd SOUT 1 24 VIN
Vdd

released, Vss (0V) is


SIN 2 23 VSS
ATN 3
BS2 22 RES
VSS 4 21 VDD (+5V)

sensed at the input of P0


P1
5

6
20

19
P15
P14

P3. P2
P3
7

8
1 18

17
P13
P12
P4 9 0 16 P11
220  P5 10 15 P10
10 k P6 11 14 P9

The 10K resistor P7 12

BS2-IC
13 P8

prevents a short circuit Vss


from Vdd to Vss
Vdd SOUT 1 24 VIN
Vdd SOUT 1 24 VIN
SIN 2 23 VSS SIN 2 23 VSS
BS2 BS2
When the switch is
ATN 3 22 RES ATN 3 22 RES
VSS 4 21 VDD (+5V) VSS 4 21 VDD (+5V)
P0 5 20 P15 P0 5 20 P15

pressed, Vdd (+5V)


P1
P2 1
6

7 is 19

18
P14
P13
P1
P2
6

7 1
19

18
P14
P13

sensed 220
at the input of
P3 8 17 P12 P3 8 17 P12
P4 09 16 P11 P4 9 0 16 P11
P5 10 15 P10 220  P5 10 15 P10

P3.
10 k P6
P7
11

12
14

13
P9
P8
10 k P6
P7 12
11 14

13
P9
P8

BS2-IC BS2-IC

Vss Vss
Reading a Pushbutton
In this configuration, the 10K is said to be a Pull-Down
resistor since it is pulling the input down to ground or
Vss when the button is not active (not pressed).

The switch is said to be Active-High since activating it


(pressing it) will cause the input of P3 to be High.
Reading a Pushbutton
This configuration shows a Pull-Up resistor to Vdd, with an
Active-Low button.

Describe how this circuit will work considering that P3 is in


the input mode?
ATMega16 Pinout
The structure of I/O pins
777
666
XTAL1 555 PB5 (SCK)
XTAL2 444 PB4 (MISO)
RESET 333 PB3 (MOSI/OC2A)
VCC
DDRx: 7 6 5 4 3 22 2 21 0 PB2 (SS/OC1B)

GND
PORTx: 7 6 5 4 3 12 1 11 0 PB1 (OC1A)

AVCC
PINx: 7 6 5 4 3 02 0 01 0 PB0 (ICP1/CLK0)
PINB
AREF DDRB
Px7 Px6 Px5 Px4 PORTB
Px3 Px2 Px1 Px0

PORTC PORTD
DDRC DDRD
PINC PIND
(ADC0) PC0 PD7 (AIN1)
000 777
(ADC1) PC1 666 PD6 (AIN0)
111
(ADC2) PC2 222 555 PD5 (T1)
(ADC3) PC3 333 444 PD4 (T0/XCK)
(ADC4/SDA) PC4 444 333 PD3 (INT1)
(ADC5/SCL) PC5 555 222 PD2 (INT0)
666 111 PD1 (TXD)
777 000 PD0 (RXD)
The structure of I/O pins

DDRx.n
OUTPUT
PORTx.n

PINx.n INPUT

16
ATMega16 Pin out & Descriptions
The PINx register gets the reading from the input pins of the MCU
To check the status of a bit
 We can use & operator to check if bits are set or clear
xxxx xxxx xxxx xxxx
& 0000 0100 OR & (1 << 2)
------------- -------------
0000 0x00 0000 0x00
To check if the pushbutton connected to PD2 is pressed:
if(PIND & (00000100)==1) { // apply the bit-wise AND
if(PIND & (00000001 << 2)==1) { // apply the bit shift

if(PIND & (1 << 2)==1) { // write out the "1"

if ((PIND &(0x01<<2))==1) // To check if pin PD2 is set


Example 1
 Write a program to perform the following:
(a) Keep monitoring the pushbutton connected PB2 bit until it
becomes HIGH;
(b) When PB2 becomes HIGH, write value 0x45 to Port C.
#include <avr/io.h>

int main ()
{
DDRB &=~(0x01<<2); // Set PB2 as input
DDRC = 0xFF; // Set Port C as output
while(1)
{
if ((PINB &(0x01<<2))==1) //Wait for pressing the button
PORTC = 0x45;
}
return 0;
}
19
Example 2
 A switch is connected to pin VCC

PB0 and an LED to pin PB5. 4.7k AVR

Write a program to get the Switch


PB0

status of SW and send it to PB5


270
the LED.
LED

20
Example 3
 A switch is connected to pin VCC

PB0 and an LED to pin PB5. 4.7k AVR

Write a program using the Switch


PB0

following pseudo-code: PB5


270

1. If button is pressed: LED

 Blink LED quickly at 100mSec


2. Or else, if not pressed:
 Keep LED off for 100mSec
3. Loop back to Step 1

21
Example 4: 2 Pushbuttons, 2 LEDs

In this case, 2 buttons will be used to control 2 LEDs.


// Blink PC0 LED if PB2 pushbutton is pressed, and blink PC1 LED if
// PB3 pushbutton is pressed.

#include <avr/io.h>
#define F_CPU 4000000UL
Example 4: 2 Pushbuttons, 2 LEDs
#include <util/delay.h>
int main ()
{
DDRB &=~(0x01<<2); // Set PB2 as input
DDRB &=~(0x01<<3); // Set PB3 as input
DDRC = 0xFF; // Set Port C as output
while(1)
{
if ((PINB &(0x01<<2))==1) //Wait for pressing PB2 button
{
PORTC |=(0x01<<0); //Turn ON the PC0 LED
_delay_ms(50);
}
else if ((PINB &(0x01<<3))==1) //Wait for pressing PB3 button
{
PORTC |=(0x01<<1); //Turn ON the PC1 LED
_delay_ms(50);
}
PORTC &=~(0x01<<0); //Turn OFF the PC0 LED
PORTC &=~(0x01<<1); //Turn OFF the PC1 LED
_delay_ms(50);
}
What happens when both buttons
return 0;
} are pressed?
Example 5: 2 Pushbuttons, 2 LEDs

For the LED control, logical operators can be used to


make both LED's operate when both buttons are
pressed.

if (((PINB &(0x01<<2))==1) && ((PINB &(0x01<<3))==1))


{
PORTC |=(0x01<<0); //Turn ON the PC0 LED
PORTC |=(0x01<<1); //Turn ON the PC1 LED
_delay_ms(50);
}
else if ((PINB &(0x01<<2))==1) //Wait for pressing PB2 button
{
PORTC |=(0x01<<0); //Turn ON the PC0 LED
_delay_ms(50);
}
References 

You might also like