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
1. KEYPAD INTERFACING 8051
Keypad is a widely used input device with lots of application in our everyday life. From a simpletelephone 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. PortP2 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. PortP0 is used to scan input fromthe 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 caseno key is pressed the output of all the columns will be high.
2. 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.
3. 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.
4. 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 componentwhich can be made to Offer varying resistancein a circuit. The
resistanceis varied by adjusting the rotary control over it. The adjustmentcan be done by using a small screw driver or a
similar tool. The resistancedoes not vary linearly but rather varies in exponential or logarithmic manner. Such variable
resistors arecommonly used for adjusting sensitivity along a sensor.
The variable resistanceis obtained across the single terminal at frontand one of the two other terminals. The two legs at
back offer fixed resistancewhich is divided by the front leg. So, whenever only the back terminals are used, a presetacts
as a fixed resistor. Presets arefixed by their fixed value resistance.
5. AT89C51: AT89S51is an 8-bit microcontroller and belongs to Atmel’s 8051 family. AT89C51 has 4KBFlash programmable
and erasableread only memory (PROM) and128 bytes of RAM. Itcan be erased and programto a maximum of 1000 times.
In 40-pin AT89C51 thereare 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, restof 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 addressableand so there can also be accessed individually.
Port0 and Port2 are also used to providelow byte and high byte addresses respectively, when connected to an external
memory. Port3 has multiplexed pins for special functions like serial communication, hardwareinterrupts, timer inputs,
and read/writeoperation from external memory. AT89C51 has an inbuilt UART for serialcommunication. Itcan be
programmed to operate at differentbaud rates. Including two timers and hardwareinterrupts it has a total of 6 interrupts.
Pin diagramfor AT89C51 can be given as follows:
7. PIN DESCRIPTION:
Pin
No
Function Name
1
8 bit input/output port (P1) pins
P1.0
2 P1.1
3 P1.2
4 P1.3
5 P1.4
6 P1.5
7 P1.6
8 P1.7
9 Reset pin; Active high Reset
10
Input (receiver) for serial
communication
RxD
8 bit
input/output
port (P3)
pins
P3.0
11
Output (transmitter) for serial
communication
TxD P3.1
12 External interrupt 1 Int0 P3.2
13 External interrupt 2 Int1 P3.3
14 Timer1 external input T0 P3.4
15 Timer2 external input T1 P3.5
16
Write to external data
memory
Write P3.6
17
Read from external data
memory
Read P3.7
18
Quartz crystal oscillator (up to 24 MHz)
Crystal 2
19 Crystal 1
20 Ground (0V) Ground
21
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
29
Program store enable; Read from external program
memory
PSEN
30
Address 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
8. 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.
9. PIN DIAGRAM:
The above figure represents a LCD.
PIN DESCRIPTION:
Pin No Function Name
1 Ground (0V) Ground
2 Supply voltage; 5V (4.7V – 5.3V) Vcc
3 Contrastadjustment; 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/write
6 Sends data to data pins when a high to low pulseis given Enable
7
8-bit data pins
DB0
8 DB1
9 DB2
10 DB3
11 DB4
12 DB5
13 DB6
14 DB7
15 Backlight VCC (5V) Led+
16 Backlight Ground (0V) Led-
10. Interface keypadwith8051 microcontroller inC language:
How to detect pressedkey 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 A
RowA = 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 B
RowB = 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 C
RowC = 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 D
RowD = 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 lcdand keypad with8051 microcontroller inC language
Complete Program:
#include
#include
//Function declarations
void cct_init(void);
void delay(unsigned int);
void lcdinit(void);
void writecmd(int);
void writedata(char);
void Return(void);
char READ_SWITCHES(void);
11. 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; //RowA
sbit RowB = P1^1; //RowB
sbit RowC = P1^2; //RowC
sbit RowD = P1^3; //RowD
sbit C1 = P1^4; //Column1
sbit C2 = P1^5; //Column2
sbit C3 = P1^6; //Column3
sbit C4 = P1^7; //Column4
sbit E = P3^6; //E pin for LCD
sbit RS = P3^7; //RS pin for LCD
void main()
{
cct_init(); // Make input and output pins as required
lcdinit(); // Initilize LCD
writecmd(0×01); //Clear LCD screen
writecmd(0×81); //move cursor on line 1 position 1
lcd_data(“Interfacelcd”);
writecmd(0xc2); //GO line 2 position 2
lcd_data(“with 8051″);
}
void lcd_data(unsigned char *s)
{
unsigned char l,i;
l = strlen(s); // get the length of string
for(i=0;i<l;i++)
{
writedata(*s);
s++;
}
}</l;i++)
void Home(void)
{
writecmd(0x02);
delay(2500);
}
void cct_init(void)
{
P0 = 0x00; //not used
P1 = 0xf0; //used for generating outputs and taking inputs from Keypad
P2 = 0x00; //used as data port for LCD
P3 = 0x00; //used for RS and E
}
12. void delay(unsigned int a)
{
int i;
for(i=0;i
}
void writedata(char t)
{
RS = 1; // This is data
P2 = t; //Data transfer
E = 1;
delay(150);
E = 0;
delay(150);
}
void writecmd(intz)
{
RS = 0; // This is command
P2 = z; //Data transfer
E = 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 set
writecmd(0x0f); //display on,cursor off,blink on
writecmd(0x01); //clear display
writecmd(0x06); //entry mode, set increment
}
char READ_SWITCHES(void)
{
RowA = 1; RowB= 1; RowC = 1; RowD = 1; //Test Row A
RowA = 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 B
RowB = 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'; }
13. RowA = 1; RowB= 1; RowC = 1; RowD = 1; //Test Row C
RowC = 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 D
RowD = 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 fromuser
{
char key = 'n'; //assumeno key pressed
while(key == 'n') //wait untill a key is pressed
key = READ_SWITCHES(); //scan the keys again and again
return key; //when key pressed then return its value
}