PIC16F877A – MIKRO C – LCD

Photo of author

By Jackson Taylor

Home » Programming » PIC16F877A – MIKRO C – LCD

As an Amazon Associate we earn from qualifying purchases.

Character LCD module used in a PIC16F877A and Mikro C project

A character LCD is one of the easiest ways to get readable output from a microcontroller. This tutorial shows how to connect a standard HD44780-style LCD (such as a 16×2 module) to a PIC16F877A and drive it from Mikro C. It includes two examples: printing a word on the screen, and changing the displayed word when a switch is pressed.

If you are new to the compiler and the chip, start with the basics of PIC16F877A with Mikro C first, then come back here.

What you need

How the LCD connects to the PIC

An HD44780 LCD has data pins (D0 to D7), control pins (RS, RW, EN), power pins and a contrast pin. The Mikro C LCD library can drive the display in 4-bit mode, so only four data lines plus RS and EN are needed. RW is normally tied to ground because the library only writes to the display.

Hardware Hack

Stop Buying New Batteries for Your Tech

Electronics are expensive, but their power sources don't have to be. Discover the simple engineering trick to recondition dead batteries and bring your gadgets back to 100% capacity.

Watch the Free Video
  • VSS to ground and VDD to +5 V
  • VEE to the middle pin of the contrast potentiometer
  • RS and EN to two PORTD pins
  • D4 to D7 to four PORTD pins
  • RW to ground

Check the pin mapping that your Mikro C version expects. The exact wiring depends on how the library is set up, so use the library help in your compiler as the reference.

The Mikro C LCD library

Mikro C includes a ready-made LCD library, so you do not have to write the timing code yourself. The functions used in this article are:

  • lcd_init sets up the port that the LCD is connected to.
  • lcd_cmd sends a command, such as clearing the screen or turning the cursor off.
  • lcd_out(row, column, text) prints text starting at the given row and column.
  • delay_ms pauses the program for a number of milliseconds.
See also
PIC16F877A-HITECH C- EEPROM

The examples below use the older style where the port is passed to the init function. In newer versions of Mikro C PRO for PIC, you instead declare the LCD pin connections at the top of the program, call Lcd_Init() with no argument, and write the names with capital letters (for example Lcd_Cmd(_LCD_CLEAR)). The idea is the same.

1) To print data on LCD

This first program initialises the display on PORTD, clears it, hides the cursor and prints a short word on the first row.

void main()
{
    lcd_init(&portd);    //sets the port “d” as lcd output
    lcd_cmd(lcd_clear);  //clearing  of screen
    lcd_cmd(lcd_cursor_off);  //to off the cursor
    lcd_out(1,2,"hh");  // print the word “hh” starting on the 1st row and second column
}

How it works

  1. lcd_init prepares PORTD to talk to the display.
  2. lcd_cmd(lcd_clear) removes anything left on the screen.
  3. lcd_cmd(lcd_cursor_off) hides the blinking cursor.
  4. lcd_out(1,2,”hh”) writes “hh” on row 1, starting at column 2.

The program has no endless loop, so it runs once and stops. The text stays on the screen because the LCD keeps its content as long as it is powered.

2) Changing the word on LCD when a switch is pressed

The second program reads a switch on RB0. While the switch is not pressed, the LCD shows “pott”. When the switch is pressed, it shows “kollam” for half a second and then clears the screen.

void main()
{
    lcd_init(&portd);
    lcd_cmd(lcd_clear);
    lcd_cmd(lcd_cursor_off);
    trisb.f0=1;
    while (1)
    {
        if(portb.f0==1)   // when switch is  pressed
        {
            lcd_out(1,2,"kollam");
            delay_ms(500);
            lcd_cmd(lcd_clear);
        }
        while(portb.f0==0)  //when switch is not pressed
        {
            lcd_out(1,2,"pott");
        }
    }
}

How it works

  • trisb.f0=1 makes RB0 an input. This is the same idea covered in digital output basics, only with the direction reversed.
  • The outer while(1) loop keeps the program running forever.
  • When RB0 reads 1, the switch is pressed, so “kollam” is printed, held for 500 ms, then the screen is cleared.
  • The inner while loop keeps printing “pott” as long as RB0 reads 0. Because the screen was cleared just before, the shorter word does not leave stray characters behind.
See also
PIC16F877A-HITECH C - UART

For this logic to work, RB0 must read a clean 0 when the button is released. Use a pull-down resistor (for example 10k to ground) and connect the other side of the button to +5 V. Without it, the pin can float and the display may flicker between the two words.

Common problems and fixes

  • Blank screen with dark blocks on the top row: the LCD has power but is not being initialised. Check the wiring and the port passed to the init function.
  • Nothing visible at all: adjust the contrast potentiometer on VEE. A wrong contrast setting is the most common cause.
  • Garbled characters: check that the data lines are wired to the pins the library expects and that the configuration bits and clock frequency in the project match your hardware.
  • Text appears only for a moment: if your program reaches the end of main on some setups, add an endless loop such as while(1); to keep the controller running.
  • Switch seems random: add a pull-down resistor and, if needed, a short delay to ignore button bounce.

Testing in a simulator

If you do not have the hardware ready, you can simulate it in Proteus by loading the compiled HEX file into a PIC16F877A and connecting an LCD part to PORTD. Set the clock frequency in the simulation to match the one in your Mikro C project.

Where to go next

Once printing works, you can display sensor readings, counters or menus. A keypad with LCD project is a natural next step for user input, and a scrolling display shows how to handle longer messages on a 16-character screen.

FAQ

Which port should I use for the LCD on a PIC16F877A?

PORTD is a common choice because it has eight pins that are all on the same port, and it is digital by default. You can use another port, as long as your wiring matches what the library expects.

Sustainable Tech

Keep Toxic E-Waste Out of Landfills

Don't throw away your failing laptop, phone, or car batteries. Learn how to recondition them at home in just a few hours. It's better for the planet—and your wallet.

Learn How It Works
See also
MSP430-assembly language part-1.1-Interrupt & Pull up/down

Do I need to connect the RW pin?

Not for these examples. The Mikro C library only writes to the LCD, so RW can be tied to ground.

Why does the LCD show nothing even though the code is correct?

Check the contrast adjustment first, then the power connections and the data and control wiring. Also confirm that the oscillator settings in your project match the clock you actually use.

Can I use the same code with a different PIC?

The LCD functions are similar across many PIC chips in Mikro C, but port names, configuration bits and analog pin settings can differ. Check the device’s datasheet and the library help before reusing the code.

Revive Your Dead Electronics

Got a drawer full of dead gadgets or a failing car battery? Instead of paying a premium for replacements, use this simple trick to easily recondition them right from home.

See How It Works