Kali ini, saya akan membahas sedikit mengenai pemrograman LCD Karakter 2×16 dengan mikrokontroler AVR. Mikrokontroler yang saya gunakan dalam pembahasan tulisan ini yaitu seri ATmega32.
Pada desain skematik LCD, Anda harus menghubungkan keseluruhan pin-pin data dan command LCD ke port mikrokontroler yang sama (misal Port A0-A7). Pada contoh ini, saya menggunakan metode interfacing 4-bit data (LCD_4 – LCD_7). Sehingga pemilihan pin pada mikrokontroler disesuaikan dengan CVAVR di atas menjadi seperti berikut ini.
Perhatikan PA0-PA7 pada skema rangkaian! PA1 tidak digunakan, karena R/W pada LCD kita fungsikan sebagai “Write” saja sehingga cukup menghubungkan pin R/W pada LCD ke ground. Sedangkan PB3 kita gunakan sebagai pengontrol backlight pada LCD (LCD_BL).
CodeWizard akan meng-generate firmware dengan include “lcd.h”. Nah, file ini tidak saya gunakan karena saya membuat sendiri file include “lcdb.h” yang saya lampirkan pada akhir tulisan ini.
Berikut ini contoh firmware untuk pemrograman LCD Karakter 2×16.
latihan_lcd.c
#include <mega32.h>
#include <stdio.h>
// Alphanumeric LCD Module functions
#asm
.equ __lcd_port=0x1B ;PORTA
#endasm
#include <lcdb.h>
char sz[40];
void main(void)
{
// Declare your local variables here
// Input/Output Ports initialization
// Port A initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTA=0×00;
DDRA=0×00;
// Port B initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTB=0×00;
DDRB=0×00;
// Port C initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTC=0×00;
DDRC=0×00;
// Port D initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTD=0×00;
DDRD=0×00;
// LCD module initialization
LCD_Init();
// Global enable interrupts
#asm(“sei”)
//=============================================
while(1)
{
sprintf(sz, ” Ini Baris ke-1 \n Ini Baris ke-2 “);
LCD_printf(sz);
}
}
Berikut ini file library yang dibutuhkan oleh firmware di atas.
lcdb.c
#include <mega32.h>
#include <delay.h>
#include <stdio.h>
#include <string.h>
#define Clear_Display 0×01
#define Home 0×02
#define Entry_Mode 0×04
#define Set_Display 0×08
#define Set_Cursor 0×10
#define Function 0×20
#define Decrement 0×00
#define Increment 0×02
#define Shift_Off 0×00
#define Shift_On 0×01
#define Display_Off 0×00
#define Display_On 0×04
#define Cursor_Off 0×00
#define DCursor_On 0×02
#define Blink_Off 0×00
#define Blink_On 0×01
#define Four_Bits 0×00
#define Eight_Bits 0×10
#define One_Line 0×00
#define Two_Line 0×08
#define Font5x7 0×00
#define Font5x10 0×04
// Pins
#define LCD_pinRS 0×80 // PA0
#define PORT_LCD_pinRS PORTA
#define LCD_pinRD 0×40 // PA1
#define PORT_LCD_pinRD PORTA
#define LCD_pinEN 0×20 // PA2
#define PORT_LCD_pinEN PORTA
#define LCD_BL_CTRL 0×10 // PA3
#define PORT_LCD_BL_CTRL PORTA
// Macros
#define SET_LCD_pinRS PORT_LCD_pinRS |= LCD_pinRS
#define RES_LCD_pinRS PORT_LCD_pinRS &= ~LCD_pinRS
#define SET_LCD_pinRD PORT_LCD_pinRD |= LCD_pinRD
#define RES_LCD_pinRD PORT_LCD_pinRD &= ~LCD_pinRD
#define SET_LCD_pinEN PORT_LCD_pinEN |= LCD_pinEN
#define RES_LCD_pinEN PORT_LCD_pinEN &= ~LCD_pinEN
#define SET_LCD_BL_CTRL PORT_LCD_BL_CTRL |= LCD_BL_CTRL
#define RES_LCD_BL_CTRL PORT_LCD_BL_CTRL &= ~LCD_BL_CTRL
void LCD_Init(void)
{
DDRA = 0xff;
delay_ms(3);
LCD_CmdWrite(Function+Four_Bits+Two_Line+Font5x7);
delay_ms(3);
LCD_CmdWrite(Function+Four_Bits+Two_Line+Font5x7);
delay_ms(3);
LCD_CmdWrite(Function+Four_Bits+Two_Line+Font5x7);
delay_ms(3);
LCD_CmdWrite(Clear_Display);
delay_ms(3);
LCD_CmdWrite(Entry_Mode+Increment+Shift_Off);
delay_ms(3);
LCD_CmdWrite(0x0c); // display on, cursor off, non-blink
delay_ms(3);
LCD_CmdWrite(0×02); // cursor home
}
void LCD_CmdWrite(unsigned char uc)
{
char DL, DH;
DH=(((uc&0×80)>>7)+((uc&0×40)>>5)+((uc&0×20)>>3)+((uc&0×10)>>1));
DL=(((uc&0×08)>>3)+((uc&0×04)>>1)+((uc&0×02)<<1)+((uc&0×01)<<3));
RES_LCD_pinRS;
RES_LCD_pinRD;
SET_LCD_pinEN; // Enable LCD
PORTA &= 0xf0;
PORTA |= DH; // kirim High Nibble
RES_LCD_pinEN; // beri pulsa LCD EN
LCD_delay_5us();
SET_LCD_pinEN;
PORTA &= 0xf0;
PORTA |= DL; // kirim Low Nibble
RES_LCD_pinEN; // beri pulsa LCD EN
LCD_delay_3ms();
}
void LCD_DataWrite(unsigned char uc)
{
char DL, DH;
DH=(((uc&0×80)>>7)+((uc&0×40)>>5)+((uc&0×20)>>3)+((uc&0×10)>>1));
DL=(((uc&0×08)>>3)+((uc&0×04)>>1)+((uc&0×02)<<1)+((uc&0×01)<<3));
SET_LCD_pinRS;
RES_LCD_pinRD;
SET_LCD_pinEN; // Enable LCD
PORTA &= 0xf0;
PORTA |= DH; // kirim High Nibble
RES_LCD_pinEN; // beri pulsa LCD EN
LCD_delay_5us();
SET_LCD_pinEN;
PORTA &= 0xf0;
PORTA |= DL; // kirim Low Nibble
RES_LCD_pinEN; // beri pulsa LCD EN
LCD_delay_3ms();
}
void LCD_DataRead(unsigned char uc)
{
char DL, DH;
DH=(((uc&0×80)>>7)+((uc&0×40)>>5)+((uc&0×20)>>3)+((uc&0×10)>>1));
DL=(((uc&0×08)>>3)+((uc&0×04)>>1)+((uc&0×02)<<1)+((uc&0×01)<<3));
SET_LCD_pinRS;
SET_LCD_pinRD;
SET_LCD_pinEN; // Enable LCD
PORTA &= 0xf0;
PORTA |= DH; // kirim High Nibble
RES_LCD_pinEN; // beri pulsa LCD EN
LCD_delay_5us();
SET_LCD_pinEN;
PORTA &= 0xf0;
PORTA |= DL; // kirim Low Nibble
RES_LCD_pinEN; // beri pulsa LCD EN
LCD_delay_3ms();
}
void LCD_delay_5us(void)
{
unsigned char j;
for(j = 0; j < 5; j++);
}
void LCD_delay_3ms(void)
{
unsigned char i, j;
for(j = 0; j < 100; j++)
for(i = 0; i < 30; i++);
}
void LCD_delay_5ms(void)
{
unsigned char i, j;
for(j = 0; j < 100; j++)
for(i = 0; i < 50; i++);
}
void LCD_delay_10ms(void)
{
unsigned char i, j;
for(j = 0; j < 100; j++)
for(i = 0; i < 100; i++);
}
void LCD_printf(unsigned char *pc)
{
LCD_CmdWrite(0×80); // 1st line, 1st position
while(*pc != NULL){
if (*pc == ‘\n’){ // next line?
LCD_CmdWrite(0xc0); // 2nd line, 1st position
}
else{
LCD_DataWrite(*pc); // put character
}
pc++;
}
}
void LCD_Blink(void)
{ unsigned char aaa;
SET_LCD_BL_CTRL;
for(aaa = 0; aaa < 100; aaa++) // delay 1s
LCD_delay_10ms();
RES_LCD_BL_CTRL;
for(aaa = 0; aaa < 50; aaa++) // delay 1s
LCD_delay_10ms();
}
void LCD_Light_On(void)
{ unsigned char aaa;
SET_LCD_BL_CTRL;
for(aaa = 0; aaa < 50; aaa++) // delay 1s
LCD_delay_10ms();
}
void LCD_Light_Off(void)
{ unsigned char aaa;
RES_LCD_BL_CTRL;
for(aaa = 0; aaa < 50; aaa++) // delay 1s
LCD_delay_10ms();
}
Tulisan terkait: