i want to display on lcd screen but can't able working 8051 controller.plz anyone chek my code

Thread Starter

khangul

Joined Jan 31, 2015
12
Code:
#include<stdio.h>

#include<reg51.h>

sbit en =P3^2;
sbit rw =P3^1;
sbit rs =P3^0;
int i;
int j, k;

void delay();

void lcdcmd ();
void dataa();

unsigned char cmd[5]={0x01,0x02,0x06,0x0e,0x38};
unsigned char dat[5]={"hafiz"};
void main()

{
while (1)
{
rs=0;
rw=0;
en=0;

lcdcmd();
dataa();

}


}

void dataa()
{
rs=1;

for(k=0;k<10;k++)
{

P2= dat[k];
en=1;
delay();
en=0;
}

  }

void lcdcmd()
{


for(int x=0;x<=5;x++)
{
P3=cmd[x];
en=1;
delay();
en=0;


}


}

void delay()
{
for (j=0;j<=1000;j++)
    for (k=0;k<=200;k++);

    }
Edit by moderator: added code tags

When posting code on a forum do not use the index variable i since this will be interpreted as an italics tag.
 
Last edited by a moderator:

MrChips

Joined Oct 2, 2009
30,810
Do not write code with no comments.
You need to learn to communicate what you are attempting to do.

Your string cmd[5] has five values, not six. The index goes from 0 to 4.
 

Thread Starter

khangul

Joined Jan 31, 2015
12
MrChips. now i have upload my program with comment
help me

Code:
#include<stdio.h>

#include<reg51.h>

sbit en =P3^2;
sbit rw =P3^1;
sbit rs =P3^0;
int i;
int j, k;

void delay();

void lcdcmd ();//lcd initialization command function prototype
void dataa(); //lcd data funtion prototype

unsigned char cmd[5]={0x01,0x02,0x06,0x0e,0x38};//
unsigned char dat[5]={"hafiz"};
void main()

{
while (1)
{
rs=0;
rw=0;
en=0;

lcdcmd();//comand function called
dataa(); //data function called

}


}

void dataa()// data funtion
{
rs=1;

for(k=0;k<10;k++)
{

P2= dat[k];
en=1;
delay();
en=0;
}

  }

void lcdcmd()// command function
{


for(i=0;i<=5;i++)
{
P3=cmd[I];
en=1;
delay();
en=0;


}


}

void delay() //delay function
{
for (j=0;j<=1000;j++)
    for (k=0;k<=200;k++);
Moderators note: Please use code tags for pieces of code[/I]
 
Last edited by a moderator:

spinnaker

Joined Oct 29, 2009
7,830
What have you done to troubleshoot this problem?

The first step in solving this problem is making sure your code is doing what you think it is doing.

Output a byte at a time and confirm that what you think you are sending to the display is what is getting there. The best tool for this is a logic analyzer but you can use a scope, DVM or a logic probe just as well. If you don't have a logic probe, you can make one out of an led, resistor and a couple of wires.

Add use code tags when posting code!
 

shteii01

Joined Feb 19, 2010
4,644
This is how normal people do it:


1.jpg




You can do some mods to it. lcdcmd codes could be run using for loop like you were trying to do, but you got the order of the codes wrong.
 

Thread Starter

khangul

Joined Jan 31, 2015
12
#sheeti01 and spinnaker
i am really thankful to you both.
you suggestion and comments help me lot. how to pass multiple strings e.g"hello world" in a single statement without using array
regards
 

MrChips

Joined Oct 2, 2009
30,810
If you want to pass a string you have to learn to use pointers.

Code:
void LCD_text(char *s)
{
   while(*s) LCD_data(*s++);
}
 

spinnaker

Joined Oct 29, 2009
7,830
If you want to pass a string you have to learn to use pointers.

Code:
void LCD_text(char *s)
{
   while(*s) LCD_data(*s++);
}

OP should note that this code assumes your strings are terminated with a null.


A safer solution

Code:
void LCD_text(char *s, unsigned char count, unsigned char max )
{
  count = 0;

   while(*s & count < max) LCD_data(*s++);
}
strcpy has been depreciated in some versions of C in place of strncpy for this reason.
 

Thread Starter

khangul

Joined Jan 31, 2015
12
according to your suggestion i did some changes in program and get the first character only how to get the whole on lcd screen. i have just stared work on micro controller. i will really appreciate yours suggestion
thanks

Code:
#include<stdio.h>

#include<reg51.h>

sfr ldata=0xB0;
sbit en =P3^2;
sbit rw =P3^1;
sbit rs =P3^0;
int i;
int j;
unsigned char k;

void delay();

void  lcdcmd (unsigned char a[]);//lcd initialization command function prototype
void dataa(unsigned char b[]); //lcd data funtion prototype

char comm[]={0x01,0x02,0x06,0x0e,0x38};
char dat[]={"hafi1z"};
void main()

{


while (1)
{


lcdcmd(comm);
delay();
dataa(dat); //data function called



}


}
void lcdcmd(unsigned char a[])
{
P2=0x80;
rs=0;
rw=0;
en=0;
for(i=0;i<5;i++)
    { 
    P2 = a[I];
    en = 1;
    delay();
    en = 0;

      }
   }
void dataa(unsigned char b[])// data funtion
{
rs=0;
rw=0;
en=0;

for(k=0;k<6;k++)
{rs=1;

P2= b[k];
en=1;
delay();
en=0;
return;
}

  }


void delay() //delay function
{
for (j=0;j<300;j++)
    for (k=0;k<=50;k++);

    }
Moderators note: AGAIN Please use code tags for pieces of code
 
Last edited by a moderator:

MrChips

Joined Oct 2, 2009
30,810
Did you read the mods that I wrote on post #1?

Do not use the variable i when posting on forums.

Use code tags to post your code.
 

kubeek

Joined Sep 20, 2005
5,795
i want to display any word e.g "welcome to micro controller" on lcd screen using 8051 microcontroller
Do you want to display the whole phrase, or make it from different words?
You have two options, either print each word and space etc. on the display, or prepare the string in memory using strcat() and then print the result.
 

Thread Starter

khangul

Joined Jan 31, 2015
12
Did you read the mods that I wrote on post #1?

Do not use the variable i when posting on forums.

Use code tags to post your code.
Sorry, I will not repeat my mistake again. I don't know how to tag? this form help me lot Thanks
 
Last edited by a moderator:

Thread Starter

khangul

Joined Jan 31, 2015
12
Do you want to display the whole phrase, or make it from different words?
You have two options, either print each word and space etc. on the display, or prepare the string in memory using strcat() and then print the result.
I want to display the whole phrase
 

spinnaker

Joined Oct 29, 2009
7,830
To display a string simply run the command to write 1 character multiple times. I am taking from my own code

Code:
void lcd_string(unsigned char *var)
{
     while(*var)              //till string ends
       lcd_senddata(*var++);  //send characters one by one
}
and then you can just write
Code:
lcd_string("Welcome");
or something like that

Already covered by MrChips and myself.
 
Top