Microprocessor Systems & Interfacing EEE-342: Comsats University

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 7

COMSATS UNIVERSITY

MICROPROCESSOR SYSTEMS &


INTERFACING
EEE-342

LAB # 06 KEYPAD INTERFACING USING DIGITAL I/O


PORTS

Name

Registration Number

Instructor’s Name MA’AM AASMA RAMAY


OBJECTIVES:

 Using digital I/O ports of AVR microcontroller for digital Inputs.


 Use I/O ports To Interface matrix keypad with microcontroller.

TOOLS:
SOFTWARE TOOLS:
 AVR Studio/ Atmel Studio
 Proteus ISIS
 AVRDUDESS

HARDWARE TOOLS:

Name Value Quantity

Arduino Nano - 1

4X4 Keypad - -

Resistors 100Ω 8

TABLE 6.1: LIST OF COMPONENTS

PRE-LAB TASK:
4X4 MEMBRANE KEYPAD:
The Keypad 4x4 features 16 push buttons arranged in 4x4 matrix to form standard
alphanumeric keypad. It provides a useful human interface component for microcontroller
projects.Matrix keypads uses a combination of four rows and four columns to provide
button states to the microcontroller. Underneath each key is a pushbutton, with one end
connected to one row, and the other end connected to one column. These connections are
shown in figure 6.1.
KEY SPECIFICATIONS
 Maximum Rating: 24 VDC, 30 mA
 Interface: 8-pin access to 4x4 matrix
 Operating temperature: 32 to 122 °F (0 to 50°C)
 Dimensions: Keypad, 2.7 x 3.0 in (6.9 x 7.6 cm) Cable: 0.78 x 3.5 in (2.0 x 8.8 cm)

This keypad can be used in Security systems, Menu selection, Data entry for embedded
systems and various other applications.

FIGURE 6.1: 4X4 MEMBRANE KEYPAD

WORKING OF KEYPAD:

From Figure 6.11 it can be seen that if one of the 16 buttons are pressed, a pair of pins are
connected together. We will used this feature to detect with button was pressed. Under
normal conditions, the switch (key) is open. In order for the microcontroller to determine
which button is pressed, following steps are followed:

1) Create a look-up table filled with 16 entries for all keys of the keypad.
2) Four microcontroller pins should be defined as outputs, and other four pins should be
defined as inputs.
3) Connect columns of the keypad to input port and rows to the output port. Pull up the
input port and then read the value of columns.
4) Now connect rows of the keypad to input port and columns to the output port. Pull up
the input port and then read the value of rows.
5) If no key is pressed, the value of rows and columns will be 0000. If any key is pressed,
the value of rows and columns can be 0000,0001,0010,0100,1000 (1,2,4,8).
6) If no key is pressed, return 0. If a key is pressed, find its location in the keypad look-
up table and return the key pressed.
IN-LAB TASKS:
INTERFACING ATMEGA 328P WITH KEYPAD: CONNECT A KEYPAD TO THE
MICROCONTROLLER. SCAN THE KEYPAD FOR KEY PRESS AND DISPLAY
THE PRESSED KEY ON THE SERIAL MONITOR USING SERIAL
COMMUNICATION.

TASK 1- CODE:
#include <inttypes.h> //Used for type casting
#include <avr/io.h> //Basic I/O definitions
#define F_CPU 16000000UL //XTAL Frequency
#include <util/delay.h> //to generate delays

///*************************** Definitions for UART


*******************************/

#include "debug_prints.c"
#define BAUD0 9600 // Baud Rate for UART
#define MYUBRR (F_CPU/8/BAUD0-1) // U2X = 1
//************* Definitions for Keypad Interfacing **********/
//Rows are connected to PORTB, Columns to PORTD
#define KEYPAD_DDR_ROWS DDRB
#define KEYPAD_PORT_ROWS PORTB
#define KEYPAD_PIN_ROWS PINB
#define KEYPAD_DDR_COLS DDRD
#define KEYPAD_PORT_COLS PORTD
#define KEYPAD_PIN_COLS PIND

/*Lookup table for Keypad*/


unsigned char keypad_tbl1[] = {255,0,1,255,2,255,255,255,3};
unsigned char keypad_tbl2[] = {'1','2','3','A','4','5','6','B','7','8','9','C','*','0','#','D'}; // for Lab
Keypad
//unsigned char keypad_tbl2[] = {'7','8','9','/','4','5','6','X','1','2','3','-','C','0','=','+'}; // for
proteus model
unsigned char read_keypad(void);

int main()
{
unsigned char key_pressed = 0;

//Initalizting UART, Print UBRR value and F_CPU


UART0_init(MYUBRR);
printSerialStr("MYUBRR = ");
printSerialInt(MYUBRR);
printSerialStrln("");
printSerialStr("F_CPU = ");
printSerialInt((int)(F_CPU/1000000));
while (1) {
key_pressed = read_keypad();
if(key_pressed != 0) //wait for key to be pressed
{
UART0_send_char(key_pressed);//display pressed key on serial monitor
key_pressed = 0; } }
return 0; }

unsigned char read_keypad()


{ unsigned char columns;
unsigned char rows;
unsigned char key,temp;
//COMPLETE THIS SECTION OF CODE//
// Pull-up the columns
PORTD|=0xf0;
// Set columns to Input
DDRD&=0x0f;
//pull down the rows
PORTB&=0xf0;
// Set rows to Output
DDRB|=0x0f;
//give delay of 2ms
_delay_ms(2);
// read the value of columns
columns=0x0f&(~(PIND&0xf0)>>4);
_delay_us(100);
// Pull-up the rows
DDRB|=0x0f;
// Set rows to Input
DDRB&=0x00;
//pull down the columns
PORTD&=0x0f;
// Set columns to Output
DDRD|=0xf0;
//give delay of 2ms
_delay_ms(2);
// read the value of rows
rows=0x0f&(~(PINB&0x0f)); /* value of columns and rows will be 1,2,4,8
we will convert it to 0,1,2,3 using keypad_tbl1*/
temp = (keypad_tbl1[columns]) + (keypad_tbl1[rows]<<2); //multiply rows by 4
if(temp > 15) /*If value is greater than 15,return 0, otherwise return the key.*/
key = 0;
else
key = keypad_tbl2[temp];
_delay_ms(200);
return key;
}

PROTEUS SIMULATION

HARDWARE IMPLEMENTATION

SERIAL MONITOR OF ARDUINO IDE


CONCLUSION/CRITICAL ANALYSIS:
The objective of this lab was to interface the ATMEGA 382P microcontroller with the most
fundamental peripheral device the keypad. In this lab we first understood the working of the
keypad.
A 4x4 membrane keyboard is employed to show the numbers 0-9 within the seven-
phase show. Internal structure of the keyboard consists of a pushbutton beneath every key,
with one finish connected to at least one row and different finish connected to at least
one column. The keypad’s helpful human interface is applied in several comes associated
with security systems, information entry and menu choice.

TASK 1 OUTCOMES:
The given code scans the keypad for key press and display the pressed key on the serial
monitor using serial communication. The provided code was tested on Atmel/AVR studio
and a hex file that was burned in the microcontroller. Afterwards, a simulation on proteus
was done to aid the hardware implementation.

You might also like