In my project i am using Arduino uno board(atmega328) as master and Blue Pill(stm32) board as slave. I have written simple example code in both these but STM is not receiving any messages over I2C.
Code in my STM 32.
#include <Wire.h>
#define I2C_ADDR 2
TwoWire myWire1(PB7, PB6);void setup()
{
Serial.begin(115200);
myWire1.begin(I2C_ADDR); // join i2c bus with address #4
myWire1.onRequest(requestEvent); // register event
myWire1.onReceive(receiveEvent); // register event
}void loop()
{
//empty loop
}// function that executes whenever data is received from master
// this function is registered as an event, see setup()
void receiveEvent(int howMany)
{while(1 < myWire1.available()) // loop through all but the last
{
char c = myWire1.read(); // receive byte as a character
Serial.print(c); // print the character
}
int x = myWire1.read(); // receive byte as an integer
Serial.println(x); // print the integer
}// function that executes whenever data is requested by master
// this function is registered as an event, see setup()
void requestEvent()
{
myWire1.write("hello\n"); // respond with message of 6 bytes
Serial.println("Responded to request event");
// as expected by master
}
And Code in Atmega -
#define SLAVE_ADR 2
#include <Wire.h>void setup() {
Wire.begin(); // join i2c bus (address optional for master)
Serial.begin(115200);
}byte x = 0;
void loop() {
Wire.beginTransmission(SLAVE_ADR); // transmit to device #8
Wire.write("x is "); // sends five bytes
Wire.write(x); // sends one byte
Wire.endTransmission(); // stop transmittingx++;
delay(500);
}
Can somebody help me what is it that i am doing wrong. Or point me in right direction to look at.