# How to write program

Nov 27, 2011
473
I am trying to write keypad program, I work with 8051 and keil compiler so I did google search and I found so many links . I have read following three links but still I am confuse because the program has been written differently in all the three links and I don't understand how to write proficient program for keypad interfacing.

I don't want to just copy paste the program.I want to write program by proper understanding. I have seen the keypad datasheet and I understand the basic logic but I don't understand how to start to writing program.

Can anyone guide me to write program. If i make program into small parts then which one part's should I make first and then later.

#### Harald Kapp

##### Moderator
Moderator
Nov 17, 2011
12,615
program has been written differently in all the three links
... and when you find more links you'll likely find many more different implementation. Apart from hardware specifics (e.g. port adresses) that need to be adapted to the actual hardware there are so many ways of doing things. Think of your thread on loops. How many different ways do you know for implementing a loop?

When it comes to keyboards the essential issue is pincount. In theory you could use one input pin per key. A typical full size keyboard has around 110 keys. Easy to see how impractical it is to assign each key its own pin.
Arranged in a matrix this number of keys can be handled by 10*11 pins. The idea is:
• use 11 pins as output drivers.
• use 10 pins as inputs.
• arrange the inputs and outputs in a matrix like fashion (cf. your links).
• arrange the keys such that at each intersection of a row and a column of the matrix sits one key (diodes are sometimes used for decoupling to avoid crosstalk, this is of no importance for the moment)
The matrix is handled as follows:
• activate one output only (1-out-of-11), deactivate the others.
• scan all inputs (10 in all) and look for an active signal.
• If you fins an active signal, then the key at the intersection of the active output and input (row and column) is pressed.
Repeat for all outputs, then restart with the first output. This is called "scanning a keyboard matrix".

Whether you use for loops, while loops, goto or whatever element of the programming language thet allows you to perform this repeated operation is mostly a matter of taste and programming style.

Don't forget to include debouncing of the keys.

Nov 27, 2011
473
.
Whether you use for loops, while loops, goto or whatever
Don't forget to include debouncing of the keys.
Many many thanks for helping me I understand the loops, while loops, if else statements but the problem is that when I am trying to write program I am facing problems.

If 8 pins of matrix keypad connected to port P2 of 8051
Now I know how does matrix keypad connected to 8051.I don't know how to mention in program that 8 pins of matrix keypad connected to port P2 of 8051 What we do, we just define the port
Code:
#include<reg51.h>
#define keyport P2
do we write like this
Code:
#include<reg51.h >   //Header file for 8051
#define column P2_0     //column 1
#define column P2_1     //column 2
#define column P2_2     //column 3
#define column P2_3     //column 4
#define row P2_4     //row 1
#define row P2_5     //row 2
#define row P2_6     //row 3
#define row P2_6     //row 4

do we set row and column like this
Code:
//Keypad Connections
#include<reg51.h >   //Header file for 8051
sbit column1 = P2^0;
sbit column2 = P2^1;
sbit column3 = P2^2;
sbit column4 = P2^3;
sbit row1 = P2^4;
sbit row2 = P2^5;
sbit row3 = P2^6;
sbit row4 = P2^7;

I am explaining handy program for the switch debouncing
Code:
/* Debounce time */
void Debounce(unsigned int i)
{
for (i =0; i < 2000; i++);
{

}
}
if(switch == 0)
{
// wait 20 ms time  //
Debounce(20);

// check switch again //
if(Switch == 0)

{

}
This is my best attempt to start writing program. I am so confuse because of different type of explanations. I really don't know what to do, I hope you will point out me in right direction

Last edited:

#### Harald Kapp

##### Moderator
Moderator
Nov 17, 2011
12,615
I'm sorry I can't help you with the particulars of the C51 and the libraries you're using.

Nov 27, 2011
473
I'm sorry I can't help you with the particulars of the C51 and the libraries you're using.
I am only familiar with 8051 and arm cortex m0 micro-controller. If possible can you help me to make common program for keypad interfacing like handy program

#### Harald Kapp

##### Moderator
Moderator
Nov 17, 2011
12,615
I'm sorry, that's outside my domain of knowledge.

Replies
3
Views
1K
Replies
1
Views
2K
Replies
1
Views
1K
Replies
1
Views
914
Replies
0
Views
867