Character LCD is very common and basic accessory, required in most of Embedded system projects. In today’s post we are making LCD Driver library(Compact Version) for our daily life project usage. Most of the code is inspired and borrowed from Flex-LCD Driver Library. In this driver we will only specific features which are required in our day to day projects and we will avoid features which are rarely used. Because saving IO pins is essential Task in Embedded systems, so we will only focus on 4bit mode instead of 8bit. Also we will not use R/W pin and will pull this pin to ground.
Hardware Requirements
- PIC16F887 Microcontroller
- hd44780 lcd display (JHD16x2)
- Crystal 8Mhz
- LM2596 DC-DC Buck Converter Step Down Module Power Supply
Functions List
We are going to implement following functions with specified features and usage
PIN DEFINITIONS Before using library we need to define interfacing pins so that we could use them later. We had defined pins as following
#define LCD_DB4 PIN_B4
#define LCD_DB5 PIN_B5
#define LCD_DB6 PIN_B6
#define LCD_DB7 PIN_B7
#define LCD_E PIN_B3
#define LCD_RS PIN_B2
These pins are for the Microchip PicDem2-Plus board,Change these
pins to fit your own board
lcd_init(); Required to call once before using any of library function. This will send initialization commands to Character LCD and force it to operate in 4 bit mode without the usage of R/W pin.
void lcd_gotoxy(int8 x, int8 y) This function will place cursor to specified x,y position. Here x represents column number in LCD and y represents Row number. Maximum 4 rows are supported and column could be 10, 16 or 20 according to LCD type you are using. Also remember that x starts from 0 and y starts from 1.
void lcd_putc(char c) This function will print characters to LCD from current cursor position.
void lcd_cursor_on(int1 on) Sending True will turn Cursor ON and sending False will turn Cursor OFF.
Example Code and Usage
Here we are providing example code and link to the GitHub repository where you can find this and many more PIC codes to use in your projects. In this example usage we are printing Simple welcome message again and again after some delay
/*===============================================================
Example Usage of LCD4Bit Character Library Driver Usage
@Author: Abdul Rehman
@Licence:
@Note: Use this on your own. Author is not responsible for anything
=================================================================*/
#include <16F887.h>
#device *= 16
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=8000000)
#use I2C(Master, SDA = pin_C4, SCL = pin_C3)
/*------------------ LOCAL DRIVERS ---------------------------*/
#include <LCD4Bit.c> // Our Library undertest
/*-------- Global Veriable Defination Area ---------------------*/
#define __VER__ 1.0 //current example version number
/*----------- Function Defination Area --------------------------*/
void printWelcomeMessage(); //
/*------ Main Starts here --------------------------------------*/
void main(void){
lcd_init(); // Always call this first.
while(True){
printWelcomeMessage();
}//while(True) ends here
}//main ends here
/*-----------------------------------------------------------------*/
void printWelcomeMessage(){
lcd_putc("\fLCD 4BIT Driver\n");
printf(lcd_putc, " V# %1.2f", __VER__);
delay_ms(1500);
lcd_putc("\fDeveloper:");
lcd_gotoxy(2,2);
lcd_putc("Abdul Rehman");
delay_ms (1000);
}
Code language: PHP (php)