PIC24-LCD Kullanımı

Merhaba,
Bu bölümde PIC24F serisi ile 4×20 LCD’ye nasıl sürüleceğini gösteren bir program ekliyeceğim. Yararlı olacağını umuyorum.
/*
Aykut ULUSAN
Elektronik Mühendisi
IZMIR
www.uicroarm.com
işlemci : PIC24Fj32
osc: dahili fast osc, 8mhz, TCY=1us/8=125ns
LCD busy flag kullanılmayacak, 4bit interface
Board : EXPKITS EX16L-A (PIC24F ve PIC33F geliştirme boardu)
*/
#include <p24Fxxxx.h> //
_CONFIG1(0x3F20) // JTAG, WDF, flash memory'ye yazma kullanılmayacak
_CONFIG2(0x78EF) // Dahili fast rc osc enable, xtal uçları boş olacak
//_CONFIG2( IESO_OFF & FNOSC_PRI & FCKSM_CSDCMD & OSCIOFNC_OFF & IOL1WAY_ON & I2C1SEL_PRI & POSCMOD_HS );
//_CONFIG1( JTAGEN_OFF & GCP_OFF & GWRP_OFF & BKBUG_OFF & COE_OFF & ICS_PGx1 & WINDIS_OFF );
#define LCD_RS LATBbits.LATB8 // lcd için register select pin
#define LCD_E LATBbits.LATB5 // lcd enable pin
#define LCD_PORT LATB
#define LCD_TRIS TRISB
#define d 1 // lcd'ye data/karakter yazılacak
#define c 0 // lcd'ye komut yazılacak
#define D d // lcd'ye data/karakter yazılacak
#define C c // lcd'ye komut yazılacak
#define NOP() {__asm__ volatile ("nop");}
#define CLRWDT() {__asm__ volatile ("clrwdt");}
#define SLEEP() {__asm__ volatile ("pwrsav #0");}
#define IDLE() {__asm__ volatile ("pwrsav #1");}
#define LINE_1 0x80
#define LINE_2 0xC0
#define LINE_3 0x94
#define LINE_4 0xD4
/*
LCD PIC24
D4 PORTBbits.RB0
D5 PORTBbits.RB1
D6 PORTBbits.RB2
D7 PORTBbits.RB3
*/
// X*10US
void
DLY_10US(unsigned int us){
while(us--) {
__asm__ volatile ("repeat #33");
__asm__ volatile ("nop");
}
}
// 65536ms'ye kadar
void
DLY_MS(unsigned int ms){
while(ms--){
DLY_10US(100);
}
}
void
LCD_ENABLE(void){
LCD_E=1;asm("nop");asm("nop");asm("nop");asm("nop");LCD_E=0;
}
void
LCD_WRITE(unsigned char da_ta, unsigned char D_C){
if(D_C) LCD_RS=1; // data modu
else LCD_RS=0; // komut modu
LCD_PORT&=0x3F0;
LCD_PORT|=da_ta>>4; // lcd'ye gönder, RS ve E bozulmamalı
LCD_ENABLE(); // enable
// while(1);
LCD_PORT&=0x3F0;
LCD_PORT|=da_ta&0x0F; // lcd'ye gönder
LCD_ENABLE(); // enable
DLY_10US(50); // 50us delay
LCD_RS=0;
}
void
LCD_SETUP(void){
DLY_MS(100); // lcd'nin kendini hazırlaması için 100ms
LCD_PORT=0x3;
LCD_ENABLE();
DLY_MS(10);
LCD_ENABLE();
DLY_MS(10);
LCD_ENABLE();
DLY_MS(10);
LCD_PORT=0x2;
LCD_ENABLE();
DLY_MS(10);
LCD_WRITE(0x28,c); // disp on, cursor off
LCD_WRITE(0x0c,c); //
LCD_WRITE(0x06,c); //
LCD_WRITE(0x01,c); // clear display
LCD_WRITE(0x02,c); //
DLY_MS(2);
}
void
LCD_STRING(const char *str){
while(*str)
LCD_WRITE(*str++,d);
}
void
LCD_GOTO(unsigned char satir,unsigned char adres){
LCD_WRITE(satir+adres,c);
}
int
main(void){
AD1PCFG=0xFFFF; // portlar dijital
PMCON=0;
PORTB=0; // latch'leri temizle
TRISB=0; asm("NOP"); // B portu çıkış
LCD_SETUP();
LCD_GOTO(LINE_1,0);
LCD_STRING("www.uicroarm.com");
LCD_GOTO(LINE_2,0);
LCD_STRING("PIC24 by Kutay");
while(1);
return(0);
}
