/************ LCD.c *********/
#include "LCD.h"
#include "stdio.h"
/*******************************************************************/
/* -SetUpLCD ( )
/* configures the LCD pins (described in LCD.h), and initialize
/* the HD44780 Hitachi compatible module.
/*
/* -LCDSendNibble ( n )
/* -n
/* pasees the high and low nibble to be writen on Data Port,
/* mainly from the LCDSendByte ( Address, Data ) function.
/*
/* -LCDSendByte ( Address, Data )
/* -Address
/* -Data
/* pasees a Byte to be writen on Data Port.
/*
/* -LCDPutCh ( *buffer )
/* -*Buffer
/* displays the Character (as pointer) on the Display, using the .
/* LCDSendByte ( ) function.
/*
/* -LCDGotoXY ( x, y )
/* -x
/* -y
/* sets the x axis and y axis on the 2x16 HD44780 Hitachi
/* compatible module.
/*
/* -ClearLCD ( )
/* clears up the display.
/*
/* Note1: this library will SetUp the 2x16 HD44780 Hitachi
/* compatible module as 2 lines, 5x7 dots, auto increment, cursor off
/* as default.
/* Note2: this library is intended to work with a 2x16 HD44780 Hitachi
/* compatible module, even with the COG LCDs.
/* The functions were taken from the CCS library and make them
/* compatible with the XC8 Compiler.
/* Fabian H Rodriguez */
/******************************************************************/
/******* Set ups a HD44780 Hitachi compatible module **************/
void SetUpLCD( void )
{
unsigned char i;
TRIS_DATA_PORT &= 0x80; // Sets the PortData and RS, Enable
DATA_PORT &= 0x80; // and LCD BK pins as outputs,
// Command Mode (RS = 0).
// it impedes operation of the LCD (E = 0).
__delay_ms ( 15 );
BL_PIN = HIGH; // Turn On the LCD's Back Light.
for ( i = 0; i <= 2; i ++ )
{
LCDSendNibble ( 0x03 ); // Send three time the same code ( 0011 )
__delay_ms ( 5 ); // to the data port, first delayed > 4.1 ms,
} // the second one of 100 us, third one don't matter.
LCDSendNibble ( 0x02 ); // Send 0010 code to data port.
for ( i = 0; i <= 3; ++i ) // 4bits data, 5x7 dots, display on, cursor off, autoincrement.
LCDSendByte ( COMMAND, LCD_SET_UP [ i ] );
}
/***** Pasees a Nibble to be writen on Data Port **************/
void LCDSendNibble ( unsigned char n )
{
unsigned char temp;
temp = ( n | ( 0xF0 & DATA_PORT ) );// Passing the nibble "n"
// without affecting the MSB on PORTx.
// DATA_PORT = n;
DATA_PORT = temp; // Pass the "temp" value to the
// DATA_PORT.
__delay_us ( 1 ); // Generate a pulse on Enable pin
E_PIN = HIGH; // to let know the LCD an action
__delay_us ( 2 ); // has been done.
E_PIN = LOW;
}
/********* Pasees a Byte to be writen on Data Port **************/
void LCDSendByte ( unsigned char address, char data )
{
unsigned char temp;
// DATA_PORT = 0x00; // clear data_port.
DATA_PORT &= 0xF0; // clear DATA_PORT without affecting MSB on PORTx.
RS_PIN = address; // write command.
if ( address ) // Here is the trick: to avoid read the LCD's status
__delay_us ( 50 ); // most know which action was performed (Command or
// Write) to do a delay as long as the action were
else // performed.
__delay_ms ( 2 );
E_PIN = LOW;
temp = DATA_PORT & 0xF0; // Save the PORT_DATA status to a "temp" var.
if ( data == '°' ) // "°" symbol if need it.
data = 0xDF;
temp |= ( ( data >> 4 ) & 0x0F ); // pass first nibble to data_port.
LCDSendNibble ( temp );
temp &= 0xF0; // clear data_port.
temp |= (data&0x0f); // pass first nibble to data_port.
LCDSendNibble ( temp );
}
/********* Puts a Character as a pointer on the Display **************/
void LCDPutCh ( const char *buffer )
{
while ( *buffer ) // Write data to LCD up to null found.
{
LCDSendByte ( WRITE, *buffer ); // Write character to LCD.
buffer ++; // Increment buffer.
}
return;
}
/********* Set coordinates for the 2 x 16 Display **************/
void LCDGotoXY ( char x, char y )
{
char Address;
if ( y != 1 ) // If 'y' axie different of line one go to
Address = 0x40; // LCD's line two.
else
Address = 0x00; // If not, go to first memory address.
Address += x-1; // On axie 'x' add an address as need it.
LCDSendByte ( COMMAND, 0x80 | Address );
}
/********* Clears Up the Display **************/
void ClearLCD ( void )
{
LCDSendByte ( COMMAND, CLEAR_DISPLAY );
}