Aplikasi Sasakala-M32 dengan Keypad 3x4 dan LCD

Melanjutkan posting sebelumnya mengenai interfacing matrix keypad dengan mikrokotroler, posting kali ini adalah aplikasi dari Keypad 3x4 dan LCd 16x2 yang dihubungkan dengan board Sasakala-M32. Ilustrasi rangkaiannya adalah sebagai berikut :

klik disini untuk memperbesar gambar.

Skematik serta simulasi untuk aplikasi ini pada program Proteus adalah sebagai berikut :

image

Contoh kode programnya :

/*
== Sasakala-M32 - Keypad Demo ==
============================================
Project : Keypad - LCD

Hardware:
--------
- [Sasakala-M32 Board]
- LCD 16x2
- Resistor 470R
- Diode 1N4148
- Matrix Keypad 3x4

LCD Connection :
--------------
VSS -> GND
VDD -> +5V
VEE -> Anoda Diode 1N4148 (Katoda -> GND)

RS -> PA0
RW -> PA1
E -> PA2

DB0..DB3 -> N/C

DB4 -> PA4
DB5 -> PA5
DB6 -> PA6
DB7 -> PA7

LED+ -> +5V (via RES 100ohm)
LED- -> GND

KEYPAD Connection :
-----------------
COL1 -> PB6
COL2 -> PB5
COL3 -> PB4

ROW1 -> PB3
ROW2 -> PB2
ROW3 -> PB1
ROW4 -> PB0

============================================
Embedtronix (C)201011
*/
#include <avr/io.h>
#include <util/delay.h>

#define LDP PORTA //LCD Data Port
#define LCP PORTA //LCD Control pins
#define LDDR DDRA //direction register for LCD data port
#define LCDR DDRA //direction register for LCD control pins
#define LCD_RS 0 //pin LCD RS
#define LCD_RW 1 //pin LCD R/W
#define LCD_E 2 //pin LCD E
#define LCD_D4 4 //pin LCD D3
#define LCD_D5 5 //pin LCD D4
#define LCD_D6 6 //pin LCD D5
#define LCD_D7 7 //pin LCD D6

#define LCD_CLR 0 //DB0: clear display
#define LCD_HOME 1 //DB1: return to home position
#define LCD_CGRAM 6 //DB6: set CG RAM address
#define LCD_DDRAM 7 //DB7: set DD RAM address

// cursor position to DDRAM mapping
#define LCD_LINE0_DDRAMADDR 0x00
#define LCD_LINE1_DDRAMADDR 0x40
#define LCD_LINE2_DDRAMADDR 0x14
#define LCD_LINE3_DDRAMADDR 0x54

//KEYPAD Connection
#define KEYPAD_PORT PORTB
#define KEYPAD_DDR DDRB
#define KEYPAD_PIN PINB

//=========================================================
void LCDsendChar(uint8_t ch) //Sends Char to LCD
{
LDP=(ch&0b11110000);
LCP|=1<<LCD_RS;
LCP|=1<<LCD_E;
_delay_ms(1);
LCP&=~(1<<LCD_E);
LCP&=~(1<<LCD_RS);
_delay_ms(1);
LDP=((ch&0b00001111)<<4);
LCP|=1<<LCD_RS;
LCP|=1<<LCD_E;
_delay_ms(1);
LCP&=~(1<<LCD_E);
LCP&=~(1<<LCD_RS);
_delay_ms(1);
}

//=========================================================
void LCDsendCommand(uint8_t cmd) //Sends Command to LCD
{
LDP=(cmd&0b11110000);
LCP|=1<<LCD_E;
_delay_ms(1);
LCP&=~(1<<LCD_E);
_delay_ms(1);
LDP=((cmd&0b00001111)<<4);
LCP|=1<<LCD_E;
_delay_ms(1);
LCP&=~(1<<LCD_E);
_delay_ms(1);
}

