scaning keypad matrix 4×4

scaning

 

#define F_CPU 8000000UL
#include “util/delay.h”
#include “stdlib.h”
#include “avr/io.h”
#include “avr/pgmspace.h”
#include “avr/interrupt.h”

#define LCD_RS_HI PORTC|=(1<<0)       //LCD
#define LCD_RS_LO PORTC&=~(1<<0)

#define LCD_EN_HI PORTC|=(1<<1)
#define LCD_EN_LO PORTC&=~(1<<1)

#define LCD_D4_HI PORTC|=(1<<2)
#define LCD_D4_LO PORTC&=~(1<<2)
#define LCD_D5_HI PORTC|=(1<<3)
#define LCD_D5_LO PORTC&=~(1<<3)
#define LCD_D6_HI PORTC|=(1<<4)
#define LCD_D6_LO PORTC&=~(1<<4)
#define LCD_D7_HI PORTC|=(1<<5)
#define LCD_D7_LO PORTC&=~(1<<5)
#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 10

#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)
//==============================================
#define line1 0x00
#define line2 0x40
//=============================================
const unsigned char textdcu[] PROGMEM=”DCU”;
const char textmasukanid[] PROGMEM=”Masukan ID:”;
const char texthapus[] PROGMEM=”                “;
const char textmin[] PROGMEM=”Min 3 digit:”;

volatile unsigned char datakeypad;
//==============================================================
void LCD_STROBE()
{
LCD_EN_HI;
LCD_EN_LO;
}

void lcd_write(unsigned char datalcd)
{
if(datalcd & 0x80) {LCD_D7_HI;} else {LCD_D7_LO;}
if(datalcd & 0x40) {LCD_D6_HI;} else {LCD_D6_LO;}
if(datalcd & 0x20) {LCD_D5_HI;} else {LCD_D5_LO;}
if(datalcd & 0x10) {LCD_D4_HI;} else {LCD_D4_LO;}
LCD_STROBE();
if(datalcd & 0x08) {LCD_D7_HI;} else {LCD_D7_LO;}
if(datalcd & 0x04) {LCD_D6_HI;} else {LCD_D6_LO;}
if(datalcd & 0x02) {LCD_D5_HI;} else {LCD_D5_LO;}
if(datalcd & 0x01) {LCD_D4_HI;} else {LCD_D4_LO;}
LCD_STROBE();
_delay_ms(5);
}

void lcd_clrscr()
{
LCD_RS_LO;
lcd_write(0x1);
_delay_ms(2);
}

void lcd_putc(unsigned char c)
{
LCD_RS_HI;
lcd_write(c);
}

void lcd_puts(const char *s)
{
register char c;

while ( (c = *s++) ) {
lcd_putc(c);
}
}

void lcd_puts_pp(const char *progmem_s)
{
register char c;
for(;;)
{
c = pgm_read_byte(progmem_s++);
if(c==0) break;
lcd_putc(c);
}
}

void lcd_goto(unsigned char poslcd)
{
LCD_RS_LO;
lcd_write(0x80 + poslcd);
}

void initlcd()
{
LCD_RS_LO;
_delay_ms(15);
LCD_D4_HI;
LCD_D5_HI;
LCD_STROBE();
_delay_ms(5);

LCD_STROBE();
_delay_us(100);
LCD_STROBE();
_delay_ms(5);
LCD_D4_LO;
LCD_STROBE();
_delay_us(40);
lcd_write(0x28);
lcd_write(0x0C);
lcd_write(0x06);
lcd_write(0x01);
_delay_ms(10);
}
//===================================================
void get_keypad()
{
datakeypad=’g’;
kolom1_lo;kolom2_hi;kolom3_hi;kolom4_hi;            //cek kolom1
if(bit_is_clear(in_keypad,baris1))
{
loop_until_bit_is_set(in_keypad,baris1);
datakeypad=’7′;
_delay_ms(delaykeypad);
}
if(bit_is_clear(in_keypad,baris2))
{
loop_until_bit_is_set(in_keypad,baris2);
datakeypad=’4′;
_delay_ms(delaykeypad);
}
if(bit_is_clear(in_keypad,baris3))
{
loop_until_bit_is_set(in_keypad,baris3);
datakeypad=’1′;
_delay_ms(delaykeypad);
}

if(bit_is_clear(in_keypad,baris4))
{
loop_until_bit_is_set(in_keypad,baris4);
datakeypad=’a’;
_delay_ms(delaykeypad);
}

kolom1_hi;kolom2_lo;kolom3_hi;kolom4_hi;
if(bit_is_clear(in_keypad,baris1))
{
loop_until_bit_is_set(in_keypad,baris1);
datakeypad=’8′;
_delay_ms(delaykeypad);
}
if(bit_is_clear(in_keypad,baris2))
{
loop_until_bit_is_set(in_keypad,baris2);
datakeypad=’5′;
_delay_ms(delaykeypad);
}
if(bit_is_clear(in_keypad,baris3))
{
loop_until_bit_is_set(in_keypad,baris3);
datakeypad=’2′;
_delay_ms(delaykeypad);
}

if(bit_is_clear(in_keypad,baris4))
{
loop_until_bit_is_set(in_keypad,baris4);
datakeypad=’0′;
_delay_ms(delaykeypad);
}_delay_ms(1);
kolom1_hi;kolom2_hi;kolom3_lo;kolom4_hi;
if(bit_is_clear(in_keypad,baris1))
{
loop_until_bit_is_set(in_keypad,baris1);
datakeypad=’9′;
_delay_ms(delaykeypad);
}
if(bit_is_clear(in_keypad,baris2))
{
loop_until_bit_is_set(in_keypad,baris2);
datakeypad=’6′;
_delay_ms(delaykeypad);
}
if(bit_is_clear(in_keypad,baris3))
{
loop_until_bit_is_set(in_keypad,baris3);
datakeypad=’3′;
_delay_ms(delaykeypad);
}

if(bit_is_clear(in_keypad,baris4))
{
loop_until_bit_is_set(in_keypad,baris4);
datakeypad=’b’;
_delay_ms(delaykeypad);
}
_delay_ms(1);
kolom1_hi;kolom2_hi;kolom3_hi;kolom4_lo;
if(bit_is_clear(in_keypad,baris1))
{
loop_until_bit_is_set(in_keypad,baris1);
datakeypad=’c’;
_delay_ms(delaykeypad);
}
if(bit_is_clear(in_keypad,baris2))
{
loop_until_bit_is_set(in_keypad,baris2);
datakeypad=’d’;
_delay_ms(delaykeypad);
}
if(bit_is_clear(in_keypad,baris3))
{
loop_until_bit_is_set(in_keypad,baris3);
datakeypad=’e’;
_delay_ms(delaykeypad);
}

if(bit_is_clear(in_keypad,baris4))
{
loop_until_bit_is_set(in_keypad,baris4);
datakeypad=’f’;
_delay_ms(delaykeypad);
}
_delay_ms(1);
}
//===================================================

