lcd code
well, i have found the 8 bit lcd driver.
however, the program i write do not display text.
can tell me what's wrong?
the main() is the program i wrote while the rest of the fuction is 8 bit driver.
#INCLUDE<16F877a.H>
#include<string.h>
#use delay(clock=40000000)
#include<stdio.h>
#define IR 0
#define DR 1
#define READ 1
#define WRITE 0
#define lcd_2X16
/* Defines the bits for Port C */
struct {
int unused:5; //The first 5 bits are not used by the LCD
int en:1; //EN is the 6th bit of Port C RC5
int rs:1; //RS is the 7th bit of Port C RC6
int rw:1; //RW is the 8th bit of Port C RC7
}LCDControl;
#byte LCDData = 0x08 //Defines the address of the variable LCDData
//as that of Port D
#byte LCDControl = 0x07 //Defines the address of the structure
//LCDControl as that of Port C
#byte LCDDataDir =0x88 //Defines the address of the variable
//LCDDataDir as that of TrisD
#byte LCDConDir = 0x87 //Defines the address of the variable
//LCDConDir as that of TrisC
#define LCD_DATA_IN LCDDataDir|=0xFF //to set the data lines as input
#define LCD_DATA_OUT LCDDataDir&=0x00 //to set the data lines as output
#define LCD_CON_OUT LCDConDir&=0x1F //to set the control lines as output
/************************The WriteByte function *******************/
/*
This function writes a byte to the LCD module with an 8-bit
interface
Input Parameters:
int rs This variable selects the register being written to.
DR selects the data register
IR selects the instruction register
int data_to_lcd This variable stores the data that will be written to
the selected register
*/
void WriteByte(short int rs, int data_to_lcd)
{
LCD_DATA_OUT; //LCD Data Bus is an output
LCDControl.rw = WRITE; //The operation is a write operation
LCDControl.rs = rs; //Selects the register (DR or IR)
delay_us(1); //Wait a minimum of 60ns
LCDControl.en = 1; //Raise EN
LCDData = data_to_lcd; //Set the Data Bus to the desired value
delay_us(1); //Wait a minimum of 195ns
LCDControl.en = 0; //Clear EN
delay_us(1); //Keep RS and RW at their current states for a
//minimum of 20ns
//Also, keep the current value at the Data Bus
//for a minimum of 10ns
}
/************************The ReadByte function *******************/
/*
This function reads a byte from the LCD module with an 8-bit
interface
Input Parameters:
int rs This variable selects the register being read from.
DR selects the data register
IR selects the instruction register
Output Value: The function returns the value of the byte read
*/
int ReadByte(short int rs)
{
int data_from_lcd; //This variable is used to store the byte
//read from the Data Bus
LCD_DATA_IN; //Port D is an input port
LCDControl.rw = READ; //The operation is a read operation
LCDControl.rs = rs; //Selects the register (DR or IR)
delay_us(1); //Wait a minimum of 60ns
LCDControl.en = 1; //Raise EN
delay_us(1); //Wait a minimum of 360ns
data_from_lcd = LCDData;//Read the value across the Data Bus
LCDControl.en = 0; //Clear EN
delay_us(1); //Keep RS and RW at their current states
//for a minimum of 20ns
return data_from_lcd;
}
//This function reads the IR register, returning 1 if the LCD module is busy or 0 if it is not
/************************The CheckBusyFlag function ******************/
/* This function reads a byte from the instruction register and
tests the 8th bit, which is the busy Flag
Output Value: The function returns
1 if the Busy Flag is set (LCD module busy)
0 if the Busy Flag is clear (LCD module is not
busy)
*/
short int CheckBusyFlag(void)
{
int data_from_lcd; //This variable is used to store the byte
//read from the LCD
data_from_lcd = ReadByte(IR); //Read the IR (rs=0)
return (bit_test(data_from_lcd,7)); //Test the BF
//Return 1 if set
//Return 0 if clear
}
/************************The InitLCD function *******************/
/* This function initialises the LCD module (Initialisation by
instruction).
Initialisation Parameters:
Interface 8-bit
Number of display lines 2-line or 4-line
Cursor shift direction Increment
Font size 5x8dots
Display On
Cursor Off
Cursor blink Cursor blink
*/
void InitLCD(void)
{
delay_ms(15); //Delay a minimum of 15ms
WriteByte(IR,0b00111000); //Define function set
//8-bit interface, 2-line or 4-line display, 5x8 font
delay_ms(5); //Delay a minimum of 4.1ms
WriteByte(IR,0b00111000); //Redefine function set
delay_us(100); //Delay a minimum of 100us
WriteByte(IR,0b00111000); //Redefine function set
while(CheckBusyFlag()); //Wait until BF = 0
WriteByte(IR,0b00001100); //Define display on/off control
//display on, cursor off, cursor blink off
while(CheckBusyFlag()); //Wait until BF = 0
WriteByte(IR,0b00000001); //Clear Display
while(CheckBusyFlag()); //Wait until BF = 0
WriteByte(IR,0b00000110); //Entry mode set
//cursor direction increment, do not shift display
}
// WRITING A CHARACTER TO LCD
/************************The WriteChar function *******************/
/* This function displays a character on the LCD.
Input Parameters:
char character This variable stores the character to be displayed on
the LCD.
*/
void WriteChar(char character)
{
while(CheckBusyFlag()); //Wait until the LCD module is not busy
WriteByte(DR,character);//Write character to DR
}
// SENDING A COMMAND TO LCD
/************************The PutCommand function *******************/
/* This function writes a byte to the instruction register.
Input Parameters:
int command This variable stores the byte to be written to the
instruction register.
*/
void PutCommand(int command)
{
while(CheckBusyFlag()); //Wait until the LCD module is not busy
WriteByte(IR,command); //Write command to IR
}
/************************The GoToLine function *******************/
/* This function sets the cursor to the first position of a
specified line of the LCD.
Input Parameters:
int line This variable selects the LCD line on which the
cursor is to be set.
*/
void GoToLine(int line)
{
int address; //This variable is used to determine the
//address at which the cursor is to be set
switch (line) //Set address to the first DDRAM address of the
//specified line
{
case 1:
address = 0x00;
break;
case 2:
address = 0x40;
break;
case 3:
address = 0x14;
break;
case 4:
address = 0x54;
break;
default: //An undefined line set the cursor home
address = 0x00;
break;
}
bit_set(address,7); //Bit 7 identifies the instruction as Set
//DDRAM address
PutCommand(address); //Set the DDRAM address
}
//WRITING A STRING OF CHARACTERS TO LCD
#define TOTAL_CHARACTERS_OF_LCD 32
void WriteString(char LineOfCharacters[TOTAL_CHARACTERS_OF_LCD])
{
printf(WriteChar,"%c", LineOfCharacters);
}
void main ()
{
char LineOfCharacters[4]={"abc"};
InitLCD();
GoToLine(2);
printf(WriteChar,"%c", LineOfCharacters);
}