1. Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.
    Dismiss Notice

LCD library for C18.

Discussion in 'Microcontrollers' started by loup-garou, May 22, 2008.

  1. loup-garou

    loup-garou New Member

    Joined:
    Mar 28, 2008
    Messages:
    86
    Likes:
    0
    Location:
    Malta
    Hi,


    I'm looking for an LCD library (or separate "C" code) for PIC18F (C18 of Microchip) that allows me to display menues and sub-menus.


    can anybody help me please,



    thanks in advance.
     
    Last edited: May 22, 2008
  2. 3v0

    3v0 Coop Build Coordinator Forum Supporter

    Joined:
    Jul 14, 2006
    Messages:
    9,404
    Likes:
    227
    Location:
    OKLAHOMA USA
    MCC18 comes with code to run a Hitachi HD44780 LCD controller. Look in C:\MCC18\src\pmc_common\XLCD. The header file is in C:\MCC18\h

    You will need to write or find a menu system elsewhere.
     
  3. loup-garou

    loup-garou New Member

    Joined:
    Mar 28, 2008
    Messages:
    86
    Likes:
    0
    Location:
    Malta
    hi 3v0,

    thanks for answering,


    I've used this library for a simple code (dispalying a message), but it looks a bit complicated to well understand how its code function, and therefore to change and make what I'm trying to do.
     
    Last edited: May 23, 2008
  4. dave

    Dave New Member

    Joined:
    Jan 12, 1997
    Messages:
    -
    Likes:
    0


     
  5. kasunth

    kasunth New Member

    Joined:
    Apr 23, 2009
    Messages:
    4
    Likes:
    0

    Hi,

    Don't use c18 library, just use my code.before using my code, define the RS RW E and Data pins that you use. I wrote this code for 8 bit mood

    Code (text):

    #include <stdio.h>
    #include <P18CXXX.h>    
    #include <delays.h>
    #include <timers.h>
                       
    #pragma code page  // This code make sure the program starts from 0x0004, not from 0x0000
    #pragma config WDT = OFF

    #define RS PORTEbits.RE0
    #define dirRS TRISEbits.TRISE0
    #define RW PORTEbits.RE1
    #define dirRW TRISEbits.TRISE1
    #define E PORTEbits.RE2
    #define dirE TRISEbits.TRISE2
    #define DATALCD PORTD
    #define dirDATA TRISD
    #define LCDBusy PORTDbits.RD7



    void LCDdata (unsigned char);
    void busylcd(void);
    void LCDinit (void);
    void LCDDelay( void );
    void lcdcmd(unsigned char);
    void stringtoLCD(unsigned char *m);
    unsigned char * itoa (unsigned int value, unsigned char * string);

    unsigned char dataString[10];


    void main (void)        // main  function
    {
    int integer=650;
    unsigned char *biginin;
    biginin=&dataString[0];
    TRISD = 0;
    PORTD = 0; 
    TRISB = 0;              // Port,pin direction configuration
    PORTB = 0;
    TRISC = 0;
    PORTC = 0;
    TRISCbits.TRISC7 = 1;  // make sure this pin is input
    ADCON1=7;
    dirRS = 0;
    LCDinit();
    while (1)            // this is how to make a never ending loop.
        {
        lcdcmd(0x1);
        lcdcmd(0x2);
        itoa (integer, biginin);
        stringtoLCD(dataString);
        }

    }
    //////////////////////////////////////////////////////////////////////////////////////
    //LCD finctions------------------------------------------------------------------------
    //////////////////////////////////////////////////////////////////////////////////////
    void LCDdata (unsigned char value)
    {
    busylcd();
    TRISD = 0;
    DATALCD = value;
    RS=1;
    RW=0;
    E=1;
    LCDDelay();
    E=0;

    }
    //--------------------------------------------------------------------------------------
    void LCDinit(void)
    {
        TRISE=0;
        lcdcmd(0x38);                   //set 8-bit mode and 2 lines
        lcdcmd(0x10);                   //cursor move & shift left
        lcdcmd(0x06);                   //entry mode = increment
        lcdcmd(0x0d);                   //display on - cursor blink on
        lcdcmd(0x01);                   //clear display
    }
    //-------------------------------------------------------------------------------------
    void busylcd(void)
    {
    RS=0;
    RW=1;
    TRISD=255;
        E=0;
        LCDDelay();
        E=1;
    while(LCDBusy==1){
        E=0;
        LCDDelay();
        E=1;
        }
    }
    //-------------------------------------------------------------------------------------
    void lcdcmd(unsigned char temp)
    {
    busylcd();
    RS=0;
    RW=0;
    TRISD=0;
    DATALCD=temp;
    E=1;
    LCDDelay();
    E=0;
    }

    //--------------------------------------------------------------------------------------
    void LCDDelay(void)
    {
    int i=0;
    for (i=0;i<250;i++);

    }
    //---------------------------------------------------------------------------------------
    void stringtoLCD(unsigned char *m)
    {
    unsigned char i;
         i = 0;
         while(m[i] != 0)
         {
         LCDdata(m[i]);
         i++;
         }

    }
     
    Last edited by a moderator: Feb 12, 2011
  6. Stavros

    Stavros New Member

    Joined:
    Dec 27, 2010
    Messages:
    12
    Likes:
    0
    Hello
    I had take kasunths code and i try to used it. I make some changes to work fine with my LCD and i send numbers to first line and characters to the Second line.
    The numbers display properly with no problem put i have problem with characters-strings. Only the first character display to LCD.
    I work in simulation with Proteus Isis and MPLAB IDE.

    Can you help me to make the code to send all the characters to LCD?
    Here are the changes that i had made in code:

    Code (text):
    #include "p18f452.h"
    #include <stdio.h>
    #include <delays.h>
    #include <timers.h>

    // HS-PLL Enabled, Internal External Osc. Switch Over OFF Disabled
    #pragma config OSC = HS, OSCS = OFF
    // Power Up Timer: OFF Disabled
    #pragma config PWRT = ON
    // Brown Out Reset: OFF, Brown Out Voltage: OFF Disabled
    #pragma config BOR = OFF, BORV = 27
    // Watchdog Timer: OFF Disabled, Watchdog Postscaler: 1:128
    #pragma config WDT = OFF, WDTPS = 128
    // CCP2 Mux: OFF Disabled (RB3)/ON Enable (RC1)
    #pragma config CCP2MUX = ON
    // Stack Overflow Reset: OFF Disabled
    #pragma config STVR = OFF
    // Low Voltage ICSP:OFF Disabled
    #pragma config LVP = OFF
    // Background Debugger Enable: OFF Disabled
    #pragma config DEBUG = OFF
    // Code Protection Block 0-3: OFF Disabled
    #pragma config CP0 = OFF, CP1 = OFF, CP2 = OFF, CP3 = OFF
    // Boot Block Code Protection: OFF Disabled
    #pragma config CPB = OFF
    // Data EEPROM Code Protection: OFF Disabled
    #pragma config CPD = OFF
    // Write Protection Block 0-3: OFF Disabled
    #pragma config WRT0 = OFF, WRT1 = OFF, WRT2 = OFF, WRT3 = OFF
    // Boot Block Write Protection: OFF Disabled
    #pragma config WRTB = OFF
    // Configuration Register Write Protection: OFF Disabled
    #pragma config WRTC = OFF  
    // Data EEPROM Write Protection: OFF Disabled
    #pragma config WRTD = OFF
    // Table Read Protection Block 0-3: OFF Disabled
    #pragma config EBTR0 = OFF, EBTR1 = OFF, EBTR2 = OFF, EBTR3 = OFF
    // Boot Block Table Read Protection: OFF Disabled
    #pragma config EBTRB = OFF






    #define RS PORTAbits.RA0
    #define dirRS TRISAbits.TRISA0
    #define RW PORTAbits.RA1
    #define dirRW TRISAbits.TRISA1
    #define E PORTAbits.RA2
    #define dirE TRISAbits.TRISA2
    #define DATALCD PORTC
    #define dirDATA TRISC
    #define LCDBusy PORTCbits.RC7



    void LCDdata (unsigned char);
    void busylcd(void);
    void LCDinit (void);
    void LCDDelay( void );
    void lcdcmd(unsigned char);
    void stringtoLCD(unsigned char *m);
    unsigned char * itoa (unsigned int value, unsigned char * string);

    unsigned char dataString[10];
    char data[]="HELLO";

    void main (void) // main function
    {

    int integer,i,integer1;
    unsigned char *biginin;
    i=0;


    biginin=&dataString[0];
    TRISC = 0;
    PORTC = 0;

    dirRS = 0;
    LCDinit();

    while (1) // this is how to make a never ending loop.
    {
    lcdcmd(0x1);
    lcdcmd(0x2);
    LCDDelay();

    i++;
    integer=i;
    LCDDelay();
    itoa (integer, biginin);
    stringtoLCD(dataString);
    lcdcmd(0xC0);

    LCDDelay();

    stringtoLCD(data);
    }
    }

    //////////////////////////////////////////////////////////////////////////////////////
    //LCD finctions------------------------------------------------------------------------
    //////////////////////////////////////////////////////////////////////////////////////
    void LCDdata (unsigned char value)
    {
    busylcd();
    TRISC = 0;
    DATALCD = value;
    RS=1;
    RW=0;
    E=1;
    LCDDelay();
    E=0;

    }
    //--------------------------------------------------------------------------------------
    void LCDinit(void)
    {
    TRISA=0;
    lcdcmd(0x38); //set 8-bit mode and 2 lines
    lcdcmd(0x10); //cursor move & shift left
    lcdcmd(0x06); //entry mode = increment
    lcdcmd(0x0d); //display on - cursor blink on
    lcdcmd(0x01); //clear display
    }
    //-------------------------------------------------------------------------------------
    void busylcd(void)
    {
    RS=0;
    RW=1;
    TRISC=255;
    E=0;
    LCDDelay();
    E=1;
    while(LCDBusy==1){
    E=0;
    LCDDelay();
    E=1;
    }
    }
    //-------------------------------------------------------------------------------------
    void lcdcmd(unsigned char temp)
    {
    busylcd();
    RS=0;
    RW=0;
    TRISC=0;
    DATALCD=temp;
    E=1;
    LCDDelay();
    E=0;
    }

    //--------------------------------------------------------------------------------------
    void LCDDelay(void)
    {
    int i=0;
    for (i=0;i<250;i++);

    }
    //---------------------------------------------------------------------------------------
    void stringtoLCD(unsigned char *m)
    {
    unsigned char i;
    i = 0;
    while(m[i] != 0)
    {
    LCDdata(m[i]);
    i++;
    }

    }

     
     
    Last edited: Dec 28, 2010
  7. Wond3rboy

    Wond3rboy Member

    Joined:
    Jun 25, 2008
    Messages:
    762
    Likes:
    7
    Location:
    Sindh,Pakistan

    Attached Files:

  8. 3v0

    3v0 Coop Build Coordinator Forum Supporter

    Joined:
    Jul 14, 2006
    Messages:
    9,404
    Likes:
    227
    Location:
    OKLAHOMA USA
  9. Stavros

    Stavros New Member

    Joined:
    Dec 27, 2010
    Messages:
    12
    Likes:
    0
    Hello
    Thanks Wond3rboy for your help. I am working with your code but i have some difficulties to make the appropriate changes to the code to work with my LCD because it has assembly and C code (i don't know assembly well). I need to send the data from PORTC of pic18f452 to D0-7 of LCD and RS, RW, E to PortA0-2. I wiil try to find it and i will tell you.
    Also i will download new version of Proteus because i have Proteus 7 and can't open the file that you send me.

    Thanks 3v0 i will read the pages that you have send me
     
    Last edited: Dec 28, 2010
  10. Wond3rboy

    Wond3rboy Member

    Joined:
    Jun 25, 2008
    Messages:
    762
    Likes:
    7
    Location:
    Sindh,Pakistan
    Hi, pleaes no PMs stavros. Did you add all the files that i have put in the zip folder? Also did you replace the original xlcd.h file in MCC18\h folder?. Pin definitions are given in the xlcd.h file so if you want to do change the pins, change it accordingly. You will require to go through the library documentation to understand how this is being done. Here is the hex file that i generated from the project.
     

    Attached Files:

  11. Stavros

    Stavros New Member

    Joined:
    Dec 27, 2010
    Messages:
    12
    Likes:
    0
    Thank you very much Wond3rboy for your help
    I forgot to replace xlcd.h file. Now it works properly. I will try to change the pins to work with pic18f452.
    Your help is very important
     
  12. Stavros

    Stavros New Member

    Joined:
    Dec 27, 2010
    Messages:
    12
    Likes:
    0
    Hello
    I made some changes to the code that Wond3rboy send me and now it work fine with PIC18F452.
    Now i have 2 problems:
    1. I want to scroll the string-characters to the LCD but i can't scroll all the string. Some characters didn't display
    2. I can't display a large string because i have this error "Error [1300] stack frame too large"

    I search on the internet but i didn't find the solution yet. Can anyone help me?

    I have upload the code of Wond3rboy with the changes that i had make and the schematic from the simulation. I work with Proteus 7.6, MPLAB IDE V8.63 and C18 V3.36
     

    Attached Files:

    Last edited: Jan 1, 2011
  13. Stavros

    Stavros New Member

    Joined:
    Dec 27, 2010
    Messages:
    12
    Likes:
    0
    Hello HAPPY NEW YEAR

    After a lot of hours i had spend on the internet searching for my problem i found a solution for my 2nd problem. I find out that when i write static const char and overlay char the problem solved. Now i can write larger strings than before, but if i write a very big string i had this error:

    MPLINK 4.37, Linker
    Copyright (c) 1998-2010 Microchip Technology Inc.
    Error - section '.idata_lcdfunc.o' can not fit the section. Section '.idata_lcdfunc.o' length=0x0000015a
    Errors : 1

    Also i find out that when i wrote static rom char i can write a very big string with no errors, but LCD didn't display anything.

    Here are the changes that i had made. If you replace that in the test.zip file that i had upload it will work with out errors.

    Now i am trying to find the solution for my 1st problem, to make the strings to scroll properly on the LCD and to find a better solution for the 2nd problem
     
    Last edited: Jan 1, 2011
  14. ston10

    ston10 New Member

    Joined:
    Sep 10, 2008
    Messages:
    1
    Likes:
    0
    Hi Stavros i was seeing you code iv been trying to get my lcd (2x16 p18f4550) working on c18 and still no luck my friend help me and we use CSS but my computer is mac and theres only c18 for it but no luck i was using xlcd.h but nothing yet I'm using 4 bit and port b for lcd hope for some help..
     

Share This Page