#include<P18F4520.h>
#pragma config OSC = HS
#pragma config WDT=OFF, LVP=OFF, MCLRE = OFF
#define RS PORTDbits.RD4
#define E PORTDbits.RD5
#define LCDdata PORTD
char msg[] = "Hello";
void DelayFor18TCY( void )
{
Delay10TCYx(2); // 5us delay
return;
}
void DelayPORXLCD (void)
{
Delay1KTCYx(75); // Delay of 15ms
// Cycles = (TimeDelay * Fosc) / 4
// Cycles = (15ms * 20MHz) / 4
// Cycles = 75,000
return;
}
void DelayXLCD (void)
{
Delay1KTCYx(25); // Delay of 5ms
// Cycles = (TimeDelay * Fosc) / 4
// Cycles = (5ms * 20MHz) / 4
// Cycles = 25,000
return;
}
void SendLCDdata(char data,char rs)
{
LCDdata=data>>4;
RS = rs;
E = 1;
E = 0;
Delay10TCYx(20); // 50us delay
LCDdata = data&0x0F;
RS = rs;
E = 1;
E = 0;
Delay10TCYx(20);
}
void InitLCD(void)
{
char a;
Delay1KTCYx(100); //20ms
for(a=0;a<3;a++)
{
SendLCDdata(0x20,0);
Delay1KTCYx(30); //5ms
}
SendLCDdata(0x28,0);
SendLCDdata(0x01,0);
Delay1KTCYx(10); //2ms
SendLCDdata(0x0C,0);
SendLCDdata(0x06,0);
}
void String(char *str, char position)
{
int ptr = 0;
SendLCDdata(position,0);
while(str[ptr]!=0)
SendLCDdata(str[ptr++],1);
}
void main(void)
{
TRISA = 0xFF;
TRISB = 0x0F;
TRISC = 0x00;
TRISD = 0x00;
TRISE = 0;
PORTA = 0;
PORTB = 0;
PORTC = 0;
PORTD = 0;
PORTE = 0;
InitLCD();
while(1)
{
String(msg,0x80);
Delay10TCYx(20);
}
}