17
KEYPAD INTERFACING 8051 Keypad is a widely used input device with lots of application in our everyday life. From a simple telephone to keyboard of a computer, ATM, electronic lock, etc., keypad is used to take input from the user for further processing. In this article we are interfacing keypad with the MCU AT89C51 and displaying the corresponding number on LCD. This module can be further used in a number of systems to interfaced keypad with microcontroller and other processors to get desired output. The program to interface keypad with controller is written in C language which is very easy to understand. The above figure explains how a keypad can be interfaced with 8051. DESCRIPTION: Keypad is organized as a matrix of switches in rows and column. The article uses a 4X3 matrix keypad and a 16x2 LCD for displaying the output of keypad. The circuit diagram shows the connection of keypad with the controller. Port P2 of the microcontroller is used to send the data for displaying on the LCD. P1^1, P1^2, P1^3 pins of microcontroller is connected to RS, RW, EN pins of LCD respectively. Port P0 is used to scan input from the keypad (refer circuit diagram for connection).The concept of interfacing keypad with the MCU is simple. Every number is assigned two unique parameters, i.e., row and column number (n(R, C) for

Keypad interfacing 8051 -NANOCDAC

Embed Size (px)

DESCRIPTION

Searching for Embedded Systems,VLSI,Matlab, PLC scada Training Institute in Hyderabad-Get the Best Embedded Systems,VLSI,Matlab, PLC scada Training with Real time Projects from Nanocdac. Register now for new batches Call Us-040 -23754144,+91- 9640648777

Citation preview

Page 1: Keypad interfacing 8051 -NANOCDAC

KEYPAD INTERFACING 8051

Keypad is a widely used input device with lots of application in our everyday life. From a simple telephone to keyboard of a computer, ATM, electronic lock, etc., keypad is used to take input from the user for further processing. In this article we are interfacing keypad with the MCU AT89C51 and displaying the corresponding number on LCD. This module can be further used in a number of systems to interfaced keypad with microcontroller and other processors to get desired output. The program to interface keypad with controller is written in C language which is very easy to understand.

The above figure explains how a keypad can be interfaced with 8051.

DESCRIPTION:

Keypad is organized as a matrix of switches in rows and column. The article uses a 4X3 matrix keypad and a 16x2 LCD for displaying the output of keypad. The circuit diagram shows the connection of keypad with the controller. Port P2 of the microcontroller is used to send the data for displaying on the LCD. P1^1, P1^2, P1^3 pins of microcontroller is connected to RS, RW, EN pins of LCD respectively. Port P0 is used to scan input from the keypad (refer circuit diagram for connection).The concept of interfacing keypad with the MCU is simple. Every number is assigned two unique parameters, i.e., row and column number (n(R, C) for example 6 (2, 3)). Hence every time a key is pressed the number is identified by detecting the row and column number of the key pressed. Initially all the rows are set to zero by the controller and the columns are scanned to check if any key is pressed. In case no key is pressed the output of all the columns will be high.

Page 2: Keypad interfacing 8051 -NANOCDAC

Diagramatically it can be represented as follows:

Whenever a key is pressed the row and column corresponding to the key will get short, resulting in the output of the corresponding column goes to go low (since we have made all the rows zero). This gives the column number of the pressed key.

Once the column number is detected, the controller set’s all the rows to high. Now one by one each row is set to zero by controller and the earlier detected column is checked if it becomes zero. The row corresponding to which the column gets zero is the row number of the digit.

Page 3: Keypad interfacing 8051 -NANOCDAC

The above process is very fast and even if the switch is pressed for a very small duration of time the controller can detect the key which is pressed. The controller displays the number corresponding to the row and column on the LCD.

Page 4: Keypad interfacing 8051 -NANOCDAC

CIRCUIT DIAGRAM:

The above circuit diagram explains how a keypad which is nothing but a seven segment display can be interfaced with AT89S51 microcontroller.

COMPONENTS:

PRESET: A preset is a three legged electronic component which can be made to Offer varying resistance in a circuit. The resistance is varied by adjusting the rotary control over it. The adjustment can be done by using a small screw driver or a similar tool. The resistance does not vary linearly but rather varies in exponential or logarithmic manner. Such variable resistors are commonly used for adjusting sensitivity along a sensor.

The variable resistance is obtained across the single terminal at front and one of the two other terminals. The two legs at back offer fixed resistance which is divided by the front leg. So, whenever only the back terminals are used, a preset acts as a fixed resistor. Presets are fixed by their fixed value resistance.

Page 5: Keypad interfacing 8051 -NANOCDAC

AT89C51: AT89S51 is an 8-bit microcontroller and belongs to Atmel’s 8051 family. AT89C51 has 4KB Flash programmable and erasable read only memory (PROM) and128 bytes of RAM. It can be erased and program to a maximum of 1000 times.

In 40-pin AT89C51 there are four ports designated as P0, P1, P2 and P3. All these ports are 8-pin bidirectional ports i.e., they can be used as both input ports and output ports. Except P0 which needs external pull-ups, rest of the ports have internal pull-ups. When 1’s are written to these port pins, they are pulled high by the internal pull-ups and can be used as inputs. These ports are also bit addressable and so there can also be accessed individually.

Port0 and Port2 are also used to provide low byte and high byte addresses respectively, when connected to an external memory. Port 3 has multiplexed pins for special functions like serial communication, hardware interrupts, timer inputs, and read/write operation from external memory. AT89C51 has an inbuilt UART for serial communication. It can be programmed to operate at different baud rates. Including two timers and hardware interrupts it has a total of 6 interrupts.

Pin diagram for AT89C51 can be given as follows:

Page 6: Keypad interfacing 8051 -NANOCDAC

PIN DIAGRAM:

Page 7: Keypad interfacing 8051 -NANOCDAC

PIN DESCRIPTION:

Pin No

Function Name

1

8 bit input/output port (P1) pins

P1.02 P1.13 P1.24 P1.35 P1.46 P1.57 P1.68 P1.79 Reset pin; Active high Reset

10Input (receiver) for serial

communicationRxD

8 bit input/outpu

t port (P3) pins

P3.0

11Output (transmitter) for serial

communicationTxD P3.1

12 External interrupt 1 Int0 P3.213 External interrupt 2 Int1 P3.314 Timer1 external input T0 P3.415 Timer2 external input T1 P3.5

16Write to external data

memoryWrite P3.6

17Read from external data

memoryRead P3.7

18Quartz crystal oscillator (up to 24 MHz)

Crystal 219 Crystal 120 Ground (0V) Ground21

8 bit input/output port (P2) pins/

High-order address bits when interfacing with external memory

P2.0/ A8

22 P2.1/ A9

23 P2.2/ A10

24 P2.3/ A11

25 P2.4/ A12

26 P2.5/ A13

27 P2.6/ A14

28 P2.7/ A15

29Program store enable; Read from external program

memoryPSEN

30Address Latch Enable ALE

Program pulse input during Flash programming Prog

31

External Access Enable; Vcc for internal program executions

EA

Programming enable voltage; 12V (during Flash programming)

Vpp

32

8 bit input/output port (P0) pins

Low-order address bits when interfacing with external memory

P0.7/ AD7

33 P0.6/ AD6

34 P0.5/ AD5

35 P0.4/ AD4

36 P0.3/ AD3

37 P0.2/ AD2

38 P0.1/ AD1

39 P0.0/ AD0

40 Supply voltage; 5V (up to 6.6V) Vcc

Page 8: Keypad interfacing 8051 -NANOCDAC

LCD:

LCD (Liquid Crystal Display) screen is an electronic display module and find a wide range of applications. A 16x2 LCD display is very basic module and is very commonly used in various devices and circuits. These modules are preferred over and other multi segment LEDs. The reasons being: LCDs are economical; easily programmable; have no limitation of displaying special. A 16x2 LCD means it can display 16 characters per line and there are 2 such lines. In this LCD each character is displayed in 5x7 pixel matrix. This LCD has two registers, namely, Command and Data. The command register stores the command instructions given to the LCD. A command is an instruction given to LCD to do a predefined task like initializing it, clearing its screen, setting the cursor position, controlling display etc. The data register stores the data to be displayed on the LCD. The data is the ASCII value of the character to be displayed on the LCD.

The above figure represents a LCD.

Page 9: Keypad interfacing 8051 -NANOCDAC

PIN DIAGRAM:

The above figure represents a LCD.

PIN DESCRIPTION:

Pin No Function Name1 Ground (0V) Ground2 Supply voltage; 5V (4.7V – 5.3V) Vcc3 Contrast adjustment; through a variable resistor VEE

4 Selects command register when low; and data register when high Register Select

5 Low to write to the register; High to read from the register Read/write6 Sends data to data pins when a high to low pulse is given Enable7

8-bit data pins

DB08 DB19 DB2

10 DB311 DB412 DB513 DB614 DB715 Backlight VCC (5V) Led+16 Backlight Ground (0V) Led-

Page 10: Keypad interfacing 8051 -NANOCDAC

Interface keypad with 8051 microcontroller in C language:

How to detect pressed key value?

When key 1 is pressed then RowC wire is shorted with C1 wire inside the keypad. Similarly, when key 9 is pressed then RowA wire is shorted with C3 wire. This behavior is true for all the keys. How to detect this behavior in the microcontroller code?

We can detect pressed key value in the microcontroller using the “Scanning algorithm code”. This algorithm is written with the name of ‘Read_Switches ()’ function in the code. The function is shown below

char READ_SWITCHES(void){RowA = 1; RowB = 1; RowC = 1; RowD = 1; //Test Row ARowA = 0;if (C1 == 0) { delay(10000); while (C1==0); return ’7′; }if (C2 == 0) { delay(10000); while (C2==0); return ’8′; }if (C3 == 0) { delay(10000); while (C3==0); return ’9′; }if (C4 == 0) { delay(10000); while (C4==0); return ‘/’; }

RowA = 1; RowB = 1; RowC = 1; RowD = 1; //Test Row BRowB = 0;if (C1 == 0) { delay(10000); while (C1==0); return ’4′; }if (C2 == 0) { delay(10000); while (C2==0); return ’5′; }if (C3 == 0) { delay(10000); while (C3==0); return ’6′; }if (C4 == 0) { delay(10000); while (C4==0); return ‘x’; }

RowA = 1; RowB = 1; RowC = 1; RowD = 1; //Test Row CRowC = 0;if (C1 == 0) { delay(10000); while (C1==0); return ’1′; }if (C2 == 0) { delay(10000); while (C2==0); return ’2′; }if (C3 == 0) { delay(10000); while (C3==0); return ’3′; }if (C4 == 0) { delay(10000); while (C4==0); return ‘-’; }

RowA = 1; RowB = 1; RowC = 1; RowD = 1; //Test Row DRowD = 0;if (C1 == 0) { delay(10000); while (C1==0); return ‘C’; }if (C2 == 0) { delay(10000); while (C2==0); return ’0′; }if (C3 == 0) { delay(10000); while (C3==0); return ‘=’; }if (C4 == 0) { delay(10000); while (C4==0); return ‘+’; }

return ‘n’; // Means no key has been pressed}

Interface lcd and keypad with 8051 microcontroller in C language

Complete Program :

#include#include

//Function declarationsvoid cct_init(void);void delay(unsigned int);void lcdinit(void);void writecmd(int);void writedata(char);void Return(void);char READ_SWITCHES(void);

Page 11: Keypad interfacing 8051 -NANOCDAC

char get_key(void);void check_pwd(int);void lcd_data(unsigned char *s);void Home(void);void ip_pwd(void);void pass_code(void);

//*******************//Pin description

sbit RowA = P1^0; //RowAsbit RowB = P1^1; //RowBsbit RowC = P1^2; //RowCsbit RowD = P1^3; //RowD

sbit C1 = P1^4; //Column1sbit C2 = P1^5; //Column2sbit C3 = P1^6; //Column3sbit C4 = P1^7; //Column4

sbit E = P3^6; //E pin for LCDsbit RS = P3^7; //RS pin for LCD

void main(){cct_init(); // Make input and output pins as requiredlcdinit(); // Initilize LCDwritecmd(0×01); // Clear LCD screenwritecmd(0×81); // move cursor on line 1 position 1lcd_data(“Interface lcd”);writecmd(0xc2); // GO line 2 position 2lcd_data(“with 8051″);

}

void lcd_data(unsigned char *s){unsigned char l,i;l = strlen(s); // get the length of stringfor(i=0;i<l;i++) {writedata(*s);s++;}}</l;i++)

void Home(void){writecmd(0x02);delay(2500);}

void cct_init(void){P0 = 0x00; //not usedP1 = 0xf0; //used for generating outputs and taking inputs from KeypadP2 = 0x00; //used as data port for LCDP3 = 0x00; //used for RS and E}

Page 12: Keypad interfacing 8051 -NANOCDAC

void delay(unsigned int a){int i;for(i=0;i }

void writedata(char t){RS = 1; // This is dataP2 = t; //Data transferE = 1;delay(150);E = 0;delay(150);}

void writecmd(int z){RS = 0; // This is commandP2 = z; //Data transferE = 1;delay(150);E = 0;delay(150);}

void lcdinit(void){

delay(15000);writecmd(0x30);delay(4500);writecmd(0x30);delay(300);writecmd(0x30);delay(650);

writecmd(0x38); //function setwritecmd(0x0f); //display on,cursor off,blink onwritecmd(0x01); //clear displaywritecmd(0x06); //entry mode, set increment}

char READ_SWITCHES(void){RowA = 1; RowB = 1; RowC = 1; RowD = 1; //Test Row ARowA = 0;if (C1 == 0) { delay(10000); while (C1==0); return '7'; }if (C2 == 0) { delay(10000); while (C2==0); return '8'; }if (C3 == 0) { delay(10000); while (C3==0); return '9'; }if (C4 == 0) { delay(10000); while (C4==0); return '/'; }

RowA = 1; RowB = 1; RowC = 1; RowD = 1; //Test Row BRowB = 0;if (C1 == 0) { delay(10000); while (C1==0); return '4'; }if (C2 == 0) { delay(10000); while (C2==0); return '5'; }if (C3 == 0) { delay(10000); while (C3==0); return '6'; }if (C4 == 0) { delay(10000); while (C4==0); return 'x'; }

Page 13: Keypad interfacing 8051 -NANOCDAC

RowA = 1; RowB = 1; RowC = 1; RowD = 1; //Test Row CRowC = 0;if (C1 == 0) { delay(10000); while (C1==0); return '1'; }if (C2 == 0) { delay(10000); while (C2==0); return '2'; }if (C3 == 0) { delay(10000); while (C3==0); return '3'; }if (C4 == 0) { delay(10000); while (C4==0); return '-'; }

RowA = 1; RowB = 1; RowC = 1; RowD = 1; //Test Row DRowD = 0;if (C1 == 0) { delay(10000); while (C1==0); return 'C'; }if (C2 == 0) { delay(10000); while (C2==0); return '0'; }if (C3 == 0) { delay(10000); while (C3==0); return '='; }if (C4 == 0) { delay(10000); while (C4==0); return '+'; }

return 'n'; // Means no key has been pressed}

Char get_key(void) //get key from user{char key = 'n'; //assume no key pressed

while(key == 'n') //wait untill a key is pressedkey = READ_SWITCHES(); //scan the keys again and again

return key; //when key pressed then return its value}