Liquid Crystal Display (LCD) Using mikroC's Library
By Bernice Zuiya
Introduction
Liquid Crystal Display (LCD), Is an electronic device which find its importance in many embedded systems , 16x2 means can display 16 characters into each lines, this module is more preferred than the seven segment display because of its ability offered to be easily programmed with the help of different libraries such as XC8 library , mikroC library etc ... here we are going to use for displaying the different events , output from a micro controller .
In this project we will learn how to interface an LCD Module with a micro-controller using mikroC' s library. from here we present a breve introduction of a 16x2 LCD Display module.
The image at the left side is an LCD Module snapped from Proteus simulator tool and at the right side is a pictured LCD Module, the paragraph below will explain more about this module.
The LCD Display module is basically an electronic device widely utilized in embedded system for displaying output after processing or displaying a message to user or it maybe also a warning message, comparing to its neighbor 7 segment display used also to display only the hexadecimal characters (0 to 9 and A to F) but consequently this has a limited number of characters possible to display, however the LCD module 16x2 is able of display any ASCII character onto the two lines, the 16x2 is simply mean that it is possible to display 32 characters onto two line ( 16 characters on each line ).
This module has 16 pins which are explained bellow :
-
GND this a a ground pin.
-
VCC is connected to +5 Volt supply
-
VEE this pin is used to increase or decrease the contrast of the display
-
RS : select command register when this pin is low ( 0 volt) and data register when this is high (+5 volt)
-
read/write : if this pin is set low then the module is used to write the register and if high the read from it.
-
enable : send data to data pins when when a high to low pulse is given
-
D0 to D7 data pins 8-bits it start from pin 7th to 14th.
-
the 15 and 16th pin of this LCD display are use for the LED (pin 15th +LED and 16th -LED)
Mode of operation:
this LCD module may work in one of the two ways:
-
8 Lines mode
-
4 Lines mode
these are the two ways which may be used while configuring or writing its embedded C program, but here we will show only how to work on 4 bits mode configuration using mikroC compiler.
4 Lines mode: the four bit mode means that only 4 lines are going to be used for data input to the module, the technique I usually use is first to know that the pins which are going to be used for data are : D4 , D5 , D6 , D7 and all 4 data pins remaining D0 to D3 are basically grounded and as basically in many application we use the LCD Module for displaying a message from a micro-controller which means writing something and not reading from the module then we can ground also the 5th pin of the module to allow only writing, some people like to leave these five pins floating it depend to what looks credible to you. but me I suggest to ground these 5 pins while working in 4 bits mode. The remaining pins are used as instructed in the module's datasheet, please have a look on its datasheet for more information.
LCD Module with mikroC's Library
As we already know we are going to use mikroC to display messages on the screen of the module, mikroC compiler is one of the best environment which offers a very large number of libraries and simple to understand, it doesn't required to be an engineer to understand the concept and how to implement function and all. I hope you already know how to create a project in mikroC compiler For PIC micro-controller, if no ! please step back to our home page there is a tutorial dedicated for that, but in case you have already know then let start !
mikroC compiler offers the LCD' s library and this can be see if the project is ready and open you should probably see at right side - bottom of the environment "Library Manager" , click and search for "LCD" double click on it then you will see LCD command. the good thing here is everything maybe completed in four step only as show:
-
you need to initialize the global variables for LCD connections as we are working in 4 bits mode
these are the global variable to be written on top, ex:
sbit LCD_RS at RB4_bit;
sbit LCD_EN at RB5_bit;
sbit LCD_D7 at RB3_bit;
sbit LCD_D6 at RB2_bit;
sbit LCD_D5 at RB 1_bit;
sbit LCD_D4 at RB0_bit;
// Pin direction
sbit LCD_RS_Direction at TRISB4_bit;
sbit LCD_EN_Direction at TRISB5_bit;
sbit LCD_D7_Direction at TRISB3_bit;
sbit LCD_D6_Direction at TRISB2_bit;
sbit LCD_D5_Direction at TRISB1_bit;
sbit LCD_D4_Direction at TRISB0_bit;
these line show basically the connection of the module to the PORTB of our controller.
-
Initialize the LCD with the following function : LCD_Init(); this should be done in the main function
-
To access the clear the display you need to used the function LCD_Cmd(_LCD_Clear); and to switch of the cursor you will need LCD_Cmd(_LCD_CURSOR_OFF);
-
the last this you may need is to display anything from the screen of the module for example "HELLO ZUIYA" use the function LCD_Out(line , position , message); this function has three parameter as described :
-
Line : the module maybe display into two line: if you give 1 it means the string will be displayed in the first line of the screen and 2 will be displayed on the second line.
-
Position : there is 16 possible position
-
string to be displayed
Circuit Diagram & connections to PIC16F877A
The circuit diagram shown below is the very basic diagram required for ultrasonic range meter using the 16F877A micro-controller, you need to respect the connections.
-
a resistor of value 4k7 ohm is connected in series with +5 volt and the reset pin (1st pin)
-
Q1 is the crystal oscillator in this case its frequency oscillation is 20 MHz.
-
supply: the circuit is connected with a voltage maximum equal 5 volt
-
C1 & C2 are of 22pF.
-
LCD display
-
Potentiometer R1 is used to modify the contrast of the module
LCD 16x2 Module using with mikroC's library
There is lots of libraries written for the LCD module which can be found in different forum or on internet, but there is a different between what I am presenting you here and the other you may find from internet or anyway else. The difference between them is :
-
mikroC compiler come directly with a set of libraries inbuilt, and you don't need to worry because even if you are working with a free compiler in light mode you can sill enjoying the all features. but in other environment such as the MPLAB IDE using a compiler such as HI-TECH, you need to write a library separately then include it in the project.
-
It is not easy to modify the standard library but in case of MPLAB IDE as the library is written separately then includes you are still able to modify you lines of code in the file, but let me tell you that even just by including it is not that easy to test an lcd module because sometime it fail and you may decide to give up, but mikroC compiler present a library which really is help full.
-
if you are new in this, then I will suggest you to go for mikroC compiler to test you program very fast.
Code and Comment
A similar code is presented also as an example in LCD's library from the general library of mikroC compiler, it is very help full when you want to go fast while designing an embedded system dealing with LCD Module, such as a digital thermometer with the LM35 module you will need probably a LCD to display the output or a warning message to prevent something occurring while the micro-controller execute instruction.
sbit LCD_RS at RB4_bit;
sbit LCD_EN at RB5_bit;
sbit LCD_D4 at RB0_bit;
sbit LCD_D5 at RB1_bit;
sbit LCD_D6 at RB2_bit;
sbit LCD_D7 at RB3_bit;
sbit LCD_RS_Direction at TRISB4_bit;
sbit LCD_EN_Direction at TRISB5_bit;
sbit LCD_D4_Direction at TRISB0_bit;
sbit LCD_D5_Direction at TRISB1_bit;
sbit LCD_D6_Direction at TRISB2_bit;
sbit LCD_D7_Direction at TRISB3_bit;
char txt1[] = " DISPLAY ON LCD";
char txt2[] = " - 16 X 2 - ";
char txt3[] = " WELCOME ALL";
char txt4[] = " OSAT PRESENT";
char i;
void Move_Delay() {
Delay_ms(1000);
}
void main(){
Lcd_Init();
Lcd_Cmd(_LCD_CLEAR);
Lcd_Cmd(_LCD_CURSOR_OFF);
Lcd_Out(1,1,txt3);
Lcd_Out(2,1,txt4);
Delay_ms(5000);
Lcd_Cmd(_LCD_CLEAR);
Lcd_Out(1,1,txt1);
Lcd_Out(2,1,txt2);
Delay_ms(500);
while(1) {
for(i=0; i<8; i++) {
Lcd_Cmd(_LCD_SHIFT_LEFT);
Move_Delay();
}
for(i=0; i<8; i++) {
Lcd_Cmd(_LCD_SHIFT_RIGHT);
Move_Delay();
}
}
}
This code were written using mikroC compiler software, you can copy and compile it or modify it as well,LCD module using mikroC's library.
Download the full project here