GLCD 128x64 interfacing with PIC18F4550 Microcontroller.

Overview of GLCD 128x64

GLCD is a display device that can be used in embedded systems for displaying data and/or images/custom characters.

  • Basically, 128x64 Graphical LCD is a matrix of pixels.
  • Each pixel is accessed by their X and Y address.
  • We can simply visualize any pixel by making its value HIGH (1).

Hence, we can make any graphical design pixel by pixel using GLCD.

To get familiar with GLCD pins and their functions refer to GLCD 128x64.

 

This is the picture of Basic Structure of GLCD 128x64 Displays
Basic Structure of GLCD 128x64 Displays

 

Programming GLCD

Let's program the PIC18F4550 microcontroller to print text character on GLCD JHD12864E.

Initialization

To initialize the display, we need to do below steps,

  • Send Display OFF command i.e. 0x3E
  • Send Y address e.g. here 0x40 (Start address).
  • Send X address (Page) e.g. here 0xB8 (Page0).
  • Send Z address (Start line) e.g. here 0xC0 (from 0th line).
  • Now send Display ON command i.e. 0x3F

GLCD_Init function

Input arguments: It has no input arguments.

Return type: It does not return any data type.

void GLCD_Init()		/* GLCD Initialization function */
{
    Ctrl_dir = 0;  		/* Make Control pin as output */
    Data_dir = 0;  		/* Make Data pin as output */
    OSCCON = 0x72;  		/* Internal 8MHz OSC Frequency */
    RST = 1;  			/* Make reset pin High */
    CS1 = 1; CS2 = 1;	  	/* Select Left half of display */
    MSdelay(20);
    GLCD_Command(0x3E);		/* Display OFF */
    GLCD_Command(0x40);		/* Set Y address (column=0) */
    GLCD_Command(0xB8);		/* Set x address (page=0) */
    GLCD_Command(0xC0);		/* Set z address (start line=0) */
    GLCD_Command(0x3F);		/* Display ON */
}

 

Command Write

To write a command do the below steps

  • Send command on data pins.
  • Make RS = 0 (Command Register) and RW = 0 (Write Operation).
  • Make High to Low transition on Enable pin of min 1 us period.

GLCD_Command function

Input arguments: It has an input argument of Command.

Return type: It does not return any data type.

void GLCD_Command(char cmd)	/* GLCD Command function */
{
    LCD_data = cmd;		/* Copy command on data pin */
    RS = 0;			/* Make RS LOW for command register*/
    EN = 1;			/* Make HIGH-LOW transition on Enable*/
    NOP();
    NOP();
    EN = 0;
    NOP();
    NOP();
}

 

Data Write

To write data do the below commands

  • Send Data on data pins.
  • Make RS = 1 (Data Register) and RW = 0 (Write Operation).
  • Make High to Low transition on Enable pin of min 1us period.

GLCD_Data function

Input arguments: It has input argument Data.

Return type: It does not return any data type.

void GLCD_Char(char data)	/* GLCD Data function */
{
    LCD_data = data;		/* Copy Data on data pin */
    RS = 1;			/* Make RS HIGH for data register */
    EN = 1;  			/* Make HIGH-LOW transition on Enable */
    NOP();
    NOP();
    EN = 0;
    NOP();
    NOP();
}

 

Connection Diagram of GLCD 128x64 to PIC18F4550

This is the picture of Interfacing GLCD128x64 with PIC microcontroller
Graphical LCD interfacing with PIC18F4550

 

GLCD 128x64 Text Print Code for PIC18F4550

/*
 * GLCD128x64 interface with PIC18F4550
 * http://electronicwings.com
 * 
 */

#include <pic18f4550.h>
#include "Configuration_header_file.h"
#include "Font_Header.h"

void GLCD_Init();
void GLCD_Command(char);
void GLCD_Char(char);
void GLCD_String(char page_no, char *str);
void GLCD_Clear();
void MSdelay(unsigned int);

#define RS 		LATB0
#define EN 		LATB1
#define CS1 		LATB2
#define CS2 		LATB3
#define RST 		LATB4
#define LCD_data 	LATD
#define Ctrl_dir 	TRISB
#define Data_dir 	TRISD


void main() 
{
    GLCD_Init();	/* GLCD Initialize function */
    GLCD_Clear();	/* GLCD Display clear function */
    GLCD_String(4,"PIC Microcontroller");  /* Print string on 4th page of GLCD */
    while(1);
}

