Learn To Write Code For 8051, Arduino, AVR, dsPIC, PIC, STM32 ARM Microcontroller, etc.
Coding Embedded Controller With C/C++.
Printed Circuit Board (PCB) Project For Electronics Hobbyists.
The PCF8574 could be used for a HD44780 based character LCD controlling using the 4-bit data transfer mode. The popular one's is an Arduino TWI LCD driving using this chip.
A long hours running program
This chip has only 8 bits inputs/outputs. So it can interface with an 8-bit character LCD using the 4-bit data transfer mode. Using this method the micro-processor need to send the 8-bit command or data two time, first the higher nibble and then the lower nibble. For more information about using the PCF8574 please see this post. If you are a beginner in micro-controller programming you can see this post about 4-bit LCD interfacing.
I use use my own DIY PCF8574AP character LCD module. Its schematic is shown below.
PCF8574AP Character LCD Module Schematic
Its slave address are 0x70 and 0x71.
DIY PCF8574AP Character LCD Module
DIY PCF8574AP Character LCD Module
You can use an Arduino PCF8574 LCD module with different address.
Arduino PCF8574 LCD module
Arduino PCF8574 LCD module
In this example, The micro-controller send a counter variable to the display. It updates for every 250ms. I use the I2C module of PIC16F887. The program written in C using MPLABX IDE and its XC8 C compiler. It's free to use.
The PCF8574 series is popular among Arduino users especially the character LCD driving. Its output port is 8-bit bi-directional. So we can use this chip to make a 4x4 matrix keypad. The microprocessor uses only two pins (SDA and SCL) via its I2C communication module to interface with this serial matrix keypad.
Hardware Experiment on PIC16F887 Prototype Board
I use a 4x4 membrane keypad module for Arduino. It's low cost that we doesn't need to make our own PCB.
The I2C slave address of PCF8574AP (DIP-16) is 0x70 (write) and 0x71 (read) when A2...A0 are wired to GND.
PCF8574AP 16-Pin DIP
I use the on-board TinSharp TC1604A-01 16x4 character LCD module I posses.
I use the 4-bit data transfer mode to save the I/O pins of micro-controller.
Using the MPLABX IDE and its XC8 C compiler is pretty easy. Generated firmware is light-weight comparable the Assembly language. This program keep scanning the key press. Key value will show on the character LCD. The display is auto make a new line when ever the character counts reach 16. The the display is full it will clear the display and return home.
Proteus simulation will work at slow speed due to custom firmware simulation. Using the prototype board, the firmware operation works very well without error and slowness.
This DIY PCB has a 20MHz crystal oscillator. But the PIC16F887 has an internal 8MHz RC oscillator. So I left the XTAL1 and XTAL2 pin unconnected.
Keypad scanning runs very flexible without error on this PIC DIY prototype board.
If you use the PCF8574 you need to change its I2C slave address.
The PCF8574 is an 8-bit input/output (I/O) expander for the two-wire bidirectional bus (I2C), designed for 2.5V to 6V VCC operation.
The PCF8574 device provides general-purpose remote I/O expansion for most micro-controller families by way of the I2C interface [serial clock (SCL), serial data (SDA)].
Simulating Program
The device features an 8-bit quasi-bidirectional I/O port (P0–P7), including latched outputs with high current drive capability for directly driving LEDs. Each quasi-bidirectional I/O can be used as an input or output without the use of a data-direction control signal. At power on, the I/Os are high. In this mode, only a current source to VCC is active.
This chip is suitable for,
Telecom Shelters: Filter Units
Servers
Routers (Telecom Switching Equipment)
Personal Computers
Personal Electronics
Industrial Automation
Products with GPIO-Limited Processors
PCF8574 Interfacing Diagram
PCF8574AP DIP-16
We can event use this chip for a 4x4 matrix keypad, or driving an HD44780-base character LCD.
Pin Configuration and Functions
This device has various package types. A PDIP-16 or a SOIC-16 are common for most of electronics hobbyists.
Simplified Block Diagram of Device
This device has an 8-bit customizable slave address with R/W bit.
PCF8574 Interface Definition
We can custom its optional slave address via A2...A0 bits. Its default write address when A2...A0 are wired to GND is 0x40.
Address Reference
The Master I2C device need to write device slave address followed by output port data twice.
PCF8574 Write Mode
To read from this I2C slave device, the microprocessor need to write device read address (eg. 0x41), followed by I2C in read mode.
PCF8574 Read Mode
We can use various I/O devices with this chip.
PCF8574 Typical Application
Interrupt Output (INT) pin is optional.
PIC16F887 MPLABX IDE and XC8 Programming
The PIC16F887 micro-controller has a high speed Inter Integrated Circuit (I2C) module that can operate in both master and slave mode.
In this example, I set up its I2C module to operate in master mode at 100kHz serial clock frequency. The program keeps reading input data from PCF8574 I/O port, its 8-bit content will show on PORTD of PIC16F887.
/*
* File: main.c
* Author: Admin
*
* Created on January 15, 2024, 8:17 PM
*/
#include <xc.h>
#include "config.h"
#include "i2c.h"
#define _XTAL_FREQ 8000000UL
#define PCF8574_W 0x40
#define PCF8574_R 0x41
void pcf8574Write(uint8_t data){
i2c_start();
i2c_write(PCF8574_W);
i2c_write(0x20);
i2c_write(data);
i2c_stop();
}
uint8_t pcf8574Read(){
uint8_t data;
pcf8574Write(0xFF);
i2c_start();
i2c_write(PCF8574_R);
data=i2c_read(0);
i2c_stop();
return data;
}
void main(void){
OSCCONbits.IRCF=7;
i2c_init(100000);
PORTD=0;
TRISD=0;
while(1){
PORTD=pcf8574Read();
__delay_ms(200);
}
return;
}
Since A2...A0 pins of PCF8574 are logic 0. So its device write address is 0x40, and 0x41 for device read address.