Continue to Site

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.

  • 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.

how to initialize LCD 16x2

Status
Not open for further replies.

Djsarkar

Member
Hi,
I have PIC18F45K80. I am using MPLABX 5.40 and XC8 2.30. I have JHD162A 659M10 LCD 16X2. I want to interface LCD with PIC18F45K80

Datasheet https://datasheetspdf.com/pdf-file/512991/ETC/JHD162A/1

The description given in diagram is quite confusing

LCD_init.jpg


any help would be appreciate
 
What is confusing about that? Seems pretty straight forward to me.

Mike.
 
What is confusing about that? Seems pretty straight forward to me.

Mike.
There are total 7 bits, 4 data bits and 3 control bits RS,RW and EN

function set (interface is 8 bit long) need to be repeat three times

What should be set from the below box ?
LCD_init.jpg
 
It starts as a 4 bit interface otherwise you can never set 4 bit mode. Just do as it says.

Mike.
 
It starts as a 4 bit interface otherwise you can never set 4 bit mode. Just do as it says.

Mike.

so we have three control signal RS, R/W and EN and we send 8 bit instruction or 8 bits data to LCD

To use 4-bit interface, we need to send the one-byte command/data as two nibbles

I am looking help to implement LCD_Initialize function

C:
#define _XTAL_FREQ 8000000
#include <xc.h>
// PIC18F45K80 Configuration Bit Settings
// 'C' source line config statements
// CONFIG1L
#pragma config RETEN = OFF      // VREG Sleep Enable bit (Ultra low-power regulator is Disabled (Controlled by REGSLP bit))
#pragma config INTOSCSEL = HIGH // LF-INTOSC Low-power Enable bit (LF-INTOSC in High-power mode during Sleep)
#pragma config SOSCSEL = HIGH   // SOSC Power Selection and mode Configuration bits (High Power SOSC circuit selected)
#pragma config XINST = OFF       // Extended Instruction Set (Enabled)
// CONFIG1H
#pragma config FOSC = INTIO2    // Oscillator (Internal RC oscillator)
#pragma config PLLCFG = OFF     // PLL x4 Enable bit (Disabled)
#pragma config FCMEN = OFF      // Fail-Safe Clock Monitor (Disabled)
#pragma config IESO = OFF       // Internal External Oscillator Switch Over Mode (Disabled)
// CONFIG2L
#pragma config PWRTEN = OFF     // Power Up Timer (Disabled)
#pragma config BOREN = SBORDIS  // Brown Out Detect (Enabled in hardware, SBOREN disabled)
#pragma config BORV = 3         // Brown-out Reset Voltage bits (1.8V)
#pragma config BORPWR = ZPBORMV // BORMV Power level (ZPBORMV instead of BORMV is selected)
// CONFIG2H
#pragma config WDTEN = OFF      // Watchdog Timer (WDT disabled in hardware; SWDTEN bit disabled)
#pragma config WDTPS = 1048576  // Watchdog Postscaler (1:1048576)
// CONFIG3H
#pragma config CANMX = PORTB    // ECAN Mux bit (ECAN TX and RX pins are located on RB2 and RB3, respectively)
#pragma config MSSPMSK = MSK7   // MSSP address masking (7 Bit address masking mode)
#pragma config MCLRE = ON       // Master Clear Enable (MCLR Enabled, RE3 Disabled)
// CONFIG4L
#pragma config STVREN = ON      // Stack Overflow Reset (Enabled)
#pragma config BBSIZ = BB2K     // Boot Block Size (2K word Boot Block size)
// CONFIG5L
#pragma config CP0 = OFF        // Code Protect 00800-01FFF (Disabled)
#pragma config CP1 = OFF        // Code Protect 02000-03FFF (Disabled)
#pragma config CP2 = OFF        // Code Protect 04000-05FFF (Disabled)
#pragma config CP3 = OFF        // Code Protect 06000-07FFF (Disabled)
// CONFIG5H
#pragma config CPB = OFF        // Code Protect Boot (Disabled)
#pragma config CPD = OFF        // Data EE Read Protect (Disabled)
// CONFIG6L
#pragma config WRT0 = OFF       // Table Write Protect 00800-01FFF (Disabled)
#pragma config WRT1 = OFF       // Table Write Protect 02000-03FFF (Disabled)
#pragma config WRT2 = OFF       // Table Write Protect 04000-05FFF (Disabled)
#pragma config WRT3 = OFF       // Table Write Protect 06000-07FFF (Disabled)
// CONFIG6H
#pragma config WRTC = OFF       // Config. Write Protect (Disabled)
#pragma config WRTB = OFF       // Table Write Protect Boot (Disabled)
#pragma config WRTD = OFF       // Data EE Write Protect (Disabled)
// CONFIG7L
#pragma config EBTR0 = OFF      // Table Read Protect 00800-01FFF (Disabled)
#pragma config EBTR1 = OFF      // Table Read Protect 02000-03FFF (Disabled)
#pragma config EBTR2 = OFF      // Table Read Protect 04000-05FFF (Disabled)
#pragma config EBTR3 = OFF      // Table Read Protect 06000-07FFF (Disabled)
// CONFIG7H
#pragma config EBTRB = OFF      // Table Read Protect Boot (Disabled)

#define RS            LATCbits.LATC4
#define RW            LATCbits.LATC5
#define EN            LATCbits.LATC6

#define LCD_Port      LATD

void Port_pins_Initialized (void)
{
    LATA = LATB = LATC = LATD = LATE =  0;
    TRISA = 0b0000000;// all are output, Unused
    TRISB = 0b0000000;// all are output, Unused
    TRISC = 0b0000000;// RS RW EN
    TRISD = 0b0000000;//  LCD
    TRISE = 0b0000000;// All are output, Unused
 
    ANCON0 = 0; // digital port
    ANCON1 = 0; // digital port
    CM1CON = 0; // Comparator off
    CM2CON = 0; // Comparator off
    ADCON0 = 0; // A/D conversion Disabled
}
void LCD_Instruction(unsigned char Command) //Function to send instruction to LCD
{
    LCD_Port = Command & 0xF0 ;  // send upper nibble
   
    RS = 0; // select instruction register
    RW = 0 ; // write mode
   
   // falling edge
    EN = 1;
    __delay_us(1);
    EN = 0;
   
    LCD_Port = (Command << 4) & 0xF0; // send lower nibble
   
       // falling edge
    EN = 1;
    __delay_us(1);
    EN = 0;
}
void LCD_Data(unsigned char Data) //Function to send message to LCD
{
   LCD_Port = Data & 0xF0;    // send upper nibble
 
   RS = 1;  // select Data register
   RW = 0 ;  // write mode
   
   // falling edge
    EN = 1;
    __delay_us(1);
    EN = 0;
   
  LCD_Port = (Data << 4) & 0xF0; //send lower nibble
   
     // falling edge
    EN = 1;
    __delay_us(1);
    EN = 0;
}
void LCD_Initialize (void)  //LCD Initialize
{
     __delay_ms(50); // 5o milliseconds  
   
     
}
void main(void)
{
  Port_pins_Initialized ();
  LCD_Initialize ();
    while (1)
    {
    }
}
 
 
Here's mine
C:
void LCD_Init(void)
    {
    delayMs(20);   //15mS +
    PORTB = 0x30;  // sent 0x3 twice
    LCD_Clk();
    delayMs(10);    // 5mS +
    LCD_Clk();
    delayMs(10);    // 5mS +
    PORTB = 0x20;   // set 4 line
    LCD_Clk();
    delayMs(5);        // ready..
    LCD_Cmd(0x2C);  // 4 bit mode.. two lines.. no cursor.. clear
    LCD_Cmd(0xC);
    LCD_Cmd(0x6);
    LCD_Cmd(0x1);
    }
 
Here's mine
have looked table posted ? I think your routine doesn't match with table

here is mine
Code:
void LCD_Initialize (void)  //LCD Initialize
{
     __delay_ms(50); // 50 milliseconds  
      RS = RW = 0;  
     // falling edge
      EN = 1;
      __delay_us(1);
      EN = 0;  
      LCD_Port = 0b00110000 ; // Function set  
       __delay_ms(40);
     
      EN = 1;
      __delay_us(1);
      EN = 0;  
      LCD_Port = 0b00110000 ; // Function set  
       __delay_ms(40);
     
      EN = 1;
      __delay_us(1);
      EN = 0;  
      LCD_Port = 0b00110000 ; // Function set  
       __delay_ms(40);
     
      EN = 1;
      __delay_us(1);
      EN = 0;  
      LCD_Port = 0b00100000 ; //  4 bit length
      __delay_ms(40);
     
       EN = 1;
      __delay_us(1);
      EN = 0;  
      LCD_Port = 0b00100000 ; //  4 bit length
      __delay_ms(40);
     
      EN = 1;
      __delay_us(1);
      EN = 0;  
      LCD_Port = 0b00000000 ; //  clear display
      __delay_ms(40);
     
      EN = 1;
      __delay_us(1);
      EN = 0;  
      LCD_Port = 0b00000000 ; //  clear display
      __delay_ms(40);
   
}
 
