praktek 3avr_d4 keypad

10
Praktek 3 KEYPAD 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 1

Upload: vicky

Post on 13-Apr-2015

33 views

Category:

Documents


3 download

DESCRIPTION

codevision avr

TRANSCRIPT

Page 1: Praktek 3AVR_D4 Keypad

Praktek 3KEYPAD

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

1

Page 2: Praktek 3AVR_D4 Keypad

Pin No. Keypad Pin AVR Fungsi1 Kolom 2 PORTC.0 input2 Baris 1 PORTC.1 output3 Kolom 1 PORTC.2 input4 Baris 4 PORTC.3 output5 Kolom 3 PORTC.4 input6 Baris 3 PORTC.5 output7 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)

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)

2

1 … 7

Page 3: Praktek 3AVR_D4 Keypad

PERCOBAAN 1

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

#include <mega16.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;}if (a==0xf6) {tombol=11; goto selesai;}if (a==0xe7) tombol=12;

selesai:return(tombol);}

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

PORTA=0x00; // out LEDDDRA=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) {

3

Page 4: Praktek 3AVR_D4 Keypad

nilai= baca_keypad();PORTA=~nilai; delay_ms(100);

}}

PERCOBAAN 2

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

#include <mega16.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;}

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 hereint nilai;

PORTA=0x00; // out LED

4

Page 5: Praktek 3AVR_D4 Keypad

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);

}}

PERCOBAAN 3

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

#include <mega16.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")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;}

5

Page 6: Praktek 3AVR_D4 Keypad

if (a==0xe7) tombol=12;

selesai:return(tombol);}

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

PORTA=0x00; // out LEDDDRA=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: 9600UCSRA=0x00;UCSRB=0x18;UCSRC=0x86;UBRRH=0x00;UBRRL=0x47;

while (1) { nilai= baca_keypad();

PORTA=~nilai; putchar(nilai+0x30);delay_ms(100);

}}

PERCOBAAN 4

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 <mega16.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

6

Page 7: Praktek 3AVR_D4 Keypad

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 hereint nilai,b; int pswin[4]; //buffer password user int psw[4]={0x04,0x01,0x03,0x09}; //password 4 digit //psw=4139PORTA=0x00; // out LEDDDRA=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: 9600UCSRA=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++)

7

Page 8: Praktek 3AVR_D4 Keypad

{ 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); } }

}

PERCOBAAN 5

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

by: darwis_angga@oct 17th, 2007FA Lab.

8