0% found this document useful (0 votes)
93 views

4X4 Keyboard Interfacing AIM

This document describes interfacing a 4x4 matrix keyboard with an ATMEGA32 microcontroller. The keyboard's rows are connected to PORTD pins 4-7 and columns to pins 0-3. Scanning the keyboard involves setting one row low at a time while reading the column pins to detect any pressed keys. The detected key codes are then output in ASCII form through PORTB. The program uses a switch statement to map the codes to character values from '0' to 'F' which are displayed on the LEDs. After running, students will be able to interface 4x4 keyboards and generalize embedded C programs.

Uploaded by

Rog Rog
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
93 views

4X4 Keyboard Interfacing AIM

This document describes interfacing a 4x4 matrix keyboard with an ATMEGA32 microcontroller. The keyboard's rows are connected to PORTD pins 4-7 and columns to pins 0-3. Scanning the keyboard involves setting one row low at a time while reading the column pins to detect any pressed keys. The detected key codes are then output in ASCII form through PORTB. The program uses a switch statement to map the codes to character values from '0' to 'F' which are displayed on the LEDs. After running, students will be able to interface 4x4 keyboards and generalize embedded C programs.

Uploaded by

Rog Rog
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 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;
} }

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy