I2C master/slave help

For Flowcode users to discuss projects, flowcharts, and any other issues related to Flowcode 7.

Moderator: Benj

mnf
Valued Contributor
Valued Contributor
Posts: 1188
Joined: Wed May 31, 2017 11:57 am
Has thanked: 70 times
Been thanked: 439 times
Contact:

Re: I2C master/slave help

Post by mnf »

Can you look at the data on the scope - one end is getting it wrong. But is it send, receive (or both)?

User avatar
fotios
Posts: 458
Joined: Mon Feb 08, 2010 10:17 am
Location: Greece
Has thanked: 109 times
Been thanked: 117 times
Contact:

Re: I2C master/slave help

Post by fotios »

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.
Capture-1.jpg
Capture-1.jpg (97.8 KiB) Viewed 5834 times
Best Regards FOTIS ANAGNOSTOU

User avatar
fotios
Posts: 458
Joined: Mon Feb 08, 2010 10:17 am
Location: Greece
Has thanked: 109 times
Been thanked: 117 times
Contact:

Re: I2C master/slave help

Post by fotios »

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 :lol: :lol:
So, I suppose for 3 bytes it should be 3 and so on.

A big thank to you again
Best Regards FOTIS ANAGNOSTOU

mnf
Valued Contributor
Valued Contributor
Posts: 1188
Joined: Wed May 31, 2017 11:57 am
Has thanked: 70 times
Been thanked: 439 times
Contact:

Re: I2C master/slave help

Post by mnf »

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

User avatar
fotios
Posts: 458
Joined: Mon Feb 08, 2010 10:17 am
Location: Greece
Has thanked: 109 times
Been thanked: 117 times
Contact:

Re: I2C master/slave help

Post by fotios »

For anyone interested, here is the correct I2C Slave Wire (Arduino) code for UINT16 type (2 bytes) number reception:

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);
}
And here is the I2C Master FCF for UINT type (2 bytes) number transmission:
I2C_STM32.fcfx
(14.77 KiB) Downloaded 228 times
Best Regards FOTIS ANAGNOSTOU

User avatar
fotios
Posts: 458
Joined: Mon Feb 08, 2010 10:17 am
Location: Greece
Has thanked: 109 times
Been thanked: 117 times
Contact:

Re: I2C master/slave help

Post by fotios »

mnf wrote:Good spot.
Do you believe that I was able to find the bug? :lol: I'm not so smart, and so I asked in Particle community forum :roll: and got the fix.
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())?" :lol:
So, I usually avoid Particle forums unless is absolutely necessary :wink:
Your solution

Code: Select all

uint16_t STdataRX = (STdataH << 8) + STdataL;
is also the correct, I also had bug on it and thanks again.
All the best
Fotis
Best Regards FOTIS ANAGNOSTOU

mnf
Valued Contributor
Valued Contributor
Posts: 1188
Joined: Wed May 31, 2017 11:57 am
Has thanked: 70 times
Been thanked: 439 times
Contact:

Re: I2C master/slave help

Post by mnf »

Yes - some sites are less than friendly... Always seems a shame - everyone needs to learn something sometime.....


Martin

Post Reply