I am using ATmega8 with 12MHz crystal, on my breadboard. I can use SDA
and SCL
pins with default arduino library wire.h
, and it works. But I want another pins to use I2C protocol.
I tried this library, even on real SDA
and SCL
pins, but it doesn't work.
I have pull up resistors, I tried with 10k and 4.7k without any success. (But those resistors worked with hardware I2C | TWI)
What am I doing wrong? (All connections is fine, because hardware TWI works but software does not.) Thank you for your help.
(Fuses are ok, because the Atmega won't respond if I remove the crystal oscillator, the LED blinking duration is ok.)
What i want to run (that doesn't work):
#define SDA_PORT PORTC
#define SDA_PIN 4
#define SCL_PORT PORTC
#define SCL_PIN 5
//#define I2C_TIMEOUT 100
#include <SoftI2CMaster.h>
uint8_t ADDR = 0x68;
void setup(void) {
Serial.begin(19200);
Serial.println("Initializing ...");
i2c_init();
if (!i2c_start(ADDR | I2C_WRITE)) Serial.println(F("Device does not respond"));
if (!i2c_write(0x00)) Serial.println(F("Cannot address reg 0"));
i2c_stop();
}
void loop (void) {
unsigned int low0, high0, low1, high1;
unsigned int chan0, chan1;
unsigned int lux;
int state1;
int state2;
delay(1000);
i2c_start(ADDR | I2C_WRITE);
i2c_write(0x00);
i2c_rep_start(ADDR | I2C_READ);
low0 = i2c_read(false);
high0 = i2c_read(false);
low1 = i2c_read(false);
high1 = i2c_read(true);
i2c_stop();
Serial.print(low0);
}
And the code with hardware TWI that works:
#include "Wire.h"
#define DS3231_I2C_ADDRESS 0x68
// Convert normal decimal numbers to binary coded decimal
byte decToBcd(byte val)
{
return( (val/10*16) + (val%10) );
}
// Convert binary coded decimal to normal decimal numbers
byte bcdToDec(byte val)
{
return( (val/16*10) + (val%16) );
}
void setup()
{
Wire.begin();
Serial.begin(19200);
pinMode(13, OUTPUT);
pinMode(5, INPUT);
}
void readDS3231time(byte *second,
byte *minute,
byte *hour,
byte *dayOfWeek,
byte *dayOfMonth,
byte *month,
byte *year)
{
Wire.beginTransmission(DS3231_I2C_ADDRESS);
Wire.write(0); // set DS3231 register pointer to 00h
Wire.endTransmission();
Wire.requestFrom(DS3231_I2C_ADDRESS, 7);
// request seven bytes of data from DS3231 starting from register 00h
*second = bcdToDec(Wire.read() & 0x7f);
*minute = bcdToDec(Wire.read());
*hour = bcdToDec(Wire.read() & 0x3f);
*dayOfWeek = bcdToDec(Wire.read());
*dayOfMonth = bcdToDec(Wire.read());
*month = bcdToDec(Wire.read());
*year = bcdToDec(Wire.read());
}
Schematic is the same as: https://www.arduino.cc/en/main/standalone.
Except instead of 16MHz, I use a 12MHz and ch210x serial.
Arduino's bootloader works well, I compiled it from source.
-
It would help if you posted code and schematics of both situations (hardware and software I2C attempts). Also, to debug I2C effectively, you'd need a logic analyzer or at very least an oscilloscope.Ricardo– Ricardo2015年07月20日 16:50:09 +00:00Commented Jul 20, 2015 at 16:50
-
What do you get on the serial output?Gerben– Gerben2015年07月20日 18:04:58 +00:00Commented Jul 20, 2015 at 18:04
-
Initializing ... Device does not respond Cannot address reg 0 SDA - SCL 0 0 Raw values: chan0=65535 SDA - SCL 0 0ONSC– ONSC2015年07月20日 18:34:01 +00:00Commented Jul 20, 2015 at 18:34
-
actually i should get the ohur:minute:second day/month/year that i can get in hardware twi,ONSC– ONSC2015年07月20日 18:35:25 +00:00Commented Jul 20, 2015 at 18:35
-
i ve removed some of code, just before he stop function , i get the pinstate to whats happening, so that output is showedONSC– ONSC2015年07月20日 19:18:57 +00:00Commented Jul 20, 2015 at 19:18
1 Answer 1
OK let me cry till morning...
i was having this issue during day, but the solution was sooo easy
i need to pay attention more ...
i2c_start(addr | R/W-bit) Initiates a transfer to the slave device with the (8-bit) I2C address addr.
what i was trying is to push 7 bit address.
i hope this might help some others.
Thank you for help.