Help: LCD interfacing using MPLAB

Discussion in 'Programmer's Corner' started by jon-eCe, Mar 2, 2013.

  1. jon-eCe

    Thread Starter New Member

    Feb 23, 2013
    1
    0
    Hello,

    We are currently on LCD interfacing using PIC16f877A and MPLAB. Could someone give me any example source code for this just to get an idea how its done. Thank you. :)
     
  2. MrChips

    Moderator

    Oct 2, 2009
    12,440
    3,361
    Here are some LCD functions:

    Code ( (Unknown Language)):
    1.  
    2. // LCD Interface
    3. // 4-bit Data on RB3-RB0
    4.  
    5. #undef CLOCK32K
    6. #define FAST_CLOCK
    7.  
    8. #define LCD_RS RA4
    9. #define LCD_RW RA3
    10. #define LCD_E  RA2
    11. #define LCD_Data PORTB
    12. #define LCD_TRIS TRISB
    13.  
    14. #define NUL    0
    15. #define SPACE 32
    16.  
    17. //-----------------------------------------------------------
    18. //                 Display Functions
    19. //------------------------------------------------------------
    20.  
    21. void LCDrdy(void)
    22. {
    23. #ifdef FAST_CLOCK
    24.   char b;
    25.   // wait for LCD ready
    26.   // read data and wait for bit-7 = 0
    27.   LCD_TRIS = 0b00001111;
    28.   LCD_RS = 0;
    29.   LCD_RW = 1;
    30.   do
    31.    {
    32.      LCD_E = 1;
    33.      b = LCD_Data & 0x00001000;
    34.      LCD_E = 0;
    35.      LCD_E = 1;
    36.      LCD_E = 0;
    37.    } while (b != 0);
    38. #endif
    39. }
    40.  
    41. void LCD(char b)
    42. {// send to data register
    43.  // send HI-byte, LO-byte
    44.   LCDrdy();
    45.   LCD_RS = 1;
    46.   LCD_RW = 0;
    47.   LCD_TRIS = 0b00000000;
    48.   LCD_Data = (b >> 4);
    49.   LCD_E = 1;
    50.   LCD_E = 0;
    51.   LCD_Data = b;
    52.   LCD_E = 1;
    53.   LCD_E = 0;
    54. }
    55.  
    56. void LCDir(char b)
    57. {
    58.  // send to instruction register
    59.  // send HI-byte, LO-byte
    60.   LCDrdy();
    61.   LCD_RS = 0;
    62.   LCD_RW = 0;
    63.   LCD_TRIS = 0b00000000;
    64.   LCD_Data = (b >> 4);
    65.   LCD_E = 1;
    66.   LCD_E = 0;;
    67.   LCD_Data = b;
    68.   LCD_E = 1;
    69.   LCD_E = 0;
    70. }
    71.  
    72. void LCDinit(void)
    73. {
    74. // initialize LCD for 4-bit mode
    75. // send $28, $06, $0C, $10
    76. #ifdef FAST_CLOCK
    77.   _delay(50000); // 50ms delay
    78. #endif
    79.   LCDir(0x28);
    80.   LCDir(0x28);
    81.   LCDir(0x06);
    82.   LCDir(0x0C);
    83.   LCDir(0x10);
    84. }
    85.  
    86. void LCDclr(void)
    87. {
    88. // clear LCD
    89. // send $01
    90.   LCDir(0x01);
    91.   #ifdef FAST_CLOCK
    92.   _delay(2000); //2ms delay
    93.   #endif
    94. }
    95.  
    96. void LCD_Line1(void)
    97. {
    98.   LCDir(0x80);
    99. }
    100.  
    101. void LCD_Line2(void)
    102. {
    103.   LCDir(0xC0);
    104. }
    105.  
    106. void LCD_Posn(int n)
    107. // position n = 0 to 79
    108. // Line 1 begins at n = 0
    109. // Line 2 begins at n = 40
    110. {
    111.   if (n < 40)
    112.    {
    113.      LCDir(0x80 + n);
    114.    }
    115.   else
    116.    {
    117.      LCDir(0x80 + n + 24);
    118.    }
    119. }
    120.  
    121. void LCD_Txt(char *s)
    122. {
    123.   while (*s)
    124.    {
    125.      LCD(*s++);
    126.    };
    127. }
    128.  
    129. // End of LCD functions
    130.  
    131.  
     
    Last edited: Mar 2, 2013
  3. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    Sure but you did not tell us if you are using assembler or C. And If C which compiler you are using
     
  4. bhodz

    New Member

    Jan 29, 2013
    18
    0
    hello do you have an idea on how to scroll the text in 2x16 lcd using 877a and mplab..pls help:( i want to scroll only the second line and make the first line steady..:( help.:(:(
     
  5. MrChips

    Moderator

    Oct 2, 2009
    12,440
    3,361
    Simply redraw the second line with all characters moved over by one position.
     
Loading...