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

Discussion in 'Embedded Systems and Microcontrollers' started by khangul, Jan 31, 2015.

  1. khangul

    Thread Starter New Member

    Jan 31, 2015
    12
    0
    Code (Text):
    1.  
    2. #include<stdio.h>
    3.  
    4. #include<reg51.h>
    5.  
    6. sbit en =P3^2;
    7. sbit rw =P3^1;
    8. sbit rs =P3^0;
    9. int i;
    10. int j, k;
    11.  
    12. void delay();
    13.  
    14. void lcdcmd ();
    15. void dataa();
    16.  
    17. unsigned char cmd[5]={0x01,0x02,0x06,0x0e,0x38};
    18. unsigned char dat[5]={"hafiz"};
    19. void main()
    20.  
    21. {
    22. while (1)
    23. {
    24. rs=0;
    25. rw=0;
    26. en=0;
    27.  
    28. lcdcmd();
    29. dataa();
    30.  
    31. }
    32.  
    33.  
    34. }
    35.  
    36. void dataa()
    37. {
    38. rs=1;
    39.  
    40. for(k=0;k<10;k++)
    41. {
    42.  
    43. P2= dat[k];
    44. en=1;
    45. delay();
    46. en=0;
    47. }
    48.  
    49.   }
    50.  
    51. void lcdcmd()
    52. {
    53.  
    54.  
    55. for(int x=0;x<=5;x++)
    56. {
    57. P3=cmd[x];
    58. en=1;
    59. delay();
    60. en=0;
    61.  
    62.  
    63. }
    64.  
    65.  
    66. }
    67.  
    68. void delay()
    69. {
    70. for (j=0;j<=1000;j++)
    71.     for (k=0;k<=200;k++);
    72.  
    73.     }
    74.  
    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: Jan 31, 2015
  2. MrChips

    Moderator

    Oct 2, 2009
    12,449
    3,364
    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.
     
  3. khangul

    Thread Starter New Member

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

    Code (Text):
    1.  
    2. #include<stdio.h>
    3.  
    4. #include<reg51.h>
    5.  
    6. sbit en =P3^2;
    7. sbit rw =P3^1;
    8. sbit rs =P3^0;
    9. int i;
    10. int j, k;
    11.  
    12. void delay();
    13.  
    14. void lcdcmd ();//lcd initialization command function prototype
    15. void dataa(); //lcd data funtion prototype
    16.  
    17. unsigned char cmd[5]={0x01,0x02,0x06,0x0e,0x38};//
    18. unsigned char dat[5]={"hafiz"};
    19. void main()
    20.  
    21. {
    22. while (1)
    23. {
    24. rs=0;
    25. rw=0;
    26. en=0;
    27.  
    28. lcdcmd();//comand function called
    29. dataa(); //data function called
    30.  
    31. }
    32.  
    33.  
    34. }
    35.  
    36. void dataa()// data funtion
    37. {
    38. rs=1;
    39.  
    40. for(k=0;k<10;k++)
    41. {
    42.  
    43. P2= dat[k];
    44. en=1;
    45. delay();
    46. en=0;
    47. }
    48.  
    49.   }
    50.  
    51. void lcdcmd()// command function
    52. {
    53.  
    54.  
    55. for(i=0;i<=5;i++)
    56. {
    57. P3=cmd[I];
    58. en=1;
    59. delay();
    60. en=0;
    61.  
    62.  
    63. }
    64.  
    65.  
    66. }
    67.  
    68. void delay() //delay function
    69. {
    70. for (j=0;j<=1000;j++)
    71.     for (k=0;k<=200;k++);
    72.  
    Moderators note: Please use code tags for pieces of code[/I]
     
    Last edited by a moderator: Jan 31, 2015
  4. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    4,887
    1,016
    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!
     
  5. shteii01

    AAC Fanatic!

    Feb 19, 2010
    3,395
    497
    The code looks really bad.
     
  6. shteii01

    AAC Fanatic!

    Feb 19, 2010
    3,395
    497
    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.
     
    khangul likes this.
  7. khangul

    Thread Starter New Member

    Jan 31, 2015
    12
    0
    #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
     
  8. kubeek

    AAC Fanatic!

    Sep 20, 2005
    4,670
    804
    "hello world" is a single string. What are you trying to do?
     
  9. MrChips

    Moderator

    Oct 2, 2009
    12,449
    3,364
    If you want to pass a string you have to learn to use pointers.

    Code (Text):
    1.  
    2. void LCD_text(char *s)
    3. {
    4.    while(*s) LCD_data(*s++);
    5. }
    6.  
     
  10. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    4,887
    1,016

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


    A safer solution

    Code (Text):
    1.  
    2.  
    3.  
    4.  
    5. void LCD_text(char *s, unsigned char count, unsigned char max )
    6. {
    7.   count = 0;
    8.  
    9.    while(*s & count < max) LCD_data(*s++);
    10. }
    11.  
    strcpy has been depreciated in some versions of C in place of strncpy for this reason.
     
  11. khangul

    Thread Starter New Member

    Jan 31, 2015
    12
    0
    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 (Text):
    1.  
    2. #include<stdio.h>
    3.  
    4. #include<reg51.h>
    5.  
    6. sfr ldata=0xB0;
    7. sbit en =P3^2;
    8. sbit rw =P3^1;
    9. sbit rs =P3^0;
    10. int i;
    11. int j;
    12. unsigned char k;
    13.  
    14. void delay();
    15.  
    16. void  lcdcmd (unsigned char a[]);//lcd initialization command function prototype
    17. void dataa(unsigned char b[]); //lcd data funtion prototype
    18.  
    19. char comm[]={0x01,0x02,0x06,0x0e,0x38};
    20. char dat[]={"hafi1z"};
    21. void main()
    22.  
    23. {
    24.  
    25.  
    26. while (1)
    27. {
    28.  
    29.  
    30. lcdcmd(comm);
    31. delay();
    32. dataa(dat); //data function called
    33.  
    34.  
    35.  
    36. }
    37.  
    38.  
    39. }
    40. void lcdcmd(unsigned char a[])
    41. {
    42. P2=0x80;
    43. rs=0;
    44. rw=0;
    45. en=0;
    46. for(i=0;i<5;i++)
    47.     {
    48.     P2 = a[I];
    49.     en = 1;
    50.     delay();
    51.     en = 0;
    52.  
    53.       }
    54.    }
    55. void dataa(unsigned char b[])// data funtion
    56. {
    57. rs=0;
    58. rw=0;
    59. en=0;
    60.  
    61. for(k=0;k<6;k++)
    62. {rs=1;
    63.  
    64. P2= b[k];
    65. en=1;
    66. delay();
    67. en=0;
    68. return;
    69. }
    70.  
    71.   }
    72.  
    73.  
    74. void delay() //delay function
    75. {
    76. for (j=0;j<300;j++)
    77.     for (k=0;k<=50;k++);
    78.  
    79.     }
    80.  
    Moderators note: AGAIN Please use code tags for pieces of code
     
    Last edited by a moderator: Feb 1, 2015
  12. khangul

    Thread Starter New Member

    Jan 31, 2015
    12
    0
    i want to display any word e.g "welcome to micro controller" on lcd screen using 8051 microcontroller
     
  13. MrChips

    Moderator

    Oct 2, 2009
    12,449
    3,364
    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.
     
  14. kubeek

    AAC Fanatic!

    Sep 20, 2005
    4,670
    804
    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.
     
  15. khangul

    Thread Starter New Member

    Jan 31, 2015
    12
    0
    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: Feb 1, 2015
  16. khangul

    Thread Starter New Member

    Jan 31, 2015
    12
    0
    I want to display the whole phrase
     
  17. MrChips

    Moderator

    Oct 2, 2009
    12,449
    3,364

    Enclose your code like this using tags:

    [code]
    your program here
    [/code]

     
  18. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    4,887
    1,016
    And I like the plain tag to override the code tag. Nice tip Mrchips. Two of them in one.
     
  19. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    4,887
    1,016

    Already covered by MrChips and myself.
     
  20. sevenfold4

    Member

    Jan 12, 2015
    80
    7
    I am so sorry, somehow i did not see your post before i posted.
     
Loading...