10 years, 1 month ago.

I can't I2C mbed and mbed.

I can't I2C mbed and mbed.

Sorry, I'm japanese. So, I don't speak English good.

This is master program.

// master

#include "mbed.h"
#include "ACM1602NI.h"

DigitalOut led1(LED1);
DigitalOut led2(LED2);
DigitalOut led3(LED3);
DigitalOut led4(LED4);

I2C LCD(p9, p10);
ACM1602NI lcd(LCD);

I2C i2c(p28, p27);        // sda, scl
Serial pc(USBTX, USBRX); // tx, rx

const int addr = 0xA0; // define the I2C Address
int val; 

int main() {
	wait(0.5);

	i2c.frequency(100000); 
    char cmd[] = "CBA";

    while(1) {
    	i2c.start();
        val = i2c.write(addr, cmd, 3);	wait(0.07);
        if(val) 	{led1 = 1;	led2 = 0;}
        if(!val) 	{led1 = 0;	led2 = 1;}
        lcd.locate(0, 0);
 		lcd.printf("%s\n", cmd);
        wait(0.1);              // Could also poll, 65ms is typical

        i2c.start();
        val = i2c.read(addr, cmd, 3);	wait(0.07);
        if(val) 	{led1 = 1;	led2 = 0;}
        if(!val) 	{led1 = 0;	led2 = 1;}
        lcd.locate(0, 1);
		lcd.printf("%s\n", cmd);
		wait(0.1);

		led1 = 0;	led2 = 0;
    }
}

This is slave program.

// slave

#include <mbed.h>
#include "ACM1602NI.h"

DigitalOut led1(LED1);
DigitalOut led2(LED2);
DigitalOut led3(LED3);
DigitalOut led4(LED4);

I2CSlave slave(p9, p10);


int main() {
    char buf[] = "ABC";
    char msg[] = "Slave!";
    int val;
 
	slave.frequency(100000);
    slave.address(0xA0);

    while (1) {
        int i = slave.receive();
        switch (i) {
            case I2CSlave::ReadAddressed:
                val = slave.write(buf, 3); // Includes null char
		        if(val) 	{led1 = 1;	led2 = 0;}
		        if(!val) 	{led1 = 0;	led2 = 1;}
                break;
            case I2CSlave::WriteGeneral:
                val = slave.read(buf, 3);
		        if(val) 	{led1 = 1;	led2 = 0;}
		        if(!val) 	{led1 = 0;	led2 = 1;}
                break;
            case I2CSlave::WriteAddressed:
                val = slave.read(buf, 3);
		        if(val) 	{led1 = 1;	led2 = 0;}
		        if(!val) 	{led1 = 0;	led2 = 1;}
                break;
        }
        led1 = 0;	led2 = 0;
    }
}

Please, help!

2 Answers

10 years, 1 month ago.

Please use <<code>> and <</code>> tags around you posted code to keep it readable.

Did you make sure that the I2C bus is connected correctly between the two mbeds:

  1. SDA p28 of master to SDA p9 of slave,
  2. SCL p27 of master to SCL p10 of slave,
  3. GND of master to GND of slave
  4. Pullup resistors connected between SDA and 3V3 and between SCL and 3V3

Obviously you need to check that the i2c bus to the LCD is connected correctly also. Did you check that the display works.

Incorrect I2C bus connections will hang the mbed I2C library.

You dont need the separate i2c.start() commands. They are part of the i2c read and write methods.

9 years, 5 months ago.

When naming your i2c master device you have called it LCD but when you use it you call it i2c.

Change the line <<I2C LCD(p9, p10)>> to << I2C i2c(p9, p10) and your code should run.

This question is from some time ago. May have been solved. Anyhow, the problem is not with the i2c/LCD names. The I2C master uses 2 different i2c interfaces in this example. The one named LCD is the interface to the ACM1602NI LCD display which has a native I2C bus interface. The second bus (i2c) is used to communicate with the mbed node running the I2CSlave code.

posted by Wim Huiskamp 27 Oct 2014