Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronic 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.

Register Log in

Function for scaning the 4*4 keypad

Status
Not open for further replies.

mf1364

Full Member level 3
Joined
Dec 4, 2007
Messages
189
Helped
2
Reputation
4
Reaction score
2
Trophy points
1,298
Activity points
2,721
Hi every body
I want a Function for scanning the 4*4 key pad , i am using MicroC compiler . I know that there is function in microC library
but , I want to write my own please help me
 

ckshivaram

Advanced Member level 5
Joined
Apr 21, 2008
Messages
5,070
Helped
2,148
Reputation
4,302
Reaction score
2,088
Trophy points
1,393
Location
villingen (Germany) / Bangalore
Activity points
30,097

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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#include <18F452.h> 
#fuses EC_IO,NOWDT,NOPROTECT,BROWNOUT,PUT,NOLVP 
#use delay(clock=4000000)  
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS) 
 
//============================= 
 
//Keypad connection: 
#define row0 PIN_B4 
#define row1 PIN_B5 
#define row2 PIN_B6 
#define row3 PIN_B7 
#define col0 PIN_B0 
#define col1 PIN_B1 
#define col2 PIN_B2 
#define col3 PIN_B3 
 
// Keypad layout: 
char const KEYS[4][4] = 
{{'1','2','3','A'}, 
 {'4','5','6','B'}, 
 {'7','8','9','C'}, 
 {'*','0','#','D'}}; 
 
 
#define KBD_DEBOUNCE_FACTOR 33 // Set this number to apx n/333 where 
// n is the number of times you expect 
// to call kbd_getc each second 
 
void kbd_init() 
{ 
//set_tris_b(0xF0); 
//output_b(0xF0); 
port_b_pullups(true);  
} 
 
short int ALL_ROWS (void) 
{ 
if(input (row0) & input (row1) & input (row2) & input (row3)) 
   return (0); 
else 
   return (1); 
} 
 
 
 
char kbd_getc() 
{ 
static byte kbd_call_count; 
static short int kbd_down; 
static char last_key; 
static byte col; 
 
byte kchar; 
byte row; 
 
kchar='\0'; 
 
if(++kbd_call_count>KBD_DEBOUNCE_FACTOR) 
  { 
   switch (col) 
     { 
      case 0: 
        output_low(col0); 
        output_high(col1); 
        output_high(col2); 
        output_high(col3); 
        break; 
    
      case 1: 
        output_high(col0); 
        output_low(col1); 
        output_high(col2); 
        output_high(col3); 
        break; 
 
      case 2: 
        output_high(col0); 
        output_high(col1); 
        output_low(col2); 
        output_high(col3); 
        break; 
 
      case 3: 
        output_high(col0); 
        output_high(col1); 
        output_high(col2); 
        output_low(col3); 
        break; 
      } 
 
   if(kbd_down) 
     { 
      if(!ALL_ROWS()) 
        { 
         kbd_down=false; 
         kchar=last_key; 
         last_key='\0'; 
        } 
     } 
   else 
     { 
      if(ALL_ROWS()) 
        { 
         if(!input (row0)) 
            row=0; 
         else if(!input (row1)) 
            row=1; 
         else if(!input (row2)) 
            row=2; 
         else if(!input (row3)) 
            row=3; 
 
         last_key =KEYS[row][col]; 
         kbd_down = true; 
        } 
      else 
        { 
         ++col; 
         if(col==4) 
            col=0; 
        } 
     } 
   kbd_call_count=0; 
  } 
return(kchar); 
} 
 
//=========================== 
void main() 
{ 
char k; 
 
kbd_init(); 
 
printf("\r\Starting ..."); 
 
while(TRUE) 
  { 
   k=kbd_getc(); 
   if(k!=0) 
     { 
      if(k=='*') 
         printf("%c", '*'); 
      else 
         printf("%c", k); 
     } 
  } 
 
}

 
  • Like
Reactions: mf1364

    mf1364

    points: 2
    Helpful Answer Positive Rating

mf1364

Full Member level 3
Joined
Dec 4, 2007
Messages
189
Helped
2
Reputation
4
Reaction score
2
Trophy points
1,298
Activity points
2,721
Thank you very much Mr ckshivaram , but I am so amateur in c language so can you explain line by line and can you change this code for microC compiler because I am using the Micro C compiler, and I want to show output on a character Lcd so i dont need RS232 protocol by the way so thanks for your reply
 
Last edited:

Status
Not open for further replies.
Toggle Sidebar

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Top