//=========================================================
void LCDinit(void)//Initializes LCD
{
_delay_ms(15);
LDP=0x00;
LCP=0x00;
LDDR|=1<<LCD_D7|1<<LCD_D6|1<<LCD_D5|1<<LCD_D4;
LCDR|=1<<LCD_E|1<<LCD_RW|1<<LCD_RS;
//---------one------
LDP=0<<LCD_D7|0<<LCD_D6|1<<LCD_D5|1<<LCD_D4; //4 bit mode
LCP|=1<<LCD_E|0<<LCD_RW|0<<LCD_RS;
_delay_ms(1);
LCP&=~(1<<LCD_E);
_delay_ms(1);
//-----------two-----------
LDP=0<<LCD_D7|0<<LCD_D6|1<<LCD_D5|1<<LCD_D4; //4 bit mode
LCP|=1<<LCD_E|0<<LCD_RW|0<<LCD_RS;
_delay_ms(1);
LCP&=~(1<<LCD_E);
_delay_ms(1);
//-------three-------------
LDP=0<<LCD_D7|0<<LCD_D6|1<<LCD_D5|0<<LCD_D4; //4 bit mode
LCP|=1<<LCD_E|0<<LCD_RW|0<<LCD_RS;
_delay_ms(1);
LCP&=~(1<<LCD_E);
_delay_ms(1);
//--------4 bit--dual line---------------
LCDsendCommand(0b00101000);
//-----increment address, invisible cursor shift------
LCDsendCommand(0b00001100);
LCDsendCommand(0b00001100);
}

//=========================================================
void LCDclr(void) //Clears LCD
{
LCDsendCommand(1<<LCD_CLR);
}

//=========================================================
void LCDhome(void) //LCD cursor home
{
LCDsendCommand(1<<LCD_HOME);
}

//=========================================================
void LCDWriteString(const char *msg)
{
while(*msg!='\0')
{
LCDsendChar(*msg);
msg++;
}
}

//===================================================================
void LCDWriteInt(int val,unsigned int field_length)
{
char str[5]={0,0,0,0,0};
int i=4,j=0;
while(val)
{
str[i]=val%10;
val=val/10;
i--;
}
if(field_length==-1)
while(str[j]==0) j++;
else
j=5-field_length;

if(val<0) LCDsendChar('-');
for(i=j;i<5;i++)
{
LCDsendChar(48+str[i]);
}
}

//=========================================================
void LCDGotoXY(uint8_t x, uint8_t y) //Cursor to X Y position
{
register uint8_t DDRAMAddr;
// remap lines into proper order
switch(y)
{
case 0: DDRAMAddr = LCD_LINE0_DDRAMADDR+x; break;
case 1: DDRAMAddr = LCD_LINE1_DDRAMADDR+x; break;
case 2: DDRAMAddr = LCD_LINE2_DDRAMADDR+x; break;
case 3: DDRAMAddr = LCD_LINE3_DDRAMADDR+x; break;
default: DDRAMAddr = LCD_LINE0_DDRAMADDR+x;
}
// set data address
LCDsendCommand(1<<LCD_DDRAM | DDRAMAddr);

}

//=========================================================
void WritetoLCD(uint8_t x,uint8_t y,const char msg)
{
LCDGotoXY(x,y-1);
LCDWriteString(msg);
}

//=========================================================
void LCDWriteIntXY(uint8_t x,uint8_t y,uint8_t value,uint8_t FieldLength)
{
LCDGotoXY(x,y-1);
LCDWriteInt(value,FieldLength);
}

//=========================================================
void delay1s(void)
{
uint8_t i;
for(i=0;i<100;i++)
{
_delay_ms(10);
}
}

//==========================================
//Function return the keycode of keypressed
//on the Keypad. Keys are numbered as follows
//
// [00] [01] [02]
// [03] [04] [05]
// [06] [07] [08]
// [09] [10] [11]
//
//Return:
// Any number between 0-11 depending on
// keypressed.
//
// 255 (hex 0xFF) if NO keypressed.
//
//==========================================
uint8_t GetKeyPressed()
{
uint8_t Row,Col;

KEYPAD_PORT|= 0X0F;

for(Col=0;Col<3;Col++)
{
KEYPAD_DDR&=~(0X7F);

KEYPAD_DDR|=(0X40>>Col);
for(Row=0;Row<4;Row++)
{
if(!(KEYPAD_PIN & (0X08>>Row)))
{
return (Row*3+Col+1);
}
}
}
return 0XFF; //Indicate No keypressed
}

