free 8051 Microcontroller Projects AVR PIC Microcontroller Projects Tutorials Ebooks Libraries, interfacing tutorials, lcd tutorial, stepper motor, dc motor 8051 assembly language programming electronics and communication ECE CSE pdf ebooks library BE final year project ideas Embedded systems

 
8051 microcontroller 8051 microcontroller
Forums

Rickey's World :: Discussion Forums :: Discuss and Learn :: 8051 Discussion Forum
 
<< Previous thread | Next thread >>
keypad connected to 8051
Moderators: Ajay Bhargav, Arun Kumar V, pdi33, Shailesh NAYAK, ۞ TPS ۞, shyam, sashijoseph, ExperimenterUK, DavesGarage
Author Post
nismo
Thu Feb 04 2010, 09:24AM
 User Offline
Registered Member #9479
Joined: Thu Jul 31 2008, 09:03AM

Posts: 92
Thanked 1 time in 1 posts
I am now interfacing 8051 with a 4x3 keypad. may i know what is the function of this symbol "~" in the code with i have highlighted below in red?


#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
}

Back to top

Ajay Bhargav
Thu Feb 04 2010, 01:32PM
Rickey's World Admin

 User Offline

Registered Member #1
Joined: Fri Feb 24 2006, 04:56AM

Posts: 7542
Thanked 1330 times in 1254 posts
~ means complement.

keyport &=~(0x80>>i);

keyport = keyport AND (complement of (0x80 rightshifted i times)

i goes from 0 to 3 for 4 rows. rows are at higher nibble of port. so this line will clear every row one by one. put value of i from anywhere between 0 to 3 keyport will be 0xFF by default so see what happens when you execute that above statement.

www.rickeyworld.info
If you feel satisfied with the user's forum reply please click on the thank button.

Obey forum rules!
Respect others!
Back to top


This post has been thanked 1 time
 nismo 
nismo
Thu Feb 04 2010, 06:50PM
 User Offline
Registered Member #9479
Joined: Thu Jul 31 2008, 09:03AM

Posts: 92
Thanked 1 time in 1 posts
How about the one i have highlighted now. Why do we have to use "&=" instead of "="?

#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
}
Back to top

Ajay Bhargav
Sun Feb 07 2010, 10:48AM
Rickey's World Admin

 User Offline

Registered Member #1
Joined: Fri Feb 24 2006, 04:56AM

Posts: 7542
Thanked 1330 times in 1254 posts
keyport &=0x0F

means keyport = keyport & 0x0F
i am clearing the only high bits of port.

now the question why &= why not just =? right?

lets say that lower bits of port are used for LEDs so when you are manipulating higher bits of the port you dont want LEDs to be disturbed..
if my port condition is currently 0x4 i.e. third LED is on, then after
keyport = 0x0F all LEDs will get on which we do not want..
so if we do keyport &= 0x0F then
first 0x0F is ANDed with keyport value then stored back..
so keyport & 0x0F = 0x04 & 0x0F = 0x04
no keyport = 0x04
this only clears the high bits and leave the lower bits intact. so you get the point of using &=?

www.rickeyworld.info
If you feel satisfied with the user's forum reply please click on the thank button.

Obey forum rules!
Respect others!
Back to top


This post has been thanked 1 time
 nismo 
nismo
Mon Feb 08 2010, 05:27PM
 User Offline
Registered Member #9479
Joined: Thu Jul 31 2008, 09:03AM

Posts: 92
Thanked 1 time in 1 posts
are all the pins of the chip is always high?
Back to top

Ajay Bhargav
Tue Feb 09 2010, 11:52AM
Rickey's World Admin

 User Offline

Registered Member #1
Joined: Fri Feb 24 2006, 04:56AM

Posts: 7542
Thanked 1330 times in 1254 posts
i didnt understand your question

www.rickeyworld.info
If you feel satisfied with the user's forum reply please click on the thank button.

Obey forum rules!
Respect others!
Back to top

nismo
Tue Feb 09 2010, 03:24PM
 User Offline
Registered Member #9479
Joined: Thu Jul 31 2008, 09:03AM

Posts: 92
Thanked 1 time in 1 posts
Before we set any pin of the micro controller to either '0' or '1'. Is the preset value for all the pins is '1' or in other word "high"?
Back to top

Ajay Bhargav
Fri Feb 12 2010, 04:25AM
Rickey's World Admin

 User Offline

Registered Member #1
Joined: Fri Feb 24 2006, 04:56AM

Posts: 7542
Thanked 1330 times in 1254 posts
yes correct.. be default or say on reset all the pins are high/input mode.

www.rickeyworld.info
If you feel satisfied with the user's forum reply please click on the thank button.

Obey forum rules!
Respect others!
Back to top

 

Jump:     Back to top

Syndicate this thread: rss 0.92 Syndicate this thread: rss 2.0 Syndicate this thread: RDF
Powered by e107 Forum System

8051 Microcontroller Projects 8051 AVR tutorials PIC microcontroller, 8051 assembly language programming electronics and communication ECE CSE pdf ebooks library BE final year project ideas Embedded systems