Hi,
I have made I2C communication between ATMega644(16MHz) and ATMega328(16MHz)
according Arduino Wire library examples ("Master Reader" and "Slave Sender").
But I have a problem in I2C slave in its Wire.onRequest handler.
If I change some global variable in this handler,
afterwards this global variable sometimes contains wrong value.
Here is the code of the I2C master (ATMega644):
#include <Wire.h>
#define TWI_RESPONSE_LENGTH 30
void setup()
{
Wire.begin();
Serial.begin(9600);
}
void loop()
{
Wire.requestFrom(1, TWI_RESPONSE_LENGTH);
while(Wire.available())
{
char c = Wire.receive();
Serial.print(c);
};
delay(500);
}
I want to know number of program cycles between two calls of Wire.onRequest handler
in the slave. This number I will send to master.
Due to I set "cycles_a=0" "cycles_b=0" inside of Wire.onRequest handler.
But this setting makes problems.
Here is the code of the I2C slave (ATMega328):
#include <Wire.h>
#define TWI_RESPONSE_LENGTH 30
long int cycles_a = 0;
long int cycles_b = 0;
//-------------------------------
void twiRequestHandler()
//-------------------------------
{
char sTwiResponse[TWI_RESPONSE_LENGTH + 1];
sTwiResponse[0] = '\0';
//--
char buf[20];
//-- write cycles_a
ltoa(cycles_a, buf, 10);
strcat(sTwiResponse, buf);
//---
strcat(sTwiResponse, ":");
//-- write cycles_b
ltoa(cycles_b, buf, 10);
strcat(sTwiResponse, buf);
//-- fill in remainder by spaces
int countOfSpaces = TWI_RESPONSE_LENGTH - strlen(sTwiResponse);
for (int i = strlen(sTwiResponse); i < TWI_RESPONSE_LENGTH; ++i) {
sTwiResponse[i] = ' ';
};
sTwiResponse[TWI_RESPONSE_LENGTH] = '\0';
//--
Wire.send(sTwiResponse);
//--
cycles_a = 0; //problematic instruction
cycles_b = 0; //problematic instruction
}
//-------------------------------
void setup()
//-------------------------------
{
cycles_a = 0;
cycles_b = 0;
Wire.begin(1);
Wire.onRequest(twiRequestHandler);
}
//-------------------------------
void loop()
//-------------------------------
{
float f = sqrt(micros()); //due to slow down main loop
f = micros()*micros(); //due to slow down main loop
f = micros()/f; //due to slow down main loop
++cycles_a;
++cycles_b;
}
Result from the master which I get is e.g. this:
cycles_a:cycles_b
-----------------
35543:35543
35543:35543
35542:35542
35543:35542 <- difference
35541:71084 <- difference
35542:35542
35542:35542
35757:35543 <- difference
35542:35542
35542:35542
35541:35541
35542:35542
35543:35542 <- difference
35541:35542 <- difference
35541:35541
35543:35543
35543:35543
35542:35542
35541:35541
35542:35542
35542:35542
35543:35543
35543:35543
35542:35542
35543:35543
35542:35542
35542:35542
35542:35542
35756:35542 <- difference
35543:35543
35543:35543
35541:35541
I have discovered, that in the Wire.onRequest handler is possible only reading
of global variables, but write access sometimes fails.
When I null "cycles_a=0" "cycles_b=0" in Wire.onRequest handler,
result is sometimes wrong.
Could somebody to explain me why ?
Is it possible to change global variables in Wire.onRequest handler ?
Thank you for your answer.
Vojtech