GLCD 128x64 interfacing with PIC18F4550 Microcontroller.

GLCD 128x64 interfacing with PIC18F4550 Microcontroller.

Introduction

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 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 GLCD 128x64.
Basic Structure of GLCD 128x64 Displays
Basic Structure of GLCD 128x64 Displays

Programming GLCD

Let's program 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 command do 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 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 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();
}

Interfacing Diagram

Interfacing GLCD128x64 with PIC microcontroller
Graphical LCD interfacing with PIC18F4550

Program for Text Print

/*
 * 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
Displaying text on GLCD

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

Program for Image Print

/*
 * 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 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 Image2GLCD application.
  • And print them on GLCD in series of sequence. It will create animation.
  • Note that provide sufficient delay in between images.

No comments:

Post a Comment