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)
int baca_keypad()
{
int a,tombol;
PORTC= 0xff;
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;}
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;
// 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)
int baca_keypad()
{
int a,tombol;
PORTC= 0xff;
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;}
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;}
selesai:
return(tombol);
}
void main(void)
{
// Declare your local variables here
int nilai;
// 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
int baca_keypad()
{
int a,tombol;
PORTC= 0xff;
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;}
selesai:
return(tombol);
}
void main(void)
{
// Declare your local variables here
int nilai;
// 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).
int baca_keypad()
{
int a,tombol;
PORTC= 0xff;
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;}
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: ");
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; }
7
Buatlah aplikasi dengan Visual Basic yang dapat menampilkan data penekanan tombol
keypad (petujuk: gunakan pemrograman serial RS232 dan keypad)