Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[SOLVED] [help] on keypad Im facing some LOGIc problem..

Status
Not open for further replies.

romel_emperado

Advanced Member level 2
Joined
Jul 23, 2009
Messages
606
Helped
45
Reputation
132
Reaction score
65
Trophy points
1,318
Location
philippines
Activity points
6,061
hi all

I have problem with this keypad.. I know how to interface in MCu but I dont get the logic how do I make the keypad increement the value of x ONLY once even if we hold or press one button for a long time
in my example I use button 1 to increment the value of x but I cannot get the logic how..

see my example code... hehe.. pls help..



Code C++ - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#include <REG51.h>
 
#define KEYPAD P2
#define display P3
sbit key = P1^7;
void DelayMs(unsigned int) ;
unsigned char keypad[4][3]= { 1 , 2 , 3 ,
                                         4 , 5 , 6 ,
                        7 , 8 , 9 ,
                             15 , 0 , 15 } ;
 
unsigned char get_key()
{
      unsigned char colloc,rowloc;
 
    
        do
        {
            KEYPAD=0xF0;
            colloc=KEYPAD & 0xF0;
        }
    while(colloc ==0xF0);        // if any key pressed
    DelayMs(1);                  // some delay
    do
    {
    colloc=KEYPAD;
    colloc &=0xF0;
    }while(colloc==0xF0);       // to verify is really key pressed
    
        KEYPAD=0xFE;
        colloc=KEYPAD & 0xF0;
        if(colloc !=0xF0)
        {
            rowloc=0;
            goto next;
        }
 
        KEYPAD=0xFD;
        colloc=KEYPAD & 0xF0;
        if(colloc !=0xF0)
        {
            rowloc=1;
            goto next;
        }
        KEYPAD=0xFB;
        colloc=KEYPAD & 0xF0;
        if(colloc !=0xF0)
        {
            rowloc=2;
            goto next;
        }
                
        KEYPAD=0xF7;
        colloc=KEYPAD & 0xF0;
        rowloc=3;
        goto next;
 
next:                                                                               
if (colloc==0xE0)
{
display = (keypad[rowloc][0]);
return display;
}
else if(colloc==0xD0)
{
display=(keypad[rowloc][1]);
return display;
}
else
{
display=(keypad[rowloc][2]);
return display;
}
 
 
void main()
{
  char x;
  P1 = 0;
  while(1)
  {
    if(get_key() == 1)
    {
        x++;
        if(x==2)
        P1=1;
    }
  }
}
 
 
//---------------------------------------
// Delay mS function
//---------------------------------------
void DelayMs(unsigned int count) 
{  // mSec Delay 11.0592 Mhz 
    unsigned int i;                 
    while(count) {
        i = 115; 
        while(i>0) i--;
        count--;
    }
}

 

you have to first define the time to detect key press including de-bounce effect....
then count the number of time period your key was pressed and consider it as short key press or long key press and do the necessary operation....
 
hi ckshivaram,

I dont Understand the example code you posted. when I run it in proteus when I press 1 key it displays many things in the LCD... anyway, I'll try to solve this problem with my existing code.
 

hi ckshivaram

i have this my new practice code of 4x3 keypad.. this is how I scan..

I made all rows as input then the colums are set to law one by one so that I can detect which key is pressed..

can you add something for this code or a hint in which effective way of getting the pressed key?


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include <REG51.H>
 
#define keyport P2      //keypad connected to P2
#define col1 P2^4       //column 1
#define col2 P2^5       //column 2
 
 
//---------------------------------------
// Delay mS function
//---------------------------------------
void DelayMs(unsigned int count) 
{  // mSec Delay 11.0592 Mhz 
    unsigned int i;                 
    while(count) {
        i = 115; 
        while(i>0) i--;
        count--;
    }
}
 
void key_init(){
        keyport &=0xF7; //make Rows as o/p and cols are i/p
}
 
unsigned char get_key()
{
        unsigned char i,k,key=0;
        k=1;
 
        for(i=0;i<3;i++)
        {
          keyport &=~(0x01<<i);
//        DelayMs(400);
//
          if(keyport == 0xE3)
          return  keyport;
 
          if(keyport == 0xE5)
          return  keyport;
 
          keyport |= 0x01<<i;
//        DelayMs(400);
        
        }
 
}
 
 
void main()
{
   P1 =0x00;
  key_init();
 
  while(1)
  {
  
   if(get_key() == 0xE3)  //key 1 pressed
   P1 =0xff;
 
   if(get_key() == 0xE5)   //key 2 pressed
   P1 =0x00;
 
  }
}



---------- Post added at 10:48 ---------- Previous post was at 10:35 ----------

from that method above I just tested key1 to ON P1 and key 2 to OFF P2... it's working but I think there's a better way of doing that... any hint
 
Last edited:

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top