void init_devices()
{
DDRB=0B00001111;
PORTB=0B11111111;
DDRC=255;
initlcd();
}

int main()
{
_delay_ms(100);
init_devices();
for(;;)
{
get_keypad();
if(datakeypad!=’g’)
{
lcd_goto(line1);
lcd_putc(datakeypad);
}
}
}

untuk teorinya sudah banyak yang ngebahas tentang ini.

Iklan

KEYPAD 3X4 dihubungkan ke ADC ATMEGA8

Keypad 3×4 dihubungkan ke ADC, pada dasarnya adalah push button yang disusun secara matriks kemudian ditambahkan rangkaian pembagi tegangan. Dengan memakai prinsip pembagi tegangan ini, maka jika ada tombol yang ditekan maka akan didapat tegangan keluaran yang berbeda dengan tombol lain. Jika tegangan keluaran antar tombol berbeda maka nilai yang diambil ADC pun akan berbeda sehingga kita dapat memetakan tombol mana yang ditekan.

TAH IEU RANGKAIANNA

keypadadcberikut program bascomnya

$regfile = “m8def.dat”
$crystal = 8000000
$baud = 9600

Config Adc = Single , Prescaler = Auto , Reference = Avcc
Start Adc
Satu Alias 819
Dua Alias 630
Tiga Alias 493
Empat Alias 786
Lima Alias 584
Enam Alias 446
Tujuh Alias 731
Delapan Alias 511
Sembilan Alias 376
Batal Alias 614
Nol Alias 384
Ok Alias 264

Dim Temphigh As Word
Dim Templow As Word
Dim Dataadc As Word

Declare Sub Ambildatakeypad
Do
Dataadc = Getadc(0)
Call Ambildatakeypad
Loop

Sub Ambildatakeypad
Temphigh = Satu + 5
Templow = Satu – 5
If Dataadc > Templow And Dataadc < Temphigh Then
Print “angka 1”
Goto Ambildatakeypadx
End If

Temphigh = Dua + 5
Templow = Dua – 5
If Dataadc > Templow And Dataadc < Temphigh Then
Print “angka 2”
Goto Ambildatakeypadx
End If

Temphigh = Tiga + 5
Templow = Tiga – 5
If Dataadc > Templow And Dataadc < Temphigh Then
Print “angka 3”
Goto Ambildatakeypadx
End If

Temphigh = Empat + 5
Templow = Empat – 5
If Dataadc > Templow And Dataadc < Temphigh Then
Print “angka 4”
Goto Ambildatakeypadx
End If

Temphigh = Lima + 5
Templow = Lima – 5
If Dataadc > Templow And Dataadc < Temphigh Then
Print “angka 5”
Goto Ambildatakeypadx
End If

Temphigh = Enam + 5
Templow = Enam – 5
If Dataadc > Templow And Dataadc < Temphigh Then
Print “angka 6”
Goto Ambildatakeypadx
End If

Temphigh = Tujuh + 5
Templow = Tujuh – 5
If Dataadc > Templow And Dataadc < Temphigh Then
Print “angka 7”
Goto Ambildatakeypadx
End If

Temphigh = Delapan + 5
Templow = Delapan – 5
If Dataadc > Templow And Dataadc < Temphigh Then
Print “angka 8”
Goto Ambildatakeypadx
End If

Temphigh = Sembilan + 5
Templow = Sembilan – 5
If Dataadc > Templow And Dataadc < Temphigh Then
Print “angka 9”
Goto Ambildatakeypadx
End If

Temphigh = Batal + 5
Templow = Batal – 5
If Dataadc > Templow And Dataadc < Temphigh Then
Print “batal”
Goto Ambildatakeypadx
End If
Temphigh = Nol + 5
Templow = Nol – 5
If Dataadc > Templow And Dataadc < Temphigh Then
Print “angka 0”
Goto Ambildatakeypadx
End If

Temphigh = Ok + 5
Templow = Ok – 5
If Dataadc > Templow And Dataadc < Temphigh Then
Print “oke”
Goto Ambildatakeypadx
End If

Print “tidak ditekan”
Ambildatakeypadx:
End Sub

Hasil simulasi proteus contoh untuk penekanan tombol angka1 dan tombol tidak ditekan

keypadadc1nilai resistor dapat diubah ubah sesuai dengan keinginan anda, nu aya dipake, pake wen rek sabaraha nilaina gen, elektronika for heheh ieu. hahahahahah