• AVR Freaks

Hot!LCD 16x2 with XC8

Page: 123 > Showing page 1 of 3
Author
ArakelTheDragon
Starting Member
  • Total Posts : 64
  • Reward points : 0
  • Joined: 2016/12/11 07:12:00
  • Location: 0
  • Status: offline
2020/04/09 08:06:39 (permalink)
0

LCD 16x2 with XC8

Good day! I am trying to use and LCD in 4 bit mode, I see 10 instructions only to initializa the display. Is this really so hard? Is there some good documentation for doing this, I tried searching but there is nothing good and the examples don't work.
 
XC8 v2.10
Windows 10
 

/* Main.c file generated by New Project wizard
 *
 * Created:   Thu Mar 26 2020
 * Processor: PIC16F690
 * Compiler:  MPLAB XC8
 */
                                       //// Includes ////
#include <xc.h>
                                       //// Configuration register ////
__CONFIG(MCLRE_OFF, CP_OFF, WDT_OFF, OSC_IntRC, INTRCIO);    // Mclear ON, Code protect off, watchdog timer off, Internal RC oscilator with 1% factory calibrated accuracy, Oscilator pins as digital

               
                                       //// Definitions ////
#define RS PORTCbits.RC3
#define RW PORTCbits.RC4
#define EN PORTCbits.RC5
#define D4 PORTCbits.RC6
#define D5 PORTCbits.RC7
#define D6 PORTBbits.RB7
#define D7 PORTBbits.RB6

                                       /***** MAIN PROGRAM *****/
