LCD display problem

Discussion in 'Embedded Systems and Microcontrollers' started by baba_bhuri, Jul 8, 2013.

  1. baba_bhuri

    Thread Starter New Member

    Jul 2, 2013
    17
    1
    Hi,
    I(a newbie) am trying to display a character on LCD.Instead black boxes are displayed on the first row; the second row remaining empty. I am sure trouble does not lie in the connections.
    Kindly check out my code:
    Code ( (Unknown Language)):
    1.  
    2. #include<avr/io.h>
    3. #include<util/delay.h>
    4. #define PIN_RS PINB0
    5. #define PIN_RW PINB1
    6. #define PIN_ENABLE PINB2
    7. #define LCD_CONTROL_DIR DDRB
    8. #define LCD_CONTROL_PIN  PORTB //ports for DB0 to DB7
    9. void enable();// to make negative edge
    10. void check_busy();//to check busy flag
    11. void address_ddram(char);// to giv memory address of ddram
    12. void Display(char);// to display character on lcd
    13. main()
    14. {
    15.   DDRD =0xFF;//setting PORTD as output
    16.   LCD_CONTROL_DIR |=(1<<PIN_RS)|(1<<PIN_RW)|(1<<PIN_ENABLE);//setting port R/S RW output
    17.   check_busy();
    18.   address_ddram(0x0);
    19.   check_busy();
    20.   Display(0x31);
    21.   while(1);
    22. }
    23. void enable()
    24. {
    25.   LCD_CONTROL_PIN |=(1<<PIN_ENABLE);/* negative eedge transition for data transfer
    26.   but data is read after negative edge and remains avaliable till enable is high*/
    27.  asm volatile("nop");
    28.  asm volatile("nop");
    29.   LCD_CONTROL_PIN &=~(1<<PIN_ENABLE);
    30. }
    31.  
    32. void check_busy()
    33. {
    34.  LCD_CONTROL_PIN |=1<<PIN_RW;//read
    35.  LCD_CONTROL_PIN &=~(1<<PIN_RS);//instruction
    36.  enable();
    37.  LCD_CONTROL_PIN |=(1<<PIN_ENABLE);//raise Enable to read data
    38.  while(PORTD >=0x80);//wait till db7 is 1
    39. }
    40.  
    41. void address_ddram(char address)
    42. {
    43.   LCD_CONTROL_PIN &=~(1<<PIN_RW);//WRITE
    44.   //instruction is enabled in check_busy() function
    45.   PORTD |=(1<<PIN7);
    46.   enable();
    47.   check_busy();
    48.   LCD_CONTROL_PIN &=~(1<<PIN_RW);//WRITE
    49.   //instruction is enabled in check_busy() function
    50.   PORTD =address;
    51.   enable();
    52. }
    53.  
    54. void Display( char character)
    55. {
    56.   LCD_CONTROL_PIN &=~(1<<PIN_RW);//WRITE
    57.   LCD_CONTROL_PIN |=1<<PIN_RS;//DATA
    58.   PORTD = character;
    59.   enable();
    60. }
    61.  
     
  2. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,394
    1,606
    You can only be sure of the connections by checking each and every one of them, preferable by single stepping thru some test code to test exactly that.

    Alphanumeric displays init themselves in 8 bit mode. While you seem to be using that you do not allow the LCD to complete it's power on routine by waiting until after that completes.

    The web has 10,000 sites devoted to LCD code on every chip out there. Have you tried to Google "AVR LCD" yet?
     
  3. baba_bhuri

    Thread Starter New Member

    Jul 2, 2013
    17
    1
    Thanks for reply.
    Do you mean that I should use initialise by instruction? While going through the datasheet (for 8bit initialisation) the first three instructions are same with different time delays after each instruction.
    why can't one give these instruction just once and do a cumulative time delay?(I googled and found a nice page on initialisation but it doesnot answer my above question)
     
    Last edited: Jul 9, 2013
  4. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,394
    1,606
    I believe that's the 4 bit initialization ylou are looking at. For 8 bits all you need do it WAIT till it is done doing it's internal init. Leave it along, don't send commands or data, it will probably ignore them anyway.

    Do look at the data set up time and the min clock width too: you may need to add a bit of delay between the data and the control signals, and make sure the control signals are on long enough.
     
  5. baba_bhuri

    Thread Starter New Member

    Jul 2, 2013
    17
    1
    I checked the busy flag between control and data signals (and also tried giving delays) .Also , gave a full delay of up to 1 second during power on (far more than necessary) ,but it still won't work .
    If its not asking much can I be provided with a LCD program which includes initialization in 8 bit mode and displaying a character...
    I will greatly appreciate any help.
     
  6. tshuck

    Well-Known Member

    Oct 18, 2012
    3,531
    675
    baba_bhuri likes this.
  7. baba_bhuri

    Thread Starter New Member

    Jul 2, 2013
    17
    1
    Thanks for help. My code works now.
    The error was in initialize function ,I was not giving the required time delays in the last 4 instructions.
     
    ErnieM likes this.
  8. tshuck

    Well-Known Member

    Oct 18, 2012
    3,531
    675
    That's usually how it works for most people starting with these LCD modules...on the bright side, you will probably never spend this much time with a bad initialization again!

    Glad to hear you've got it working!
     
Loading...