Skip to main content

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 the details you can check out projects that were made with this library in the link below.

Use LCD Library In 3 Steps

To use 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. An 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  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() the 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 cursor home use this function. To execute this function you need about 2ms.

LCDDisplayToggle(time, n)

When you need a display toggle use this function. time is between 0 and 256. n is the 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 displayed at on state. This function can be used for toggle display with the LCDDisplayOff() function.

LCDDisplayOff()

Turn the display off when the display is on or stay displayed 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 the 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 the char that you want to print. y is the line number. The first line is 1, and 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.

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, and 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 a 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's 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;
    }
    
}

In the end, 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 other projects and tutorials.

Comments

Popular posts from this blog

Ultrasonic Sensor HC-SR04 With PIC Microcontroller

How Ultrasonic Sensor Works An ultrasonic sensor is a very useful sensor that measures distances with sounds. We used the PIC16F628A microcontroller for this project. This microcontroller has TMR1 which we are going to use. We need 2 I/O pins for TRIG and ECHO pins. For showing distance, we used an LCD. For more information about the LCD library and usage click the button below. LCD Interfacing with PIC Using XC8

How to Make Blink LED with PIC16F628A

How to Make Blink LED With PIC16F628A In this project tutorial, we are going to make blink-led projects with a pic microcontroller. After you read this tutorial you can build a PIC microcontroller project with XC8 on MPLAB IDE. We are going to make 3 different blinking projects. These projects are basic. Just learn how to make a basic project with a PIC microcontroller. You can see below which projects we are going to make; Blinking LED with 1s delay, LED with button, Blinking LED with a button.

DC Motor Speed Control With PIC16F628A

DC Motor Speed Control Simulation In this project, we will control the speed and direction of a DC motor using the PIC16F628A microcontroller. We will write the software in MPLAB IDE with the XC8 language that will send a PWM signal to L293D to control the motor. Finally, we will simulate it through the Proteus software. PWM (Pulse Width Modulation) You can get more information about the PWM signal from the link above. Now let’s examine the L293D motor driver.