Lecture 07 Digital Input
Lecture 07 Digital Input
Microprocessor
Lecture #7
Digital Input
Md Rakibul Hasan
Lecturer, Dept. of EEE, BRAC University
Slide Courtesy of Imtiaz Ahmed, Lecturer, Dept. of EEE, BRAC University
Role of Pull Up Resistors at the Port Pins
• Let us say you have configured
a port pin as input pin.
• If there is nothing connected
with the pin and your program
reads the state of the pin, will A switch connected at the
it be high of low? It will be input pin without pull up
difficult to tell. resistor
• This state of the pin is referred
to as floating state.
• To prevent this unknown state,
pull up resistor is used.
• Pull ups are often used with A switch connected at the
button and switches. input pin with pull up resistor 2
Internal pull up resistor in AVR and its control
• In AVR ATmega32 microcontroller,
each of the port pins have a pull up
resistor having the facility of
connecting or disconnecting it with
the pin.
• The provision of connection or
disconnection is controlled by
PORTx register. [see the figure]
• If we put 1s into bits of PORTx
register, the pull up resistors are
activated. Remember that during output
of data, PORTx is the register
in which data has to be
written to make it out.
3
Example for Input from a Push Button
• Let us say a push button is connected in PortD.2
• And, an LED is connected in PortC.2
• Write a program that will the following:
– If the switch is pressed, the LED will glow
– and if it is not pressed LED will remain OFF.
4
Interfacing Circuit
5
#include <mega32.h>
#define Push PIND.2
#define Led PORTC.2
void main(void)
{
DDRC=0xFF;
The Code while (1)
{
if (Push==0)
Led=1;
else
Led=0;
}
}
6
Keyboard Interfacing
• Keyboards and LCDs are the most widely used input/output
devices in microcontrollers such as the AVR.
• We shall discuss keyboard fundamentals, key pressed and
key detection mechanism.
• At the lowest level, keyboards are organized in a matrix of
rows and columns.
• The CPU accesses both the rows and columns through ports.
• Therefore, with two 8-bit ports, an 8x8 keyboard can be
connected to a microcontroller.
7
Keyboard as a Matrix
• Figure shows 16 keys interfaced
PB.0
using one port. Row0
• The rows are connected to 4 pins PB.1 Row1
of port B (PB3 to PB0) and the PB.2 Row2
columns are connected to other 4 PB.3 Row3
pins of the same port (PB7 to PB.4
PB4). Colm0
PB.5
• If no key has been pressed and Colm1
PB.6
rows are read, all rows will be Colm2
PB.7
read as ‘1’ since +5V is connected Colm 3
to the rows through internal pull 8
up resistors.
+5v
PB.0
Row0
How Does It Work?
Pull up PB.1 Row1
resistors PB.2 Row2
PB.3 Row3
PB.4
PB.5 Colm0
Inside the PB.6
Colm1
microcontroller PB.7 Colm2
Colm 3
7 6 5 4 3 2 1 0
DDRB 1 1 1 1 0 0 0 0
PORTB 1/0 1/0 1/0 1/0 1 1 1 1
To send data to the output For switching the internal pullup
10
Interfacing
Circuit
11
The Code (Part 1)
#include <mega32.h>
#include <alcd.h>
void main(void)
{
DDRB=0xF0;
lcd_init(16);
12
while (1)
The Code (Part 2) { PORTB=0b11011111;
PORTB=0b11101111; lcd_gotoxy(0,0);
lcd_gotoxy(0,0); if (PINB.0==0)
if (PINB.0==0) lcd_putsf(“2 ");
lcd_putsf(“1"); if (PINB.1==0)
if (PINB.1==0)
lcd_putsf("5 ");
lcd_putsf("4 ");
if (PINB.2==0)
if (PINB.2==0)
lcd_putsf(“8 ");
lcd_putsf(“7 ");
if (PINB.3==0)
if (PINB.3==0)
lcd_putsf(“*"); lcd_putsf("0 ");
13
PORTB=0b10111111;
lcd_gotoxy(0,0);