Libstock prefers package manager

Package Manager

We strongly encourage users to use Package manager for sharing their code on Libstock website, because it boosts your efficiency and leaves the end user with no room for error. [more info]

Project Request

Return
Willy Lievens
willy.zon

posted on 2013/09/26 03:18:33 PM CEST

Other Codes

Read a PCF8574 I/O-expander with PIC16F887 on EasyPic7 platform

Hi! Im some new here!

I have a problem with the PCF8574 I/O Expander.
To write to the expander is easy!
But i can't read from the I/O-expander...
Thera are 8 pull-up resistors (10k) on the 8 input switches)

I have transform de c-code for the RTC-clock (PCF8583)

I2C1_Init(100000); //Init I2C with desired clock
{
I2C1_Start(); // issue I2C start signal
I2C1_Wr(0b01001110); // send byte via I2C (device address + W) A0-A1-A2 = 1
I2C1_Repeated_Start(); // issue I2C signal repeated start
I2C1_Wr(0b01001111); // send byte (device address + R)

PORTB = I2C1_Rd(0); // Read the data (NO acknowledge) to PORTB (8 x LED)
I2C1_Stop(); // issue I2C stop signal
}

Can you help mee?
Thanks


USER Comments

Willy Lievens
willy.zon

posted on 2013/10/05 09:32:27 AM CEST

Hello,

Thanks for your comment and your support!
I'm not a C-code expert, I am just a beginner.
At this moment I'm not interessed in the mixed mode of the PCF8574 (I/O-Expander)
I use only 8 inputs switches. There are NO output leds on this PCF8574.
So, your code seems rather complex for a simple -read 8 input- instruction.
Maybe, it is necessary!
But is it possible to write the C-code without FUNCTIONS?

Best wishes


  • Rating:
0
No Abuse Reported

Do you want to report abuse comment ID: 890 in "Read a PCF8574 I/O-expander with PIC16F887 on EasyPic7 platform" request.

Robson Electronic
RTEK1000

posted on 2013/09/30 07:34:05 AM CEST

Hello!

You must set the port bit before reading.

Eg:

If the PCF has an entry on this bit and bit 0 is pull up, you must define one. Or better WRITE 1 to this bit.

But if you do not set to 1 and the pin is low, the read will return 0.

The PCF is free direction control, refer to this datasheet:
"The device has a quasi-bidirectional I / O port 8 bits (P0-P7), including latched outputs capable of handling high current for driving LEDs directly. Almost Each bidirectional I / O can be used as an input or exit without the use of a steering control signal data. "

See data sheet for additional information:
http://www.ti.com/lit/ds/symlink/pcf8574.pdf

Look for a good VSM simulator, for testing without damaging components, so you can test the code and then submit to the board real.

Try this code bellow,

Good luck!

// ----------------- Begin Code

//Declarations section
const PCF_Addr0 = 0B01000000; // 0 1 0 0 A2 A1 A0 R/W - 1 PCF
const PCF_Addr1 = 0B01000010; // 0 1 0 0 A2 A1 A0 R/W - 2 PCF
const PCF_Addr3 = 0B01000100; // 0 1 0 0 A2 A1 A0 R/W - 3 PCF

// Software I2C connections
sbit Soft_I2C_Scl at RC3_bit;
sbit Soft_I2C_Sda at RC4_bit;
sbit Soft_I2C_Scl_Direction at TRISC3_bit;
sbit Soft_I2C_Sda_Direction at TRISC4_bit;
// End Software I2C connections

int WrPCF(int addr, int Data)
{
int ack1;
Soft_I2C_Start();
Soft_I2C_Write(addr);
ack1 = Soft_I2C_Write(Data); // return ACK for check
Soft_I2C_Stop();

return ack1;
}

int RdPCF(int addr)
{
int rd_data;
addr = addr | 1; // r/w=1
Soft_I2C_Start();
Soft_I2C_Write(addr);
rd_data = Soft_I2C_Read(0);
Soft_I2C_Stop();

return rd_data;
}


void main() {
TRISA = 0;
TRISB = 0;
TRISC = 0;
TRISD = 0;
TRISE = 0;

Soft_I2C_Init();

delay_ms(500);

while (1)
{
WrPCF(PCF_Addr0, 0B10111001);
//______________________|||||||^ - this bit (0) with button switch to low
//______________________|||||||--- this bit(s) (1 up to 7) with led enable high
delay_ms(500);

PORTB = RdPCF(PCF_Addr0); // PORTB is 10111001 or 10111000

delay_ms(500);

WrPCF(PCF_Addr0, 0B00000001);
//______________________|||||||^ - this bit (0) with button switch to low
//______________________|||||||--- this bit(s) (1 up to 7) with led enable high
delay_ms(500);

PORTB = RdPCF(PCF_Addr0); // PORTB is 00000001 or 00000000

delay_ms(500);

}
}

// ----------------- End Code

  • Rating:
0
No Abuse Reported

Do you want to report abuse comment ID: 875 in "Read a PCF8574 I/O-expander with PIC16F887 on EasyPic7 platform" request.