LCD use in many device to display message, like in printer, coffee machine, remote etc.There are a lot of type LCD present in market, these are may be alphanumeric, graphical or any other type. Here I will discuss about the alphanumeric LCD.
Alphanumeric LCD comes in different size 8*1, 8*2, 16*1, 16*2 or 20*4 etc. These LCD only able to display characters, who have the ASCII value. Some alphanumeric LCD provide the facility to generate own custom characters.
In this article I will describe the steps to display moving message on 16*2 alphanumeric LCD, but before it I want to discuss about its pin configuration and connection.
Pin configuration of 16*2 alphanumeric LCD
Pin | Description of Pin |
1. | VSS (Ground Pin). |
2. | VCC (+3.3 to +5V). |
3. | VEE (Use to adjust the contrast). |
4. | RS (Resistor select pin).
RS = 0; select command resistor. RS = 1; select data resistor. |
5. | R/W (Read and Write Pin)
R/W = 0; write operation. R/W = 1; Read operation. |
6. | E (clock enable pin). |
7. | D0 (I/O) |
8. | D1 (I/O) |
9. | D2 (I/O) |
10. | D3 (I/O) |
11. | D4 (I/O) |
12. | D5 (I/O) |
13. | D6 (I/O) |
14. | D7 (I/O) |
15. | Led (+ve). |
16. | Led (-ve). |
Note: Interface of HD44780 supports two modes operation, 8 bit mode and 4 bit mode.In this article I will only discuss about the 8 bit mode.
Some useful commands for LCD
Command | Description |
0x01 | To clear LCD. |
0x0e | Display on and cursor blink. |
0x0c | Display on and cursor off. |
0x38 | 8 bit mode and 2 line 5*8 matrix. |
0x06 | Increment cursor (shift toward right) |
0x04 | Decrement cursor (shift toward left) |
0x80 | Cursor in beginning of first line. |
0xc0 | Cursor in beginning of second line. |
Steps to write display message on LCD
- Initialize the LCD using the LCD initialization commands.
- Set the address of DDRAM ,where you want to display the character.
- Write the character on the data bus of LCD.
When we pass command or data on LCD then follow following steps.
- If passing the command then make RS pin low (RS = 0) either for data make RS pin high (RS = 1).
- Set Enable pin high (EN = 1).
- Write the command or data on the data bus.
- Set Enable pin low (EN = 0).
If you want to learn more about the c language, here 10 Free days (up to 200 minutes) C video course for you.
Source code to display moving message on 16×2 alphanumeric LCD.
#include<reg51.h> #include<string.h> #define LCD P2 /*Attached Lcd on Port*/ sbit rs =P3^0; /*Configure RS Pin*/ sbit rw=P3^1; /*Configure R/W pin*/ sbit e =P3^2; /*Configure Enable pin*/ /*Function to write command on Lcd*/ void LcdCommand(const char cCommand); /*Function to display message on Lcd*/ void DisplayMessage(const char *pszMessage); /*Function To Initialize Lcd*/ void LcdInit(void); /*Function to Provide delay*/ void Delay(unsigned int); int main() { short siLoop = 0; short siLen = 0; const char *pszDisplayMsg = "Welcome to aticleworld."; siLen = strlen(pszDisplayMsg); /*Calculate length of Message*/ LcdInit(); /*Lcd Initialize*/ while(1) { LcdCommand(0x8f); /*Address of DDRAM*/ DisplayMessage(pszDisplayMsg); for(siLoop=0; siLoop < siLen; siLoop++) { LcdCommand(0x1c); Delay(25); } } } /*Function to write command on Lcd*/ void LcdCommand(const char cCommand) { rs = 0; rw = 0; e = 1; LCD = cCommand; Delay(1); e=0; } /*Function to Display message on Lcd*/ void DisplayMessage(const char *pszMessage) { rs = 1; rw = 0; while(*pszMessage!='\0') { e = 1; LCD = *pszMessage; Delay(1); e=0; pszMessage++; } } /*Function to Provide Delay*/ void Delay(unsigned int i) { int j,k; for(j=0; j<i; j++) { for(k=0; k<1275; k++); } } /*Initialise the LCD*/ void LcdInit(void) { LcdCommand(0x01); LcdCommand(0x38); LcdCommand(0x06); LcdCommand(0x0c); }
Recommended Post:
- Led blinking program in c for 8051.
- Interfacing of switch and led using the 8051
- Interfacing of Relay with 8051 microcontroller
- Moving message display on LCD using 8051
- LCD 4-bit mode c code for 8051.
- Create LCD custom characters for 16×2 alphanumeric LCD
- Interfacing of keypad with 8051
- Electronic digital lock using the 8051
- Interfacing of EEPROM with 8051 microcontrollers using I2C
- Embedded c interview questions.
- 8051 Microcontroller Pin Diagram and Pin Description.
- Can protocol interview questions.
- 8051 Architecture.
Please can this code scroll more than 80 characters, up to 150 characters on the LCD, thanks in anticipation for a quick response
Sir, can you please tell the values of capacitors you used in the circuit
Sir, can you please resend the cicircuit diagram it is not clear
sir please help me with the code i am not able to display character on glcd instead it fill up with the pixel can you help me with this. please brother
void DisplayMessage(const char *pszMessage) //GLCD STRING WRITE FUNCTION.
{
//IOSET1 = RW;
//IOCLR1 = RW;
while(*pszMessage!=’\0′)
{
int column,page;
for(page=0;page<8;page++) // PRINT 16 PAGE / PAGE OF EACH HALF OF DISPLAY.
{
IOSET1 = CS1;
IOCLR1 = CS2;
GLCD_Command(0x40); //set Y ADDRESS (COLUMN=0).
GLCD_Command(0xB8+page);
for (column=0;column<128;column++)
{
if (column==64)
{
IOCLR1 = CS1; // IF YES THEN CHANGE SEGMENT CONTROLLER.
IOSET1 = CS2;
GLCD_Command(0x40); // SET Y ADDRESS (COLUMN=0).
GLCD_Command((0xB8+page));
}
GLCD_Data(*pszMessage++); // print 64 column on each page.
}
}
}
}
above code is the main string msg display code. when i am using hex code instead of character then i am able to get the output . I thik that it is not converting my character to hex code , and the software i am using is the keil micro vision 5 . please do reply with the please brother . Thank you
This is the complete code …..
#include
#include
#define LCD_D0 (1<<24)
#define LCD_D1 (1<<25)
#define LCD_D2 (1<<26)
#define LCD_D3 (1<<27)
#define LCD_D4 (1<<28)
#define LCD_D5 (1<<29)
#define LCD_D6 (1<<30)
#define LCD_D7 (1<<31)
#define RS (1<<23)
#define RW (1<<22)
#define EN (1<<21)
#define CS1 (1<<20)
#define CS2 (1<<19)
#define Total page 8
void delay ();
void DisplayMessage(const char *pszMessage);
void GLCD_Command(int Command) //GLCD function.
{
IOCLR1= (LCD_D0|LCD_D1|LCD_D2|LCD_D3|LCD_D4|LCD_D5|LCD_D6|LCD_D7);
Command = Command<<24;
IOSET1= Command; // COPY COMMAND ON DATA PIN.
IOCLR1 = RS; // Make RS low to select command register.
IOCLR1 = RW; // Make RW low for right operation.
IOSET1 = EN; // MAKE HIGH FOR TRANSMISSION ENABLE PIN.
delay(10);
IOCLR1 = EN;
}
void GLCD_Data(int Data) // GLCD DATA FUNCTION.
{
IOCLR1= (LCD_D0|LCD_D1|LCD_D2|LCD_D3|LCD_D4|LCD_D5|LCD_D6|LCD_D7);
Data = Data<<24;
IOSET1 = Data; // COPY DATA ON DATA PIN.
IOSET1 = RS; // Make RS HIGH to select DATA register.
IOCLR1 = RW; // Make RW low for right operation.
IOSET1 = EN; // MAKE HIGH TO LOW FOR TRANSMISSION ENABLE PIN.
delay(10);
IOCLR1 = EN;
}
void GLCD_Init() // GLCD INITIALIZE FUNCTION
{
PINSEL2=0x00000000;
IODIR1 = (LCD_D0|LCD_D1|LCD_D2|LCD_D3|LCD_D4|LCD_D5|LCD_D6|LCD_D7|RS|RW|EN|CS1|CS2);
IOSET1 = (CS1|CS2); // SELECT BOTH LEFT AND RIGHT HALF OF DISPLAY.
delay(10);
GLCD_Command(0x3E); // DISPLAY OFF.
GLCD_Command(0x40); // SET Y ADDRESS (COLOUM=0)
GLCD_Command(0xB8); // SET X ADDRESS (PAGE=0)
GLCD_Command(0xC0); // SET Z ADDRESS (START LINE=0)
GLCD_Command(0x3F); // DISPLAY ON.
}
void GLCD_ClearAll() //GLCD ALL DISPLAY CLEAR FUNCTION
{
int column,page;
for(page=0;page<8;page++) // PRINT 16 PAGE / PAGE OF EACH HALF OF DISPLAY.
{
IOSET1 = CS1;
IOCLR1 = CS2;
GLCD_Command(0x40); //set Y ADDRESS (COLUMN=0).
GLCD_Command((0xB8+page));
for (column=0;column<128;column++)
{
if (column == 64)
{
IOSET1 = CS1; // IF YES THEN CHANGE SEGMENT CONTROLLER.
IOSET1 = CS2;
GLCD_Command(0x40); // SET Y ADDRESS (COLUMN=0).
GLCD_Command((0xB8+page));
}
GLCD_Data(0); // print 64 column on each page.
}
}
}
/*
void DisplayMessage(const char *pszMessage)
{
IOSET1 = RW;
IOCLR1 = RW;
while(*pszMessage!='\0')
{
IOSET1 = EN;
IODIR1 = *pszMessage;
//GLCD_Data = *pszMessage;
delay(10);
IOCLR1 = EN;
pszMessage++;
}
}
*/
void DisplayMessage(const char *pszMessage) //GLCD STRING WRITE FUNCTION.
{
//IOSET1 = RW;
//IOCLR1 = RW;
while(*pszMessage!='\0')
{
int column,page;
for(page=0;page<8;page++) // PRINT 16 PAGE / PAGE OF EACH HALF OF DISPLAY.
{
IOSET1 = CS1;
IOCLR1 = CS2;
GLCD_Command(0x40); //set Y ADDRESS (COLUMN=0).
GLCD_Command(0xB8+page);
for (column=0;column<128;column++)
{
if (column==64)
{
IOCLR1 = CS1; // IF YES THEN CHANGE SEGMENT CONTROLLER.
IOSET1 = CS2;
GLCD_Command(0x40); // SET Y ADDRESS (COLUMN=0).
GLCD_Command((0xB8+page));
}
GLCD_Data(*pszMessage++); // print 64 column on each page.
}
}
}
}
void delay (int k)
{
int a,b;
for(a=0;a<=k;a++)
for(b=0;b<500;b++);
}
int main()
{
GLCD_Init(); /*Lcd Initialize*/
GLCD_ClearAll();
short siLoop = 0;
short siLen = 0;
const char *pszDisplayMsg = "Welcome To Pragathi Solutions.";
siLen = strlen(pszDisplayMsg); /*Calculate length of Message*/
while(1) {
GLCD_Command(0x8f); /*Address of DDRAM*/
DisplayMessage(pszDisplayMsg);
for(siLoop=0;siLoop < siLen;siLoop++) {
GLCD_Command(0x40); // SET Y ADDRESS (COLOUM=0)
GLCD_Command(0xB8); // SET X ADDRESS (PAGE=0)
delay(25);
}
}
}