I2C LCD driver for CCS PIC C compiler

Interfacing LCD displays with a PIC microcontroller using CCS C compiler needs at least 7 data pins (using the built-in LCD driver). Number of pins needed for the LCD can be reduced to 2 by using an I2C I/O (Input/Output) expander like PCF8574 or PCF8574A. Adding the I2C I/O expander builds an I2C LCD. The I2C LCD is connected with the microcontroller via 2 lines: SDA (serial data) and SCL (serial clock). I2C = IIC = Inter-Integrated Circuit.
This post shows how to interface PIC microcontroller with I2C LCD using CCS C compiler where a driver (library) will be used to simplify the interfacing source codes for further project.
This driver works also with DFRobot I2C LCD displays since they’ve the same main circuit connections.

Interfacing PIC microcontroller with I2C LCD circuit:

PIC microcontroller I2C LCD with PCF8574 expander

(All grounded terminals are connected together)

The main component of the I2C LCD display is the PCF8574 I/O expander, with only two pins SDA and SCL we get a maximum of 8 pins from P0 to P7. PCF8574A also can be used but it has a different address.

All LCD data pins are connected to the PCF8574 where: RS, RW, E, D4, D5, D6 and D7 are connected to P0, P1, P2, P4, P5, P6 and P7 respectively.

PCF8574 I/O expander SDA and SCL pins are connected to the PIC microcontroller SDA and SCL pins respectively.

PCF8574 I/O expander A0, A1 and A2 pins are the address pins which decide the I2C address of the chip. In this example each pin is connected to +5V through a 10k ohm resistor (the 10k resistor is optional, each pin can be connected directly to +5V).

The I2C address of the PCF8574 is: 0 1 0 0 A2 A1 A0 0
In the circuit shown above A2, A1 and A0 are connected to +5V (through 10k resistors) which means the I2C address is equal to 0x4E.

If the PCF8574A is used instead of the PCF8574 the I2C address is: 0 1 1 1 A2 A1 A0 0 = 0x7E.

The LCD and the PCF8574 are supplied with 5V.
Note that PCF8574T is the same as PCF8574 and PCF8574AT is the same as PCF8574A.

I2C LCD driver for CCS PIC C compiler:
Driver source code is below which can be downloaded from the link below.
I2C LCD driver download

User functions:
LCD_Begin(unsigned int8 _i2c_addr);  // Must be called before any other function, _i2c_addr is the I2C I/O expander address, for example 0x27.
LCD_Goto(unsigned int8 col, unsigned int8 row);  // Set write position on LCD (upper left is 1,1 and second row first position is 1,2)
LCD_Out(unsigned int8 LCD_Char);    // Display Char on the LCD
LCD_Cmd(unsigned int8 Command);  // Send a command to the LCD

The following commands can be used with LCD_Com function (example: LCD_Com(LCD_CLEAR);):

Command Description
LCD_FIRST_ROW Move cursor to the 1st row
LCD_SECOND_ROW Move cursor to the 2nd row
LCD_THIRD_ROW Move cursor to the 3rd row
LCD_FOURTH_ROW Move cursor to the 4th row
LCD_CLEAR Clear display
LCD_RETURN_HOME Return cursor to home position, returns a shifted display to its original position. Display data RAM is unaffected
LCD_CURSOR_OFF Turn off cursor
LCD_UNDERLINE_ON Underline cursor on
LCD_BLINK_CURSOR_ON Blink cursor on
LCD_MOVE_CURSOR_LEFT Move cursor left without changing display data RAM
LCD_MOVE_CURSOR_RIGHT Move cursor right without changing display data RAM
LCD_TURN_ON Turn Lcd display on
LCD_TURN_OFF Turn Lcd display off
LCD_SHIFT_LEFT Shift display left without changing display data RAM
LCD_SHIFT_RIGHT Shift display right without changing display data RAM

Driver source code:

Interfacing PIC12F1822 microcontroller with I2C LCD

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.