void GLCD_Init()	/* GLCD Initialization function */
{
    Ctrl_dir = 0;	/* Make Control pin as output */
    Data_dir = 0;	/* Make Data pin as output */
    OSCCON = 0x72;	/* Internal 8MHz OSC Frequency */
    RST = 1;		/* Make reset pin High */
	CS1 = 1; CS2 = 1;/* Select Left half of display */
    MSdelay(20);
    GLCD_Command(0x3E);	/* Display OFF */
    GLCD_Command(0x40);	/* Set Y address (column=0) */
    GLCD_Command(0xB8);	/* Set x address (page=0) */
    GLCD_Command(0xC0);	/* Set z address (start line=0) */
    GLCD_Command(0x3F);	/* Display ON */
}

void GLCD_Command(char cmd)/* GLCD Command function */
{
    LCD_data = cmd;	/* Copy command on data pin */
    RS = 0;		/* Make RS LOW for command register*/
    EN = 1;		/* Make HIGH-LOW transition on Enable */
    NOP();
    NOP();
    EN = 0;
    NOP();
    NOP();
}

void GLCD_Char(char data)/* GLCD Data function */
{
    LCD_data = data;	/* Copy Data on data pin */
    RS = 1;		/* Make RS HIGH for data register */
    EN = 1;		/* Make HIGH-LOW transition on Enable */
    NOP();
    NOP();
    EN = 0;
    NOP();
    NOP();
}

void GLCD_String(char page_no, char *str)/* GLCD string write function */
{
	unsigned int i,column,Page=((0xB8)+page_no),Y_address=0;	
	float Page_inc=0.5;													
	CS1 = 1; CS2 = 0;	/* Select Left half of display */
	GLCD_Command(Page);
	for(i=0;str[i]!=0;i++)	/* Print char in string till null */
	{
       if (Y_address>(1024-(((page_no)*128)+5)))
          break;
        if (str[i]!=32)
        {
            for (column=1;column<=5;column++)
            {
                if ((Y_address+column)==(128*((int)(Page_inc+0.5))))
                {
                    if (column==5)
                        break;
                    GLCD_Command(0x40);
                    Y_address = Y_address+column;
                    CS1 = ~CS1; CS2 = ~CS2;
                    GLCD_Command((Page+Page_inc));
                    Page_inc=Page_inc+0.5;
                }
            }
        }
        if (Y_address>(1024-(((page_no)*128)+5)))
            break;
        if((font[((str[i]-32)*5)+4])==0 || str[i]==32)
        {
            for(column=0;column<5;column++)
            {
                GLCD_Char(font[str[i]-32][column]);
                if((Y_address+1)%64==0)
                {
                    CS1 = ~CS1; CS2 = ~CS2;
                    GLCD_Command((Page+Page_inc));
                    Page_inc=Page_inc+0.5;
                }
                Y_address++;
            }
        }
        else
        {
            for(column=0;column<5;column++)
            {
                GLCD_Char(font[str[i]-32][column]);
                if((Y_address+1)%64==0)
                {
                    CS1 = ~CS1; CS2 = ~CS2;
                    GLCD_Command((Page+Page_inc));
                    Page_inc=Page_inc+0.5;
                }
                Y_address++;
            }
            GLCD_Char(0);
            Y_address++;
            if((Y_address)%64==0)
            {
                CS1 = ~CS1; CS2 = ~CS2;
                GLCD_Command((Page+Page_inc));
                Page_inc=Page_inc+0.5;
            }
        }
    }
	GLCD_Command(0x40);
}

void GLCD_Clear()
{
	int i,j;
	CS1 = 1; CS2 = 1;
	for(i=0;i<8;i++)
	{
		GLCD_Command((0xB8)+i);
		for(j=0;j<64;j++)
		{
			GLCD_Char(0);
		}
	}
	GLCD_Command(0x40);
	GLCD_Command(0xB8);
}

/*****************************Delay Function*****************************/

void MSdelay(unsigned int val)
{
     unsigned int i,j;
        for(i=0;i<=val;i++)
            for(j=0;j<165;j++);
}

Output Image

This is the picture of Displaying text on GLCD

 

Print Image on GLCD 128x64 using PIC18F4550

Programming PIC18F4550 to Display Image on GLCD

We are using the same function that used for displaying text except for GLCD_String, which we have modified here to print image data on GLCD.

Image Display Code for PIC18F4550

/*
 * GLCD128x64 interface with PIC18F4550
 * http://www.electronicwings.com
 */