void main()
{
                                      //// Local variables and Initialisation ////
   unsigned int i = 0;
                                       // Initializa the TRIS registers.
           //TRISA = 0;                           // 1 = input, 0 = output.
        TRISB = 0;                           // 1 = input, 0 = output.
        TRISC = 0;                                 // 1 = input, 0 = output.
      
                                       // Initialize display 16x2 in 4 bit mode according to the datasheet
           PORTCbits.RC3 = 0;                       // Enable pin on the LCD.
           PORTCbits.RC4 = 0;                       // Read / write pin on the LCD.
           PORTCbits.RC5 = 0;                       // Reset pin on the LCD.

                                            //// Main loop ////
    for (;;)
    {
                                       //// LCD initialization ////
                           // Instruction 1
       RS = 0;                   // Send pin on the LCD.
       RW = 0;               // Send pin on the LCD.
       EN = 0;                   // Send pin on the LCD.
       D4 = 1;                   // Send pin on the LCD.
       D5 = 1;                   // Send pin on the LCD.
       D6 = 0;                   // Send pin on the LCD.
       D7 = 0;                   // Send pin on the LCD.
       _delay (15000);               // Wait atleast 15ms for the initialization to complete. Instruction clock = 4MHz / 4 = 1MHz, T = 1 / 1MHz = 0.000 000 1 S = 1uS.
 
                           // Instruction 2
       RS = 0;                   // Send pin on the LCD.
       RW = 0;               // Send pin on the LCD.
       EN = 0;                   // Send pin on the LCD.
       D4 = 1;                   // Send pin on the LCD.
       D5 = 1;                   // Send pin on the LCD.
       D6 = 0;                   // Send pin on the LCD.
       D7 = 0;                   // Send pin on the LCD.
       _delay (4100);               // Wait atleast 4.1mS for the initialization to complete. Instruction clock = 4MHz / 4 = 1MHz, T = 1 / 1MHz = 0.000 000 1 S = 1uS.

                           // Instruction 3
       RS = 0;                   // Send pin on the LCD.
       RW = 0;               // Send pin on the LCD.
       EN = 0;                   // Send pin on the LCD.
       D4 = 1;                   // Send pin on the LCD.
       D5 = 1;                   // Send pin on the LCD.
       D6 = 0;                   // Send pin on the LCD.
       D7 = 0;                   // Send pin on the LCD.
       _delay (100);               // Wait atleast 100uS for the initialization to complete. Instruction clock = 4MHz / 4 = 1MHz, T = 1 / 1MHz = 0.000 000 1 S = 1uS.
       
                           // Instruction 4
       RS = 0;                   // Send pin on the LCD.
       RW = 0;               // Send pin on the LCD.
       EN = 0;                   // Send pin on the LCD.
       D4 = 0;                   // Send pin on the LCD.
       D5 = 1;                   // Send pin on the LCD.
       D6 = 0;                   // Send pin on the LCD.
       D7 = 0;                   // Send pin on the LCD.
       _delay (4100);               // Wait atleast 4.1mS for the initialization to complete. Instruction clock = 4MHz / 4 = 1MHz, T = 1 / 1MHz = 0.000 000 1 S = 1uS.
       
                           // Instruction 5
       RS = 0;                   // Send pin on the LCD.
       RW = 0;               // Send pin on the LCD.
       EN = 0;                   // Send pin on the LCD.
       D4 = 0;                   // Send pin on the LCD.
       D5 = 1;                   // Send pin on the LCD.
       D6 = 0;                   // Send pin on the LCD.
       D7 = 0;                   // Send pin on the LCD.
       _delay (40);               // Wait atleast 4.1mS for the initialization to complete. Instruction clock = 4MHz / 4 = 1MHz, T = 1 / 1MHz = 0.000 000 1 S = 1uS.
       
                           // Instruction 6
       RS = 0;                   // Send pin on the LCD.
       RW = 0;               // Send pin on the LCD.
       EN = 0;                   // Send pin on the LCD.
       D4 = 0;                   // Send pin on the LCD.
       D5 = 0;                   // Send pin on the LCD.
       D6 = 0;                   // Send pin on the LCD.
       D7 = 0;                   // Send pin on the LCD.
       _delay (40);                      // Wait atleast 4.1mS for the initialization to complete. Instruction clock = 4MHz / 4 = 1MHz, T = 1 / 1MHz = 0.000 000 1 S = 1uS.
    
                           // Instruction 6 continued
       RS = 0;                   // Send pin on the LCD.
       RW = 0;               // Send pin on the LCD.
       EN = 0;                   // Send pin on the LCD.
       D4 = 0;                   // Send pin on the LCD.
       D5 = 1;                   // Send pin on the LCD.
       D6 = 0;                   // Send pin on the LCD.
       D7 = 1;                   // Send pin on the LCD.
       _delay (40);                     // Wait atleast 4.1mS for the initialization to complete. Instruction clock = 4MHz / 4 = 1MHz, T = 1 / 1MHz = 0.000 000 1 S = 1uS.    

                           // Instruction 7
       RS = 0;                   // Send pin on the LCD.
       RW = 0;               // Send pin on the LCD.
       EN = 0;                   // Send pin on the LCD.
       D4 = 0;                   // Send pin on the LCD.
       D5 = 0;                   // Send pin on the LCD.
       D6 = 0;                   // Send pin on the LCD.
       D7 = 0;                   // Send pin on the LCD.
       _delay (40);                      // Wait atleast 4.1mS for the initialization to complete. Instruction clock = 4MHz / 4 = 1MHz, T = 1 / 1MHz = 0.000 000 1 S = 1uS.
    
                           // Instruction 7 continued
       RS = 0;                   // Send pin on the LCD.
       RW = 0;               // Send pin on the LCD.
       EN = 0;                   // Send pin on the LCD.
       D4 = 1;                   // Send pin on the LCD.
       D5 = 0;                   // Send pin on the LCD.
       D6 = 0;                   // Send pin on the LCD.
       D7 = 0;                   // Send pin on the LCD.
       _delay (40);                     // Wait atleast 4.1mS for the initialization to complete. Instruction clock = 4MHz / 4 = 1MHz, T = 1 / 1MHz = 0.000 000 1 S = 1uS.    
     }
    }

#1

