Thursday, 13 September 2012

ALPHANUMERIC LCD INTERFACE WITH AT89C2051




/*************************************************************************
This software is a freeware,
                Developed By: Sarmad Tanwir
                Modified By: Khalid Nawaz Khan,
                @ VisualSoft pvt ltd, Rawalpindi, Pakistan.
                Version: 1.0.0
                Released On: 18th May, 2001
*************************************************************************

 This piece of code provides c51 functions for controlling HD44780 based LCD modules using AT9C2051 microcontroller with 8 bit interface at general purpose I/O port. Any C51 program may use these functions by just including these functions. These functions help the programmer to execute following tasks:
1. Clear or reset the display using ResetLCD function
2. Print text on the LCD using PrintLCD and PutCharLCD functions
3. Print characters or strings at user specified locations using LocateLCD function
4. Scroll the displayed text at different speeds and in different directions using ScrollLCD function
5. Shift the displayed text left or right using ShiftLCD function
6. Switch to second line of the LCD using NextLineLCD function
************************************************************************
The use of some of these functions is described below
ResetLCD function
Always used as
ResetLCD();
It resets the LCD into four bit data transfer and two line display mode, clears the LCD by writing space to all addresses and returns the cursor to the home position (i.e line 1, position 1).

PrintLCD function
Input: A null terminated (i.e. with appended \0) string in double quotes. No Return Value
Output: Prints the string on LCD starting at current cursor location
Example: PrintLCD("Welcome to VS\0");
NOTE: FOR STANDARD 'C' STRING FORMATTING USING sprintf FUNCTION BY INCLUDING 'stdio.h' IN YOUR CODE
Example:
char buffer[10];
int Integer = 10;
float Decimal = 4.43;
.
.
sprintf(buffer,"Sum = %d, Quotient = %f\0", Integer, Decimal)
PrintLCD(buffer);
Note that null termination ('\0') of the string has been catered for.

PutCharLCD function
Input: Single character in single quotes or ASCII code without quotes. No Return Value
Output: Prints the given character at current cursor location
Examples:
PutCharLCD('A');
PutCharLCD(0x41);
Both lines print the character 'A' on the lcd screen

ScrollLCD function
Inputs:
1. Direction ('r', 'l' for right and left respectively)
2. Speed (‘f’, 'm',’s’ for fast, medium and slow respectively)
3. No of repetitions (integer)
Output: Scrolls the displayed data according to the abovementioned inputs.
Example:
ScrollLCD('r','f',3);
Scrolls the contents of LCD 3 times towards right side at faster speed

LocateLCD function
Inputs:
1. Line no. (1 or 2)
2. Position (1 to 40 while only 1-16 are visible. Rest must be scrolled for viewing)
Output:
Takes the cursor to the specified location
Examples:
LocateLCD (1, 5);
PutCharLCD ('u');
Prints the character ‘u’ at line 1 position 5. Whereas
LocateLCD (2, 3);
PrintLCD("VS\0");
Prints 'VS' starting from position 3 on line 2

NextLineLCD function
Always used as
NextLineLCD();
Shifts from line 1 to line 2

ShiftLCD function
Input: Direction ('r' for right, 'l' for left)
Shifts the entire text once in the specified direction
***************************************************************************

Suggestions for further development:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

A controlled 'Highlighting' function is needed for such application as highlighting selected text on the LCD.
A 'Vertical Scroll' function for paragraph display may also be developed
It is requested that any further improvements in this software should be brought to the notice of the developer before the release of a newer version is considered.
Instructions for Distribution
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Distribution of LCD manufacturer's application notes and data sheets along with this C51 function library is recommended as it will greatly benefit the user.
***************************************************************************/

                                         (COMPLETE CODE in keil uV2 c51)

//***************************************************************************

#include <AT892051.H>
#include <intrins.h>

#define Hi 1
#define Lo 0

#define RegSel P3_0
#define RdWrt P3_1
#define Enable P3_7

#define DataPort P1

void ResetLCD (void);
void Reset_LCD (void);
void LocateLCD (unsigned char,unsigned char);
void PutCharLCD (unsigned char);
void PrintLCD (char string[]);
void ShiftLCD (char dir);
void ScrollLCD (char dir,char speed,int limit);
void NextLineLCD (void);


void DelayLCD (void);
void WriteLCD (unsigned char);
void WriteHiLCD (unsigned char);
void WriteLoLCD (unsigned char);

void main (void)
{
                int ii;
                Reset_LCD();
                LocateLCD(1,1);
                PrintLCD("In the Name Of Allah");
                NextLineLCD();
                PrintLCD("    Allah-O-Akbar");
                for (ii=0;ii<100;ii++)
                DelayLCD();
                LocateLCD(1,1);
                PrintLCD("My LCD Demo.........");
                NextLineLCD();
                PrintLCD("Hello World.........");
                for (ii=0;ii<10;ii++)
                                DelayLCD();
                ScrollLCD('r','f',10);
                while(1);
}


void ResetLCD (void)
{
  RegSel = Lo;
  RdWrt = Lo;
  WriteLCD(0x38);DelayLCD();//0x28 for 4 bits
  WriteLCD(0x06);DelayLCD();
  WriteLCD(0x0c);DelayLCD();
  WriteLCD(0x01);DelayLCD();
}

void Reset_LCD (void)
{
  ResetLCD ();
  ResetLCD ();
  ResetLCD ();

void LocateLCD (unsigned char line,unsigned char pos)
{
                RegSel = Lo;
                if (line == 1)
                WriteLCD(0x7f+pos);
                if (line == 2)
                WriteLCD(0xbf+pos);
}

void PutCharLCD(unsigned char byte)
{
   RegSel = Hi;
   WriteLCD(byte);
}

void PrintLCD(char String[])
{
   int i = 0;
   do
   {
                  PutCharLCD(String[i]);
     i++;
                } while(String[i] != '\0');
}

void ShiftLCD(char Dir)
{
   RegSel = Lo;
                if (Dir == 'l') WriteLCD(0x18);
                if (Dir == 'r') WriteLCD(0x1C);
}

void ScrollLCD (char Dir,char Speed,int Limit)
{
                int i,j,k;
                RegSel = Lo;
                RdWrt = Lo;

                for (j=0;j<Limit;j++)
                {
                                for (i=0;i<40;i++)
                                {
                                                if (Dir == 'l') WriteLCD(0x18);
                if (Dir == 'r') WriteLCD(0x1C);
                if (Speed == 'f')
                {for (k=0;k<=15;k++) DelayLCD();}
                if (Speed == 'm')
                {for (k=0;k<=40;k++) DelayLCD();}
                if (Speed == 's')
                {for (k=0;k<=75;k++) DelayLCD();}
      }
                }
}



void NextLineLCD(void)
{
                RegSel = 0;
                WriteLCD(0xC0);
}

void DelayLCD (void)
{
                int del1,del2;
                for(del1=0;del1<10;del1++)
                {
                for(del2=0;del2<200;del2++);
                }
}

void WriteLCD(unsigned char Val)
{
                Enable = Hi;
                DataPort = Val;
                Enable = Lo;
                DelayLCD();
}

No comments:

Post a Comment