Berikut ini saya akan memberikan sedikit tutorial Keypad Matriks 4x4 dengan AVR GCC. Kalau belum kenal dengan AVR GCC dan cara menggunakannya, bisa dipelajari tutorial sebelumnya Tutorial LCD Display with AVR GCC.
Keypad Matriks ini akan ditampilkan datanya pada LCD 16x2 dengan skematik sebagai berikut:
Listing Programnya sebagai berikut:
// Programmer : Wahyu Rahmaniar
// Company : Technologination
#include<util/delay.h>
#include<stdlib.h>
#include<avr/io.h>
#include<avr/pgmspace.h>
#include<avr/interrupt.h>
#include"lcdkey.h"
#define selamanya for(;;) {;}
#define out_keypad PORTB
#define in_keypad PINB
#define kolom1 0
#define kolom2 1
#define kolom3 2
#define kolom4 3
#define baris1 4
#define baris2 5
#define baris3 6
#define baris4 7
//#define delaykeypad 1
#define kolom1_hi out_keypad|=(1<<kolom1)
#define kolom1_lo out_keypad&=~(1<<kolom1)
#define kolom2_hi out_keypad|=(1<<kolom2)
#define kolom2_lo out_keypad&=~(1<<kolom2)
#define kolom3_hi out_keypad|=(1<<kolom3)
#define kolom3_lo out_keypad&=~(1<<kolom3)
#define kolom4_hi out_keypad|=(1<<kolom4)
#define kolom4_lo out_keypad&=~(1<<kolom4)
//volatile unsigned char datakeypad;
const uint8_t Tampil[] PROGMEM="Test Keypad\0";
const uint8_t key1[] PROGMEM="1\0";
const uint8_t key2[] PROGMEM="2\0";
const uint8_t key3[] PROGMEM="3\0";
const uint8_t key4[] PROGMEM="4\0";
const uint8_t key5[] PROGMEM="5\0";
const uint8_t key6[] PROGMEM="6\0";
const uint8_t key7[] PROGMEM="7\0";
const uint8_t key8[] PROGMEM="8\0";
const uint8_t key9[] PROGMEM="9\0";
const uint8_t key0[] PROGMEM="0\0";
const uint8_t keyX[] PROGMEM="*\0";
const uint8_t keyY[] PROGMEM="#\0";
const uint8_t keyA[] PROGMEM="A\0";
const uint8_t keyB[] PROGMEM="B\0";
const uint8_t keyC[] PROGMEM="C\0";
const uint8_t keyD[] PROGMEM="D\0";
void get_keypad()
{
kolom1_lo;kolom2_hi;kolom3_hi;kolom4_hi;
if(bit_is_clear(in_keypad,baris1))
{
loop_until_bit_is_set(in_keypad,baris1);
LCDclr();
CopyStringtoLCD(key1, 8, 0);
}
if(bit_is_clear(in_keypad,baris2))
{
loop_until_bit_is_set(in_keypad,baris2);
LCDclr();
CopyStringtoLCD(key2, 8, 0);
}
if(bit_is_clear(in_keypad,baris3))
{
loop_until_bit_is_set(in_keypad,baris3);
LCDclr();
CopyStringtoLCD(key3, 8, 0);
}
if(bit_is_clear(in_keypad,baris4))
{
loop_until_bit_is_set(in_keypad,baris4);
LCDclr();
CopyStringtoLCD(keyA, 8, 0);
}
//PORTB =0b10111111;
kolom1_hi;kolom2_lo;kolom3_hi;kolom4_hi;
if(bit_is_clear(in_keypad,baris1))
{
loop_until_bit_is_set(in_keypad,baris1);
LCDclr();
CopyStringtoLCD(key4, 8, 0);;
}
if(bit_is_clear(in_keypad,baris2))
{
loop_until_bit_is_set(in_keypad,baris2);
LCDclr();
CopyStringtoLCD(key5, 8, 0);
}
if(bit_is_clear(in_keypad,baris3))
{
loop_until_bit_is_set(in_keypad,baris3);
LCDclr();
CopyStringtoLCD(key6, 8, 0);
}
if(bit_is_clear(in_keypad,baris4))
{
loop_until_bit_is_set(in_keypad,baris4);
LCDclr();
CopyStringtoLCD(keyB, 8, 0);
};
//PORTB =0b11011111;
kolom1_hi;kolom2_hi;kolom3_lo;kolom4_hi;
if(bit_is_clear(in_keypad,baris1))
{
loop_until_bit_is_set(in_keypad,baris1);
LCDclr();
CopyStringtoLCD(key7, 8, 0);
}
if(bit_is_clear(in_keypad,baris2))
{
loop_until_bit_is_set(in_keypad,baris2);
LCDclr();
CopyStringtoLCD(key8, 8, 0);
}
if(bit_is_clear(in_keypad,baris3))
{
loop_until_bit_is_set(in_keypad,baris3);
LCDclr();
CopyStringtoLCD(key9, 8, 0);
}
if(bit_is_clear(in_keypad,baris4))
{
loop_until_bit_is_set(in_keypad,baris4);
LCDclr();
CopyStringtoLCD(keyC, 8, 0);
}
kolom1_hi;kolom2_hi;kolom3_hi;kolom4_lo;
if(bit_is_clear(in_keypad,baris1))
{
loop_until_bit_is_set(in_keypad,baris1);
LCDclr();
CopyStringtoLCD(keyX, 8, 0);
}
if(bit_is_clear(in_keypad,baris2))
{
loop_until_bit_is_set(in_keypad,baris2);
LCDclr();
CopyStringtoLCD(key0, 8, 0);
}
if(bit_is_clear(in_keypad,baris3))
{
loop_until_bit_is_set(in_keypad,baris3);
LCDclr();
CopyStringtoLCD(keyY, 8, 0);
}
if(bit_is_clear(in_keypad,baris4))
{
loop_until_bit_is_set(in_keypad,baris4);
LCDclr();
CopyStringtoLCD(keyD, 8, 0);
}
}
int main()
{
DDRB=0B00001111;
PORTB=0B11111111;
LCDinit();//init LCD bit
LCDclr();//bersihkan tampilan LCD
CopyStringtoLCD(Tampil, 2, 0);
_delay_ms(100);
for(;;)
{
get_keypad();
}
}
Simulasi dan file-file program lengkapnya dapat didownload disini.
Tempat Jual Komponen dan Elektronik.
-ayuchan-