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.

Tinggalkan Balasan

Isikan data di bawah atau klik salah satu ikon untuk log in:

Logo WordPress.com

You are commenting using your WordPress.com account. Logout / Ubah )

Gambar Twitter

You are commenting using your Twitter account. Logout / Ubah )

Foto Facebook

You are commenting using your Facebook account. Logout / Ubah )

Foto Google+

You are commenting using your Google+ account. Logout / Ubah )

Connecting to %s