#define LCDPORT LATA
#define LCDTRIS TRISA
#define LCD_RS LATBbits.LATB4
#define LCD_TRIS_RS TRISBbits.TRISB4
#define LCD_E LATBbits.LATB1
#define LCD_TRIS_E TRISBbits.TRISB1
void Delay(int);
void WriteCommand(unsigned char);
void WriteChar(unsigned char);
void WriteNibble(unsigned char);
void WaitLCDBusy(void);
void WriteLCD(unsigned char);
void InitLCD(void);
void PutMessage(rom char*);
void PutMessageAt(unsigned char,unsigned char,rom char*);
void PutHex(unsigned char);
void PutNibble(unsigned char);
//Write a string to the LCD
void PutMessage(rom char *Message){
while(*Message!=0)
WriteChar(*Message++);
}
//Set position and write string.
void PutMessageAt(unsigned char X,unsigned char Y,rom char *Message){
WriteCommand(0x80+(--Y<<6)+--X);
while(*Message!=0)
WriteChar(*Message++);
}
void PutHex(unsigned char Num){
PutNibble(Num>>4);
PutNibble(Num&0x0f);
}
void PutNibble(unsigned char num){
if (num>9){
WriteChar(num+'A'-10);
}
else{
WriteChar(num+'0');
}
}
//Initialises the LCD
void InitLCD(void){
LCDTRIS &= 0xE1; //ensure data bits are output
LCD_E=0; //clear enable
LCD_RS = 0; //going to write command
LCD_TRIS_E=0; //Set enable to output
LCD_TRIS_RS=0; //set RS to output
Delay(30); //delay for LCD to initialise.
WriteNibble(0x30); //Required for initialisation
Delay(5); //required delay
WriteNibble(0x30); //Required for initialisation
Delay(1); //required delay
WriteCommand(0x20); //set to 4 bit interface
WriteCommand(0x2c); //set to 4 bit interface, 2 line and 5*10 font
WriteCommand(0x01); //clear display
WriteCommand(0x06); //move cursor right after write
WriteCommand(0x0C); //turn on display
}
//wait for the LCD to finish last command.
void WaitLCDBusy(void){
Delay(5);
}
//Send a command to the LCD
void WriteCommand(unsigned char command){
WaitLCDBusy(); //wait until not busy
LCD_RS = 0; //setup to send command
WriteNibble(command>>4); //write the high nibble
WriteNibble(command); //then the low nibble
}
//Send a character to the LCD
void WriteChar(unsigned char chr){
WaitLCDBusy(); //wait until not busy
LCD_RS=1; //Setup to send character
WriteNibble(chr>>4); //write the high nibble
WriteNibble(chr); //then the low nibble
}
//Send any 4 bits to the LCD
void WriteNibble(unsigned char command){
LCDPORT &= 0xE1; //clear the data bits
LCDPORT |= ((command<<1) & 0x1E); //or in the new data
LCD_E = 1; //enable the LCD interface
LCD_E = 0; //disable it
}
// delay mS at 20MHz
void Delay(int Time){
int i,j;
for(j=0;j<Time;j++)
for(i=0;i<275;i++);
}