Important changes to forums and questions
All forums and questions are now archived. To start a new conversation or read the latest updates go to forums.mbed.com.
All forums and questions are now archived. To start a new conversation or read the latest updates go to forums.mbed.com.
Hello people, does somebody know if there's a way to send a char and not a const char through I2c form the slave to the master? Both my master and my slave are mbeds, and i was able to sendo a char from the master to the slave, but not the other way around. my code follows: Slave:
#include "mbed.h" I2CSlave slave(p9,p10); Serial pc(USBTX, USBRX); int main() { char test[]="message slave"; char buf[15]; int i,b; slave.frequency(100000); slave.address(0xA0); pc.printf("Test I2C Slave:\n\r"); while(1) { for(int j = 0; j < 15; j++) buf[j] = 0;/*effacer le buffer*/ /*strcpy(test, "");*/ i = slave.receive(); switch (i) { case I2CSlave::ReadAddressed: /*pc.printf("Tapez la message a etre envoye vers le Master:\n\r"); pc.scanf("%s", test); pc.printf("%s", test);*/ b=slave.write(test, strlen(test) + 1); wait(0.1);/*wait nécessaire pour faire le write*/ pc.printf("Write %d\n\r",b); break; case I2CSlave::WriteGeneral: slave.read(buf, 15); wait(0.1);/*wait nécessaire pour faire le read*/ pc.printf("SLAVE Read General: %s\n\r", buf); break; case I2CSlave::WriteAddressed: slave.read(buf, 15); wait(0.1);/*wait nécessaire pour faire le read*/ pc.printf("SLAVE Read Addressed: %s\n\r", buf); break; } } }Master:
#include "mbed.h" I2C i2c(p28, p27); Serial pc(USBTX, USBRX); int main() { const int addr = 0xA0; int a,b; char buff[15]; char data[15]; i2c.frequency(100000); while(1) { for(int j = 0; j < 15; j++) buff[j] = 0;/*effacer le buffer*/ /************************************************Ecrire************/ strcpy(data, ""); pc.printf("Tapez la message a etre envoye vers le Slave:\n\r"); pc.scanf("%s", data); pc.printf("%s\n\r", data); b=i2c.write( addr, data, strlen(data)+1); wait(0.1);/*wait nécessaire pour faire le write*/ pc.printf("Write %d\n\r", b); /*****************************Lire***************/ a=i2c.read(addr, buff,15); wait(0.1);/*wait nécessaire pour faire le read*/ pc.printf("%d", a); pc.printf("Read %s\n\r", buff); } }