Вы находитесь на странице: 1из 6

EX NO: 19

DATE:

4x4 KEYBOARD INTERFACING


AIM
To interface 4x4 matrix keyboard PORTD of ATMEGA32 and to output key
press values in ASCII to PORTB.

OBJECTIVE
After completion of this experiment the student will be able to interface 4x4
keyboards. He / She will be able to generalize embedded C programs.

THEORY
Matrix Keypads are commonly used in calculators, telephones etc where a
large number of input switches are required. We know that matrix keypad is
made by arranging push button switches in row and columns. If the 16 switches
are straightly connected to microcontroller we need 16 inputs pins. But arranging
switches in matrix form, we can read the status of each switch using 8 pins of the
microcontroller.
The status of each key can be determined by a process called Scanning. For
the sake of explanation let’s assume that all the column pins (Col1 – Col4) are
connected to the inputs pins and all the row pins are connected to the output
pins of the microcontroller. In the normal case all the column pins are pulled up
(HIGH state) by internal pull up resistors. Now we can read the status of each
switch through scanning.

1. A logic LOW is given to Row1 and others (Row2 – Row-4) HIGH

2. Now each Column is scanned. If any switch belongs to 1st row is pressed
corresponding column will pulled down (logic LOW) and we can detect the
pressed key.

3. This process is repeated for all rows.


PROGRAM
/*------------------------------------------------------------- Program to interface 4x4 matrix
keyboard. rows are connected to PD4 to PD7 columns to PD0 to PD3 and display the contents
IN ASCII to LEDs connected to PORTB ---------------------------------------------------------------*/

#define F_CPU 8000000U

#include <avr/io.h>

#include <util/delay.h>

/* the following is done as the part of generalizing the code This code can be used any
where, all you need to do is change PORT below*/

#define KB_PORT_OUT PORTD

#define KB_PORT_IN PIND

#define DISPLAY_PORT PORTB

void port_init(void)

DDRD = 0x0f;

PORTD = 0xff;

DDRB = 0xff;

PORTB = 0x00;
}

//---------------------- MAIN FUNCTION -------------------------//

int main(void)

Unsigned char upperNibble, keyCode, keyPressed = 0, i;

port_init();

while (1)

{
upperNibble = 0xff;

for(i=0; i<4; i++)

_delay_ms(1);

KB_PORT_OUT = ~(0x01 << i);

_delay_ms(1);

upperNibble = KB_PORT_IN | 0x0f;

if (upperNibble != 0xff)

_delay_ms(20);

upperNibble = KB_PORT_IN | 0x0f;

if(upperNibble == 0xff) goto OUT;

keyCode = (upperNibble & 0xf0) | (0x0f & ~(0x01 << i));

while (upperNibble != 0xff)

upperNibble = KB_PORT_IN | 0x0f;

_delay_ms(20);

switch (keyCode) I
{
case (0xee):
keyPressed = 'F'; // read as F
break;
case (0xed):
keyPressed = 'E'; // read as E
break;
case (0xeb):
keyPressed = 'D'; // read as D
break;
case (0xe7):
keyPressed = 'C'; // read as C
break;
case (0xde):
keyPressed = 'B'; // read as B
break;
case (0xdd):
keyPressed = 'A'; // read as A
break;
case (0xdb):
keyPressed = '9'; // read as 9
break;
case (0xd7):
keyPressed = '8'; // read as 8
break;
case (0xbe):
keyPressed = '7'; // read as 7
break;
case (0xbd):
keyPressed = '6'; // read as 6
break;
case (0xbb):
keyPressed = '5'; // read as 5
break;
case (0xb7):
keyPressed = '4'; // read as 4
break;
case (0x7e):
keyPressed = '3'; // read as 3
break;
case (0x7d):
keyPressed = '2'; // read as 2
break;
case (0x7b):
keyPressed = '1'; // read as 1
break;
case (0x77):
keyPressed = '0'; // read as 0
break;
default : break;
}
OUT:;
}

_delay_ms(10);

DISPLAY_PORT = keyPressed;
} }

Вам также может понравиться