Connect with us

Switch button reading function

Discussion in 'Microcontrollers, Programming and IoT' started by vead, Sep 4, 2017.

Scroll to continue with content
  1. vead


    Nov 27, 2011
    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
    void keypad_reading ()
     // logic //
    before going to write program, I want to understand logic. how do we read the inputs of four buttons manually ?
  2. Arouse1973

    Arouse1973 Adam

    Dec 18, 2013
    How do you plan on knowing which one is pressed and how many times? LEDs or something else?
  3. vead


    Nov 27, 2011
    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
  4. kellys_eye


    Jun 25, 2010
    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.
  5. vead


    Nov 27, 2011
    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
Ask a Question
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.
Electronics Point Logo
Continue to site
Quote of the day