//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
int main()
{
unsigned char key;

LCDinit(); //Initialize LCD module
LCDclr(); //Clear the screen
WritetoLCD(0,1,"[Sasakala Board]");
WritetoLCD(0,2,"Embedtronix.com");
delay1s(); //Wait
LCDclr(); //Clear the screen
WritetoLCD(0,1,"KeyNumber: "); //String on line 1
WritetoLCD(0,2,"KeyPress : "); //String on line 1

while(1)
{
key=GetKeyPressed();
if(key==0xFF) {WritetoLCD(11,1,"--");}
else {LCDWriteIntXY(11,1,key,2);}
switch(key)
{
case 0xFF: WritetoLCD(11,2,"-"); break;
case 10: WritetoLCD(11,2,"*"); break;
case 11: WritetoLCD(11,2,"0"); break;
case 12: WritetoLCD(11,2,"#"); break;
default: LCDWriteIntXY(11,2,key,1);
}
}
}
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%



Penjelasannya:



Aplikasi ini akan membaca data dari keypad dan menampilkannya pada LCD. Data yang ditampilkan berupa nomor tombol yang ditekan dan karakter yang mewakilinya. Misalnya jika ditekan tombol nonor 5, maka pada LCD akan tampil : “KeyNumber : 05” – “ KeyPress : 5” dan jika tombol ‘#’ yang ditekan maka pada LCD akan tampil : “KeyNumber : 12” – “ KeyPress : #”.



LCDinit();                //Initialize LCD module
LCDclr(); //Clear the screen
WritetoLCD(0,1,"[Sasakala Board]");
WritetoLCD(0,2,"Embedtronix.com");
delay1s(); //Wait
LCDclr(); //Clear the screen
WritetoLCD(0,1,"KeyNumber: "); //String on line 1
WritetoLCD(0,2,"KeyPress : "); //String on line 1



Pertama, program akan memanggil rutin untuk inisialisasi LCD (selengkapnya baca Interfacing LCD dengan mikrokontroler Bagian-3), kemudian tampilan LCD akan dibersihkan (clear), lalu akan ditampilkan tittle image



Gambar 1. Judul Project imageGambar 2. Tampilan saat tidak ada tombol yang ditekan imageGambar 3. Tampilan saat tombol no 2 ditekan



while(1)
{
key=GetKeyPressed();
if(key==0xFF)
{
LCDGotoXY(11,1); LCDsendChar('-');
LCDGotoXY(12,1); LCDsendChar('-');
}
else {LCDWriteIntXY(11,1,key,2);}
LCDGotoXY(11,2);
switch(key)
{
case 0xFF: LCDsendChar('-'); break;
case 10: LCDsendChar('*'); break;
case 11: LCDsendChar('0'); break;
case 12: LCDsendChar('#'); break;
default: LCDWriteIntXY(11,2,key,1);
}
}



Program utamanya adalah menampilkan nomor dan karakter tombol yang ditekan. Rutin ini akan berulang terus menerus. Jika tidak ada tombol yang ditekan, maka tampilan akan kembali seperti gambar 2 diatas.



Untuk koneksi LCD bisa diubah port-nya dengan cara menganti instruksi pada baris berikut, ganti PORTA, dan DDRA dengan port yang akan diganti.



#define LDP  PORTA    //LCD Data Port
#define LCP PORTA //LCD Control pins
#define LDDR DDRA //direction register for LCD data port
#define LCDR DDRA //direction register for LCD control pins



Sementara port untuk keypad bisa diubah pada baris



//KEYPAD Connection
#define KEYPAD_PORT PORTB
#define KEYPAD_DDR DDRB
#define KEYPAD_PIN PINB



Download project lengkapnya disini



Selamat Belajar !

Tidak ada komentar:

Posting Komentar

author
Embedtronix
Build Your Embedded System. Belajar bareng ngoprek Microcontroller, Arduino, TPLink, GLiNet dan ilmu Microelectronic lainnya.