C code to scan keypad

Go To Last Post
13 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi,
I'm a student and i'm working on a project. I want to use a 4x4 keypad but the only language i know pretty well is C. I'm using CodeVision. I've found an example (AVR240) of the connection and scanning of such a keypad but i can't change it according to my needs cause i don't know assembly. Is there an example or a project in C.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

This is a project from Academia

Attachment(s): 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

A keypad matrix is organized in rows and columns, and the basic principle is to set all lines of one row to an active state, followed by a read of the columns after that. If a key on that row is pressed, then you'll see an active state on the corresponding column. If not, proceed to the next row. All you have to do in advance is to prepare a proper column state when no key is pressed, just to make sure you don't read a false active state. This usually done by some high-value resistor tied to ground if you're working with active-high rows.

Note that you can freely exchange rows and columns in the above explanation. You tie all your row lines to some pins declared as outputs of the micro, while the columns are connected to pins declared as inputs.

That's it! No need to search/ask for readily available solutions, just implement that algorithm above!

-- Thilo

Einstein was right: "Two things are unlimited: the universe and the human stupidity. But i'm not quite sure about the former..."

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Written for GCC, but easily convertable. Can only interprete ONE key at a time - multiple keys can lead to ghosting. Use 1K resistors in series on all the port pins for protection.

- Dean :twisted:

Attachment(s): 

Make Atmel Studio better with my free extensions. Open source and feedback welcome!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Wouldn't it be great if this was in the 'examples' directory? Wouldn't it be great if there WAS an 'examples' directory?

Imagecraft compiler user

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

CodeVision has an example (in C of course) in cvavr\examples\keypad folder.
So, it is already done! :-)

Real men don't use backups, they post their stuff on a public ftp server and let the rest of the world make copies.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I think Thilo's response is much more useful for students rather than just handing everything to them on a plate. I'm sure most of these students could actually work it out for themselves if they sat down for 5-20 minutes to think about it. (Actual time depends on how much they drank and smoked on the weekend).

Dave.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I like the trick to 'check for ANY key pressed real fast'

Imagecraft compiler user

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

bobgardner wrote:
I like the trick to 'check for ANY key pressed real fast'

Hi Bob! Can you elaborate, please?

Real men don't use backups, they post their stuff on a public ftp server and let the rest of the world make copies.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Turn on all 4 outputs, read the port with the 4 inputs, anything other than 'pulled up' means something is pressed. Call regular 'scan for key' now to find out what.

Imagecraft compiler user

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

bobgardner wrote:
Turn on all 4 outputs, read the port with the 4 inputs, anything other than 'pulled up' means something is pressed. Call regular 'scan for key' now to find out what.

Oh, yeah...
Or even better: connect all 4 inputs (with a diode for each) on ext. int. pin.
Quite fast with ISR. But polling will do also (using timer or not).
Of course, if you can afford extra HW (4 diodes) and waste an AVR pin...

Real men don't use backups, they post their stuff on a public ftp server and let the rest of the world make copies.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:
Or even better: connect all 4 inputs (with a diode for each) on ext. int. pin

Or, on the newer parts with "interrupt on pin change" you don't need an external interrupt if you're already feeding the row or column outputs directly to the AVR.

There is an Application Note showing how to connect a 3x4 matrix keypad using only 3 I/O lines. It does, of course, require additional external components. The interface code is in Basic but someone that knows C should be able to read it without too much trouble and get the gist of what's going on.

I have also worked out a way to connect a 4x4 keypad using only 1 external component - a PCF8574 I/O expander. If you already have other I2C devices connected the net cost is zero I/O lines.

Don Kinzer
ZBasic Microcontrollers
http://www.zbasic.net

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Also, see Atmel applictaion note AVR240....for 4*4 solution as Bob describes.
It's fairly easy.

Include a switch debounce mechanism...e.g. check swith is pressed (stable) for 0.1 sec.