Maker Pro
Maker Pro

Switch button reading function

vead

Nov 27, 2011
473
Joined
Nov 27, 2011
Messages
473
Hello Everyone
I want to learn keypad interfacing programming. I am starting with very simple example. suppose keypad is made by arranging four push button. Than I have to find out which one switch has been pressed and how many time it has been pressed. how do we read the inputs of four buttons?
Embedded c , 8051 and Four Push buttons
Code:
void keypad_reading ()
{
 // logic //
}
before going to write program, I want to understand logic. how do we read the inputs of four buttons manually ?
 

Arouse1973

Adam
Dec 18, 2013
5,178
Joined
Dec 18, 2013
Messages
5,178
How do you plan on knowing which one is pressed and how many times? LEDs or something else?
Adam
 

vead

Nov 27, 2011
473
Joined
Nov 27, 2011
Messages
473
How do you plan on knowing which one is pressed and how many times? LEDs or something else?
Adam
I will use LCD display 16x2.

its like that , when we dial number on mobile, we press keypad buttons. if We press "1" we see number "1" on mobile screen. if We press "2" we see number "2" on mobile screen ....and so on. display will show only four digits 1 to 4
digits xxxx
it can be any four numbers like 1234, 4321, 2413,...and so on
 

kellys_eye

Jun 25, 2010
6,514
Joined
Jun 25, 2010
Messages
6,514
First determine how the keypresses will result in a signal being generated.

You can use pull-up resistors on each switch and read their logic level, detecting a 'low' when a key is pressed. This would mean a routine to continually scan the input ports and branch out for a detected keypress.

Alternatively you could arrange for the keyboard to generate an interrupt so the main program can be 'off doing something else' until a key press generates the interrupt and you branch off to deal with it.

Then there is a multiplex arrangement, usually used when there are more buttons than ports available to read them - this involves sending a logic 'high' to each row of the keyboard matrix (assuming an x-y arrangement) and looking for the appropriate signal on the column lines.

But each keypress will need a 'debounce' routine to prevent multiple instances being recorded as the contacts 'bounce' when a key is pressed - happens to all buttons and it is standard practise to 'debounce' the signal.
 

vead

Nov 27, 2011
473
Joined
Nov 27, 2011
Messages
473
First determine how the keypresses will result in a signal being generated.

You can use pull-up resistors on each switch and read their logic level, detecting a 'low' when a key is pressed. This would mean a routine to continually scan the input ports and branch out for a detected keypress.

Alternatively you could arrange for the keyboard to generate an interrupt so the main program can be 'off doing something else' until a key press generates the interrupt and you branch off to deal with it.

Then there is a multiplex arrangement, usually used when there are more buttons than ports available to read them - this involves sending a logic 'high' to each row of the keyboard matrix (assuming an x-y arrangement) and looking for the appropriate signal on the column lines.

But each keypress will need a 'debounce' routine to prevent multiple instances being recorded as the contacts 'bounce' when a key is pressed - happens to all buttons and it is standard practise to 'debounce' the signal.
Sorry but I am too long to write program. I am just looking for logical explanation. Than I will try to implement that logic with programming

something like this
1. If switch is not pressed, return switch not pressed
2. if switch is pressed, wait for denounce period
than if switch is no longer pressed, return switch not pressed and if switch pressed return switch is pressed
 
Top