Another very good reference for understanding and a grounding in LCD display with the HD44780 is to search for the L.C.D.'s part1/part2 by Julyan Ilett.
Max.
 
Seriously.. I know mine works. If it teaches you nothing, then just discard it..
I am using your routine when I run below code I don't see message on display

C:
#define _XTAL_FREQ 8000000
#include <xc.h>
// PIC18F45K80 Configuration Bit Settings
// 'C' source line config statements
// CONFIG1L
#pragma config RETEN = OFF      // VREG Sleep Enable bit (Ultra low-power regulator is Disabled (Controlled by REGSLP bit))
#pragma config INTOSCSEL = HIGH // LF-INTOSC Low-power Enable bit (LF-INTOSC in High-power mode during Sleep)
#pragma config SOSCSEL = HIGH   // SOSC Power Selection and mode Configuration bits (High Power SOSC circuit selected)
#pragma config XINST = OFF       // Extended Instruction Set (Enabled)
// CONFIG1H
#pragma config FOSC = INTIO2    // Oscillator (Internal RC oscillator)
#pragma config PLLCFG = OFF     // PLL x4 Enable bit (Disabled)
#pragma config FCMEN = OFF      // Fail-Safe Clock Monitor (Disabled)
#pragma config IESO = OFF       // Internal External Oscillator Switch Over Mode (Disabled)
// CONFIG2L
#pragma config PWRTEN = OFF     // Power Up Timer (Disabled)
#pragma config BOREN = SBORDIS  // Brown Out Detect (Enabled in hardware, SBOREN disabled)
#pragma config BORV = 3         // Brown-out Reset Voltage bits (1.8V)
#pragma config BORPWR = ZPBORMV // BORMV Power level (ZPBORMV instead of BORMV is selected)
// CONFIG2H
#pragma config WDTEN = OFF      // Watchdog Timer (WDT disabled in hardware; SWDTEN bit disabled)
#pragma config WDTPS = 1048576  // Watchdog Postscaler (1:1048576)
// CONFIG3H
#pragma config CANMX = PORTB    // ECAN Mux bit (ECAN TX and RX pins are located on RB2 and RB3, respectively)
#pragma config MSSPMSK = MSK7   // MSSP address masking (7 Bit address masking mode)
#pragma config MCLRE = ON       // Master Clear Enable (MCLR Enabled, RE3 Disabled)
// CONFIG4L
#pragma config STVREN = ON      // Stack Overflow Reset (Enabled)
#pragma config BBSIZ = BB2K     // Boot Block Size (2K word Boot Block size)
// CONFIG5L
#pragma config CP0 = OFF        // Code Protect 00800-01FFF (Disabled)
#pragma config CP1 = OFF        // Code Protect 02000-03FFF (Disabled)
#pragma config CP2 = OFF        // Code Protect 04000-05FFF (Disabled)
#pragma config CP3 = OFF        // Code Protect 06000-07FFF (Disabled)
// CONFIG5H
#pragma config CPB = OFF        // Code Protect Boot (Disabled)
#pragma config CPD = OFF        // Data EE Read Protect (Disabled)
// CONFIG6L
#pragma config WRT0 = OFF       // Table Write Protect 00800-01FFF (Disabled)
#pragma config WRT1 = OFF       // Table Write Protect 02000-03FFF (Disabled)
#pragma config WRT2 = OFF       // Table Write Protect 04000-05FFF (Disabled)
#pragma config WRT3 = OFF       // Table Write Protect 06000-07FFF (Disabled)
// CONFIG6H
#pragma config WRTC = OFF       // Config. Write Protect (Disabled)
#pragma config WRTB = OFF       // Table Write Protect Boot (Disabled)
#pragma config WRTD = OFF       // Data EE Write Protect (Disabled)
// CONFIG7L
#pragma config EBTR0 = OFF      // Table Read Protect 00800-01FFF (Disabled)
#pragma config EBTR1 = OFF      // Table Read Protect 02000-03FFF (Disabled)
#pragma config EBTR2 = OFF      // Table Read Protect 04000-05FFF (Disabled)
#pragma config EBTR3 = OFF      // Table Read Protect 06000-07FFF (Disabled)
// CONFIG7H
#pragma config EBTRB = OFF      // Table Read Protect Boot (Disabled)

