0
\$\begingroup\$

I am trying to send message from slave(STM2) to my master(STM1).but it doesnot show anything on my serial terminal .just i want to send and receive data in between master and slave

my master code:

#include "mbed.h"

I2C i2c(PB_9,PB_6);      
Serial pc(USBTX, USBRX); 



int main() {
    const int addr = 0xA0;
    int a,b;
    char buff[10]; const char data[]="message master"; 

    i2c.frequency(10000);

    while(1) { 
    /* i2c.start();                             
        a=i2c.read(addr, buff,10,1);
        wait(1);        
        pc.printf("Read %d", a);        
        i2c.stop();                
        pc.printf("MASTER READ: %s \n\r", buff);*/         
        i2c.start();        
        b=i2c.write( addr, data, strlen(data)+1);        
        wait(0.07);          
        i2c.stop();        
        pc.printf("Write %d\n\r", b);        
        for(int i = 0; i < 10; i++) buff[i] = 0;

        }
 }

My slave code:

#include "mbed.h"

I2C i2c(PB_9,PB_6);
Serial pc(USBTX, USBRX);   

int main() {    

    const char test[]= "message slave";
    char buf[10];
    int i, b;

    i2c.frequency(10000);
    slave.address(0x01);    

    while(1) {

        i = slave.receive();
        wait(0.75);

        switch (i) {

            case I2CSlave::ReadAddressed:

                slave.stop();
                b=slave.write(test, strlen(test) + 1); 
                wait(0.75);

                pc.printf("Return Slave %d", b);// only to check return value

                break;
            case I2CSlave::WriteGeneral:

                slave.stop();
                slave.read(buf, 10);
                wait(0.75);
                pc.printf("SLAVE Read G: %s\n\r", buf);
                break;
            case I2CSlave::WriteAddressed:

                slave.stop();
                slave.read(buf, 10);
                wait(0.75);
                pc.printf("SLAVE Read A: %s\n\r", buf);
                break;
        }
        for(int i = 0; i < 10; i++) buf[i] = 0;        

    }
}
\$\endgroup\$
0

2 Answers 2

1
\$\begingroup\$

Your sending address and slave address are not matched.

You set accessing address as 0xA0 in master, and you set the slave address as 0x01. They have to match each other.

Also, in I2C addressing, the least significant bit is used for Read/Write bit. so the LSB bit in the slave address is ignored, meaning 0x01 for slave address is invalid and the slave address might be general call address, 0x00. So try changing it to slave.address(0xA0); in your slave code.

Added: Don't forget to add pull up resistors on the two wires.

\$\endgroup\$
0
\$\begingroup\$

I think you should use 8bit addresses

const int addr7bit = 0x48;      // 7 bit I2C address
const int addr8bit = 0x48 << 1; // 8bit I2C address, 0x90

https://docs.mbed.com/docs/mbed-os-api-reference/en/latest/APIs/interfaces/digital/I2C/

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.