LCD Software Help PIC 16F877A

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

  1. oiyela

    Thread Starter New Member

    Mar 6, 2012
    3
    0
    Hi,
    I am currently doing my project and i need to test my microcontroller with an LCD screen, i am using HI-tech compiler on mp lab. my code builds but does not display anything or put on the LCD. Please Help
    Here is what i mapped the microcontroller pins to

    Code ( (Unknown Language)):
    1.  
    2. RD4 to LCD_RS
    3. RD5 to LCD_EN
    4.  
    5. RD0 to LCD_D4
    6. RD1 to LCD_D5
    7. RD2 to LCD_D6
    8. RD3 to LCD_D7
    9.  
    10. Here is my software:
    11.  
    12. #ifndef _XTAL_FREQ
    13. // Unless specified elsewhere, 4MHz system frequency is assumed
    14. #define _XTAL_FREQ 8000000
    15. #endif
    16.  
    17. #include    <htc.h>
    18. #include    "lcd.h"
    19.  
    20. #define LCD_RS RD4
    21. #define LCD_EN RD5
    22.  
    23. #define LCD_DATA    PORTD
    24.  
    25. #define LCD_STROBE()    ((LCD_EN = 1),(LCD_EN=0))
    26.  
    27. /* write a byte to the LCD in 4 bit mode */
    28.  
    29. void
    30. lcd_write(unsigned char c)
    31. {
    32. __delay_us(40);
    33. LCD_DATA = ( ( c >> 4 ) & 0x0F );
    34. LCD_STROBE();
    35. LCD_DATA = ( c & 0x0F );
    36. LCD_STROBE();
    37. }
    38.  
    39. /*
    40. * Clear and home the LCD
    41. */
    42.  
    43. void
    44. lcd_clear(void)
    45. {
    46. LCD_RS = 0;
    47. lcd_write(0x1);
    48. __delay_ms(2);
    49. }
    50.  
    51. /* write a string of chars to the LCD */
    52.  
    53. void
    54. lcd_puts(const char * s)
    55. {
    56. LCD_RS = 1; // write characters
    57. while(*s)
    58. lcd_write(*s++);
    59. }
    60.  
    61. /* write one character to the LCD */
    62.  
    63. void
    64. lcd_putch(char c)
    65. {
    66. LCD_RS = 1; // write characters
    67. lcd_write( c );
    68. }
    69.  
    70.  
    71. /*
    72. * Go to the specified position
    73. */
    74.  
    75. void
    76. lcd_goto(unsigned char pos)
    77. {
    78. LCD_RS = 0;
    79. lcd_write(0x80+pos);
    80. }
    81.  
    82. /* initialise the LCD - put into 4 bit mode */
    83. void
    84. lcd_init()
    85. {
    86. char init_value;
    87.  
    88.  
    89.  
    90. init_value = 0x3;
    91. TRISA=0;
    92. TRISD=0;
    93. LCD_RS = 0;
    94. LCD_EN = 0;
    95.  
    96.  
    97. __delay_ms(15); // wait 15mSec after power applied,
    98. LCD_DATA     = init_value;
    99. LCD_STROBE();
    100. __delay_ms(5);
    101. LCD_STROBE();
    102. __delay_us(200);
    103. LCD_STROBE();
    104. __delay_us(200);
    105. LCD_DATA = 2;   // Four bit mode
    106. LCD_STROBE();
    107.  
    108. lcd_write(0x28); // Set interface length
    109. lcd_write(0xF); // Display On, Cursor On, Cursor Blink
    110. lcd_clear();    // Clear screen
    111. lcd_write(0x6); // Set entry Mode
    112. }
    113.  
     
    Last edited by a moderator: Mar 8, 2012
  2. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    Can you show your main program also. One thing you must remember. Is that the Hi-Tech C example you use. Use two different ports. One for the control signal and one for the data. If you use the same port for both data and and control. You must modify that example slightly. As a hint take a look on how the control signal LCD_RS are used ;)
     
  3. MrChips

    Moderator

    Oct 2, 2009
    12,440
    3,361
    Did you connect the contrast pin properly?
     
  4. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    4,884
    1,005
    Did you step through your code and confirm that inputs are changing as expected on the connected LCD?
     
Loading...