Email: Password: Remember Me | Create Account (Free)

Back to Subject List

Old thread has been locked -- no new posts accepted in this thread
???
04/30/08 07:36
Read: times


 
Msg Score: -1
 -1 Message Not Useful
#154197 - C code for your keypad
Responding to: ???'s previous message
#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
}


List of 11 messages in thread
TopicAuthorDate
4x4 keypad            01/01/70 00:00      
   Which part are you stuck with?            01/01/70 00:00      
      4x4 keypad.            01/01/70 00:00      
         Do you understand how the matrix works?            01/01/70 00:00      
         use matrix keypad & timer interrupt            01/01/70 00:00      
            code for your keyboard.            01/01/70 00:00      
   C code for your keypad            01/01/70 00:00      
      C code for your keypad - do not use            01/01/70 00:00      
         Eric, you must have good eyesight            01/01/70 00:00      
            naah, I spotted the basic error immediately, ...            01/01/70 00:00      
      How to post source code            01/01/70 00:00      

Back to Subject List