i2C Expander PCF8574A Read Write and Acknowledge

For Flowcode users to discuss projects, flowcharts, and any other issues related to Flowcode 6.

Moderator: Benj

Post Reply
User avatar
Jay Dee
Posts: 398
Joined: Tue Aug 18, 2009 6:42 pm
Has thanked: 121 times
Been thanked: 154 times
Contact:

i2C Expander PCF8574A Read Write and Acknowledge

Post by Jay Dee »

Hi guys,
I'm playing with the PCF8574A expander from TI. I've set the hardware address pins to A0=Lo A1=Hi A2=Hi So the Basic address is 0x3E
The datasheet gives; Address+Read = 0x7D and Address+Write = 0x7C

However when testing in hardware, using 0x7D the system seems to Write to the device.
I've checked the output at the hardware and with a datastream decoder. they both agree and seem to show a Write operation. A correct Ack is also seen.

However, when I use 0x7D to read the device, nothing is read back or any Ack.

So..
A) Any idea why the Read and Write bits seem to be the wrong way around ?
B) Any reason why I dont see an Ack bit after a Read ?

I guess the two are linked or a bit is being dropped somewhere but I cant see any obvious reason. Test Flowcode file attached.
Thanks, J.
Attachments
PCF8574A i2C Tests V2.fcfx
Test file for Read and Write on PCF8574A
(11.59 KiB) Downloaded 348 times

MJU
Posts: 502
Joined: Wed Nov 07, 2007 6:51 pm
Location: Antwerp Belgium
Has thanked: 121 times
Been thanked: 108 times
Contact:

Re: i2C Expander PCF8574A Read Write and Acknowledge

Post by MJU »

I had something similar in the past.

Please read this: viewtopic.php?f=54&t=17422&p=73504#p73491
Maybe that can help?

User avatar
Jay Dee
Posts: 398
Joined: Tue Aug 18, 2009 6:42 pm
Has thanked: 121 times
Been thanked: 154 times
Contact:

Re: i2C Expander PCF8574A Read Write and Acknowledge

Post by Jay Dee »

Yeah thanks for that.. I've had a read through and not found a solution so far.. I'm running a few more tests on the bench and see what I can find.

Rudi
Posts: 666
Joined: Mon Feb 10, 2014 4:59 am
Has thanked: 493 times
Been thanked: 187 times

Re: i2C Expander PCF8574A Read Write and Acknowledge

Post by Rudi »

Jay Dee wrote:Hi guys,
I'm playing with the PCF8574A expander from TI. I've set the hardware address pins to A0=Lo A1=Hi A2=Hi So the Basic address is 0x3E
The datasheet gives; Address+Read = 0x7D and Address+Write = 0x7C

However when testing in hardware, using 0x7D the system seems to Write to the device.
I've checked the output at the hardware and with a datastream decoder. they both agree and seem to show a Write operation. A correct Ack is also seen.

However, when I use 0x7D to read the device, nothing is read back or any Ack.

So..
A) Any idea why the Read and Write bits seem to be the wrong way around ?
B) Any reason why I dont see an Ack bit after a Read ?

I guess the two are linked or a bit is being dropped somewhere but I cant see any obvious reason. Test Flowcode file attached.
Thanks, J.
hi

i have not tested your flowchart, but i will write a few things, perhabs it helps:

The PCF8574 and PCF8574A have addresses ranging from 0x38 to
0x3F (up to eight PCF8574A devices may be used on the same
I2C bus)

The expanders can be configured to have a unique 7-bit address. The first
four bits of the PCF8574’s 7-bit address are 0100, and those for the
PCF8574A are 0111.

0111 ( 000 ) = 0x 38

what is the base adress of the setup Adresspins (A0:2)?
A0=Lo A1=Hi A2=Hi So the Basic address is 0x3E

110 = 0x 6

together as 7 bit address:

Code: Select all

 0111 000    = 0x38
 0000 110    = 0x06
-------------------------
 0111 110    = 0x3E  
 
= 0 x 3E ( total )

The lower three bits are the settings on the device
pins A2, A1, and A0.

This ability to set unique addresses for the expanders
makes it possible to have up to eight PCF8574 and eight PCF8574A
devices on the same I2C bus

now your base adress for the expander is 0x3E

0111 110 ( 7 bit address )

if you write (0)
-------------------------
0x3E + 0 = 0x3E
7 bit address and a write to device means, the address 0x3E + 0 for write then the address byte is 0x3E
the expander know, you will write to expander with base address 0x3E and gives you an ACK if match