#define RS            LATCbits.LATC4
#define RW            LATCbits.LATC5
#define EN            LATCbits.LATC6

#define LCD_Port      LATD

void Port_pins_Initialized (void)
{
    LATA = LATB = LATC = LATD = LATE =  0;
    TRISA = 0b0000000;// all are output, Unused
    TRISB = 0b0000000;// all are output, Unused
    TRISC = 0b0000000;// RS RW EN
    TRISD = 0b0000000;//  LCD
    TRISE = 0b0000000;// All are output, Unused

    ANCON0 = 0; // digital port
    ANCON1 = 0; // digital port
    CM1CON = 0; // Comparator off
    CM2CON = 0; // Comparator off
    ADCON0 = 0; // A/D conversion Disabled
}

void LCD_Clk(void)
{
    EN = 1;                // Give a pulse on E pin
    __delay_us(1);      // so that LCD can latch the
    EN = 0;                // data from data bus
    __delay_us(1);
}

void LCD_Instruction(unsigned char Command) //Function to send instruction to LCD
{
    RS = 0; // select instruction register
    RW = 0 ; // write mode
    LCD_Port = Command & 0xF0 ;  // send upper nibble
    LCD_Clk();
 
    __delay_ms(50);
   
    LCD_Port = (Command << 4) & 0xF0; // send lower nibble
    LCD_Clk();
}
void LCD_Data(unsigned char Data) //Function to send message to LCD
{
  RS = 1;  // select Data register
  RW = 0 ;  // write mode
  LCD_Port = Data & 0xF0;    // send upper nibble  
  LCD_Clk();

  __delay_ms(50);
 
  LCD_Port = (Data << 4) & 0xF0; //send lower nibble  
  LCD_Clk();
}
void LCD_Initialize (void)  //LCD Initialize
{

    __delay_ms(20);   //15mS +
    LCD_Port = 0x30;  // sent 0x3 twice
    LCD_Clk();
    __delay_ms(5);        // ready..
    LCD_Clk();
    __delay_ms(10);    // 5mS +
    LCD_Port = 0x20;   // set 4 line
    LCD_Clk();
    __delay_ms(5);        // ready..
    LCD_Instruction(0x2C);  // 4 bit mode.. two lines.. no cursor.. clear
    LCD_Instruction(0xC);
    LCD_Instruction(0x6);
    LCD_Instruction(0x1);
    }
   

void buffer( unsigned char *p)
{

    while (*p != '\0')
    {
      LCD_Data(*p);
 
        p++;
    }
}

void main(void)
{
   unsigned int i = 0;
   unsigned char Data1 []="Hello World";
 
   Port_pins_Initialized ();
   LCD_Initialize ();

   buffer(Data1);

    while (1)
    {
    }
}
IMG_20201005_113318.jpg
 
Last edited:
The display contrast is the third pin... It needs to be controlled via a potentiometer.

Normally needs to be closer to 0 than 5v a guess is 1.5v
There is potentiometer on board. I can adjust value

Is there any problem with code?
 

Attachments

  • IMG_20201005_141143.jpg
    IMG_20201005_141143.jpg
    12 KB · Views: 226
A little observation, the LCD backlights look to be very bright.

How are they being powered?
Is there a current limiting resistor in series with them?

There is potentiometer on board.
Yes the one labelled CONTRASTADJUST, try adjusting it.

JimB
 
A little observation, the LCD backlights look to be very bright.

How are they being powered?
Is there a current limiting resistor in series with them?


Yes the one labelled CONTRASTADJUST, try adjusting it.

JimB

This is development board I am using with 12 v DC power supply.


IMG_20201005_161012.jpg


Lcd 1 pin connected gnd onboard
Lcd 2 pin connected to 5v onboard
Lcd 3 pin connected to gnd onboard

IMG_20201005_162058.jpg
 
And you've still not told anyone what your LCD data lines are connected to. Answer the questions asked and you might progress. Do you even know if you're using 8 bit or 4 bit mode? Edit, it looks like 4 bit mode but no idea which nibble.

Mike.
 
Last edited:
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top