►Programming 8051 Microcontroller

►8051 Assembly Program for 4x4 Keypad Matrix

keyport equ P2			;Keypad port connected here
col1 equ P2.0			;Column 1
col2 equ P2.1			;Column 2
col3 equ P2.2			;Column 3
col4 equ P2.3			;Column 4

keyval equ 30H			;To store key number
pressed bit 0H			;Flag

key_init:
	mov keyport,#0FH	;Make rows as o/p and col as i/p
	ret

get_key:
	mov keyval,#0		;reset the number
	mov keyport,#7FH	;make Row1 low
	acall read_col		;read columns

	jb pressed, done	;check if flag is set

	mov keyval,#4		;if not then read next row
	mov keyport,#0BFH	;make Row2 low
	acall read_col		;read columns

	jb pressed, done	;check if flag is set

	mov keyval,#8		;if not then read next row
	mov keyport,#0DFH	;make row3 low
	acall read_col		;read columns

	jb pressed, done	;check if flag is set

	mov keyval,#12		;if not read row4
	mov keyport,#0EFH	;make row4 low
	acall read_col		;read columns

done:
	ret

read_col:			;read columns routine
	clr pressed		;reset the flag

	jb col1, nextcol	;check if first key is pressed
	jnb col1,$		;if yes then wait for key release	
	setb pressed		;set the flag
	ret

nextcol:			;read col2
	jb col2, nextcol1	;check if second key is pressed
	jnb col2,$		;if yes then wait for key release
	inc keyval		;its key number 2
	setb pressed		;set the flag
	ret

nextcol1:			;read col3
	jb col3, nextcol2	;check if third key is pressed
	jnb col3,$		;if yes then wait for key release
	inc keyval		;its key 3
	inc keyval
	setb pressed		;set the flag
	ret

nextcol2:			;read column 4
	jb col4, exit		;check if fourth key pressed
	jnb col4,$		;if yes then wait for key release
	inc keyval		;its key 4
	inc keyval
	inc keyval
	setb pressed		;set the flag
	ret

exit:				;if no key is pressed
	clr pressed		;clr the flag
	clr keyval		;reset the number
	ret

	end




►C Program for 4x4 Keypad Matrix

#include <AT89X51.H>
	//Include file for 8051
#define keyport P2	//keypad connected to P2
#define col1 P2_0	//column 1
#define col2 P2_1	//column 2
#define col3 P2_2	//column 3
#define col4 P2_3	//column 4
#define TRUE 1		//some defines
#define FALSE 0

/*
+---------------------------------------+
| Prototype: void key_init(void);       |
| Return Type: void                     |
| Arguments: None                       |
| Description: Initialize ports and     |
|              Keypad.                  |
+---------------------------------------+
*/
void key_init(){
	keyport &=0x0F;	//make Rows as o/p and cols are i/p
}

/*
+-----------------------------------------------+
| Prototype: unsigned char get_key(void);       |
| Return Type: unsigned char                    |
| Arguments: None                               |
| Description: To read key from the keypad      |
+-----------------------------------------------+
*/
unsigned char get_key(){
	unsigned char i,k,key=0;
	k=1;
	for(i=0;i<4;i++){		//loop for 4 rows
		keyport &=~(0x80>
>
i);	//to make rows low 1 by 1
			if(!col1){	//check if key1 is pressed
				key = k+0;	//set key number
				while(!col1);	//wait for release
				return key;	//return key number
			}
			if(!col2){	//check if key2 is pressed
				key = k+1;	//set key number
				while(!col2);	//wait for release
				return key;	//return key number
			}
			if(!col3){	//check if key3 is pressed
				key = k+2;	//set key number
				while(!col3);	//wait for release
				return key;	//return key number
			}
			if(!col4){	//check if key4 is pressed
				key = k+3;	//set key number
				while(!col4);	//wait for release
				return key;	//return key number
			}
		k+=4;			//next row key number
		keyport |= 0x80>
>
i;	//make the row high again
	}
	return FALSE;			//return false if no key pressed
}