if you read (1)
------------------
0x3E + 1 = 0x3F
7 bit address and a read to device means, the address 0x3E + 1 for read then the address byte is 0x3F
the expander know, you will read from expander with base address 0x3E and gives you an ACK if match


7 bit address means, there are 127 possible devices in i2c.
the format of address is 7 bit. this knows all i2c device, so they read the address right only as 7 bit too, and (0)/(1) for write/read is the (i say allways : direction bit ) for write/read


now you will say, if you take 0x3F as base, then a read at 0x3E is the same like a write to 0x3F
no, because the i2c device read the same way back, its read the 7 bit address at left and on bit on the right for direction 7 + 1 bit = 8bit, a valid address byte.

Code: Select all

// base address 0x3E
// expander read the 7 bit as address from a Byte( 8 bit )
// read the left 7 bits..from a valid byte address
// 7 bits    
// 
7654321 +   0 (w/r)
0111 110   (0)  = 0x3E  7bit address 
0111 111   (0)  = 0x3F  7bit address 

if you write to a 0x3E 
0111 110    0 = 0111 1100   left 7 bits are = 0x3E , and right lowest bit (0) for write
if you read from a 0x3E
0111 110    1 = 0111 1101   left 7 bits are = 0x3E , and rigt lowest bit (1) for read

// base address 0x3F
// expander read the 7 bit as address from a Byte( 8 bit ) 
// read the left 7 bits..from a valid byte address
// 7 bits    
// 
7654321 +   0 (w/r)
0111 111   (0)  = 0x3F  7bit address 

if you write to a 0x3F 
0111 111    0 = 0111 1110   left 7 bits are = 0x3F , and right lowest bit (0) for write
if you read from a 0x3F
0111 111    1 = 0111 1111   left 7 bits are = 0x3F , and rigt lowest bit (1) for read

write (0):
so do

i2c_cal_master_start

ACK= i2c_cal_master_txByte(0x3E + 0)
and you will get a 0=ACK for the address match and a 1=NACK for missmatch at 0x3E
if ACK=0 then address is matched and because you send 0 you can write to the expander
ACK= i2c_cal_master_txByte(Data)
if expander want more, it send you an 0 for the ACK
then you can send the next Byte..
if expander want no more, it send you an 1 for the NACK
then you can stop the write condition
i2c_cal_master_stop


read (1) :

ACK = i2c_cal_master_txByte(0x3E + 1)
and you will get an ACK/NACK for the read command at 0x3E
if ACK = 0 then the address was matched and you can send your data byte..

so do
//edit:
Data = i2c_cal_master_rxByte(Last)
if Last = 0
then you can read a next byte
//edit:
Data = i2c_cal_master_rxByte(Last)
if Last = 1
then this was the LAST byte , there are no more byte to read from expander by the master
and then you can
i2c_cal_master_stop

do not forget to i2c_cal_master_init

hope this helps and clear the 7 bit address

best wishes
rudi ;-)


other look:
( not standard and not right but for better understand ..looks similar )

Code: Select all

byte Expander    = 0b00111000;
byte Base        = 0b00000110;
byte read        = 0b00000001;
byte write       = 0b00000000;

byte validAddress;

// if write
validAddress= Explander + Base + write ;

// if read
validAddress=Expander + Base + read;

// *********************************************

// right seen in 7 bit address
// the address is 7 bits as Byte then 
byte Expander    = 0b00111000;
byte Base        = 0b00000110;
byte read        = 0b00000001;
byte write       = 0b00000000;


// write
byte validAddress=0;
validAddress = validAddress +  Expander;
validAddress = validAddress +  Base;
validAddress = (validAddress << 1 | write ) // ups 0x7C

//read
byte validAddress=0;
validAddress = validAddress +  Expander;
validAddress = validAddress +  Base;
validAddress = (validAddress << 1 | read ) // ups 0x7D

yes its "the same" and "not the same"

you must start with one way:
the validAddressByte is then 7 bit Adress(asbyte) + yourDirection
your think is
the validAddressByte is then 7 bit Adress(as7bit) and you shift your direction onetime left into
i know that confussed total :mrgreen:
if i read the post again, i am confussed again :mrgreen: :mrgreen:
because i am not fit in english but hope the base is clear.

perhabs this helps a lot better:
look at writeAddress and then at readAddress there is simply address +1 for a read..
btw: the address is 0x54 ( 7 bits: 1010100 )
like your think then would be the address (1010100 << 1 |1 ) 0xA9...(thats wrong as validAddressByte for write then no ACK comes )

hope this helps.

Image
Last edited by Rudi on Mon May 16, 2016 10:49 pm, edited 1 time in total.

Rudi
Posts: 666
Joined: Mon Feb 10, 2014 4:59 am
Has thanked: 493 times
Been thanked: 187 times

