Monday, January 15, 2024

PIC16F887 PCF8574 I2C Example using XC8

Overview

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)]. 

PIC16F887 PCF8574 I2C Example using XC8
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

PIC16F887 PCF8574 I2C Example using XC8
PCF8574 Interfacing Diagram


PIC16F887 PCF8574 I2C Example using XC8
PCF8574AP DIP-16

We can event use this chip for a 4x4 matrix keypad, or driving an HD44780-base character LCD.

PIC16F887 PCF8574 I2C Example using XC8
Pin Configuration and Functions

This device has various package types. A PDIP-16 or a SOIC-16 are common for most of electronics hobbyists.

PIC16F887 PCF8574 I2C Example using XC8
Simplified Block Diagram of Device

This device has an 8-bit customizable slave address with R/W bit.

PIC16F887 PCF8574 I2C Example using XC8
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.

PIC16F887 PCF8574 I2C Example using XC8
Address Reference
The Master I2C device need to write device slave address followed by output port data twice.
 
PIC16F887 PCF8574 I2C Example using XC8
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.

PIC16F887 PCF8574 I2C Example using XC8
PCF8574 Read Mode
We can use various I/O devices with this chip.

PIC16F887 PCF8574 I2C Example using XC8
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. 

  1. /*
  2.  * File: main.c
  3.  * Author: Admin
  4.  *
  5.  * Created on January 15, 2024, 8:17 PM
  6.  */
  7.  
  8.  
  9. #include <xc.h>
  10. #include "config.h"
  11. #include "i2c.h"
  12.  
  13. #define _XTAL_FREQ 8000000UL
  14. #define PCF8574_W 0x40
  15. #define PCF8574_R 0x41
  16.  
  17. void pcf8574Write(uint8_t data){
  18. i2c_start();
  19. i2c_write(PCF8574_W);
  20. i2c_write(0x20);
  21. i2c_write(data);
  22. i2c_stop();
  23. }
  24.  
  25. uint8_t pcf8574Read(){
  26. uint8_t data;
  27. pcf8574Write(0xFF);
  28.  
  29. i2c_start();
  30. i2c_write(PCF8574_R);
  31. data=i2c_read(0);
  32. i2c_stop();
  33. return data;
  34. }
  35. void main(void) {
  36. OSCCONbits.IRCF=7;
  37. i2c_init(100000);
  38. PORTD=0;
  39. TRISD=0;
  40. while(1){
  41. PORTD=pcf8574Read();
  42. __delay_ms(200);
  43. }
  44. return;
  45. }
  46.  

Since A2...A0 pins of PCF8574 are logic 0. So its device write address is 0x40, and 0x41 for device read address.

Click here to download its source file. See Also,

No comments:

Post a Comment

Search This Blog

Labels

25AA010A (1) 8051 (7) 93AA46B (1) ADC (30) Analog Comparator (1) Arduino (15) ARM (6) AT89C52 (7) ATMega32 (54) AVR (57) CCS PICC (28) DAC (1) DHT11 (2) Display (105) Distance Sensor (3) DS18B20 (3) dsPIC (2) dsPIC30F1010 (2) EEPROM (5) Environment Sensor (4) esp8266 (1) I2C (29) Input/Output (67) Interrupt (19) Keil (5) Keypad (10) LCD (46) Master/Slave (1) MAX7221 (1) MCP23017 (5) MCP23S17 (4) Meter (3) MikroC (2) Motor (15) MPLABX (66) Nokia 5110 LCD (3) OLED (2) One-Wire (6) Oscillator (8) PCB (6) PCD8544 (3) PCF8574 (5) PIC (107) PIC12F (2) PIC16F628A (2) PIC16F630 (1) PIC16F716 (3) PIC16F818 (10) PIC16F818/819 (2) PIC16F84A (15) PIC16F876A (1) PIC16F877A (9) PIC16F88 (1) PIC16F887 (60) PIC18 (19) PIC18F1220 (4) PIC18F2550 (3) PIC18F4550 (12) PWM (11) RTC (8) Sensor (10) SH1106 (1) Shift Register (11) Shift Registers (2) SPI (24) STM32 (6) STM32 Blue Pill (6) STM32CubeIDE (6) STM32F103C8T6 (6) SysTick (3) temperature sensor (11) Thermometer (21) Timer/Counter (30) TM1637 (2) UART (7) Ultrasonic (4) Voltmeter (7) WDT (1) XC16 (2) XC8 (94)