Pic and LCD Screen

Discussion in 'Embedded Systems and Microcontrollers' started by jbradley, Mar 8, 2012.

  1. jbradley

    Thread Starter New Member

    Mar 6, 2012
    I'm having problems getting my LCD screen to initialize properly I was wondering if anybody could tell me what I'm doing wrong.

    I'm using a PIC18F2520
    here is the datasheet for the LCD I'm using

    I'm trying to use 4 bit mode my pin setup is
    Enable RB6
    R/W RB5
    RS RB4
    Data7-4 RB3-0

    This is my first time ever using a pic and I'm not that familiar with embedded systems in general. I have used this website as my reference for how to initalize the lcd screen http://joshuagalloway.com/lcd.html.

    Code ( (Unknown Language)):
    1. void write_lcd_4(unsigned char hex){
    2.     //set enable to low
    3.     PORTBbits.RB6 = 0;
    4.     //set r/s to zero for command
    5.     PORTBbits.RB4 = 0;
    6.     //put in high byte
    7.     PORTBbits.RB0 = getBit(hex, 0);
    8.     PORTBbits.RB1 = getBit(hex, 1);
    9.     PORTBbits.RB2 = getBit(hex, 2);
    10.     PORTBbits.RB3 = getBit(hex, 3);
    11.     //set enable high
    12.     PORTBbits.RB6 = 1;
    13.     //kill time
    14.     Delay10KTCYx(3);
    15.     //set enable to low for signal send
    16.     PORTBbits.RB6 = 0;
    17.     Delay10KTCYx(15);
    18. }
    20. void write_lcd(unsigned char hex){
    21.     //set enable to low
    22.     PORTBbits.RB6 = 0;
    23.     //set r/s to zero for command
    24.     PORTBbits.RB4 = 0;
    25.     //put in high byte
    26.     PORTBbits.RB0 = getBit(hex, 4);
    27.     PORTBbits.RB1 = getBit(hex, 5);
    28.     PORTBbits.RB2 = getBit(hex, 6);
    29.     PORTBbits.RB3 = getBit(hex, 7);
    30.     //set enable high
    31.     PORTBbits.RB6 = 1;
    32.     //kill time
    33.     Delay10KTCYx(3);
    34.     //set enable to low for signal send
    35.     PORTBbits.RB6 = 0;
    36.     Delay10KTCYx(15);
    37.     //put in low byte
    38.     PORTBbits.RB0 = getBit(hex, 0);
    39.     PORTBbits.RB1 = getBit(hex, 1);
    40.     PORTBbits.RB2 = getBit(hex, 2);
    41.     PORTBbits.RB3 = getBit(hex, 3);
    42.     /************************************************/
    43.     //set enable high
    44.     PORTBbits.RB6 = 1;
    45.     //kill time
    46.     Delay10KTCYx(3);
    47.     //set enable to low for signal send
    48.     PORTBbits.RB6 = 0;
    49.     Delay10KTCYx(15);
    50. }
    52. void lcd_init(void){
    53.     Delay10KTCYx(60);
    54.     write_lcd_4(0x03);
    55.     write_lcd_4(0x03);
    56.     write_lcd_4(0x03);
    57.     write_lcd_4(0x02);
    58.     //function set
    59.     write_lcd(0x28);
    60.     //turn off
    61.     write_lcd(0x08);
    62.     //display clear
    63.     write_lcd(0x01);
    64.     //entry mode
    65.     write_lcd(0x06);
    66.     //turn back on
    67.     write_lcd(0x09);
    68. }
  2. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    What makes you think it is not initializing properly?

    Do you have a pot on your contrast pin? Is it adjusted properly?

    Have you stepped through your code outputting bits to the LCD and confirming the bits are being seen as expected on the LCD? (basically confirming that you have everything wired correctly).
  3. jbradley

    Thread Starter New Member

    Mar 6, 2012
    The screen weather I attempt to initialize or not starts out with all of the "squares" on the lcd screen just filled in black. When I attempt to initialize it remains that way and one of the commands during my attempt to initialize is a clear, which should make them all go away and the screen just be blank. I am 100% positive it is wired up the way I state at the way I state it is at the beginning of my post.

    When it comes to the contrast pin I don't really know anything about that, as I said first time using a pic and new in general to this. I do know that I can run other code on the chip and use the LED light for output so the chip does work so it is being set up correctly.
  4. MrChips


    Oct 2, 2009
    Many times people ignore the contrast pin and cannot figure out why the LCD does not work.
    If the contrast voltage is set to an extreme, you may end up with nothing or all "squares".
  5. jbradley

    Thread Starter New Member

    Mar 6, 2012
    With my board the voltage to the lcd can either be 5v or 3.3v I believe I forget which one I have it set on right now (its on the one a EE grad student told me to put it on), and I have switched it out of frustration before just to see if it would initialize then and that still didn't change the result.
  6. t06afre

    AAC Fanatic!

    May 11, 2009
    Are you 100% sure your timing is correct. from your setup I am not sure about it. Take a look here http://www.taoli.ece.ufl.edu/teaching/4744/labs/lab7/LCD_V1.pdf
    One thing that may be confusing is the text on page one
    That do not make much sense to me. I think it is a typo. And the text should have been something like this​
    Then you have LCD working. You should replace coding like this​
    Code ( (Unknown Language)):
    2. [LEFT]write_lcd_4(0x03);
    3. write_lcd_4(0x03);
    4. write_lcd_4(0x03);
    5. write_lcd_4(0x02);[/LEFT]
    It will work. But it is kind of clumsy way of doing things. Try to use Mask Operators instead. This may be of some help
    jbradley likes this.
  7. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    Actually no where did you state it is wired correctly. But that is irrelevant. Unless you have actually toggle each bit and confirmed that it toggles on the corresponding LCD pin then you can't be 100% sure everything is connected and working correctly.

    But your issue is probably contrast. You need a 10K trimmer resistor. Tie one end of the trimmer to ground, the over to your supply and the wiper to the contrast pin on the LCD.
    jbradley likes this.