Categories
Electronics

LCD Library for 8-bit PIC Microcontrollers

16x2 LCD Display
16×2 LCD Display

In this post, we are going to share our LCD Library. This library is a powerful library for 8-bit PIC microcontrollers written in the XC8 language. You can use it in an 8-bit microcontroller with small changes. Click the button end of the post to reach the library files.

If you do not want to read details you can check out projects that were made with this library in the link below.

Use LCD Library In 3 Steps

To using this library, create a project in MPLAB IDE and follow the steps below.

Step 1

Firstly, you must create config.h file in Header Files and include it in the main.c file. Write your Configuration Bit Settings and #define _XTAL_FREQ FrequencyInHertz in config.h file. After that add include statements. For the LCD library you need;

  • #include <xc.h>
  • #include <stdint.h>
  • #include <stdio.h>
  • #include <"lcd.h"> 

In this file, you should include all of the used libraries. Example (config.h file) is shown for the PIC16F628A microcontroller.

Step 2

Install lcd.c and lcd.h files and move them to your *.X directory. Then in the Projects panel right-click to Header Files and click Add Existing Item.... Select lcd.h file and click Select. Do the same thing for lcd.c file but add it to Source Files.

Step 3

Finally, go into lcd.h file and edit section between stars for your microcontroller. You must use one PORT for LCD.

Usage of Functions

LCDInitialize()

This function initializes the LCD. Before using any function make sure that LCDInitialize() function is called once like the code below.

void main(void) {

	LCDInitialize();
	while(1) {
		LCDPrintString("Trion Projects", 1, 1);
	}
	
}

LCDClearDisplay()

To clear display use this function. To execute this function you need about 2ms.

LCDReturnHome()

To move curser home use this function. To execute this function you need about 2ms.

LCDDisplayToggle(time, n)

When you need a display toggle to use this function. time is between 0 and 256. n is toggle number. Each time multiplied by 100ms. See the example below.

void main(void) {

	LCDInitialize();
	
	while(1) {
		LCDPrintString("Trion Projects", 1, 1);
		
		// Toggle display 3 times with 500ms time.
		LCDDisplayToggle(5, 3);
	}
	
}

LCDDisplayOn()

Turn the display on when the display is off or stay display at on state. This function can be used for toggle display with LCDDisplayOff() function.

LCDDisplayOff()

Turn the display off when the display is on or stay display at off state.

LCDCursorOn()

To turn on the cursor use this function.

LCDCursorOff()

To turn off the cursor use this function.

LCDCursorBlinkOn()

To turn on the cursor blink use this function.

LCDCursorBlinkOff()

To turn off the cursor blink use this function.

LCDShiftDisplayRight()

To shift display right use this function.

LCDShiftDisplayLeft()

To shift the display left use this function.

LCDShiftCursorRight()

To shift the cursor right use this function.

LCDShiftCursorLeft()

To shift the cursor left use this function.

LCDPrintChar(ch, y, x)

If you want to print one character to a specific position use this function. ch is char which you want to print. y is the line number. The first line is 1, the second line is 2. x is a position of the line. x must be between 0 and 17. Which can be a minimum of 0 and a maximum of 16.

LCDPrintString(string, y, x)

If you want to print a string to a specific position use this function. string is the data that you want to print. y is the line number. The first line is 1, the second line is 2. x is the position of the line. x must be between 0 and 17. Which can be a minimum of 0 and a maximum of 16. See examples below:

void main(void) {

	LCDInitialize();
	while(1) {
		LCDPrintString("Trion Projects", 1, 1);
	}
	
}

Or you can define a char string and pass it to function.

void main(void) {

	LCDInitialize();
	char str[15] = "Trion Projects";
	
	while(1) {
		LCDPrintString(str, 1, 1);
	}
	
}

How to Print Numbers?

If you want to print some numbers, firstly, you need to convert them into a string and then pass it to function. Let see the example below:
This example shows how to print variable integer numbers to LCD.

void main(void) {

	LCDInitialize();
	char str[10];
	uint8_t num = 0;
	
	while(1) {
		sprintf(str, "%d", num);
		LCDPrintString(str, 1, 1);
		__delay_ms(1000);
		num++;
	}
		
}

This example shows how to print variable double numbers to LCD.

void main(void) {

	LCDInitialize();
	char str[10];
	double num = 0.15;
	
	while(1) {
		sprintf(str, "%.2f", num);
		LCDPrintString(str, 1, 1);
		__delay_ms(1000);
		num += 0.05;
	}
	
}

At the and, this LCD Library is very useful for displaying data. You can use this library with 8-bit PIC microcontrollers with just a few changes. You can install the library with the link below. Stay healthy and see on other projects and tutorials.

Share This Tutorial
0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments