Coding no working.

02 Nov 2011

Dear All,

I'm using this sample code to do some modification.

The problem that i facing is when i write a variable value to PCF8574 it is not working. It just work for const. value which is like sample code.

Following coding is what i trying to test.

#include "mbed.h"

I2C i2c(p9,p10);
DigitalOut led1(LED1);
const int addr = 0x7E; // define the (8-bit) I2C Address for the PC

int write(char byteOut) {
    char * cmd = new char[1];
    cmd[0] = byteOut;

    return i2c.write(addr, cmd, 1);
}

int main() {
    char i=0;
    i2c.frequency(100000);
    
    while (1) {
        /* now flash on-bosrd LED1 if the write to I2C works
           This confirms that you've got the right address
           and that you remembered the pull-up resistors on SCL/SDA :) */
        if (0 == write(i)) {
            led1 = 1;
            wait(0.2);
            led1 = 0;
        }
       
        i++;
        printf("i = %d\n\r", i);
        wait(0.2);
    }
}

Is it the coding problem or compiler issue?

Thank you...

02 Nov 2011

Are you sure about the 0x7E address? I found the following description:

Quote:

The PCF8574's I2C address is 0100xxxy, with three bits (x) determined by the state of the address pins A2-0, and a final bit (y) that sets the read (1) or write (0) mode.

Which only produces addresses from 0x40 to 0x4E.

03 Nov 2011

Hello Igorsk,

Thanks for your reply.

Yup, i think the address should be correct. I'm using PCF8574AP and wired the A0-A2 to 5Volt. According to the datasheet said if we using PCB8574A then the slave address format should be 0111xxxy.

/media/uploads/bc/sa.jpg

PCB8574 addresses: 0x40 -to- 0x4E (for write mode) PCB8574A addresses: 0x70 -to- 0x7E (for write mode)

I did adjust the frequency but seen like same. Wonder is there any bug...

03 Nov 2011

Try writing to all addresses from 0x40 to 0x7E, just in case? See if any of them replies.

03 Nov 2011

Hello IgorSK,

Ya, thanks for your advice. I just found one useful method to test their replies. Simon have wrote a useful test program to check the exist of I2C devices. I2CU

The I2CU program checkup is 0x7E address. This address is working for me is we put fix/const value.

....
if (0 == write(0x55)) {
...
...
write(0xAA);
wait(0.2);
..
.

The output LED is blinking ON and OFF.

If i put something variable value then my output LED will always stay ON.

....
if (0 == write(i)) {
...
...
i++;
wait(0.2);
..
.

P/s: may i know how to put the text to next line? I still can't type my sentence to next line when i do post reply. Is it every sentence we must put a '.'+'enter' then only can go to next line? :(

03 Nov 2011

Hello lgorSK,

I did found my mistake, very sorry. I wired the LED at MSB didn't realize it. So, every time when after reset the mbed the iteration will start counting from 0,1,2,3,4....till 16 then only the first LED will light up. To reach 16 will take some time so that i was thought the coding is not working.

The problem is solve. Thanks a lot ya. ^^