Hello fellow Arduino enthusiasts, I am trying to make the ATTiny85 talk I²C with a MMA7660 breakout board I have (it's an accelerometer, this is a follow up question to my previous one).
I would state in advance that the I²C chip works perfectly with Arduino Nano, that I am using the Arduino IDE v1.5.6-r2, using the arduino-tiny core (from here) and that until now I had no special issues working at 8MHz.
The official documentation for the TinyWireM library (at the Arduino playground website) states that:
By default the I2C master library (TinyWireM) is set to run at 1MHz. To run at 8MHz, #defines in USI_TWI_Master.h / .cpp must be changed.
I have made the following changes in USI_TWI_Master.cpp:
#define F_CPU 8000000UL // was 1000000UL
I have made the following changes in USI_TWI_Master.h:
#define SYS_CLK 8000.0 // was 1000.0
#define T2_TWI 1.3 // was 5
#define T4_TWI 0.6 // was 4
My guess is that the T2_TWI & the T4_TWI values are wrong. I also tried T2_TWI 40 and T4_TWI 32 (as stated here), and T2_TWI 2 & T4_TWI 1 (as stated here) but no luck. I also tried some other values in between that I thought might make sense but it won't work.
The code I am running is the I²C scanner from here (which as I mentioned works perfectly with the Nano and finds the device), the blow is a modified version for interfacing the TinyWireM library instead of the Wire library:
#include <TinyWireM.h>
#include <SoftwareSerial.h>
SoftwareSerial mySerial(-1,4);
void setup()
{
TinyWireM.begin();
mySerial.begin(9600);
mySerial.println("\nI2C Scanner");
}
void loop()
{
byte error, address;
int nDevices;
mySerial.println("Scanning...");
nDevices = 0;
for(address = 1; address < 127; address++ )
{
TinyWireM.beginTransmission(address);
error = TinyWireM.endTransmission();
if (error == 0)
{
mySerial.print("I2C device found at address 0x");
if (address<16)
mySerial.print("0");
mySerial.print(address,HEX);
mySerial.println(" !");
nDevices++;
}
else if (error==4)
{
mySerial.print("Unknow error at address 0x");
if (address<16)
mySerial.print("0");
mySerial.println(address,HEX);
}
}
if (nDevices == 0)
mySerial.println("No I2C devices found\n");
else
mySerial.println("done\n");
delay(5000); // wait 5 seconds for next scan
}
As a side note I would say that my logic analyzer was broken recently and I'm sure it would have helped here... Also I am aware of the sound-card hack to be used as logic analyzer, but would prefer to avoid adding more error-prone variables to the system, if possible, for now.
Here is a picture of probing with Saleae logic analyzer of the two channels. The result of the code above is "No I2C devices found":
Would appreciate the insights of anyone with experience using ATTiny85 at 8Mhz with I²C successfully.