Interfacing with an LCD Screen

Discussion in 'Embedded Systems and Microcontrollers' started by HarG, Dec 19, 2012.

  1. HarG

    Thread Starter New Member

    Nov 20, 2012
    18
    0
    Hello everyone,

    I'm trying to operate a 2-line LCD HD44780. I am just wondering if I need to set EN high to low for every command I send? Also if I just write the initialisation piece, will I notice if it has worked on the display (like a flashing cursor) or will it stay blank until an actual command has beed sent?

    Thanks
     
  2. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,386
    1,605
    Yes it needs to toggle low then high for every command.

    Stays blank till initialized. If you have a contrast control you sometimes see half the rows show as complete blocks with a very misaligned contrast on an uninitialized display.
     
  3. MrChips

    Moderator

    Oct 2, 2009
    12,424
    3,359
    E should go high then low.
     
  4. HarG

    Thread Starter New Member

    Nov 20, 2012
    18
    0
    Thank you. That was very quick and very helpful.

    Cheers guys
     
  5. HarG

    Thread Starter New Member

    Nov 20, 2012
    18
    0
    Hi guys.

    Been having a go with it and can't seem to get it going. I think I have about got it but could you just take a quick gander at my code to make sure I'm not hugely mistaken?

    I set a delay of 2ms (as its just larger than the longest time for the display to do a function) instead of using the busy flag. I can't see the problem really. I have the 8 data bits in Port C of an 18F45K20.

    Code ( (Unknown Language)):
    1.  
    2. #include <xc.h>
    3. #include <stdio.h>
    4. #include <stdlib.h>
    5. #pragma config FOSC = INTIO67, FCMEN = OFF, IESO = OFF, PWRT = OFF, BOREN = OFF
    6. #pragma config BORV = 18, WDTEN = OFF, WDTPS = 1, MCLRE = ON, HFOFST = ON
    7. #pragma config LPT1OSC = OFF, PBADEN = OFF, CCP2MX = PORTC, STVREN = OFF
    8. #pragma config LVP = OFF,  XINST = OFF, CP0 = OFF, CP1 = OFF, CP2 = OFF
    9. #pragma config CP3 = OFF, CPB = OFF, CPD = OFF, WRT0 = OFF, WRT1 = OFF
    10. #pragma config WRT2 = OFF, WRT3 = OFF, WRTB = OFF, WRTC = OFF, WRTD = OFF
    11. #pragma config EBTR0 = OFF, EBTR1 = OFF, EBTR2 = OFF, EBTR3 = OFF
    12. #pragma config EBTRB = OFF
    13. #define LCD_data PORTC
    14. #define RS   RB5
    15. #define RW   RB6
    16. #define EN   RB7
    17. #define _XTAL_FREQ 16000000
    18.  
    19. void PIC_init()
    20. {
    21.     TRISC=0;
    22.     TRISB=0;
    23. }
    24. void LCD_busy()
    25. {
    26.   __delay_ms(2);
    27. }
    28.  
    29. void LCD_init()
    30. {
    31.      LCD_data = 0x30;
    32.      EN   = 1;              //Enable H->L
    33.      EN   = 0;
    34.      __delay_ms(4.5);
    35.      LCD_data = 0x30;
    36.      EN   = 1;        
    37.      EN   = 0;
    38.     __delay_us(300);
    39.      LCD_data = 0x30;
    40.      EN   = 1;        
    41.      EN   = 0;
    42.  
    43.      LCD_data = 0x30;       //Function set
    44.      RS   = 0;
    45.      RW   = 0;
    46.      EN   = 1;
    47.      EN   = 0;
    48.      LCD_busy();
    49.      LCD_data = 0x08;       //Display off
    50.      RS   = 0;
    51.      RW   = 0;
    52.      EN   = 1;
    53.      EN   = 0;
    54.      LCD_busy();            
    55.      LCD_data = 0x01;       //Clear LCD
    56.      RS   = 0;
    57.      RW   = 0;
    58.      EN   = 1;
    59.      EN   = 0;
    60.      LCD_busy();
    61.      LCD_data = 0x03;       //Entry mode - increment, shift left
    62.      RS   = 0;
    63.      RW   = 0;
    64.      EN   = 1;
    65.      EN   = 0;
    66.      LCD_busy();
    67. }
    68.  
    69. void main(void)
    70. {
    71.      __delay_ms(20);
    72.     PIC_init();
    73.     LCD_init();
    74.     RS = 1;
    75.     RW = 0;
    76.     LCD_data = 0b00110000;  //'0' ASCII
    77.     EN = 1;
    78.     EN = 0;
    79.     LCD_busy();
    80.     RS = 0;
    81.     LCD_data = 0b00001111;  //display on, cursor on, blink on
    82.     EN = 1;
    83.     EN = 0;
    84. }
    85.  
     
  6. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    4,866
    989
    Instead of us looking at your code, you should first verify that everything is connected the way it should be and pins are changing state as expected.

    You can do this by single stepping through your code and measuring the pins on the LCD with a scope, logic probe or voltmeter. You can make a logic probe with an led and resistor if you don't have one.

    One of the biggest problems of an LCD not working is improper connections.

    Many compilers have LCD libraries and there really is not need to write one. Also many out there on the internet.
     
  7. HarG

    Thread Starter New Member

    Nov 20, 2012
    18
    0
    Thank you spinnaker. I will give it a go.
     
  8. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,386
    1,605
    Check the delays you use. I did notice you immediately bring EN hi then lo. It may need a bit longer dwell, as well as setup times for the data.
     
  9. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    Here is some code that work on a 18F25K22. It use 4 bit mode but it should be no problem using 8 bit mode. See if it can help you
     
  10. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    4,866
    989
    4 bit mode is easier anyway. Less connections to screw up. :)
     
  11. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    This is how it was connected in 4 bit mode if it can help you
     
  12. HarG

    Thread Starter New Member

    Nov 20, 2012
    18
    0
    Thank you very much guys. I will have another go tomorrow and let you know how it goes.

    Once again thank you.
     
  13. HarG

    Thread Starter New Member

    Nov 20, 2012
    18
    0
    Hi guys,

    I had a bit of a go with it tonight. I changed the 8 data lines to LEDs to try and see what is going on. They seem to be doing the right thing but unfortunately they are continuously looping.

    I was under the impression that if I did not put a while(1) command in the main program it would only run once? It seems to be continuously running through my main program.

    Any thoughts?
     
  14. MrChips

    Moderator

    Oct 2, 2009
    12,424
    3,359
    If your main program is being repeated check to see if the watch-dog timer is active and hence restarting your program after a predetermined time-out.
     
  15. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,386
    1,605
    The opposite is true, though it depends on how and where you insert the while(1)
     
Loading...