#include <pic18f4550.h>
#include "Configuration_header_file.h"
#include "Image.h"

void GLCD_Init();
void GLCD_Command(char);
void GLCD_Char(char);
void GLCD_String(char *str);
void GLCD_Clear();
void MSdelay(unsigned int);

#define RS LATB0
#define EN LATB1
#define CS1 LATB2
#define CS2 LATB3
#define RST LATB4
#define LCD_data LATD
#define Ctrl_dir TRISB
#define Data_dir TRISD


void main() 
{
    GLCD_Init();	/* GLCD Initialize function */
    GLCD_Clear();	/* GLCD Display clear function */
    GLCD_String(img);	/* Print string on 0th page of GLCD */
    while(1);
}

void GLCD_Init()	/* GLCD Initialization function */
{
    Ctrl_dir = 0;	/* Make Control pin direction as output */
    Data_dir = 0;	/* Make Data pin direction as output */
    OSCCON = 0x72;	/* Internal 8MHz OSC Frequency */
    RST = 1;		/* Make reset pin High */
	CS1 = 1; CS2 = 1;/* Select first Left half of display */
    MSdelay(20);
    GLCD_Command(0x3E);	/* Display OFF */
    GLCD_Command(0x40);	/* Set Y address (column=0) */
    GLCD_Command(0xB8);	/* Set x address (page=0) */
    GLCD_Command(0xC0);	/* Set z address (start line=0) */
    GLCD_Command(0x3F);	/* Display ON */
}

void GLCD_Command(char cmd)/* GLCD Command function */
{
    LCD_data = cmd;	/* Copy command on data pin */
    RS = 0;		/* Make RS LOW for command register */
    EN = 1;		/* Make HIGH-LOW transition on Enable */
    NOP();
    NOP();
    EN = 0;
    NOP();
    NOP();
}

void GLCD_Char(char data)/* GLCD Data function */
{
    LCD_data = data;	/* Copy Data on data pin */
    RS = 1;		/* Make RS HIGH for data register */
    EN = 1;		/* Make HIGH-LOW transition on Enable */
    NOP();
    NOP();
    EN = 0;
    NOP();
    NOP();
}

void GLCD_String(char *str)/* GLCD string write function */
{
	int column,page,page_add=0xB8,k=0;
	float page_inc=0.5;
	CS1 = 1; CS2 = 0;	/* Select left half of display */
	for(page=0;page<16;page++)  /* Print 16 pages(8 page of each half)*/
	{
		for(column=0;column<64;column++)
			{
				GLCD_Char(str[column+k]);
			}
			CS1 = ~CS1; CS2 = ~CS2;
			GLCD_Command((page_add+page_inc));
			page_inc=page_inc+0.5;
			k=k+64;  /* Increment pointer */
	}
	GLCD_Command(0x40);	/* Set Y address (column=0) */
	GLCD_Command(0xB8);	/* Set x address (page=0) */
}

void GLCD_Clear()
{
	int i,j;
	CS1 = 1; CS2 = 1;	/* Select both left & right half of display */
	for(i=0;i<8;i++)
	{
		GLCD_Command((0xB8)+i);	/* Increment page after 64 column */
		for(j=0;j<64;j++)
		{
			GLCD_Char(0);	/* Write zeros to all 64 column */
		}
	}
	GLCD_Command(0x40);	/* Set Y address (column=0) */
	GLCD_Command(0xB8);	/* Set x address (page=0) */
}
/****************************Delay Function********************************/
void MSdelay(unsigned int val)
{
     unsigned int i,j;
        for(i=0;i<=val;i++)
            for(j=0;j<165;j++);
}

Output Image

Displaying Animation on GLCD

 

Animation

To make animation on GLCD 128x64 JHD12864E display do the below steps,

  • Take two or more images in a manner that their sequence will create an illusion of motion.
  • Convert it to Binary image data using the Image2GLCD application.
  • And print them on GLCD in a series of sequences. It will create animation.
  • Note that provides a sufficient delay in between images.

 

Video of Image Display on GLCD 128x64 using PIC18F4550


Components Used

GLCD 128x64
GLCD 128x64 is a Graphical LCD having 128x64 pixel resolution. It is used to display values, text with different fonts, binary images, animation, custom character.
1
PICKit 4 MPLAB
PICKit 4 MPLAB
1
PIC18f4550
PIC18f4550
1

Downloads

GLCD 128x64 Datasheet Download
PIC18F4550 Interface with GLCD Project File Download
Ad