I2C master/slave help
Moderator: Benj
- fotios
- Posts: 458
- Joined: Mon Feb 08, 2010 10:17 am
- Location: Greece
- Has thanked: 109 times
- Been thanked: 117 times
Re: I2C master/slave help
Thanks a lot again
I got a new screenshot of the scope. Everything looks OK. From the left, the first byte is the Slave ID = 0x38<<1 = 0x70, the second byte is the DataH=20d, and the third byte is the DataL=150d. The black triangles point the 9th bit=ACK. Additionally, I use 2 blue LEDs mounted on the STM32 application board, to show if the ACK is received each time by the master and both are ON. This time I added in the FCF two extra calculations to set again the ACK=1 after each ACK=0 at the end of each byte transmission.
However, the Argon still receives correctly only the last Data byte transmitted (i.e. 150) while the first Data byte is shown on Serial Monitor as 255d.
I got a new screenshot of the scope. Everything looks OK. From the left, the first byte is the Slave ID = 0x38<<1 = 0x70, the second byte is the DataH=20d, and the third byte is the DataL=150d. The black triangles point the 9th bit=ACK. Additionally, I use 2 blue LEDs mounted on the STM32 application board, to show if the ACK is received each time by the master and both are ON. This time I added in the FCF two extra calculations to set again the ACK=1 after each ACK=0 at the end of each byte transmission.
However, the Argon still receives correctly only the last Data byte transmitted (i.e. 150) while the first Data byte is shown on Serial Monitor as 255d.
Best Regards FOTIS ANAGNOSTOU
- fotios
- Posts: 458
- Joined: Mon Feb 08, 2010 10:17 am
- Location: Greece
- Has thanked: 109 times
- Been thanked: 117 times
Re: I2C master/slave help
Hi Martin
OK, solved.
The error was in the expression while(1 < Wire.available()) { // loop through all but the last
It might be while(2 < Wire.available()) { // loop through all but the last to receive two bytes
So, I suppose for 3 bytes it should be 3 and so on.
A big thank to you again
OK, solved.
The error was in the expression while(1 < Wire.available()) { // loop through all but the last
It might be while(2 < Wire.available()) { // loop through all but the last to receive two bytes


So, I suppose for 3 bytes it should be 3 and so on.
A big thank to you again
Best Regards FOTIS ANAGNOSTOU
-
- Valued Contributor
- Posts: 1208
- Joined: Wed May 31, 2017 11:57 am
- Has thanked: 70 times
- Been thanked: 440 times
Re: I2C master/slave help
Good spot.
Wire.available() returns number of bytes available - so use a variable (i=0) and use while( i < wire.available()){data = wire.read(); i+=1;}
It's okay loading to named variables for one or two bytes but for more you'll want to use an array..
Sorry - this from my phone. I'll tidy the code later!
Martin
Wire.available() returns number of bytes available - so use a variable (i=0) and use while( i < wire.available()){data = wire.read(); i+=1;}
It's okay loading to named variables for one or two bytes but for more you'll want to use an array..
Sorry - this from my phone. I'll tidy the code later!
Martin
- fotios
- Posts: 458
- Joined: Mon Feb 08, 2010 10:17 am
- Location: Greece
- Has thanked: 109 times
- Been thanked: 117 times
Re: I2C master/slave help
For anyone interested, here is the correct I2C Slave Wire (Arduino) code for UINT16 type (2 bytes) number reception:
And here is the I2C Master FCF for UINT type (2 bytes) number transmission:
Code: Select all
void receiveEvent(int howMany) {
while(2 < Wire.available()) { // loop through all but the last
char c = Wire.read(); // receive byte as a character
Serial.print(c); // print the character
}
uint8_t STdataH = Wire.read();
uint8_t STdataL = Wire.read();
uint16_t STdataRX = (STdataH << 8) + STdataL;
Serial.println(STdataRX);
}
void setup() {
Wire.begin(0x38); // join i2c bus with address 0x38
Wire.onReceive(receiveEvent); // register event
Serial.begin(9600); // start serial for output
}
void loop() {
delay(100);
}
Best Regards FOTIS ANAGNOSTOU
- fotios
- Posts: 458
- Joined: Mon Feb 08, 2010 10:17 am
- Location: Greece
- Has thanked: 109 times
- Been thanked: 117 times
Re: I2C master/slave help
Do you believe that I was able to find the bug?mnf wrote:Good spot.


Simply, the members of it, are not so polite like all of you here in Matrix forums.
The reply I got nearly was : "Hey man, are you serious? You expect to receive 2 bytes with (1 < Wire.available())?"

So, I usually avoid Particle forums unless is absolutely necessary

Your solution
Code: Select all
uint16_t STdataRX = (STdataH << 8) + STdataL;
All the best
Fotis
Best Regards FOTIS ANAGNOSTOU