// **************************************************************
// *** LCD Driver V1.0 ***
// *** Written By P. Fletcher-Jones 9/5/01 ***
// *** Last MOD 9/5/01 ***
// *** Compiled with ImageCraft C compiler Version 6.21E ***
// **************************************************************
#include "LCD.h"
#include <iom8515v.h>
#include <macros.h>
// ***** Define I/O pins ***** //
#define BIT7 0x80
#define BIT6 0x40
#define BIT5 0x20
#define BIT4 0x10
#define BIT3 0x08
#define BIT2 0x04
#define BIT1 0x02
#define BIT0 0x01
#if 0
// *** Set port for LCD Data Bus 8 bit mode *** //
#define LCD_OP_PORT PORTA
#define LCD_IP_PORT PINA
#define LCD_DIR_PORT DDRA
// *** Set LCD Enable Port and Bit values *** //
#define LCD_EN_PORT PORTC
#define EN_BIT BIT7
// *** Set LCD Register Select Port and Bit values *** //
#define LCD_RS_PORT PORTC
#define RS_BIT BIT6
// *** Set LCD Read/Write Port and Bit values *** //
#define LCD_RW_PORT PORTC
#define RW_BIT BIT5
#endif
volatile unsigned char *LCD_EN_PORT = &PORTC;
volatile unsigned char *LCD_DIR_PORT = &DDRA;
volatile unsigned char *LCD_IP_PORT = &PINA;
volatile unsigned char *LCD_OP_PORT = &PORTA;
volatile unsigned char *LCD_RS_PORT = &PORTC;
volatile unsigned char *LCD_RW_PORT = &PORTC;
char LCD_EN_BIT = BIT(7);
char LCD_RS_BIT = BIT(6);
char LCD_RW_BIT = BIT(5);
#define SET_LCD_E *LCD_EN_PORT |= LCD_EN_BIT
#define CLEAR_LCD_E *LCD_EN_PORT &= ~LCD_EN_BIT
#define SET_LCD_DATA *LCD_RS_PORT |= LCD_RS_BIT
#define SET_LCD_CMD *LCD_RS_PORT &= ~LCD_RS_BIT
#define SET_LCD_READ *LCD_RW_PORT |= LCD_RW_BIT
#define SET_LCD_WRITE *LCD_RW_PORT &= ~LCD_RW_BIT
#define LCD_ON 0x0C
#define LCD_CURS_ON 0x0D
#define LCD_OFF 0x08
#define LCD_HOME 0x02
#define LCD_CLEAR 0x01
#define LCD_NEW_LINE 0xC0
#define LCD_FUNCTION_SET 0x38
#define LCD_MODE_SET 0x06
//*****************************************************//
// This routine will return the busy flag from the LCD //
//*****************************************************//
unsigned char LCD_Busy ( void )
{
unsigned char temp;
CLEAR_LCD_E; // Disable LCD
*LCD_DIR_PORT = 0x00; // Make I/O Port input
SET_LCD_READ; // Set LCD to READ
SET_LCD_CMD; // Set LCD to command
SET_LCD_E; //
asm("nop");
temp = *LCD_IP_PORT; // Load data from port
CLEAR_LCD_E; // Disable LCD
return temp; // return busy flag
}
// ********************************************** //
// *** Write a control instruction to the LCD *** //
// ********************************************** //
void LCD_WriteControl (unsigned char CMD)
{
while (LCD_Busy()& 0X80); // Test if LCD busy
CLEAR_LCD_E; // Disable LCD
SET_LCD_WRITE ; // Set LCD to write
SET_LCD_CMD; // Set LCD to command
*LCD_DIR_PORT = 0xFF; // LCD port output
*LCD_OP_PORT = CMD; // Load data to port
SET_LCD_E; // Write data to LCD
asm("nop");
CLEAR_LCD_E; // Disable LCD
}
// ***************************************** //
// *** Write one byte of data to the LCD *** //
// ***************************************** //
void LCD_WriteData (unsigned char Data)
{
while (LCD_Busy() & 0X80); // Test if LCD Busy
CLEAR_LCD_E; // Disable LCD
SET_LCD_WRITE ; // Set LCD to write
SET_LCD_DATA; // Set LCD to data
*LCD_DIR_PORT = 0xFF; // LCD port output
*LCD_OP_PORT = Data; // Load data to port
SET_LCD_E; // Write data to LCD
asm("nop");
CLEAR_LCD_E; // Disable LCD
}
// ********************************* //
// *** Initialize the LCD driver *** //
// ********************************* //
void Init_LCD(void)
{
LCD_WriteControl (LCD_FUNCTION_SET);
LCD_WriteControl (LCD_OFF);
LCD_WriteControl (LCD_CLEAR);
LCD_WriteControl (LCD_MODE_SET);
LCD_WriteControl (LCD_ON);
LCD_WriteControl (LCD_HOME);
}
// ************************************************ //
// *** Clear the LCD screen (also homes cursor) *** //
// ************************************************ //
void LCD_Clear(void)
{
LCD_WriteControl(0x01);
}
// *********************************************** //
// *** Position the LCD cursor at row 1, col 1 *** //
// *********************************************** //
void LCD_Home(void)
{
LCD_WriteControl(0x02);
}
// ****************************************************************** //
// *** Display a single character, at the current cursor location *** //
// ****************************************************************** //
void LCD_DisplayCharacter (char Char)
{
LCD_WriteData (Char);
}
// ********************************************************************* //
// *** Display a string at the specified row and column, using FLASH *** //
// ********************************************************************* //
void LCD_DisplayString_F (char row, char column ,const unsigned char *string)
{
LCD_Cursor (row, column);
while (*string)
LCD_DisplayCharacter (*string++);
}
// ******************************************************************* //
// *** Display a string at the specified row and column, using RAM *** //
// ******************************************************************* //
void LCD_DisplayString (char row, char column ,unsigned char *string)
{
LCD_Cursor (row, column);
while (*string)
LCD_DisplayCharacter (*string++);
}
// *************************************************** //
// *** Position the LCD cursor at "row", "column". *** //
// *************************************************** //
void LCD_Cursor (char row, char column)
{
switch (row) {
case 1: LCD_WriteControl (0x80 + column - 1); break;
case 2: LCD_WriteControl (0xc0 + column - 1); break;
case 3: LCD_WriteControl (0x94 + column - 1); break;
case 4: LCD_WriteControl (0xd4 + column - 1); break;
default: break;
}
}
// ************************** //
// *** Turn the cursor on *** //
// ************************** //
void LCD_Cursor_On (void)
{
LCD_WriteControl (LCD_CURS_ON);
}
// *************************** //
// *** Turn the cursor off *** //
// *************************** //
void LCD_Cursor_Off (void)
{
LCD_WriteControl (LCD_ON);
}
// ******************** //
// *** Turn Off LCD *** //
// ******************** //
void LCD_Display_Off (void)
{
LCD_WriteControl(LCD_OFF);
}
// ******************* //
// *** Turn On LCD *** //
// ******************* //
void LCD_Display_On (void)
{
LCD_WriteControl(LCD_ON);
}
void delay(int k)
{
int i;
for (i=0;i<800*k;i++) i++;
}