I2C - PCF8574AP

04 Dec 2010 . Edited: 04 Dec 2010

Hello,

Giving the information that i am new to the C language, i want to use my mbed to drive 8 led's on the I²C bus by using a PCF8574AP. This becouse i am using the I²C bus in my dissertation for my high school studies and i want to get used with it. Previously i tried it with the library for the PCF8574, but it did not work (the led's where glowing very faintly. So i wrote my own program, very simple : turn all the led's on, and turn them back off, so here it is:

#include "mbed.h"

I2C i2c(p28, p27);


int main() {
while (1) {

void start(void);    //start condition
i2c.write(0x40);    //write adress
i2c.write(0xf);       //write data
void stop(void) ;  //stop condition

wait(1);

void start(void);    //start condition
i2c.write(0x40);    //write adress
i2c.write(0x0);      //write data
void stop(void);   //stop condition

wait(1);
}
}

The compiler is not giving any errors/warnings, but the led's are still just glowing faintly and do not seem to be affected by my code.

All help is welcome, thanks in advance :)

 

 

04 Dec 2010

Hi Stef,

In your example, you need to call start() and stop() as methods of the i2c object. So something like:

#include "mbed.h"

I2C i2c(p28, p27);

int main() {
  while (1) {
    i2c.start();    //start condition
    i2c.write(0x40);    //write adress
    i2c.write(0xf);       //write data
    i2c.stop();  //stop condition

    wait(1);

    i2c.start();    //start condition
    i2c.write(0x40);    //write adress
    i2c.write(0x0);      //write data
    i2c.stop();   //stop condition

    wait(1);
  }
}

Nothing to say this will make it work, but you'll be closer at least :)

Simon

04 Dec 2010 . Edited: 04 Dec 2010

Try this, it works with a pcf8574 and a pcf8574A

 

#include "mbed.h"

I2C i2c(p28, p27);

int main() {
int addr;
char cmd[3];

while (1) {

    addr = 0x40;                // adres van pcf8574
    cmd[0] = 0xFF;              //
    i2c.write(addr, cmd, 1);    // Send command string
    i2c.stop();

    wait(0.5);

    addr = 0x40;                // adres van pcf8574
    cmd[0] = 0x00;              //
    i2c.write(addr, cmd, 1);    // Send command string
    i2c.stop();

    wait(0.5);

    addr = 0x70;                // adres van pcf8574A version
    cmd[0] = 0xFF;              //
    i2c.write(addr, cmd, 1);    // Send command string
    i2c.stop();

    wait(0.5);

    addr = 0x70;                // adres van pcf8574A version
    cmd[0] = 0x00;              //
    i2c.write(addr, cmd, 1);    // Send command string
    i2c.stop();

    wait(0.5);

wait(1);
}
}

04 Dec 2010 . Edited: 04 Dec 2010

The program works indeed. From this base i will be able to continue with my project.

Thanks alot for getting me on track :)

 

One question tough, in the previous post there is the i2c command written like i2c.write(adress, data, 1); but, what does this "1" refer to, and is it neccesary?

04 Dec 2010

The "1" means the number of bytes send in the complete command. See the next sample code when using a ds1621. Its initialize the registers at starrt.

  addr = 0x90;                // adres van ds1621
    cmd[0] = 0xac;              // access config prot
    cmd[1] = 0x02;              // output pol high , continu conv
    i2c.write(addr, cmd, 2);    // Send command string
    i2c.stop();

    wait(0.07);

    addr = 0x90;                // adres van ds1621
    cmd[0] = 0xa1;              // access TH
    cmd[1] = 0x18;              // set TH = 24 graden
    cmd[2] = 0x00;              // set TH = 00
    i2c.write(addr, cmd, 3);    // Send command string
    i2c.stop();

    wait(0.07);

    addr = 0x90;                // adres van ds1621
    cmd[0] = 0xa2;              // access TL
    cmd[1] = 0x16;              // set TL = 22 graden
    cmd[2] = 0x00;              // set TL = 00
    i2c.write(addr, cmd, 3);    // Send command string
    i2c.stop();

    wait(0.07);

    addr = 0x90;                // adres van ds1621
    cmd[0] = 0xee;              // start conv t
    i2c.write(addr, cmd, 1);    // Send command string
    i2c.stop();