44 Replies Related Threads

    1and0
    Access is Denied
    • Total Posts : 10780
    • Reward points : 0
    • Joined: 2007/05/06 12:03:20
    • Location: Harry's Gray Matter
    • Status: offline
    Re: LCD 16x2 with XC8 2020/04/09 08:19:30 (permalink)
    +1 (1)
    ArakelTheDragon
    Good day! I am trying to use and LCD in 4 bit mode, I see 10 instructions only to initializa the display. Is this really so hard? Is there some good documentation for doing this, I tried searching but there is nothing good and the examples don't work.

    #define RS PORTCbits.RC3
    #define RW PORTCbits.RC4
    #define EN PORTCbits.RC5
    #define D4 PORTCbits.RC6
    #define D5 PORTCbits.RC7
    #define D6 PORTBbits.RB7
    #define D7 PORTBbits.RB6


           RS = 0;                   // Send pin on the LCD.
           RW = 0;               // Send pin on the LCD.
           EN = 0;                   // Send pin on the LCD.
           D4 = 1;                   // Send pin on the LCD.
           D5 = 1;                   // Send pin on the LCD.
           D6 = 0;                   // Send pin on the LCD.
           D7 = 0;                   // Send pin on the LCD.


    You should really consider connecting D4 to D7 with consecutive pins on the SAME port. Setting and clearing those ports bits will likely cause RMW issue.
    #2
    Jim Nickerson
    User 452
    • Total Posts : 6654
    • Reward points : 0
    • Joined: 2003/11/07 12:35:10
    • Location: San Diego, CA
    • Status: offline
    Re: LCD 16x2 with XC8 2020/04/09 08:21:51 (permalink)
    +1 (1)
    What does the documentation for the LCD say ?
    #3
    1and0
    Access is Denied
    • Total Posts : 10780
    • Reward points : 0
    • Joined: 2007/05/06 12:03:20
    • Location: Harry's Gray Matter
    • Status: offline
    Re: LCD 16x2 with XC8 2020/04/09 08:57:05 (permalink)
    0
    Looking at your code again, you are not toggling the EN connection.
    #4
    Joel_C
    New Member
    • Total Posts : 14
    • Reward points : 0
    • Joined: 2019/12/28 14:13:53
    • Location: 0
    • Status: offline
    Re: LCD 16x2 with XC8 2020/04/09 09:41:13 (permalink)
    0
    As 1and0 said,  it is necessary to toggle the EN pin to latch the data into the LCD.
    The delays given on the data sheets are minimum. Especially when starting out it is a good idea to use longer delays than called out on the data sheets. I would recommend at least twice as long.
     
    Joel
     
    #5
    ArakelTheDragon
    Starting Member
    • Total Posts : 64
    • Reward points : 0
    • Joined: 2016/12/11 07:12:00
    • Location: 0
    • Status: offline
    Re: LCD 16x2 with XC8 2020/04/09 12:24:42 (permalink)
    0
    When you say toggle the enable bit, is this:

    Instruction
    EN bit goes from 0 -> 1
    wait mS
    EN bit goes from 1 -> 0

    or

    Instruction
    EN bit goes from 0 -> 1
    wait mS
     
    Instruction
    EN bit geos from 1 -> 0
    wait mS

    I know its a stupid question, but when I am looking at so many details and descriptions and I begin to doubt every step and detail.
    #6
    Joel_C
    New Member
    • Total Posts : 14
    • Reward points : 0
    • Joined: 2019/12/28 14:13:53
    • Location: 0
    • Status: offline
    Re: LCD 16x2 with XC8 2020/04/09 12:44:43 (permalink)
    0
    I have the EN normally low.  I take the EN high for 1 microsecond and then take it back low. I know that my times may be longer than what is shown in the data sheets, but I want to be sure that everything is stable.  Twice the minimum times on LCDs has worked flawlessly for me for many years. 
     
    Joel
     
    #7
    du00000001
    Just Some Member
    • Total Posts : 3665
    • Reward points : 0
    • Joined: 2016/05/03 13:52:42
    • Location: Germany
    • Status: offline
    Re: LCD 16x2 with XC8 2020/04/09 13:12:44 (permalink)
    0
    ArakelTheDragon
    When you say toggle the enable bit, is this:

     
    Instruction
     
    EN bit goes from 0 -> 1
     
    wait mS
     
    EN bit goes from 1 -> 0
     

    or

     
    Instruction
     
    EN bit goes from 0 -> 1
     
    wait mS
     
     
     
    Instruction
     
    EN bit geos from 1 -> 0
     
    wait mS
     

    I know its a stupid question, but when I am looking at so many details and descriptions and I begin to doubt every step and detail.



    Golden rule: if you have to ask: both !
    Rule #2: . . . but check: some functions result in some dead time following the EN H->L transition. During the dead time, no new data can be processed. If new data had been written, this will be lost if the dead time is not respected.

    PEBKAC / EBKAC / POBCAK / PICNIC (eventually see en.wikipedia.org)
    #8
    Joel_C
    New Member
    • Total Posts : 14
    • Reward points : 0
    • Joined: 2019/12/28 14:13:53
    • Location: 0
    • Status: offline
    Re: LCD 16x2 with XC8 2020/04/09 13:24:53 (permalink)
    0
    The data is latched into the LCD on the falling edge of the EN signal.  The data that you are sending to the LCD can be put on the PIC output pins before or after raising the EN line, but a delay of several hundred nanoseconds should be used to allow things to stabilize before dropping the EN line.
     
    Joel
     
    #9
    1and0
    Access is Denied
    • Total Posts : 10780
    • Reward points : 0
    • Joined: 2007/05/06 12:03:20
    • Location: Harry's Gray Matter
    • Status: offline
    Re: LCD 16x2 with XC8 2020/04/09 13:32:58 (permalink)
    +1 (1)
    Open a random LCD datasheet here. It specifies Enable Cycle Time of 1200 ns min and Enable Pulse Width of 140 ns min. ;)
     
    #10
    ArakelTheDragon
    Starting Member
    • Total Posts : 64
    • Reward points : 0
    • Joined: 2016/12/11 07:12:00
    • Location: 0
    • Status: offline
    Re: LCD 16x2 with XC8 2020/04/10 04:48:57 (permalink)
    0
    You'd be surprised how hard it is to find a 4bit datasheet.
     
    I can't upload the file so I give a dropbox link.
    https://www.dropbox.com/s....Instructions.pdf?dl=0
    #11
    Jim Nickerson
    User 452
    • Total Posts : 6654
    • Reward points : 0
    • Joined: 2003/11/07 12:35:10
    • Location: San Diego, CA
    • Status: offline
    Re: LCD 16x2 with XC8 2020/04/10 06:51:46 (permalink)
    0
    whom did you buy the LCD from ?
    #12
    ArakelTheDragon
    Starting Member
    • Total Posts : 64
    • Reward points : 0
    • Joined: 2016/12/11 07:12:00
    • Location: 0
    • Status: offline
    Re: LCD 16x2 with XC8 2020/04/10 06:53:24 (permalink)
    0
    Local electronics shop. I tried it with a different compiler and it works. Therefore the problem must be in the code?
    #13
    Jim Nickerson
    User 452
    • Total Posts : 6654
    • Reward points : 0
    • Joined: 2003/11/07 12:35:10
    • Location: San Diego, CA
    • Status: offline
    Re: LCD 16x2 with XC8 2020/04/10 06:56:11 (permalink)
    0
    the LCD came with no documentation ?
    which compiler did work ?
    #14
    ArakelTheDragon
    Starting Member
    • Total Posts : 64
    • Reward points : 0
    • Joined: 2016/12/11 07:12:00
    • Location: 0
    • Status: offline
    Re: LCD 16x2 with XC8 2020/04/10 06:58:23 (permalink)
    0
    CCS PIC C v5.81.
     
    The code of CCS for the LCD driver is copyrighted, so I can't post it here?
    #15
    Jim Nickerson
    User 452
    • Total Posts : 6654
    • Reward points : 0
    • Joined: 2003/11/07 12:35:10
    • Location: San Diego, CA
    • Status: offline
    Re: LCD 16x2 with XC8 2020/04/10 07:02:17 (permalink)
    +1 (1)
    Maybe you could study the CCS code, learn what each line of code does and "port" it to xc8.
    If you do not understand what a line of code does you must research it till you understand.
    You will "learn" nothing if you copy some existing code.
    #16
    ArakelTheDragon
    Starting Member
    • Total Posts : 64
    • Reward points : 0
    • Joined: 2016/12/11 07:12:00
    • Location: 0
    • Status: offline
    Re: LCD 16x2 with XC8 2020/04/10 07:03:40 (permalink)
    +1 (1)
    The CCS code is made on their built-in fuctions. It will be too hard to study it.
     
    EDIT: someone on the forum must have used an LCD in 4bit mode before. How did you do it?
    post edited by ArakelTheDragon - 2020/04/10 07:10:26
    #17
    Jim Nickerson
    User 452
    • Total Posts : 6654
    • Reward points : 0
    • Joined: 2003/11/07 12:35:10
    • Location: San Diego, CA
    • Status: offline
    Re: LCD 16x2 with XC8 2020/04/10 07:07:17 (permalink)
    0
    since CCS uses "built-in" functions and do not share the source code it is quite true there is nothing to study.
    In the future I would suggest you buy items like the LCD that come with documentation on how to use them
    #18
    ArakelTheDragon
    Starting Member
    • Total Posts : 64
    • Reward points : 0
    • Joined: 2016/12/11 07:12:00
    • Location: 0
    • Status: offline
    Re: LCD 16x2 with XC8 2020/04/10 07:08:40 (permalink)
    0
    Please read the edit on my previous post.
    #19
    Jim Nickerson
    User 452
    • Total Posts : 6654
    • Reward points : 0
    • Joined: 2003/11/07 12:35:10
    • Location: San Diego, CA
    • Status: offline
    Re: LCD 16x2 with XC8 2020/04/10 07:16:24 (permalink)
    +1 (1)
    when is your assignment due ?
    #20
    Page: 123 > Showing page 1 of 3
    Jump to:
    © 2020 APG vNext Commercial Version 4.5