fbpx

Interface Keypad With AVR

Description

Keypad is an input device.Keypad is essential in all embedded devices where user needs to interact with devices. From Calculator to Computer, the keypad is essential where a number of input switches are required.

  • Keypad is made by arranging push button switches in row and columns in a matrix format.
  • In 4×3 matrix keypad, 12 input switch is required to connect with a microcontroller.
  • The status of the input switch can be determined by scanning row and column of the keypad.
  • In 4×3 matrix keypad, 4 row and 3 columns will be available.
  • A logic 0 is given to Row1 and logic 1 is given to Row2&Row4.
  • The column is scanned. If any switch belongs to 1st row is pressed the corresponding column will be logic 0 and we can detect the pressed key.
  • This process is repeated for all rows.

Hardware Required

  • 4×3 Keypad.
  • Atmega16 MicroController.

Circuit

Virtual Circuit

Code

 #include <avr/io.h>
 #include <util/delay.h>
 # define R1 PC0
 # define R2 PC1
 # define R3 PC2
 # define R4 PC3
 # define C1 PC4
 # define C2 PC5
 # define C3 PC6
 void ROW1(void);
 void ROW2(void);
 void ROW3(void);
 void ROW4(void);
 int main(void)
 {
 unsigned char keyval;
 DDRC=0x0F;
 PORTC=0Xf0;
 while(1)
 {
 PORTC=0xF0;
 keyval=PINC;
 if(keyval!=0xf0)
 {
 ROW1();
 ROW2();
 ROW3();
 ROW4();
 }
 }
 return 0;
 }
 void ROW1(void)
 {
 PORTC =0XFE;
 _delay_us(10);
 if(bit_is_clear(PINC,C1))
 return ('1');
 else if(bit_is_clear(PINC,C2))
 return ('2');
 else if(bit_is_clear(PINC,C3))
 return ('3');
 }
 void ROW2(void)
 {
 PORTC =0XFD;
 _delay_us(10);
 if(bit_is_clear(PINC,C1))
 return('4');
 else if(bit_is_clear(PINC,C2))
 return('5');
 else if(bit_is_clear(PINC,C3))
 return('6');
 }
 void ROW3(void)
 {
 PORTC =0XFB;
 _delay_us(10);
 if(bit_is_clear(PINC,C1))
 return('7');
 else if(bit_is_clear(PINC,C2))
 return('8');
 else if(bit_is_clear(PINC,C3))
 return('9');
 }
 void ROW4(void)
 {
 PORTC =0XF7;
 _delay_us(10);
 if(bit_is_clear(PINC,C1))
 return('*');
 else if(bit_is_clear(PINC,C2))
 return('0');
 else if(bit_is_clear(PINC,C3))
 return('#');
 } 

Leave a Reply

Your email address will not be published. Required fields are marked *