Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

4x3 Matrix Keypad

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 10

43 Matrix Keypad Interface AVR Tutorial

Posted By Avinash On October 13th, 2010 09:46 AM. Under AVR Tutorials

Share on facebookShare on twitterShare on deliciousShare on diggShare on stumbleuponShare on redditShare on emailMore Sharing Services
Many application requires large number of keys connected to a computing system. Example includes a PC keyboard, Cell Phone keypad and Calculators. If we connect a single key to MCU, we just connect it directly to i/o line. But we cannot connect, say 10 or 100 keys directly MCUs i/o. Because :-

It will eat up precious i/o line. MCU to Keypad interface will contain lots of wires.

Buy Matrix Keypad


We want to avoid all these troubles so we use some clever technique. The technique is called multiplexed matrix keypad. In this technique keys are connected in a matrix (row/column) style as shown below.

Matrix Keypad Basic Connection


The rows R0 to R3 are connected to Input lines of Microcontroller. The i/o pins where they are connected are made Input. This is done by setting the proper DDR Register in AVR and TRIS Register in PIC. The column C0 to C3 are also connected to MCUs i/o line. These are kept at High Impedance State (AKA input), in high z state (z= impedance) state these pins are neither HIGH or LOW they are in TRISTATE. And in their PORT value we set them all as low, so as soon as we change their DDR bit to 1 they become output with value LOW.

One by One we make each Column LOW (from high Z state) and read state of R0 to R3.

Column 0 Selected

As you can see in the image above C0 is made LOW while all other Columns are in HIGH Z State. We can read the Value of R0 to R3 to get their pressed status. If they are high the button is NOT pressed. As we have enabled internal pullups on them, these pullups keep their value high when they are floating (that means NOT connected to anything). But when a key is pressed it is connected to LOW line from the column thus making it LOW.

After that we make the C0 High Z again and make C1 LOW. And read R0 to R3 again. This gives us status of the second column of keys. Similarly we scan all columns.

Column 1 Selected
How to Do it All with AVRs
Each i/o port in AVR has three related registers PORTx, DDRx and PINx. For example port A has

PORTA Port Driver - when any bit is set to 1 it appears as HIGH i.e. 5v . But this is the case only if that bit is OUTPUT. If it is input, setting any bit to 1 enables the internal pullup on that bit. DDRA DATA DIRECTION REGISTER - Make any pin on than port as IN or OUT. When bit is 1 it represents Output. When bit is 0 it represents Input. Input state is also called tristate or high Z state. PINA - Read it to get the level (HIGH or LOW) at the actual i/o pin. It is read when the pin is made input.

So now you know

How to make any i/o line Input(high Z) or Output. How to enable internal pullup register on input lines. How to read value that is present on input lines.

Please see the following tutorial for more clarification.

http://extremeelectronics.co.in/avr-tutorials/part-v-digital-io-in-avrs/

Why we make other Columns High Impedance while one column is made LOW?
Lets say we selected column number C0, so we make it LOW(i.e. GND or logic 0), in the same time we make all other columns high impedance (i.e. input). If we don't make other lines high impedance (tristate or Input) they are in output mode. And in output mode they must be either LOW(GND or logic 0) or HIGH (5v or logic 1). We can't make other lines LOW as we can select only one line at a time and C0 is already low as per assumption. So the only other possible state is all other columns are HIGH. This is shown in figure below. Red colour on column indicate high state while green is for low state.

Wrong Way!
Suppose at that time the user presses KEY0 and KEY1 simultaneously as shown below.

Short Circuit !
As you can see clearly that it create a short between C0 (GND) and C1 (5v), this will burn out the buffer of the MCU immediately!

Short!
That's why all other columns are kept at tristate(neither LOW nor HIGH) but very high input impedance that prevent either source or sink of current from them. So if we kept C1 at high impedance state it wont allow current to flow to GND on C0.

