1. Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.
    Dismiss Notice

reading a 4x4 switch matrix

Discussion in 'Microcontrollers' started by MrDEB, Mar 5, 2018.

  1. MrDEB

    MrDEB Well-Known Member

    Joined:
    Apr 16, 2007
    Messages:
    4,600
    Likes:
    25
    You guys crack me up.
    I realize the diodes are not needed but it makes a better mouse trap .
    I understand the module after I followed the code flow and the need for the diodes but why not include them. It does make a better key pad.
    I admit I tend to jump the gun and assimilate bits and pieces then ask why.
    That's why I assemble bits of code, test that snippet then AFTER all the snippets work as planned, I put the entire puzzle together into one package.
    The keypad is the first snippet then get the LCD working then hopefully get both to play together nice.
     
  2. be80be

    be80be Well-Known Member

    Joined:
    Aug 23, 2008
    Messages:
    5,199
    Likes:
    159
    Location:
    morristown,tn
    ONLINE
    You don't have the trap right tho

    Your switches and diodes wrong

    Screenshot from 2018-03-06 17-27-00.png
     
  3. be80be

    be80be Well-Known Member

    Joined:
    Aug 23, 2008
    Messages:
    5,199
    Likes:
    159
    Location:
    morristown,tn
    ONLINE
    It's like this
    this one .png
     
  4. dave miyares

    Dave New Member

    Joined:
    Jan 12, 1997
    Messages:
    2
    Likes:
    -10
  5. Pommie

    Pommie Well-Known Member Most Helpful Member

    Joined:
    Mar 18, 2005
    Messages:
    10,723
    Likes:
    422
    Location:
    Brisbane Australia
    Burt, the switches and diodes should be in series. They should not be in the column lines.

    Mike.
     
  6. be80be

    be80be Well-Known Member

    Joined:
    Aug 23, 2008
    Messages:
    5,199
    Likes:
    159
    Location:
    morristown,tn
    ONLINE
    Mike that how they show it I tried it once maybe 8 years ago

    It's there drawing not mine It's only 8 buttons I added a set It did work I still got the keypad I made.
     
  7. be80be

    be80be Well-Known Member

    Joined:
    Aug 23, 2008
    Messages:
    5,199
    Likes:
    159
    Location:
    morristown,tn
    ONLINE
    Here you go
    Screenshot from 2018-03-06 18-26-22.png

    But I didn't make that one it's not complete.
     
  8. dave miyares

    Dave New Member

    Joined:
    Jan 12, 1997
    Messages:
    2
    Likes:
    -10
  9. JonSea

    JonSea Well-Known Member

    Joined:
    Oct 1, 2012
    Messages:
    1,642
    Likes:
    145
    Location:
    Seattle, WA
    No. More, unnecessary parts do not make a "better mouse trap", particularly where it is likely that they won't be added correctly. Sorry MrDEB, your history here attests to that fact.

    MrDEB is trying to make a calculator, to add up domino values. As such, there is no reason multiple keys will be pressed at the same time and therefore, no problem with ghosting.

    Don't make it more difficult than it needs to be, especially when doing so enhances nothing.
     
  10. be80be

    be80be Well-Known Member

    Joined:
    Aug 23, 2008
    Messages:
    5,199
    Likes:
    159
    Location:
    morristown,tn
    ONLINE
    Here is with the diodes
    Screenshot from 2018-03-06 20-22-59.png
     
  11. MrDEB

    MrDEB Well-Known Member

    Joined:
    Apr 16, 2007
    Messages:
    4,600
    Likes:
    25
    After reviewing all the suggestions and having no need to order smd diodes I have decided to forgo using them.
    Might regret but??
     
  12. JonSea

    JonSea Well-Known Member

    Joined:
    Oct 1, 2012
    Messages:
    1,642
    Likes:
    145
    Location:
    Seattle, WA
    Mike, what do you think? Over 12 on this one?
     
  13. be80be

    be80be Well-Known Member

    Joined:
    Aug 23, 2008
    Messages:
    5,199
    Likes:
    159
    Location:
    morristown,tn
    ONLINE
    Well if he had read the whole post that he linked the diodes don't make up for bad coding. I tried them out didn't really make anything better that was using the keypad bas
     
  14. JonSea

    JonSea Well-Known Member

    Joined:
    Oct 1, 2012
    Messages:
    1,642
    Likes:
    145
    Location:
    Seattle, WA
    They don't make for bad coding, if they are installed correctly. They add nothing for this app and increase the likelihood of errors substantially.

    With about 10 minutes of effort, I came up with this program, which reads keys as expected.

    Code (text):


    Include "keypad16pullup.bas"
    Include "usart.bas"
    Include "convert.bas"

    Dim Keypressed As Byte
    Dim Keyvalue As Byte
    SetBaudrate(br9600)

    DelayMS(5000)

    USART.Write ("Test program",13, 10, 10)

    While 1 = 1
         Keypressed = keypad16.Value
         If Keypressed <> 0 Then
             USART.Write ("Key pressed = ", HexToStr(Keypressed), 13, 10)
          End If
    Wend

     
    The output looks like this. It prints out a value as long as a key is pressed.

    Code (text):



    Key pressed = 1
    Key pressed = 1
    Key pressed = 1
    Key pressed = 1
    Key pressed = 1
    Key pressed = 1
    Key pressed = 2
    Key pressed = 2
    Key pressed = 2
    Key pressed = 2
    Key pressed = 2
    Key pressed = 2
    Key pressed = 2
    Key pressed = 2
    Key pressed = 2
    Key pressed = 1
    Key pressed = 1
    Key pressed = 1
    Key pressed = 1
    Key pressed = 1
    Key pressed = 1
    Key pressed = 1
    Key pressed = 2
    Key pressed = 2
    Key pressed = 2
    Key pressed = 2
    Key pressed = 2
    Key pressed = 2
    Key pressed = 2
    Key pressed = 2
    Key pressed = 2
    Key pressed = 2
    Key pressed = 3
    Key pressed = 3
    Key pressed = 3
    Key pressed = 3
    Key pressed = 3
    Key pressed = 3
    Key pressed = 3
    Key pressed = 3
    Key pressed = 3
    Key pressed = 3
    Key pressed = 3
    Key pressed = 4
    Key pressed = 4
    Key pressed = 4
    Key pressed = 4
    Key pressed = 4
    Key pressed = 4
    Key pressed = 4
    Key pressed = 4
    Key pressed = 4
    Key pressed = 4
    Key pressed = 4

     

    Yes, a little logic is required to return a single value when a key is pressed. A trivial amount.
     
  15. be80be

    be80be Well-Known Member

    Joined:
    Aug 23, 2008
    Messages:
    5,199
    Likes:
    159
    Location:
    morristown,tn
    ONLINE
    Jon I don't think you read what I said.

    I said they don't make up for bad coding.

    And how I showed them is the right way it straight from the page

    I don't understand where mike thinks there wrong
     
  16. JonSea

    JonSea Well-Known Member

    Joined:
    Oct 1, 2012
    Messages:
    1,642
    Likes:
    145
    Location:
    Seattle, WA
    Burt, I haven't really looked at where the diodes should go. I suspect there are different methods. But since they aren't necessary for this application, they can't improve anything; they can only screw things up. So why have them?

    Here's a picture of the keypad I used. The 4×4 matrix is completely isolated from the other switches and LEDs. Nothing but 16 switches comnected to rows and columns. And NO DIODES.

    As far as telling when a new switch is pressed, look for a value of 0. That indicates no switch is pressed.

    20180306_205626-1243x919.jpg
     
  17. Pommie

    Pommie Well-Known Member Most Helpful Member

    Joined:
    Mar 18, 2005
    Messages:
    10,723
    Likes:
    422
    Location:
    Brisbane Australia
    Burt,
    What you and MrDEB posted both had the diodes in the same place - in series with the push buttons. When you said they were wrong I mistook the green arrow in your diagram as a diode.

    Jon, 17.

    Mike.
     
  18. JonSea

    JonSea Well-Known Member

    Joined:
    Oct 1, 2012
    Messages:
    1,642
    Likes:
    145
    Location:
    Seattle, WA
    Mike, I think diodes are going to add 5 by themselves!

    It's weird to me that someone who won't spend 27 cents to use a crystal and who is complaining how crowded his board is would be so reluctant to not include diodes where they aren't needed. Such are the mysteries of life.
     
  19. be80be

    be80be Well-Known Member

    Joined:
    Aug 23, 2008
    Messages:
    5,199
    Likes:
    159
    Location:
    morristown,tn
    ONLINE
    The diodes are wrong there not going to fix the problem and your right i looked at the link he posted he has them drawn different.
    But its the same but i was more thinking adding them. Not going to help lol
     
  20. JonSea

    JonSea Well-Known Member

    Joined:
    Oct 1, 2012
    Messages:
    1,642
    Likes:
    145
    Location:
    Seattle, WA
    Here's a slight addition to my code to read each keypress only once until the key has been released. Holding the key down does not result in another keypress being recorded. I am printing out the hex value of each key, pressed in order. For whatever reason, $10 comes out of order after $F. I'm not sure if this is a problem in the keypad module or in the keypad hardware. At any rate, this won't be a problem for MrDEB, as he needs to map the keypress value against what he wants for the value of each key.

    This is still a tiny amount of code to read the keypad.

    Code (text):


    Include "keypad16pullup.bas"
    Include "usart.bas"
    Include "convert.bas"

    Dim Keypressed As Byte
    Dim Keyvalue As Byte

    SetBaudrate(br9600)

    DelayMS(5000)

    USART.Write ("Test program",13, 10, 10)


     keypressed = 0
    While 1 = 1
       
         while keypressed = 0            'print key and exit when key pressed
            Keypressed = keypad16.Value
            If Keypressed <> 0 Then
                USART.Write ("Key pressed = ", HexToStr(Keypressed), 13, 10)
            End If
        wend
       
        while keypressed <> 0                'loop until key not pressed
            Keypressed = keypad16.Value
        wend  
    wend

     
    The output of pressing each key in turn looks like this. It doesn't matter if a key is pressed and held down. It will be recorded only once.

    Code (text):


    Test program
    Key pressed = 1
    Key pressed = 2
    Key pressed = 3
    Key pressed = 4
    Key pressed = 5
    Key pressed = 6
    Key pressed = 7
    Key pressed = 8
    Key pressed = 9
    Key pressed = A
    Key pressed = B
    Key pressed = C
    Key pressed = D
    Key pressed = E
    Key pressed = F
    Key pressed = 10
    Key pressed = 1
    Key pressed = 2
    Key pressed = 3
    Key pressed = 4
    Key pressed = 5
    Key pressed = 6
    Key pressed = 7
    Key pressed = 8
    Key pressed = 9
    Key pressed = A
    Key pressed = B
    Key pressed = C
    Key pressed = D
    Key pressed = E
    Key pressed = F
    Key pressed = 10

     
     
  21. tumbleweed

    tumbleweed Active Member

    Joined:
    Jan 25, 2007
    Messages:
    135
    Likes:
    36
    That looks right to me... the function returns 1-16 for a keypress, so the last few would be 14, 15, 16 (or $0E, $0F, $10)
     
    • Like Like x 1
  22. JonSea

    JonSea Well-Known Member

    Joined:
    Oct 1, 2012
    Messages:
    1,642
    Likes:
    145
    Location:
    Seattle, WA
    d'oh! You can see how much I work with hex! Of course A comes after 9. It was late, early, I was hungry....pick a good excuse ;)
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice