Anda di halaman 1dari 8

Tujuan:

Mahasiswa mengerti cara kerja dari sebuah keypad


Mahasiswa mengerti cara membuat program C pada mikrokontroller AVR untuk
mengkodekan penekanan sebuah tombol pada keypad

Peralatan yang digunakan:


modul AVR
Modul LED dan switch
Kabel serial RS232 (cross)
Keypad 3x4

Deskripsi/ Dasar Teori:

Gambar3.1. Skematik Keypad 3x4

Gambar3.2. Bentuk Keypad 3x4

Pin No. Keypad Pin AVR Fungsi


1 Kolom 2 PORTC.0 input
2 Baris 1 PORTC.1 output
3 Kolom 1 PORTC.2 input
4 Baris 4 PORTC.3 output
5 Kolom 3 PORTC.4 input
6 Baris 3 PORTC.5 output
7 Baris 2 PORTC.6 output

Gambar3.2. Konfigurasi pinout Keypad 3x4 dan


penyambungannya dengan AVR Minimum System

Jika baris 1 diberi logic 0 dan baris yang lain diberi logic 1, maka:
PORTC=0xFD (1111 1101)
Jika ditekan tombol 1, (baris 1 kolom 1) maka PINC= 0xF9(1111 1001)
Jika ditekan tombol 2, (baris 1 kolom 2) maka PINC= 0xFC(1111 1100)
Jika ditekan tombol 3, (baris 1 kolom 3) maka PINC = 0xED(1110 1101)

Jika baris 2 diberi logic 0 dan baris yang lain diberi logic 1, maka:
PORTC=0xBF (1011 1111)
Jika ditekan tombol 4, (baris 2 kolom 1) maka PINC= 0xBB(1011 1011)
Jika ditekan tombol 5, (baris 2 kolom 2) maka PINC= 0xBE(1011 1110)
Jika ditekan tombol 6, (baris 2 kolom 3) maka PINC = 0xAF(1010 1111)

1
Jika baris 3 diberi logic 0 dan baris yang lain diberi logic 1, maka:
PORTC=0xDF (1101 1111)
Jika ditekan tombol 7, (baris 3 kolom 1) maka PINC= 0xDB(1101 1011)
Jika ditekan tombol 8, (baris 3 kolom 2) maka PINC= 0xDE(1101 1110)
Jika ditekan tombol 9, (baris 3 kolom 3) maka PINC = 0xCF(1100 1111)

Jika baris 4 diberi logic 0 dan baris yang lain diberi logic 1, maka:
PORTC=0xEF (1111 0111)
Jika ditekan tombol *, (baris 4 kolom 1) maka PINC= 0xF3(1111 0011)
Jika ditekan tombol 0, (baris 4 kolom 2) maka PINC= 0xF6(1111 0110)
Jika ditekan tombol #, (baris 4 kolom 3) maka PINC = 0xE7(1110 0111)

Membaca penekanan keypad di port C dan ditampilkan kodenya di LED port A

#include <mega .h>


#include <delay.h>

// Declare your global variables here

int baca_keypad()
{
int a,tombol;
PORTC= 0xff;

tombol=0x00; // jika tdk ada tombol ditekan

PORTC.1=0; // baris 1
#asm("nop")
a = PINC;
if (a==0xf9) {tombol=1; goto selesai;}
if (a==0xfc) {tombol=2; goto selesai;}
if (a==0xed) {tombol=3; goto selesai;}

PORTC.6=0; PORTC.1=1; // baris 2


#asm("nop")
a = PINC;
if (a==0xbb) {tombol=4; goto selesai;}
if (a==0xbe) {tombol=5; goto selesai;}
if (a==0xaf) {tombol=6; goto selesai;}

PORTC.5=0; PORTC.6=1; // baris 3


#asm("nop")
a = PINC;
if (a==0xdb) {tombol=7; goto selesai;}
if (a==0xde) {tombol=8; goto selesai;}
if (a==0xcf) {tombol=9; goto selesai;}

PORTC.3=0; PORTC.5=1; // baris 4


#asm("nop")
a = PINC;
if (a==0xf3) {tombol=10; goto selesai;}

2
if (a==0xf6) {tombol=11; goto selesai;}
if (a==0xe7) tombol=12;

selesai:
return(tombol);
}

void main(void)
{
// Declare your local variables here
int nilai;

PORTA=0x00; // out LED


DDRA=0xFF;

// Port D initialization
// d7=In d6=Out d5=Out d4=In d3=Out d2=In d1=Out d0=In
PORTC=0x00;
DDRC=0x6A;

while (1)
{
nilai= baca_keypad();
PORTA=~nilai;
delay_ms(100);
}
}

Membaca penekanan keypad di port c dan ditampilkan kodenya di LED port A (dengan
pola menyala yang berbeda)

#include <mega .h>


#include <delay.h>

// Declare your global variables here

int baca_keypad()
{
int a,tombol;
PORTC= 0xff;

tombol=0x00; // jika tdk ada tombol ditekan

PORTC.1=0; // baris 1
#asm("nop")
a = PINC;
if (a==0xf9) {tombol=0x01; goto selesai;}
if (a==0xfc) {tombol=0x02; goto selesai;}
if (a==0xed) {tombol=0x04; goto selesai;}

PORTC.6=0; PORTC.1=1; // baris 2


#asm("nop")
a = PINC;
if (a==0xbb) {tombol=0x08; goto selesai;}
if (a==0xbe) {tombol=0x10; goto selesai;}
if (a==0xaf) {tombol=0x20; goto selesai;}

3
PORTC.5=0; PORTC.6=1; // baris 3
#asm("nop")
a = PINC;
if (a==0xdb) {tombol=0x40; goto selesai;}
if (a==0xde) {tombol=0x80; goto selesai;}
if (a==0xcf) {tombol=0x81; goto selesai;}

PORTC.3=0; PORTC.5=1; // baris 4


#asm("nop")
a = PINC;
if (a==0xf3) {tombol=0x18; goto selesai;}
if (a==0xf6) {tombol=0xf0; goto selesai;}
if (a==0xe7) tombol=0x0f;

selesai:
return(tombol);
}

void main(void)
{
// Declare your local variables here
int nilai;

PORTA=0x00; // out LED


DDRA=0xFF;

// Port D initialization
// d7=In d6=Out d5=Out d4=In d3=Out d2=In d1=Out d0=In
PORTC=0x00;
DDRC=0x6A;

while (1)
{
nilai= baca_keypad();
PORTA=~nilai;
delay_ms(100);
}
}

Membaca penekanan keypad di port c dan ditampilkan kodenya di LED port A dan di
port serial

#include <mega .h>


#include <delay.h>
#include<stdio.h>

// Declare your global variables here

int baca_keypad()
{
int a,tombol;
PORTC= 0xff;

tombol=0x00; // jika tdk ada tombol ditekan

PORTC.1=0; // baris 1
#asm("nop")

4
a = PINC;
if (a==0xf9) {tombol=1; goto selesai;}
if (a==0xfc) {tombol=2; goto selesai;}
if (a==0xed) {tombol=3; goto selesai;}

PORTC.6=0; PORTC.1=1; // baris 2


#asm("nop")
a = PINC;
if (a==0xbb) {tombol=4; goto selesai;}
if (a==0xbe) {tombol=5; goto selesai;}
if (a==0xaf) {tombol=6; goto selesai;}

PORTC.5=0; PORTC.6=1; // baris 3


#asm("nop")
a = PINC;
if (a==0xdb) {tombol=7; goto selesai;}
if (a==0xde) {tombol=8; goto selesai;}
if (a==0xcf) {tombol=9; goto selesai;}

PORTC.3=0; PORTC.5=1; // baris 4


#asm("nop")
a = PINC;
if (a==0xf3) {tombol=10; goto selesai;}
if (a==0xf6) {tombol=11; goto selesai;}
if (a==0xe7) tombol=12;

selesai:
return(tombol);
}

void main(void)
{
// Declare your local variables here
int nilai;

PORTA=0x00; // out LED


DDRA=0xFF;

// Port D initialization
// d7=In d6=Out d5=Out d4=In d3=Out d2=In d1=Out d0=In
PORTC=0x00;
DDRC=0x6A;

// USART initialization
// Communication Parameters: 8 Data, 1 Stop, No Parity
// USART Receiver: On
// USART Transmitter: On
// USART Mode: Asynchronous
// USART Baud rate: 9600
UCSRA=0x00;
UCSRB=0x18;
UCSRC=0x86;
UBRRH=0x00;
UBRRL=0x47;

while (1)
{
nilai= baca_keypad();
PORTA=~nilai;
putchar(nilai+0x30);
delay_ms(100);
}
}

5
Simulasi pembuatan suatu Password untuk membuka suatu pintu.
Pesan dan password di tampilkan di port serial.
Pintu di misalkan LED di Port A bit 0, alarm di Port A bit 7 (nyala berkedip).

#include <mega .h>


#include <delay.h>
#include<stdio.h>

// Declare your global variables here

int baca_keypad()
{
int a,tombol;
PORTC= 0xff;

tombol=0xff; // jika tdk ada tombol ditekan

PORTC.1=0; // baris 1
#asm("nop")
a = PINC;
if (a==0xf9) {tombol=1; goto selesai;}
if (a==0xfc) {tombol=2; goto selesai;}
if (a==0xed) {tombol=3; goto selesai;}

PORTC.6=0; PORTC.1=1; // baris 2


#asm("nop")
a = PINC;
if (a==0xbb) {tombol=4; goto selesai;}
if (a==0xbe) {tombol=5; goto selesai;}
if (a==0xaf) {tombol=6; goto selesai;}

PORTC.5=0; PORTC.6=1; // baris 3


#asm("nop")
a = PINC;
if (a==0xdb) {tombol=7; goto selesai;}
if (a==0xde) {tombol=8; goto selesai;}
if (a==0xcf) {tombol=9; goto selesai;}

PORTC.3=0; PORTC.5=1; // baris 4


#asm("nop")
a = PINC;
if (a==0xf3) {tombol=10; goto selesai;}
if (a==0xf6) {tombol=0; goto selesai;}
if (a==0xe7) tombol=11;

selesai:
return(tombol);
}

void main(void)
{
// Declare your local variables here
int nilai,b;
int pswin[4]; //buffer password user
int psw[4]={0x04,0x01,0x03,0x09}; //password 4 digit
//psw=4139

6
PORTA=0x00; // out LED
DDRA=0xFF;

// Port D initialization
// d7=In d6=Out d5=Out d4=In d3=Out d2=In d1=Out d0=In
PORTC=0x00;
DDRC=0x6A;

// USART initialization
// Communication Parameters: 8 Data, 1 Stop, No Parity
// USART Receiver: On
// USART Transmitter: On
// USART Mode: Asynchronous
// USART Baud rate: 9600
UCSRA=0x00;
UCSRB=0x18;
UCSRC=0x86;
UBRRH=0x00;
UBRRL=0x47;

PORTA=0xff;

printf("\nPassword: ");

// entry password 4 x penekanan tombol


for (b=0;b<=3;b++)
{ ulang:
nilai= baca_keypad();
if (nilai==0xff) goto ulang; // jk tidak ada tombol ditekan

putchar(nilai+0x30);
pswin[b]=nilai;
delay_ms(700);
}

delay_ms(500);

// proses
if ((pswin[0]==psw[0]) && (pswin[1]==psw[1]) &&(pswin[2]==psw[2])
&&(pswin[3]==psw[3]) )
{
printf("\nPassword Benar. Buka Pintu.");
while(1) //correct password, open the door
PORTA.0 = 0; }

else // blink alarm


{
printf("\nPassword Salah. Alarm bunyi.");
while(1)
{PORTA.7=1;
delay_ms(100);
PORTA.7=0;
delay_ms(100); }
}

7
Buatlah aplikasi dengan Visual Basic yang dapat menampilkan data penekanan tombol
keypad (petujuk: gunakan pemrograman serial RS232 dan keypad)

by:Ary Sulistyo U, 2008


ary.utomo@gmail.com
Staf Teknis Politeknik Cilacap.

Anda mungkin juga menyukai