4x3 Matrix Keypad
4x3 Matrix Keypad
4x3 Matrix Keypad
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.
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.
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.
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.
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.
DB0 -> N/C DB1 -> N/C DB2 -> N/C DB3 -> N/C
DB4 -> PB0 DB5 -> PB1 DB6 -> PB2 DB7 -> PB3
KEYPAD
ROW1 ->
PA3
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
******************************************************************************/
#define KEYPAD A
//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
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
KEYPAD_PORT|= 0X0F;
for(c=0;c<3;c++) { KEYPAD_DDR&=~(0X7F);
uint8_t key;
LCDWriteIntXY(0,0,key,3); }
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/
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.
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.
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.
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)