Can´t interface PIC18F4520 to LCD HD44780

Discussion in 'Embedded Systems and Microcontrollers' started by LewisMF, Mar 26, 2015.

  1. LewisMF

    Thread Starter Member

    Nov 15, 2014
    63
    2
    Hi everybody,

    I am trying to interface a PIC18F4520 to a standard 16x2 LCD with Hitachi HD44780 controller in 4-bit mode, I am using MPLAB IDE v2.35 with C18 compiler.

    This is my code:

    Code (Text):
    1. /*Libraries*/
    2. #include <stdio.h>      //standard I/O library
    3. #include <stdlib.h>     //standard library
    4. #include <xlcd.h>       //LCD library
    5. #include <delays.h>     //Delays library
    6. #include <p18f4520.h>   //PIC18F4520 library
    7.  
    8. /*Fuses*/
    9. #pragma config OSC = HS,FCMEN = OFF,IESO = OFF
    10. #pragma config PWRT = ON,BOREN = OFF,BORV = 0
    11. #pragma config WDT = OFF,WDTPS = 32768
    12. #pragma config MCLRE = ON,LPT1OSC = OFF,PBADEN = OFF,CCP2MX = PORTC
    13. #pragma config STVREN = OFF,LVP = OFF,XINST = OFF,DEBUG = OFF
    14. #pragma config CP0 = OFF,CP1 = OFF,CP2 = OFF, CP3 = OFF
    15. #pragma config CPB = OFF,CPD = OFF
    16. #pragma config WRT0 = OFF,WRT1 = OFF,WRT2 = OFF
    17. #pragma config WRTB = OFF,WRTC = ON,WRTD = OFF
    18. #pragma config EBTR0 = OFF,EBTR1 = OFF,EBTR2 = OFF
    19. #pragma config EBTRB = OFF
    20.  
    21. /*Delays for LCD - All calculated for Xtal=20MHz*/
    22. void DelayFor18TCY(void)
    23. {
    24.     Delay10TCYx(10); //Delay of at least 18 Clock Cycles
    25. }
    26.  
    27. void DelayPORXLCD (void)
    28. {
    29.     Delay1KTCYx(80);    // Delay of at least 15ms
    30. }
    31.  
    32. void DelayXLCD (void)
    33. {
    34.     Delay1KTCYx(10);    // Delay of at least 5ms
    35. }
    36.  
    37. /*Main Program*/
    38. void main (void){
    39.  
    40.     ADCON1=0x0F;    //All I/O as digital
    41.    
    42.     OpenXLCD(FOUR_BIT & LINES_5X7); //Four bit mode and 5x7 lines
    43.     while(BusyXLCD());
    44.     WriteCmdXLCD(CURSOR_OFF & BLINK_OFF);
    45.  
    46.     while (BusyXLCD());         //Wait for LCD
    47.     putrsXLCD("PIC18F4520");    //Output message to LCD
    48.  
    49.     while(1){};     //Infinite loop
    50. }
    Attached you will find a screenshot of my schematic.

    I am simulating it in ISIS with an XTAL of 20MHz, when I simulate the data pins for the LCD appear disconnected and the LCD is blank.

    Can anyone spot the mistake?

    Thanks in advanced for any help.
     
  2. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,395
    1,607
    Check the data sheet of the HD44780 (or clone) controller you are using. Typically there is a delay after power up before any commant, and after the opening command too.

    Make sure your copy of XLCD has these delays properly sey up.

    Did you make a custom xlcd.h file? I believe that is where the library leans what pins your functions are on.
     
  3. LewisMF

    Thread Starter Member

    Nov 15, 2014
    63
    2

    Hi ErnieM,

    I am using standard xlcd.h file, I haven´t made any modifications.

    I programmed the delays in the main C file as described in the datasheet.

    This is the xlcd.h file, as you can see there has been no modifications (I actually tried to modify it and it doesn´t let me, it says "access denied" I dont really understand why)
    Code (Text):
    1. #ifndef __XLCD_H
    2. #define __XLCD_H
    3. #include "p18cxxx.h"
    4. /* PIC18 XLCD peripheral routines.
    5. *
    6. *   Notes:
    7. *      - These libraries routines are written to support the
    8. *        Hitachi HD44780 LCD controller.
    9. *      - The user must define the following items:
    10. *          - The LCD interface type (4- or 8-bits)
    11. *          - If 4-bit mode
    12. *              - whether using the upper or lower nibble
    13. *          - The data port
    14. *              - The tris register for data port
    15. *              - The control signal ports and pins
    16. *              - The control signal port tris and pins
    17. *          - The user must provide three delay routines:
    18. *              - DelayFor18TCY() provides a 18 Tcy delay
    19. *              - DelayPORXLCD() provides at least 15ms delay
    20. *              - DelayXLCD() provides at least 5ms delay
    21. */
    22.  
    23. /* Interface type 8-bit or 4-bit
    24. * For 8-bit operation uncomment the #define BIT8
    25. */
    26. /* #define BIT8 */
    27.  
    28. /* When in 4-bit interface define if the data is in the upper
    29. * or lower nibble.  For lower nibble, comment the #define UPPER
    30. */
    31. /* #define UPPER */
    32.  
    33. /* DATA_PORT defines the port to which the LCD data lines are connected */
    34. #define DATA_PORT              PORTB
    35. #define TRIS_DATA_PORT         TRISB
    36.  
    37. /* CTRL_PORT defines the port where the control lines are connected.
    38. * These are just samples, change to match your application.
    39. */
    40. #define RW_PIN   LATBbits.LATB6           /* PORT for RW */
    41. #define TRIS_RW  TRISBbits.TRISB6        /* TRIS for RW */
    42.  
    43. #define RS_PIN   LATBbits.LATB5           /* PORT for RS */
    44. #define TRIS_RS  TRISBbits.TRISB5        /* TRIS for RS */
    45.  
    46. #define E_PIN    LATBbits.LATB4          /* PORT for D  */
    47. #define TRIS_E   TRISBbits.TRISB4        /* TRIS for E  */
    48.  
    49. /* Display ON/OFF Control defines */
    50. #define DON         0b00001111  /* Display on      */
    51. #define DOFF        0b00001011  /* Display off     */
    52. #define CURSOR_ON   0b00001111  /* Cursor on       */
    53. #define CURSOR_OFF  0b00001101  /* Cursor off      */
    54. #define BLINK_ON    0b00001111  /* Cursor Blink    */
    55. #define BLINK_OFF   0b00001110  /* Cursor No Blink */
    56.  
    57. /* Cursor or Display Shift defines */
    58. #define SHIFT_CUR_LEFT    0b00000100  /* Cursor shifts to the left   */
    59. #define SHIFT_CUR_RIGHT   0b00000101  /* Cursor shifts to the right  */
    60. #define SHIFT_DISP_LEFT   0b00000110  /* Display shifts to the left  */
    61. #define SHIFT_DISP_RIGHT  0b00000111  /* Display shifts to the right */
    62.  
    63. /* Function Set defines */
    64. #define FOUR_BIT   0b00101100  /* 4-bit Interface               */
    65. #define EIGHT_BIT  0b00111100  /* 8-bit Interface               */
    66. #define LINE_5X7   0b00110000  /* 5x7 characters, single line   */
    67. #define LINE_5X10  0b00110100  /* 5x10 characters               */
    68. #define LINES_5X7  0b00111000  /* 5x7 characters, multiple line */
    69.  
    70. #define PARAM_SCLASS auto
    71. #define MEM_MODEL far  /* Change this to near for small memory model */
    72.  
    73. /* OpenXLCD
    74. * Configures I/O pins for external LCD
    75. */
    76. void OpenXLCD(PARAM_SCLASS unsigned char);
    77.  
    78. /* SetCGRamAddr
    79. * Sets the character generator address
    80. */
    81. void SetCGRamAddr(PARAM_SCLASS unsigned char);
    82.  
    83. /* SetDDRamAddr
    84. * Sets the display data address
    85. */
    86. void SetDDRamAddr(PARAM_SCLASS unsigned char);
    87.  
    88. /* BusyXLCD
    89. * Returns the busy status of the LCD
    90. */
    91. unsigned char BusyXLCD(void);
    92.  
    93. /* ReadAddrXLCD
    94. * Reads the current address
    95. */
    96. unsigned char ReadAddrXLCD(void);
    97.  
    98. /* ReadDataXLCD
    99. * Reads a byte of data
    100. */
    101. char ReadDataXLCD(void);
    102.  
    103. /* WriteCmdXLCD
    104. * Writes a command to the LCD
    105. */
    106. void WriteCmdXLCD(PARAM_SCLASS unsigned char);
    107.  
    108. /* WriteDataXLCD
    109. * Writes a data byte to the LCD
    110. */
    111. void WriteDataXLCD(PARAM_SCLASS char);
    112.  
    113. /* putcXLCD
    114. * A putc is a write
    115. */
    116. #define putcXLCD WriteDataXLCD
    117.  
    118. /* putsXLCD
    119. * Writes a string of characters to the LCD
    120. */
    121. void putsXLCD(PARAM_SCLASS char *);
    122.  
    123. /* putrsXLCD
    124. * Writes a string of characters in ROM to the LCD
    125. */
    126. void putrsXLCD(const rom char *);
    127.  
    128. /* User defines these routines according to the oscillator frequency */
    129. extern void DelayFor18TCY(void);
    130. extern void DelayPORXLCD(void);
    131. extern void DelayXLCD(void);
    132.  
    133. #endif
    134.  
    This is driving me crazy, I can´t find any examples anywhere and I don´t really understand why it doesn´t work.

    Any more suggestions?

    Thank you for your help ErnieM.
     
  4. MaxHeadRoom

    Expert

    Jul 18, 2013
    10,567
    2,379
    There is example files on the Picmicro site both C and assy.
    The files supplied with Picdem2 Plus Demo board are on the Picmicro site.
    Max.
     
  5. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,395
    1,607
    The xlcd.h you included seems to match your hardware. Is the file read only?
    I saw you have delays defined in your code. I do not see where or if they are used. Did you check if the starting delays are in the library?

    Since I have never used ISIS I can't comment further. If you actually build one I would have suggestions (like make sure the pins wiggle when you want them to).
     
  6. LewisMF

    Thread Starter Member

    Nov 15, 2014
    63
    2
    Hi Max,

    I have been checking the
    Hi ErnieM,

    I have checked and the file is not set as read only...strange....

    I have tried putting the delay functions in the main program, but still nothing...

    This is just driving me crazy....
     
  7. Art

    Distinguished Member

    Sep 10, 2007
    785
    61
    A .h file is a header file. All of those functions should be in a matching .c file
    that should also be included in the project.
    The main.c file just to test the LCD would be the smallest, and just call functions from the .c file with the variables populated.
    If you have just included the .h file, that is programatically nothing.
     
  8. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,395
    1,607
    Art: the TS did not state the project did not build, just that it did not work.

    Lewis: the project did state it built correctly, did it not?

    I do wonder if there are multiple coppies of the XLCD stuff in various places. I've had that problem myself.
     
  9. LewisMF

    Thread Starter Member

    Nov 15, 2014
    63
    2
    Hi lads,

    The project does build correctly, the compiler gives no errors at all. But when I simulate it, it does not output the message to the LCD and the data pins (RB0 - RB3) are marked as "disconnected"

    I still cannot get it to work :(

    I do not understand what Art is trying to say...
     
  10. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,395
    1,607
    I think I get his drift, as your project does indeed build you have covered his concerns.

    I wish I had something else to help you get this working.
     
  11. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    4,887
    1,017
    Before you do anything, you need to first confirm your LCD is wired correctly. This advice seems to always over looked here. I guess it is assumed that it is. But bad assumption.

    First confirm the LCD is powered properly, then check back light, finally the contrast pin.

    When I have trouble with the LCD, the very first thing I do is so write a test program to send data to the LCD using the library function calls. I use my debugger and step trough the code, I use a scope or logic probe to confirm all data bits and control bits are arriving at the LCD the way I think they should. Using the library also confirms that it is configured correctly to the pins from you mcu to the LCD are configured correctly.

    The next thing I do is to write a test program to check my delays. Probably not the most accurate method but certainly easy, I create a long delay and then time it with the clock on my PC.
     
  12. LewisMF

    Thread Starter Member

    Nov 15, 2014
    63
    2
    Hi Spinnaker,

    First of all I would like to thank you for your reply. I am really stuck on this...

    The LCD is wired correctly, all data pins and control pins are wired to the correspondent MCU pins. It is also powered and grounded correctly and the back light is also working.

    The program I wrote is a really basic program to test the LCD which just sends a string for the LCD to output, but it doesn´t. As I said in previous replies, when I run the program in the MCU the data pins appear to be in "high impedance" or disconnected...

    I have followed the datasheet and have written all the appropriate delays, I just can´t find the problem....
     
  13. blueroomelectronics

    AAC Fanatic!

    Jul 22, 2007
    1,758
    98
    When I was getting the hang of LCDs I'd use the 8 bit mode as you can check the busy bit. Port D+E make for an excellent LCD interface.
     
  14. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    4,887
    1,017
    But how do you know this? Just from visual confirmation of the wiring or did you actually do a test procedure as I recommended? Visual confirmation is not good enough.

    There is also the possibility that you have a bad LCD.

    Also did you check your delays? Just because you say it is delaying a certain period does not mean it is. A mistake in MCU configuration or delay function setup could easily make for an incorrect delay.
     
  15. blueroomelectronics

    AAC Fanatic!

    Jul 22, 2007
    1,758
    98
    OP post a photo of your assembled circuit.
     
  16. MaxHeadRoom

    Expert

    Jul 18, 2013
    10,567
    2,379
    You can do that with 4 bit also.
    Max.
     
  17. LewisMF

    Thread Starter Member

    Nov 15, 2014
    63
    2
    Hi Spinnaker,

    As I said in previous posts I am simulating this with ISIS, the schematic I am using worked with former programs I wrote a few years ago so I can assure that the schematic is good.

    About the delays, yes that could be the problem, I am very new to embedded programming so that´s why I posted a picture of the schematic and my code in my first post, hoping that someone would check it for me.
     
  18. LewisMF

    Thread Starter Member

    Nov 15, 2014
    63
    2
    Hi blueroomelectronics,

    In my first post you will find a picture of my schematic and also the code I wrote.
     
  19. Alberto

    Active Member

    Nov 7, 2008
    169
    36
    Why you don't try to connect lcd R/W directly to ground (disconnect pin RB6)
    And see if it works ?

    Cheers

    Alberto
     
  20. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    4,887
    1,017

    Simulation? Well I think you are on your own then. No telling ig the software is even working as it should. Why not just buy the real thing?

    There has to be plenty of samples for the HD44780 and the pic. Why not just try to find one of them and see if it works?
     
Loading...