avr-gcc C code for 4x3 matrix keypad


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 Hardware -------ATmega32 @ 16MHz external crystal. Fuse Byte setting HIGH = C9 and LOW = FF (MOST IMP.) General Skills Required >> AVR Studio Setup and use. (http://bit.ly/aZ43SZ) >> avr-gcc setup and use. Specific Skills Required >> AVR GPIO details.(http://bit.ly/aq3ouw) >> LCD Library.(http://bit.ly/agVUVc) >> Operations on bits using C.(http://bit.ly/aFqg5n) Program to learn the use of Multiplexed 4x3 keypad with AVR Microcontroller. /******************************************************************************

22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66

LCD

<->

AVR Connection

VSS ->GND VDD ->+5V VEE -> CENTER PIN OF 10K POT (OTHER TWO PIN OF POT TO +5V AND GND) ADJ. THE POT UNTIL YOU HAVE A CLEAR TEXT DISPLAY.

RS -> PD3 RW -> PD6 E -> PB4

DB0 -> N/C DB1 -> N/C DB2 -> N/C DB3 -> N/C

DB4 -> PB0 DB5 -> PB1 DB6 -> PB2 DB7 -> PB3

LED+ ->+5V (VIA 100 OHM RES) LED- ->GND

KEYPAD

COL1 -> COL2 -> COL3 ->

PA6 PA5 PA4

ROW1 ->

PA3

ROW2 -> PA2 ROW3 -> PA1

ROW4 -> PA0

NOTICE -------NO PART OF THIS WORK CAN BE COPIED, DISTRIBUTED OR PUBLISHED WITHOUT A WRITTEN PERMISSION FROM EXTREME ELECTRONICS INDIA. THE LIBRARY, NOR ANY PART OF IT CAN BE USED IN COMMERCIAL APPLICATIONS. IT IS INTENDED TO BE USED FOR HOBBY, LEARNING AND EDUCATIONAL PURPOSE ONLY. IF YOU WANT TO USE THEM IN COMMERCIAL APPLICATION PLEASE WRITE TO THE AUTHOR.

WRITTEN BY:

67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111

AVINASH GUPTA me@avinashgupta.com

******************************************************************************/

#include <avr/io.h> #include <util/delay.h>

#include "lcd.h" #include "myutils.h"

#define KEYPAD A

//KEYPAD IS ATTACHED ON PORTA

//Don't Touch the lines below //******************************* #define KEYPAD_PORT PORT(KEYPAD) #define KEYPAD_DDR #define KEYPAD_PIN DDR(KEYPAD) PIN(KEYPAD)

//*******************************

/*******************************************

Function return the keycode of keypressed on the Keypad. Keys are numbered as follows

[00] [01] [02] [03] [04] [05] [06] [07] [08] [09] [10] [11]

Arguments: None

Return: Any number between 0-11 depending on keypressed.

255 (hex 0xFF) if NO keypressed.

Precondition: None. Can be called without any setup.

112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154

*******************************************/ uint8_t GetKeyPressed() { uint8_t r,c;

KEYPAD_PORT|= 0X0F;

for(c=0;c<3;c++) { KEYPAD_DDR&=~(0X7F);

KEYPAD_DDR|=(0X40>>c); for(r=0;r<4;r++) { if(!(KEYPAD_PIN & (0X08>>r))) { return (r*3+c); } } }

return 0XFF;//Indicate No key pressed }

void main() { //Wait for LCD To Start _delay_loop_2(0);

//Now initialize the module LCDInit(LS_NONE);

uint8_t key;

while(1) { key=GetKeyPressed(); //Get the keycode of pressed key

LCDWriteIntXY(0,0,key,3); }

//Print it at location 0,0 on LCD.

The above code make use of the LCD Library. You can get more information on LCD Library here :-

http://extremeelectronics.co.in/avr-tutorials/using-lcd-module-with-avrs/

Hardware for 4x3 Matrix Keypad and AVR interface.


The test circuit will be built around ATmega32 microcontroller. The output device will be a 16x2 lcd module. So we set up a basic ATmega32 circuit. The circuit will have the following :-

1. 2. 3. 4. 5. 6. 7.

ATmega32 MCU 16MHz Crystal Reset Circuit. 5v Power Supply Circuit. ISP (For programming) LCD Module. LCD Module Contrast adjust pot.

ATmega32 + LCD + Keypad Interface.


We have built the above circuit on a Low Cost AVR Development Board, but it does not has inbuilt LCD Module connector so you need to solder it yourself at the free area (and also do the wiring).

Compile the above program using AVR Studio (compiler is avr-gcc). And finally burn the program using any ISP Programmer to the ATmega32. The fuse bits must be set as following to enable external crystal as clock source.

High Fuse = C9 (hex value) Low fuse =FF (hex value)

After burning the HEX file to MCU, finally you are ready to power up the setup. When powered on, the LCD Screen Should show you the keycode of the key pressed on the keypad. This complete our test.

Troubleshooting

NO Display on LCD

o o o o o o o o
1.

Make sure AVR Studio Project is set up for clock frequency of 16MHz(16000000Hz) Adjust the Contrast Adj Pot. Press reset few times. Power On/Off few times. Connect the LCD only as shown on schematic above.

No response to key press. Check that keypad is connected on PORTA only. If you want to attach keypad on different port, change the line 80 on source code (keypad.c) #define KEYPAD A //KEYPAD IS ATTACHED ON PORTA

Compiler Errors Many people these days has jumped to embedded programming without a solid concept of computer science and programming. They don't know the basics of compiler and lack experience. To learn basic of compilers and their working PC/MAC/Linux( I mean a desktop or laptop) are great platform. But embedded system is not good for learning about compilers and programming basics. It is for those who already have these skills and just want to apply it. 2. 3. 4. 5. 6. Make sure all files belonging to the LCD Library are "added" to the "Project". avr-gcc is installed. (The Windows Binary Distribution is called WinAVR) The AVR Studio project Type is AVR GCC. Basics of Installing and using AVR Studio with avr-gcc is described in thistutorial How to add files to project is described in this tutorial.

General Tips for newbies

o o

Use ready made development boards and programmers. Try to follow the AVR Tutorial Series from the very beginning. (Remember the list spans four pages, page 1 is most recent addition thus most advance)

You might also like