Need assistance _______LCD Program

Discussion in 'Embedded Systems and Microcontrollers' started by triplehdineshbabu, Feb 18, 2009.

Thread Status:
Not open for further replies.
  1. triplehdineshbabu

    Thread Starter Active Member

    Feb 5, 2009
    38
    0
    Hi all,

    I have written the LCD Display coding in 8051C, Kindly Check and tell me whats error in this program

    ---------_------

    #include<reg52.h>
    #include<intrins.h>
    sbit rs=P3^5;
    sbit rw=P3^6;
    sbit en=P3^7;
    sbit busy=P1^7;
    unsigned char *L1="Dinesh";
    unsigned char *L2="Babu";
    void lcd_inz(void);
    void address_write(unsigned char);
    void data_write(unsigned char);
    void delay(unsigned int);
    void string_write(char*);
    void check();
    void main()
    {
    lcd_inz();
    delay(10);
    while(1)
    {
    address_write(0x80);
    string_write(L1);
    delay(10);
    address_write(0xc0);
    string_write(L2);
    delay(10);
    }
    }
    void delay(unsigned int i)
    {
    int k;
    for(k=0;k<i;k++)
    {
    _nop_();
    }
    }
    void lcd_inz()
    {
    address_write(0x01);
    address_write(0xc0);
    address_write(0xc0);
    address_write(0x80);
    address_write(0x38);
    address_write(0xe0);
    }
    void address_write(unsigned char x)
    {
    P1=x;
    rs=0;
    rw=0;
    en=1;
    delay(10);
    en=0;
    check();
    }
    void check()
    {
    busy=1;
    en=1;
    rs=0;
    rw=1;
    while(busy==1)
    {
    en=0;
    delay(10);
    en=1;
    }
    }
    void string_write(unsigned char *v)
    {
    while(*v!='\0');
    {
    data_write(*v);
    v++;
    }
    }
    void data_write(unsigned char y)
    {
    P1=y;
    rs=1;
    rw=0;
    en=1;
    delay(10);
    en=0;
    check();
    }



    ------------------

    thanks In advance

    Regards,

    Dinesh
     
  2. futz

    Member

    Dec 14, 2008
    23
    0
    Hi Dinesh. I don't use the 8051, but I can tell right away you have a problem in your code.

    But first a quick tip: When posting code to the forum, use Code tags. Either click on the # icon just before pasting your code, or type [code] before your code and [/code] after it. This tells the forum software not to strip the formatting off your code and leaves it properly indented. Good looking code is easier for people to read and help you with. :)

    Here's your code reformatted and posted correctly:
    Code ( (Unknown Language)):
    1. #include<reg52.h>
    2. #include<intrins.h>
    3.  
    4. sbit rs=P3^5;
    5. sbit rw=P3^6;
    6. sbit en=P3^7;
    7. sbit busy=P1^7;
    8.  
    9. unsigned char *L1="Dinesh";
    10. unsigned char *L2="Babu";
    11.  
    12. void lcd_inz(void);
    13. void address_write(unsigned char);
    14. void data_write(unsigned char);
    15. void delay(unsigned int);
    16. void string_write(char*);
    17. void check();
    18.  
    19. void main()
    20. {
    21.     lcd_inz();
    22.     delay(10);
    23.     while(1)
    24.     {
    25.         address_write(0x80);
    26.         string_write(L1);
    27.         delay(10);
    28.         address_write(0xc0);
    29.         string_write(L2);
    30.         delay(10);
    31.     }
    32. }
    33.  
    34. void delay(unsigned int i)
    35. {
    36.     int k;
    37.     for(k=0;k<i;k++)
    38.     {
    39.         _nop_();
    40.     }
    41. }
    42.  
    43. void lcd_inz()
    44. {
    45.     address_write(0x01);
    46.     address_write(0xc0);
    47.     address_write(0xc0);
    48.     address_write(0x80);
    49.     address_write(0x38);
    50.     address_write(0xe0);
    51. }
    52.  
    53. void address_write(unsigned char x)
    54. {
    55.     P1=x;
    56.     rs=0;
    57.     rw=0;
    58.     en=1;
    59.     delay(10);
    60.     en=0;
    61.     check();
    62. }
    63.  
    64. void check()
    65. {
    66.     busy=1;
    67.     en=1;
    68.     rs=0;
    69.     rw=1;
    70.     while(busy==1)
    71.     {
    72.         en=0;
    73.         delay(10);
    74.         en=1;
    75.     }
    76. }
    77.  
    78. void string_write(unsigned char *v)
    79. {
    80.     while(*v!='\0');
    81.     {
    82.         data_write(*v);
    83.         v++;
    84.     }
    85. }
    86.  
    87. void data_write(unsigned char y)
    88. {
    89.     P1=y;
    90.     rs=1;
    91.     rw=0;
    92.     en=1;
    93.     delay(10);
    94.     en=0;
    95.     check();
    96. }
    97.  
    You haven't delayed before initializing the LCD. There should be at least a 15ms "settle time" delay to allow the LCD to initialize itself, before it's ready to receive any commands.

    Here's my LCD code (4-bit mode) that I use on PIC, AVR and ARM7. This happens to be the ARM version. Might give you some ideas.
    Code ( (Unknown Language)):
    1. void lcd_string(char *senpoint)
    2. {
    3.     while(*senpoint != '\0')
    4.     {
    5.         lcd_char(*senpoint);
    6.         senpoint++;
    7.     }
    8. }    
    9.  
    10. void lcd_line1(void){
    11.     lcd_cmd(0x80);
    12. }
    13. void lcd_line2(void){
    14.     lcd_cmd(0xc0);
    15. }        
    16.  
    17. void lcd_cmd(unsigned char letter)
    18. {
    19.     unsigned char temp;
    20.     temp=letter;
    21.     temp=temp>>4;
    22.     lcd_nybble(temp,0);
    23.     temp=letter;
    24.     temp=temp&0x0f;
    25.     lcd_nybble(temp,0);
    26. }
    27.  
    28. void lcd_char(unsigned char letter)
    29. {
    30.     unsigned char temp;
    31.     temp=letter;
    32.     temp=temp>>4;
    33.     lcd_nybble(temp,1);
    34.     temp=letter;
    35.     temp=temp&0x0f;
    36.     lcd_nybble(temp,1);
    37. }
    38.  
    39. void lcd_nybble(unsigned char nyb,unsigned char rs)
    40. {
    41.     int i,dat;
    42.     if(rs)
    43.         IOSET0 = RS;            //set RS pin
    44.     else
    45.         IOCLR0 = RS;            //clear RS pin
    46.     dat = nyb;                  //get the nybble in an int
    47.     IOCLR0 = 0x001e0000;        //clear D4-D7
    48.     IOPIN0 |= dat<<17;          //OR the bits in there
    49.     strobe_e();                 //latch data to LCD
    50. }
    51.  
    52. void lcd_init(void)
    53. {
    54.     delay_ms(500);              //settle time delay
    55.     lcd_nybble(0x03,0);         //reset LCD
    56.     strobe_e();
    57.     strobe_e();
    58.     lcd_nybble(0x02,0);
    59.     lcd_cmd(0x28);              //set 4-bit mode and 2 lines
    60.     lcd_cmd(0x10);              //cursor move & shift left
    61.     lcd_cmd(0x06);              //entry mode = increment
    62.     lcd_cmd(0x0e);              //display on - cursor blink on
    63.     lcd_cmd(0x01);              //clear display
    64. }
    65.  
    66. void lcd_busy(void)
    67. {
    68.     IODIR0 &= 0xffefffff;      //D7 is input
    69.     IOCLR0 = RS;               //set RS low
    70.     IOSET0 = RW;               //set R/W high
    71.     IOSET0 = E;                //set E high
    72.     while(IOPIN0 & busyflag);  //wait for busy flag to go low
    73.     IOCLR0 = E;                //set E low
    74.     IOCLR0 = RW;               //set R/W low
    75.     IODIR0 |= 0x00100000;      //D7 is output again
    76. }
    77.  
    78. void strobe_e(void)
    79. {
    80.     IOSET0 = E;
    81.     delay_us(1);
    82.     IOCLR0 = E;
    83.     lcd_busy();
    84. }
    85.  
    If you don't already have them, these two files are must-reads for character LCD use.
    http://www.epemag.wimborne.co.uk/lcd1.pdf
    http://www.epemag.wimborne.co.uk/lcd2.pdf
     
    Last edited: Feb 18, 2009
  3. triplehdineshbabu

    Thread Starter Active Member

    Feb 5, 2009
    38
    0
    HI futz,

    thanks for your help.

    But still i am not getting correct output in the LCD.

    Kindly check and tell me PLZ......

    Thanks in advance
     
Loading...
Thread Status:
Not open for further replies.