►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

Sat Dec 15 2018, 01:45 PM
Arslan ayoub
Hii ,, can I get the simulation of numeric keypad lock on proteus
ExperimenterUK
Mon Dec 03 2018, 05:17 PM
Please post questions in the forum. The chat box is for chat
Mon Dec 03 2018, 06:43 AM
help!me!
help! I don't understand busy flag!
Sun Dec 02 2018, 08:01 PM
ile
hello
Mon Nov 12 2018, 05:58 AM
Pavan_user
i have seen your replica of I2C programming using AT89C51 but i am unable to understand the "ack_bit" variable usage ...looks like it is always 1...can you elaborate how acknowledgment is working....
bgk3678
Tue Nov 06 2018, 02:36 PM
kindly any one give solution for my problem
ExperimenterUK
Mon Nov 05 2018, 09:48 PM
Pins high is the normal state. You need a program to set them low
mayengbam
Sun Nov 04 2018, 05:41 PM
hello, i am niranjoy, alll the port of my 8051 is always high, even i erase all the program. why is it so happen and what is the problem. any one please help me!
Sun Nov 04 2018, 02:16 PM
BASAVARAJ g kudamble
hi I am interfacing GSM 300 to 8051 microcontroller to receive message from mobile
ExperimenterUK
Sat Sep 29 2018, 06:28 PM
Please post questions in the forum.

Downloads

Comments

zhangd1v4bh1
Tue Dec 18 2018, 06:02 AM
mauste
Thu Dec 13 2018, 01:52 PM
sifirsekiz
Wed Dec 12 2018, 01:37 PM
cadetjeff
Fri Dec 07 2018, 10:11 AM
crak0axel
Sun Dec 02 2018, 01:12 AM
AbuHishaam
Thu Nov 29 2018, 03:43 PM
Evgen87
Thu Nov 29 2018, 10:44 AM
akshaykulkarni
Mon Nov 26 2018, 05:18 AM

Online

Guests: 51, Members: 0 ...

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

Members: 38029
Newest member: zhangd1v4bh1
Forum Activity
sifirsekiz Avatar
Posted By sifirsekiz
Sun Dec 16 2018, 11:13 PM
Guys pleaseeeee :((
sifirsekiz Avatar
Posted By sifirsekiz
Sat Dec 15 2018, 07:09 PM
HiThe pulse-sensor-amped has an analogue output.Th...
ExperimenterUK Avatar
Posted By ExperimenterUK
Sat Dec 15 2018, 07:01 PM
HiThe pulse-sensor-amped has an analogue output.Th...
sifirsekiz Avatar
Posted By sifirsekiz
Sat Dec 15 2018, 09:02 AM
and ı have pulse sensor one out but anolog use 805...
sifirsekiz Avatar
Posted By sifirsekiz
Sat Dec 15 2018, 08:56 AM
HiMembers can supply code and diagrams if they wan...
ExperimenterUK Avatar
Posted By ExperimenterUK
Fri Dec 14 2018, 11:54 PM
HiMembers can supply code and diagrams if they wan...
sifirsekiz Avatar
Posted By sifirsekiz
Fri Dec 14 2018, 05:21 PM
Guys pleaseeee 🙏🏻
sifirsekiz Avatar
Posted By sifirsekiz
Thu Dec 13 2018, 04:31 PM
sifirsekiz Avatar
Posted By sifirsekiz
Wed Dec 12 2018, 01:50 PM
Hi guys I have max30100 pulse sensor need to measu...
ExperimenterUK Avatar
Posted By ExperimenterUK
Mon Nov 19 2018, 01:11 AM