Hex Keypad

Adding a hex keypad

The goal was to add my first input device so wanted to make it as simple as possible.  These matrix keypads cost a few pounds and require only 8 connections:












One option I considered was to decode the key presses in software using all 8 pins of a 6522 VIA but I chose to use a 74LS922 16 key decoder IC.  These provide a number of useful features in a single cheap package:
  • internal clock
  • pull-ups
  • key debounce
  • 2 key roll over
  • OE pin 
  • Data available output

Objectives

  1. Provide a simple input capability to enable the very first method to control the system
  2. Understand how a key pad switching is arranged and how the matrix decoding works
  3. Experiment with practical aspects - scan rate and debouncing
  4. Use interrupts to detect new key presses

Approach

I used a MM74C922 16 key decoder because it does a lot of basic work that would otherwise need to be done with software.  My assembler is not good enough to figure all that out!

The 4 rows and 4 columns of the keypad matrix connect to 8 pins of the decoder IC.

The decoder has a built in clock and only needs a single capacitor to set the frequency - 100nF gave me 700Hz.  This sets the rate of key scanning.  Debouncing is configurable with a capacitor - the data sheet suggests 1uF and I found this worked fine so left it at that.

The 74C922 OE line is tied to ground to to keep the keypad active.

When a key is pressed the Data Available line goes high but it falls as soon as the key is released. The key value is latched on the output lines though.  This positive pulse is detected by CA1 on the 6522 VIA (configured for a positive edge).  When CA1 goes high PortA interrupt flag is set and the ISR then run and loads the value on PortA (lower 4 bits).

Setup

The 6522 VIA setup is straightforward:
  • DDRA = 000000 setting all pins to input
  • ACR    = bit 0 set to 0 so PA does not latch
  • PCR     = bit 0 set to 1 so CA1 responds to a positive pulse
  • IER      = %100000010 bit 7 set so any bit 0 - 6 set will enable that interrupt.  CA1 is bit 1
LCD is configured for 8 bit mode, 2 lines, no cursor and no display shift.  See post 'LCD' for details of the rather complicated setup or just copy this one!

Assembler Code

A working demonstration is here:

https://gist.github.com/robinharris/a02cf867ebecbeec0fc1001e4acddb69

Most of the lines are to initialise the 6522 VIA and the LCD display.  There are subroutines to check the busy flag and to provide delays.

After initialising, the program enters an endless loop waiting for interrupts.  When a key is pressed CA1 detects the positive pulse and sets the PortA interrupt flag and takes IRQ low.  The 6502 responds by starting the ISR.  First PortA is loaded and stored, resetting the interrupt flag.  Then the cursor position on the LCD is then checked and wrap around provided if required.

The 74C922 decoder provides a 4 bit value representing the key pressed (0 - F going from top left to bottom right).  This value is used to index into a set of characters and the ASCII value sent to the LCD.

Conclusions

  1. the 74C922 provides very useful features that simplifies the integration of a key pad
  2. a keypad can provide reasonable scope for responding to menus and driving branches in programs
  3. obvious maybe, but confirmed the VIA interrupts and handshaking works as expected

Parts List

  • 16 key membrane keypad
  • MM74C922 16 key decoder
  • 100nF capacitor
  • 1uF capacitor
Main system consist of 65C02, 6522 VIA, ROM, RAM and 16 x 2 LCD module together with address decoding logic.





Comments

  1. Loved it. Inspired me to bring my homebrew computer blog up to date. And the old-school decoding scheme and interfacing with microprocessor system - delightful!

    ReplyDelete
    Replies
    1. Thanks Marek - pleased you are motivated to do a bit more. It was reading about other projects that got me started and I'm really enjoying it. I'm learning the basics as I experiment so all my stuff is about as simple as it can be. Have fun!

      Delete
    2. Thanks for this write-up Robin! Looks like a promising chip, just got one to add a hex keypad to my own Ben Eater-style 6502 breadboard computer. I suspect I'll run out of VIA pins though, as I'll need at least 4 more buttons for stop / go / up & down in the monitor I'm trying to write.

      Delete
    3. Thanks for commenting Giles; this project continues to provide me with new challenges and learning opportunities. I make notes here knowing that it will make me keep adequate records and indeed I had to refer to this post recently to write some new monitor code! Pleased you found it interesting.

      I used a keypad decoder to keep the pin count on the VIA low.

      Delete
    4. Thanks for responding Robin! I have a strict Ben Eater-design project and I'm having problems adding the hex keypad, blundering in the dark a bit as a newbie! Can you explain what addresses $8000-1 are physically connected to in your project? As far as I can see Ben just uses Port A & B to send data direct to the LCD, I'm confused what you use these addresses for:
      LCDD = $8001 ; address for LCD data
      LCDC = $8000 ; address for LCD commands
      best wishes
      Giles

      Delete
    5. Hi Giles, I deviated from Ben's approach with my LCD and created some puzzles. Ben's approach is probably more reliable but uses more of the 6522 port lines.

      Address range $8000 to $800F all decode to the LCD E pin which needs to be high to activate it. Then address line A0 connects to the LCD RS pin - 0 selects the Command register and 1 selects the data register. The LCD data pins are connects to the 6502 data bus. Does that help?

      Remember that I have taken a different approach than Ben for interfacing the LCD and am connecting its data bus to the main system data bus rather than to the 6522. With hindsight I can see why Ben chose his route!

      Delete
    6. Thank you, Robin! Ingenious idea, that makes sense now. I agree Ben uses a lot of precious 6522 pins. I was considering switching the LCD to 4-bit mode but I imagine that'll be a pig to program.

      Delete
    7. Thank you again Robin - I have had some success this weekend with the hex keypad on a pure Ben Eater design: http://www.suppertime.co.uk/blogmywiki/2021/03/6502-breadboard-computer-part-5-hex-keypad/

      Delete
  2. Thanks for the link to your blog which I have been browsing. Awesome - you have an easy to read style of writing which gets the interesting stuff over without clutter. That is a rare talent - I shall be following keenly as our areas of interest overlap hugely.

    My project was suspended for the second half of 2020 while we moved house but gradually available time is returning. Other than the hex keypad I would recommend investigating a 6551 ACIA for serial communication. When I got this working it transformed my system because then I could fully interact via my Mac.

    This lead to a need for a good monitor so I ended up using a modified version of WOZMON that includes binary file transfer. Suddenly software development was 100 times faster.

    If you would like to bounce ideas and problems between us please email me at robin@g4giy.uk. I'm always pleased to hook up with a fellow enthusiast travelling down a similar path. I'm no expert though but I do enjoy blundering around in the dark looking for the light switch :-)

    ReplyDelete
    Replies
    1. Thank you Robin, I will certainly be keeping in touch. I've so much to learn and your projects may help me expand the horizons of my own project beyond a simple self-contained machine code training machine.

      Delete
    2. Hi again! I made a fair bit of progress this week and got a (very simple) monitor program working: http://www.suppertime.co.uk/blogmywiki/2021/03/6502-breadboard-computer-part-7-working-monitor/ - managed to enter and run my own first working program.

      Delete
    3. Thanks for keeping me updated. Been following your progress on your blog - excellent stuff. Liked the way you discovered about pull ups and pull downs. That is the method I use for learning -I make lots of mistakes!

      I'm wondering where to go with my project. I've got a monitor running via serial but I quite like the idea of using a your keypad arrangement and the LCD to make a really minimalistic working computer.

      Delete
  3. This comment has been removed by a blog administrator.

    ReplyDelete

Post a Comment