Professional Documents
Culture Documents
IENF641: 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
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.
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.
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
BS2-IC
13 P8
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).
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
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
20
Example 3
A switch is connected to pin VCC
21
Example 4: 2 Pushbuttons, 2 LEDs
#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