Example of how to use the above functions is given here.


◄ AVR Program  |  Back to Index ►

Conversation

Sun May 20 2018, 10:01 AM
justinb
how to write and execute a program that read the keypad button and display the number on 7 segment on intel8051 at89s52 in assembly language?
Thu May 17 2018, 06:36 AM
HAMZA TAJ
sir iwant to ask about controller of three phase supply by using back to back thyristor instead of triac....will u give me some guideline?
Sat Apr 28 2018, 12:54 PM
ashutosh jain
IR Remote Switch using TV remote (RC5 Protocol) is not working properly. opp1 & opp2 is in problem.plz help
ExperimenterUK
Fri Apr 27 2018, 08:09 PM
@kaiserslautern Post in forum
Wed Apr 25 2018, 07:09 AM
kaiserslautern
Hello everyone, using software uart is a very good idea for a second serial port. Thank you so much. I would kindly like to know how to connect this port to the PC?
ExperimenterUK
Sat Apr 21 2018, 07:50 PM
This is a community site. Feel free to help anyone needing advice
hareeshchamiyar
Thu Mar 29 2018, 10:47 AM
please help me for the configuration bits for dspic30f2010
hareeshchamiyar
Thu Mar 29 2018, 10:45 AM
can any one using dspic30f2010.
Thu Mar 29 2018, 06:03 AM
Neena
can anybody give solar tracking in 8051 using proteus and keil to mail [email protected]
ExperimenterUK
Mon Mar 19 2018, 07:33 PM
sorry bedo.. maybe next time

Downloads

Comments

bdmehta
Tue May 22 2018, 02:30 AM
kmag1953
Sun Apr 29 2018, 07:09 PM
udiwonder
Sat Apr 28 2018, 12:41 PM
pirpiri
Sat Apr 21 2018, 03:35 PM
Pater
Fri Apr 20 2018, 02:38 PM
jamiel
Sat Apr 14 2018, 12:47 AM
akrepkral76
Wed Apr 04 2018, 07:51 PM
samanthaeaves
Mon Apr 02 2018, 06:27 AM

Online

Guests: 65, Members: 0 ...

most ever online: 182184
(Members: , Guests: 182184) on 06 Aug 2010: 05:37 AM

Members: 37998
Newest member: bdmehta
Forum Activity
ExperimenterUK Avatar
Posted By ExperimenterUK
Thu Jun 21 2018, 10:49 PM
The frequency of the pulses has to be the same as ...
LAMBO Avatar
Posted By LAMBO
Thu Jun 21 2018, 02:13 PM
hi sircan we change the frequency on pin no 39 of ...
ExperimenterUK Avatar
Posted By ExperimenterUK
Sat Jun 16 2018, 11:33 PM
hi sir sorry for the late reply sir can we control...
LAMBO Avatar
Posted By LAMBO
Sat Jun 16 2018, 07:15 AM
hi sir sorry for the late reply sir can we control...
ExperimenterUK Avatar
Posted By ExperimenterUK
Mon May 14 2018, 10:17 PM
So the main problem is that I am not getting Analo...
ExperimenterUK Avatar
Posted By ExperimenterUK
Mon May 14 2018, 09:40 PM
Hi sir after checking all the connection i...
Pater Avatar
Posted By Pater
Mon May 14 2018, 05:36 PM
Basically my project is based on Data Acquisition ...
LAMBO Avatar
Posted By LAMBO
Sun May 13 2018, 02:13 PM
Hi sir after checking all the connection i...
ExperimenterUK Avatar
Posted By ExperimenterUK
Mon May 07 2018, 07:27 PM
This design should be fine.Choose a frequency betw...
Pater Avatar
Posted By Pater
Mon May 07 2018, 01:28 PM
Sorry for late reply as exams were on,Based on my ...