lcd display pic microcontroller free sample
In this tutorial, you will learn to interface anLCD with a pic microcontroller. It is very simple and easy to understand the project for beginners and is commonly used in several electronic products. LCD (Liquid Crystal Display)provides a user-friendly interface and can be very useful for debugging purposes. After completion of this tutorial, you will be able to display data on an LCD using MPLAB XC8 Compiler and Mikro C compiler. We will provide examples with two Compilers such as MPLAB XC8 Compiler and Mikro C for PIC.
The reason LCD is more popular than LED, Seven Segment displays. Because we can display characters, numbers and custom characters with ease ( Just by easily programming a module).
First of all, to interface LCD with a pic microcontroller, we used GPIO pins. GPIO pins are general-purpose input-output pins. Because we send control and data signals to LCD through these I/O pins. Therefore, you should know how to use digital input-output pins of the pic microcontroller. To know about GPIO pins programming, check these tutorials:
It can work in two modes, 4-bit and 8-bit. In this tutorial, we have used the 4-bit mode which uses only 4 data lines, thus saving pins of the microcontroller. So It is recommended to use LCD in four bits mode to save pins of the microcontroller for other applications.
As you can see in this diagram, if we use 8-bit mode interfacing, we will need to use 11 pins of pic microcontroller. On the other hand, if we use 4-bit mode, we need only 6 GPIO pins. Therefore, it is recommended to use 4-bit mode interfacing. The only difference between 4-bit and 8-bit is that data transfer speed is faster for 8-bit mode. However, it doesn’t make any major difference.
A variable resistor is used to adjust the contrast of 5×8 dot pixels according to background light. Therefore, if you are not able to see anything on LCD after programming, the maximum changes are that you need to adjust contrast with the variable resistor. This contrast register makes adjust to the voltage applied on the VEE pin.
For MPLAB XC8 Compiler, we will use the PIC18F4550 microcontroller. For MikroC Pro for PIC, we will use the PIC16F877A microcontroller. In the case of MPLAB XC8, we will develop our own LCD library. Because the XC8 compiler does not provide built-in libraries. In the contrary, MikroC Pro provides libraries for all modules such as LCD, Keypad, ADC Module, UART module.
In this section, we will see how to write example code for 16×2 LCD interfacing with PIC18F4550 microcontroller. Although, you can use see code with other Pic microcontrollers also.
As we mentioned earlier, we can use the 8-bit mode and 4-bit mode interfacing. But due to the efficient use of MCU pins, we will be using 4-bit Mode. To interface LCD, we follow these steps:
In this circuit, we used the PORTB of PIC18F4550. But you can use any PORT. To do this, we need to change the pin assignment inside the code. I will show you how to assign pins for LCD in the next section.
These lines define which pins of the pic microcontroller should connect with LCD. For instance, in this example, we used the PORTD of PIC18F4550 microcontroller. Connect RD0-RD3 pins with D4-D7 pins of LCD respectively and other pins with RW, EN, RS and Power pins. But you can change PORT to other PORT of PIC microcontroller also by changing the PORT name with these commands.
LCDWriteNibble() function is used to write a nibble. Nibble is basically a half a byte. Because we are using LCD in four bits mode. Therefore, we need to send 8-bit commands/data in four bits chunks. This function writes the specified nibble to the LCD.
Because we will use 4-bit mode, data and commands transfer in 4-bits format. Even it requires at least 8-bit to display a character. To resolve this issue, we send data in a 4-bits format two times.
void LCDPutChar(char ch): Writes a character to LCD at current cursor position. This function displays the specified ASCII character at the current position on the LCD.
LCDGoto(char pos, char ln): This function positions the cursor at the specified line and column. Column and line at which the cursor should be positioned between 0-15 for pos (column) and 0-1 for ln(row).
In last section, we have seen how to display ASCII characters or string. But in almost all practical projects, we need to display, integer, float values. This code displays the counter value which increments from 0-9 after every one second. This is the main function of code only. Because the rest of the code is same as the previous program example.
In this section, we will see how to interface LCD with pic microcontroller and programming examples using MikroC for pic. MikroC pro has a built-in library.
We have used 16×2 LCD which means there are 2 rows and 16 characters in each row. So we can display a total of 32 characters at a time in two rows with 16 characters in each row.
This is the main command which prints our text on LCD. It also gives the privilege to specify the position of the text. In the horizontal direction, we count rows number and in a vertical direction, we count the column number. In above command,
However if your string is longer than the number of characters that could be displayed in a row from the starting position, the last characters will not be displayed. E.g. Lcd_Out (1, 6 “LCD Interface”);will display text in row 1 starting from column position 6 and will display only LCD Interfacethe rest of the characters will not be displayed as there is no room for them.
void Lcd_Out_Cp(char *text);will start printing the text from the current cursor position. For example after printing Lcd_Out (1, 1, “LCD”);if you write Lcd_Out_Cp(“Hello”);it will display “Hello”at a position from column position of 4 in row 1.
void Lcd_Chr(char row, char column, char out_char);allows only single characters to be displayed at specified positions. E.g. Lcd_Chr(2, 5, ‘A’); will print only A on column 5 row 2.
void Lcd_Chr_Cp(char out_char); allows to print only single character from current cursor position like after Lcd_Chr(2, 5, ‘A’);if your writeLcd_Chr_Cp(‘B’);it will be printed at row 2 column 6.
To interface LCD withPIC16F877A and display the text ‘LCD INTERFACE’ on it. LCDs come in different sizes and shapes. For this project, we have selected a 16×2 character, alphanumeric LCD. It contains 2 rows of 16 character.
When using PIC microcontroller, the mikroC compiler has a built-in LCD library that supports the commands to carry out LCD initialization. The library consists of a number of functions to control LCDs with 4-bit data interface.
The main program first clears the LCD screen and then displays “LCD INTERFACE” in the first row of LCD. The LCD pin directions are all set as outputs. The RS pin of LCD is set to 1, which indicates that the information received from DB4-DB7 is a valid text to be printed on LCD screen. The EN pin is also set to 1 which indicates that data is send to the LCD.
Programmed LCDs are vastly used for industrial as well as commercial applications. LCDs are used in UPSs or inverters, where voltage and current readings are displayed on the screen. Instructions to be followed are displayed on an LCD screen in airports, banks, hospitals, etc. If you still have any issue after reading this article, feel free to comment on this post with your issues.
A PIC Microcontroller can be easily made to communicate with LCD by using the built in Libraries of MikroC. Interfacing between PIC and LCD can be 4-bit or 8-bit. The difference between 4-bit and 8-bit is how data are send to the LCD. In the 8-bit mode to write an 8-bit character to the LCD module, ASCII data is send through the data lines DB0- DB7 and data strobe is given through the E line.
But 4-bit mode uses only 4 data lines. In this mode the 8-bit ASCII data is divided into 2 parts which are send sequentially through data lines DB4 – DB7 with its own data strobe through the E line. The idea of 4-bit communication is to save as much pins that used to interface with LCD. The 4-bit communication is a bit slower when compared to 8-bit. The speed difference is only minimal, as LCDs are slow speed devices the tiny speed difference between these two modes is not significant. Thus the 4-bit mode data transmission is most commonly used.
The above definitions tells the compiler, how LCD is connected to the microcontroller. The two set of definitions are used to provide Data (PORT) and Direction (TRIS) registers.
This function prints the text (string) in the current cursor position. When we write data to LCD Screen, it automatically increments the cursor position.
16×2 Character LCD is a very basic LCD module which is commonly used in electronics projects and products. It contains 2 rows that can display 16 characters. Each character is displayed using 5×8 or 5×10 dot matrix. It can be easily interfaced with a microcontroller. In this tutorial we will see how to write data to an LCD with PIC Microcontroller using Hi-Tech C Compiler. Hi-Tech C has no built in LCD libraries so we require the hardware knowledge of LCD to control it. Commonly used LCD Displays uses HD44780 compliant controllers.
This is the pin diagram of a 16×2 Character LCD display. As in all devices it also has two inputs to give power Vcc and GND. Voltage at VEE determines the Contrast of the display. A 10K potentiometer whose fixed ends are connected to Vcc, GND and variable end is connected to VEE can be used to adjust contrast. A microcontroller needs to send two informations to operate this LCD module, Data and Commands. Data represents the ASCII value (8 bits) of the character to be displayed and Command determines the other operations of LCD such as position to be displayed. Data and Commands are send through the same data lines, which are multiplexed using the RS (Register Select) input of LCD. When it is HIGH, LCD takes it as data to be displayed and when it is LOW, LCD takes it as a command. Data Strobe is given using E (Enable) input of the LCD. When the E (Enable) is HIGH, LCD takes it as valid data or command. The input signal R/W (Read or Write) determines whether data is written to or read from the LCD. In normal cases we need only writing hence it is tied to GROUND in circuits shown below.
The interface between this LCD and Microcontroller can be 8 bit or 4 bit and the difference between them is in how the data or commands are send to LCD. In the 8 bit mode, 8 bit data and commands are send through the data lines DB0 – DB7 and data strobe is given through E input of the LCD. But 4 bit mode uses only 4 data lines. In this 8 bit data and commands are splitted into 2 parts (4 bits each) and are sent sequentially through data lines DB4 – DB7 with its own data strobe through E input. The idea of 4 bit communication is introduced to save pins of a microcontroller. You may think that 4 bit mode will be slower than 8 bit. But the speed difference is only minimal. As LCDs are slow speed devices, the tiny speed difference between these modes is not significant. Just remember that microcontroller is operating at high speed in the range of MHz and we are viewing LCD with our eyes. Due to Persistence of Vision of our eyes we will not even feel the speed difference.
Hope that you got rough idea about how this LCD Module works. Actually you need to read the datasheet of HD44780 LCD driver used in this LCD Module to write a Hi-Tech C program for PIC. But we solved this problem by creating a header file lcd.h which includes all the commonly used functions. Just include it and enjoy.
Lcd8_Init() & Lcd4_Init() : These functions will initialize the LCD Module connected to the following defined pins in 8 bit and 4 bit mode respectively.
Lcd8_Set_Cursor() & Lcd4_Set_Cursor() : These functions set the row and column of the cursor on the LCD Screen. By using this we can change the position of the character being displayed by the following functions.
Lcd8_Write_Char() & Lcd4_Write_Char() :These functions will write a character to the LCD Screen when interfaced through 8 Bit and 4 Bit mode respectively.
Lcd8_Shift_Left() & Lcd4_Shift_Left() : These functions are used to shift the content on the LCD Display left without changing the data in the display RAM.
Lcd8_Shift_Right() & Lcd4_Shift_Right() : Similar to above functions, these are used to shift the content on the LCD Display right without changing the data in the display RAM.
I looked at a few libraries (Arduino ones) to get inspiration. In LCD_I2C_PCF8574.c I have added a lot of background and links to where you can get hold of other source, documentation and data on the PIC18F2685, I2C, the LCD and IO expander should you be so inclined. I also added a link to the library I ripped for the character generation. Thanks Mario. This file also contains details on how you may want to customise to your implementation, these are tagged with "TODO adapt" so you can use the MPLABX task list to grab them.
I took all my details/nomenclature etc. from an Hitachi hard copy LCD manual (yes hard copy, real paper an"all!) I obtained in the early 1980"s when we were still printing on flattened trees.
The code in main.c exercises all the functionality of the display including reading DD and CG ram and the busy flag. It should give a good heads up on how to use the library. I included a link to a YouTube Video so you can see it"s potential.
In this session we will see how to interface 16×2 LCD to PIC18F4550 microcontroller which is of family PIC18F. You can get information of 16×2 LCD in the session
PIC18F4550 belongs to the PIC18F family; PIC18F4550 is an 8bit microcontroller and uses RISC architecture. PIC18F4550 has 40 pins in PDIP (dual in line package) and 44 pin in TQFP (Quad flat package).
32KB flash memory, 2048 bytes of SRAM (synchronous Random Access memory), EEPROM (Electrically Erasable Program Read Only Memory) of 256 bytes are embedded in the PIC18F4550.
PIC18F4550 has SPI (serial peripheral interface) and i2c (inter integrated circuit) for master and slave modes. It has SPP (Streaming Parallel Port) for USB streaming transfer.
PIC18F4550 is embedded with 4 timer modules (timer0 to timer3), 2 comparator modules and 3 external interrupt. It has Dual Oscillator options allow microcontroller and USB module to run at different clock speeds. It can operate in 2.0V to 5.5V
The resistor R1 is used for giving the contrast to the LCD. The crystal oscillator of 12 MHz is connected to the OSC1 and OSC2 pins of Pic microcontroller PIC18F4550 for system clock. The capacitor C2 and C3 will act filters to the crystal oscillator. You can use different ports or pins for interfacing the LCD before going to different ports please check the data sheet whether the pins for general purpose or they are special function pins.
Interfacing LCD to PIC is not different from interfacing to 8051. The basic concept and gist of the programming is almost same. Visit the following link for more information
Only the pins, registers and architecture using for interfacing will be different. When we look at the program, functions like initialization, sending data to the LCD will be almost same.
In the pic programming also for initializing the LCD the R/W pin should be low for writing the data, Enable pins should be high and register select pin (RS) should be high for writing the data. For sending a command the RS should be low, R/W pin should be low and enable pin should be high.
Install MPLAB in your system and create a new project, in selecting device and family select PIC18F family and add PIC18F4550 controller to your project.