Re: i2C Expander PCF8574A Read Write and Acknowledge

Post by Rudi »

have a look:

added variables:
0.png

changed a little, switch 3 same..:
select1_2.png

last but not least,
the edit flowchart, try it, and let me know how you get on with this.
PCF8574A i2C Tests V2_edit.fcfx
best wishes
rudi ;-)

edit:
ACK==0
or
ACK=0
you can use this or this in the decision icon,
but not in a c code icon.be sure, if you use a c code icon then use

Code: Select all

if (ACK==0)
Last edited by Rudi on Mon May 16, 2016 10:42 pm, edited 1 time in total.

User avatar
Jay Dee
Posts: 398
Joined: Tue Aug 18, 2009 6:42 pm
Has thanked: 121 times
Been thanked: 154 times
Contact:

Re: i2C Expander PCF8574A Read Write and Acknowledge

Post by Jay Dee »

Hi Rudi,
Thanks for that reply, a great description of the 7 bit addressing.
It did take me a while to work out the 7 bit addressing + Read/Write bit on a previous project
I think I'm OK for this project. I'll have a look at the flowcode examples you posted and check everything again.

More info on the problem..
Because I could not get the Hardware to work I checked with a Datastream Decode on the Scope.

The Write looks OK on Scope (decode at bottom of screenshot) and the Expanders Output levels are also correct.
i2C Write Noted.png
i2C Write
(23.59 KiB) Downloaded 4842 times
But The Read does not go so well..
The Address is acknowledged and the Data seems to be clocked out just fine (The expander output had been Written as 0xF0 at this point) ... but there is no ACK after the Read data. I believe a logic Low = Ack OK
My flowcode has the Stop command.
i2C Read Noted.png
i2C read
(25.5 KiB) Downloaded 4842 times
So the device seems to work and WRITES OK but I cannot get a successful READ.... odd?
Again thanks for the info and I'll have another look through..
but at this point I'm close to going back to the MicroChip expander :lol:

Rudi
Posts: 666
Joined: Mon Feb 10, 2014 4:59 am
Has thanked: 493 times
Been thanked: 187 times

Re: i2C Expander PCF8574A Read Write and Acknowledge

Post by Rudi »

thanks.

please try the last flowchart,
because i made the same mistake first time in read..
in a read, you get the data byte return and tell the device that you want read one or more byte by a parameter (Last=1 or Last=0)
i had crossed this too, same like you :)

so edit the statement and flowchart:
//edit:
Data = i2c_cal_master_rxByte(Last)
if Last = 0
then you can read a next byte
//edit:
Data = i2c_cal_master_rxByte(Last)
if Last = 1
then this was the LAST byte , there are no more byte to read from expander by the master
and then you can
i2c_cal_master_stop

hope this helps :)
hey mchp is allways the best :mrgreen:

best wishes
rudi ;-)
nice pics!

Rudi
Posts: 666
Joined: Mon Feb 10, 2014 4:59 am
Has thanked: 493 times
Been thanked: 187 times

Re: i2C Expander PCF8574A Read Write and Acknowledge

Post by Rudi »

Jay Dee wrote: ..
But The Read does not go so well..
The Address is acknowledged and the Data seems to be clocked out just fine (The expander output had been Written as 0xF0 at this point) ... but there is no ACK after the Read data. I believe a logic Low = Ack OK
..
So the device seems to work and WRITES OK but I cannot get a successful READ.... odd?
..

btw..your data are here and valid :mrgreen: :mrgreen:
i2C Read Noted_edit.PNG
:mrgreen:

best wishes
rudi ;-)

User avatar
Jay Dee
Posts: 398
Joined: Tue Aug 18, 2009 6:42 pm
Has thanked: 121 times
Been thanked: 154 times
Contact:

Re: i2C Expander PCF8574A Read Write and Acknowledge

Post by Jay Dee »

Ah.... :oops:
Well found the big error in my flowcode, for my i2C recived component macro.. I had not put in variable for the return byte. I had got the 'return' and 'Last' parameters confused.
Suddenly it all works much better!! Haha!

Data Line held Low after Read Byte OPeration
I have noticed though.. if I finsh an i2C read operation and just a single Stop macro, under certain circumstances the Data line can be left held Low.
I suspect this is due to some timing issue. Sending a second Stop macro and everything works just fine. This workaround works just fine for me so I'll not dig any further.
This issue is only seen in certain circumstances and so far just with the PCF8574A expander.

Good progress though but now have to fly off to next customer so this project will be on the back burner for another week. :(
Thanks for the help bashing though the i2C, much appreciated. J.

Post Reply