stevemarple / SoftWire

Software I2C implementation for Arduino and other Wiring-type environments
GNU Lesser General Public License v2.1
136 stars 31 forks source link

I2C EEPROM example not working correctly (Wire functions used) #14

Closed vitmailru closed 3 years ago

vitmailru commented 4 years ago

Wire.read() always returns 255 With Wire library example works correctly

`#include

include

SoftWire Wire(SDA, SCL);

define disk1 0x50 //Address of 24C32 eeprom chip

void setup(void) { Serial.begin(9600); while (!Serial) { ; // wait for serial port to connect. Needed for native USB port only } Serial.println("Wire"); Wire.begin();

unsigned int address = 0; Serial.println("writeEEPROM"); writeEEPROM(disk1, address, 123); Serial.println("readEEPROM"); Serial.println(readEEPROM(disk1, address), DEC); Serial.println("end"); }

void loop(){}

void writeEEPROM(int deviceaddress, unsigned int eeaddress, byte data ) { Wire.beginTransmission(deviceaddress); Wire.write((int)(eeaddress >> 8)); // MSB Wire.write((int)(eeaddress & 0xFF)); // LSB Wire.write(data); Wire.endTransmission();

delay(5); }

byte readEEPROM(int deviceaddress, unsigned int eeaddress ) { byte rdata = 0xFF;

Wire.beginTransmission(deviceaddress); Wire.write((int)(eeaddress >> 8)); // MSB Wire.write((int)(eeaddress & 0xFF)); // LSB Wire.endTransmission();

Wire.requestFrom(deviceaddress,1);

if (Wire.available()) rdata = Wire.read();

return rdata; }`

stevemarple commented 3 years ago

You must allocate TX and RX buffers and pass them into SoftWire by calling the setTxBuffer() and setRxBuffer() functions. This is shown in the newly-added ReadDS1307 example.