#include <p18f4220.h>
#include <delays.h>
#pragma config OSC = HS
#pragma config WDT = OFF
#pragma config LVP = OFF
void SendLCD(unsigned char Byte, unsigned char type);
#define LCD LATD //LCD Latch PORT
#define LCD_RS LATEbits.LATE0 //Register Select (0: Instruction/ 1: Data)
#define LCD_E LATEbits.LATE2 //Enable (Starts data read/write)
void main (void)
{
ADCON1 = 0x0F; //SET ALL PORTS as DIGITAL I/O
PORTD = 0; //initiallize PORTD
TRISD = 0x00; //LCD output
PORTE = 0; //initiallize PORTE
TRISEbits.TRISE0 = 0; //LCD Register Select (to LCD Pin 4)
TRISEbits.TRISE2 = 0; //LCD Enable (to LCD Pin 6)
//Initialize the LCD
LCD = 0x00; //clear LCD PORTD pins
Delay1KTCYx(100); //delay 20mS
SendLCD(0x03,0); //Initialization command
Delay1KTCYx(25); //delay 5mS
SendLCD(0x03,0); //Initialization command
Delay10TCYx(75); //delay 150uS
SendLCD(0x03,0); //Initialization command
Delay10TCYx(75); //delay 150uS
SendLCD(0x3C,0); //Interface lenght is 8 bits long, 2-lines, 5x10 dots
Delay10TCYx(25); //delay 50uS
SendLCD(0x10,0); //Turn off LCD
Delay10TCYx(25); //delay 50uS
SendLCD(0x01,0); //Clear LCD
Delay10TCYx(25); //delay 50uS
SendLCD(0x06,0); //Increment the cursor after each byte written
Delay10TCYx(25); //delay 50uS
SendLCD(0x0F,0); //Turn on LCD, cursor on, cursor blinking on
Delay10TCYx(25); //delay 50uS
SendLCD(0x80,0); //Send 0x80 to set line to 1
Delay10TCYx(25); //delay 50uS
SendLCD('a',1); //print letter 'a' on LCD
while(1); //loop forever
} //end of main()
void SendLCD(unsigned char Byte, unsigned char type)
{
LCD_RS = type; //Set whether it is a Command (0) or Data/Char (1)
LCD = 0x00; //Clear the LCD PORTD
LCD = Byte; //assign the new data to the LCD PORTD
//Toggle the E(enable)
LCD_E = 1; //Set Enable High
//Delay 5uS
Nop();
Nop();
Nop();
Nop();
Nop();
Nop();
Nop();
Nop();
Nop();
Nop();
Nop();
Nop();
Nop();
Nop();
Nop();
Nop();
Nop();
Nop();
Nop();
Nop();
Nop();
Nop();
Nop();
Nop();
Nop();
LCD_E = 0; //Set Enable Low
}