Microcontroller › 8051 › help with LCD(16×2) display code using 8051
- This topic has 14 replies, 8 voices, and was last updated 8 years, 4 months ago by salihu.
-
AuthorPosts
-
January 6, 2013 at 7:06 pm #2050mahrukh mohsinParticipant
hi, i am a newbie in microcontrollers, i am working on a code that i got from this side, and i have just a teensie weensie question regarding it, here’s the code,//Program to display String on LCD
#include<reg51.h>
sfr lcd_data_pin=0xA0; // data port P2
sbit rs=P3^0; // Register select pin
sbit rw=P3^1; // Read write pin
sbit en=P3^6; // Enable pin
void delay(unsigned int msec) //delay function
{
int i,j;
for(i=0;i<msec;i++)
for(j=0;j<1275;j++);
}
void lcd_command(unsigned char comm) // function to send command to LCD
{
lcd_data_pin=comm;
en=1;
rs=0;
rw=0;
delay(1);
en=0;
}
void lcd_data(unsigned char disp) // function to send data on LCD
{
lcd_data_pin=disp;
en=1;
rs=1;
rw=0;
delay(1);
en=0;
}
lcd_dataa(unsigned char *disp) // function to send string to LCD
{
int x;
for(x=0;disp[x]!=0;x++)
{
lcd_data(disp[x]);
}
}
void lcd_ini() //Function to inisialize the LCD
{
lcd_command(0x38);
delay(5);
lcd_command(0x0F);
delay(5);
lcd_command(0x80);
delay(5);
}
void main()
{
lcd_ini();
lcd_dataa("ECE Rocks");
}now my question is how can i display data on the second line of LCD? i tried inserting the command, lcd_command(0xC0) in the code like this,
void lcd_ini() //Function to inisialize the LCD
{
lcd_command(0x38);
delay(5);
lcd_command(0x0F);
delay(5);
lcd_command(0x80);
delay(5);lcd_command(0xC0);
delay(5);
but its of no use, because when i simulate this code on PROTEUS, the cursor does move to the second line but just writes data on the first line of LCD, any suggestions please?
January 7, 2013 at 10:04 am #8912BHARATH.S.HEGDEParticipanttry this initialization function……..
//lcd initialisation //
void lcd_init()
{
lcd_cmd(0x0e); //lcd on, cursor on
delay(2);
lcd_cmd(0x38); //8 bit initialize, 5×7 character font, 16×2 display
delay(2);
lcd_cmd(0x06); //right shift cursor automatically after each character is displayed
delay(2);
lcd_cmd(0x01); //clear lcd
delay(2);
lcd_cmd(0xC0);
delay(2);
}January 7, 2013 at 6:16 pm #8915mahrukh mohsinParticipanti tried your initialization code but now the data is only written on the second line of LCD, is there some code to display data on first aswell as the 2nd line of LCD?
January 8, 2013 at 4:25 am #8916BHARATH.S.HEGDEParticipantvoid lcd_init()
{
lcd_cmd(0x0e); //lcd on, cursor on
delay(2);
lcd_cmd(0x38); //8 bit initialize, 5×7 character font, 16×2 display
delay(2);
lcd_cmd(0x06); //right shift cursor automatically after each character is displayed
delay(2);
lcd_cmd(0x01); //clear lcd
delay(2);
lcd_cmd(0x80);
delay(2);
}type your display function
again type the command, lcd_cmd(0xC0);
type your display function
January 8, 2013 at 11:25 am #8919AJISH ALFREDParticipantHi Mahruh,
Dont write anything inside the initialization function.
Use the command for the display on second line only when you require.
like this,
void lcd_ini() //Function to inisialize the LCD
{
lcd_command(0x38);
delay(5);
lcd_command(0x0F);
delay(5);
lcd_command(0x80);
delay(5);
}
void main()
{
lcd_ini();
lcd_dataa("ECE Rocks");
lcd_command(0xC0); //command for display on second line
lcd_dataa("2013");
delay(2000);
lcd_command(0x80); //command for back to first line
lcd_dataa("Rocks again")
}
January 8, 2013 at 5:36 pm #8921mahrukh mohsinParticipantoh now i get it thanku guys, the code works great
January 9, 2013 at 1:06 pm #8923AJISH ALFREDParticipantOur pleasure!
Feel free to share us if you face any further problems.
April 14, 2013 at 7:30 pm #9487Samuel HyginusParticipantGuyz i am using 16×2 LCD….even i want the program in assembly language…i.e interfacing LCD with 8051…& using both the lines of the LCD…pls help me
April 16, 2013 at 3:17 pm #9508SavioParticipantYou could start with the first line with ‘0x80’ and count the characters being sent to the lcd. Once 16 characters have been sent, send the command to display from the second line – ‘0xC0’ .
and vice-versa.
April 17, 2013 at 9:29 pm #9509Samuel HyginusParticipantactually i am not able to undrstnd assembly language….can u pls give me the entire code for interfacing the 16×2 LCD to AT89S52 & i want data on both the lines of my LCD…pls help me guyz..& i want the code in assembly language…..
November 28, 2015 at 12:11 pm #13536ABIN JOHN THOMASParticipantHi,
I tried the same code to display my string on line 1. It works fine till my string has 16 characters. I am happy with it.
If my string has more than 16 characters , I wont be able to see them. This part is also clear.
But when my string has 50 characters, the characters after 40th character are automatically getting printed on line 2.
How would this be happening
Thanks
Abin
November 28, 2015 at 2:06 pm #13537rahulParticipantbro it’s 16×2 lcd means it can print only 16 character on first line…you have to take care whenever your string contain more than 16 character then first print 16 character on first line then issue command to have cursor on 2nd line then print remaining character. ..
November 30, 2015 at 2:29 pm #13539ABIN JOHN THOMASParticipantHi Rahul,
thanks for the reply.
I am trying to create a running display. So I have a 50 character string, after printing each character I shift the entire display left and it works for me except that after 40 words, it starts printing on the second line.
thanks
November 30, 2015 at 2:42 pm #13540rahulParticipantlocation of start of first line first char. is 80h so…so when you add each character counter is incremented like 81 82 so on….when keep adding char to print on display …then when counter reaches to c0h then they are printed on 2nd line as address of first char on 2nd line is c0h
December 1, 2015 at 11:32 am #13541salihuParticipantPlease help me with c source code for automatic college bell using 8051,DS1307,LCD and keypad which delays for 40 minutes and rings for 11 seconds from Monday to Friday.
I look forward for a swift response. Thank you.
-
AuthorPosts
- You must be logged in to reply to this topic.