Aim:
Matrix Keypads are commonly used in calculators, telephones etc where a
number of input switches are required. A keypad is a set of buttons
arranged in a block or “pad” which usually bear digits, symbols and
usually a complete set of alphabetical letters. If it mostly contains
numbers then it can also be called a numeric keypad. Keypads are found
on many alphanumeric keyboards and on other devices such ascalculators,
push-button telephones, combination locks, and digital door locks, which
require mainly numeric input.
Description:
We know that matrix keypad is made by arranging push button switches in
row and columns. In the straight forward way to connect a 4×4 keypad (16
switches) to a microcontroller we need 16 inputs pins. But by
connecting switches in the following way we can read the status of each
switch using 8 pins of the microcontroller.
The status of each keys can be determined by a process called Scanning.
For the sake of explanation lets assume that all the column pins (Col1 –
Col4) are connected to the inputs pins and all the row pins are
connected to the output pins of the microcontroller. In the normal case
all the column pins are pulled up (HIGH state) by internal or external
pull up resistors. Now we can read the status of each switch through
scanning.
- A logic LOW is given to Row1 and others (Row2 – Row-4) HIGH
- Now each Column is scanned. If any switch belongs to 1strow is pressed corresponding column will pulled down (logic LOW) and we can detect the pressed key.
- This process is repeated for all rows.
The keypad has 8 communication lines namely R1, R2, R3, R4, C1, C2, C3
and C4. R1 to R4 represents the four rows and C1 to C4 represents the
four columns. When a particular key is pressed the corresponding row and
column to which the terminals of the key are connected gets shorted.
For example if key 1 is pressed row R1 and column C1 gets shorted and so
on. The program identifies which key is pressed by a method known as
column scanning. In this method a particular row is kept low (other rows
are kept high) and the columns are checked for low. If a particular
column is found low then that means that the key connected between that
column and the corresponding row (the row that is kept low) is been
pressed. For example if row R1 is initially kept low and column C1 is
found low during scanning, that means key 1 is pressed.
Block Diagram
Schematic
Code
// ********************************************************* // Project: Interfacing keypad to atmega8 microcontroller // Author: Hack Projects India // Module description: Operate keypad // ********************************************************* #define F_CPU 8000000UL #include <avr/io.h> #include <util/delay.h> int main(void) { int count=0; DDRB=0xf0; PORTB=0x0f; DDRD=0xff; PORTD=0xff; while(1) { int i,s[3]={0x6f,0x5f,0x3f}; for(i=0;i<3;i++) { PORTB=s[i]; if((PINB&0x01)==0x00) { if(s[i]==0x6f) { PORTD=~0x07; _delay_ms(50); } else if(s[i]==0x5f) { PORTD=~0x7f; _delay_ms(50); } else if(s[i]==0x3f) { PORTD=~0x67; _delay_ms(50); } } } for(i=0;i<3;i++) { PORTB=s[i]; if((PINB&0x02)==0x00) { if(s[i]==0x6f) { PORTD=~0x66; _delay_ms(50); } else if(s[i]==0x5f) { PORTD=~0x6d; _delay_ms(50); } else if(s[i]==0x3f) { PORTD=~0x7c; _delay_ms(50); } } } for(i=0;i<3;i++) { PORTB=s[i]; if((PINB&0x08)==0x00) { if(s[i]==0x6f) { PORTD=~0x76; _delay_ms(50); } else if(s[i]==0x5f) { PORTD=~0x3f; _delay_ms(50); } else if(s[i]==0x3f) { PORTD=~0x77; _delay_ms(50); } } } for(i=0;i<3;i++) { PORTB=s[i]; if((PINB&0x04)==0x00) { if(s[i]==0x6f) { PORTD=~0x06; _delay_ms(50); } else if(s[i]==0x5f) { PORTD=~0x5b; _delay_ms(50); } else if(s[i]==0x3f) { PORTD=~0x4f; _delay_ms(50); } } } } }
Downloads:
The code was compiled in Atmel Studio 6 and simulation was made in Proteus v7.7.
To download code and proteus simulation click here.
Further Reading suggestions:
You may also like,
- nterfacing DAC with AVR
- Interfacing with UART of AVR controller
- Interfacing SPI communication with AVR
- AVR Displaying Custom Characters on LCD
- AVR Graphical LCD
- RTC interfacing using I2C in AVR
- Interfacing ultrasonic sensor with AVR
- Interfacing GPS Modu with AVR
- Interfacing GSM Module with AVR
- Interfacing PWM in AVR
- Interfacing ADC with AVR
- Scrolling string on LCD using AVR
- nterfacing DAC with AVR
- Interfacing with UART of AVR controller
- Interfacing SPI communication with AVR
- AVR Displaying Custom Characters on LCD
- AVR Graphical LCD
- RTC interfacing using I2C in AVR
- Interfacing ultrasonic sensor with AVR
- Interfacing GPS Modu with AVR
- Interfacing GSM Module with AVR
- Interfacing PWM in AVR
- Interfacing ADC with AVR
- Scrolling string on LCD using AVR
No comments:
Post a Comment