Tuesday, August 25, 2020

Reading The Digital Input Output From PCF8574 Using PIC12F629

The PCF8574 Overview

NXP PCF8574 is a remote digital inputs outputs expander. The interfacing method is implemented using the two-wire Inter Integrated Circuit (I2C) with the frequency up to 100 kHz. It has an 8-bit digital input output port. It's input output port is quasi-bidirectional. It does not require direction control.


Reading The Digital Input Output From PCF8574 Using PIC16F629
A sample of program PIC12F629 connects to PCF8574 via software I2C Interface

Samples of PCF8574AP I bought from on line store

PCF8574AP Pin Diagram

Each pin of this 16-pin DIP device lists below.

Reading The Digital Input Output From PCF8574 Using PIC16F629
Pin description of PCF8574 (from datasheet)

Supply voltage for this device ranges from 2.5 V to 6 V, typically 5 V for hobbyist prototyping. It's commonly found in input/output driving, keypad, and character LCD interface converter.

Programming For PCF8574

To interface to I2C devices on bus, we must know about it's device read/write address follows by an I2C start condition. It's read/write address varied with three bits A2, A1 and A0. These three pins must pull high or low externally, to make a specific slave address. The address lists below.

PCF8574 slave address

With these three bits, we could make three different slave address.

PCF8574 address map

Writing to the port of PCF8574 make its digital port as output. After a start condition, we must send the device slave address as listed above, follows by an 8-bit output value.

Write mode (output)

To read from this device, we put its read address, follows by data reading. For example, when we connect A2, A1 and A0 to VSS the device write address is 0x40 with the read address 0x41.

The INT (interrupt) pin is an output from PCF8574. It's open drain. This pin create a low signal when there's any logic state changed at input to the port of PCF8574.

Connecting the INT to Microcontroller

CCS PICC Coding

CCS PICC is a C compiler for 8-bit and 16-bit PICMicro. It's fast and easy to use with a lot of library. There are both hardware and software library. In the case of I2C, any device with I2C (for example PIC16F818) we can use hardware library or software library. Hardware I2C library is fast and reliable. Software I2C library work slower because it use program routine. Sometimes, it cause data corrupt. But with software I2C library, we can select any two-pin within the device.

PIC12F629 is an 8-pin 8-bit PICMicro in the Mid-Range family. It doesn't have I2C module shipped. However, we can use software I2C library instead.


Pin Diagram

In this example, I read data from the lower nibble of PCF8574 input and output to its higher nibble to the port.

Reading The Digital Input Output From PCF8574 Using PIC16F629
Schematic Diagram

Source Code.

#include<12F629.h>
/*Use internal RC oscillator*/
#fuses INTRC_IO
#use delay(clock=4M)
/*implementing the software-base I2C*/
#use i2c(scl=pin_a0,sda=pin_a1,FORCE_SW)

void main(void){
   char rcvData=0;
  
   while(1){
      /*Turn on lower nibble high
      for digital input*/
      i2c_start();
      i2c_write(0x40);
      i2c_write(rcvData|0x0F);
      i2c_stop();
      
      /*Read the input state of
      lower nibble*/
      i2c_start();
      i2c_write(0x41);
      rcvData=i2c_read();
      i2c_stop();
      
      /*Shift the inputs right
      to the output*/
      rcvData<<=4;
      
      /*Write Data to output*/
      i2c_start();
      i2c_write(0x40);
      i2c_write(0x0F);
      i2c_stop();
      
      /*Turn on lower nibble high
      for digital input*/
      i2c_start();
      i2c_write(0x40);
      i2c_write(rcvData|0x0F);
      i2c_stop();
      
      /*wait for 100 mS*/
      delay_ms(100);     
   }
}

Click here to download the complete example. This example uses the ATMega32 to interfaces with the PCF8574A using its TWI module.

If you want a standard PCB for ATMega32 micro-controller, you can order my AVR Microcontroller project from PCBWay with a reasonable price. Click here to get a free $5 credit for new account.

Interfacing ATMega32 to 74HC595 shift register
ATMega16 ATMega32 Experiment Board PCB from PCBWay

 


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)