Go Down

Topic: Struct data with i2cEEPROMAny problem (Read 460 times) previous topic - next topic

pescadito

Hi,

I connected a 24LC64 to a Nano V3.0 and use Arduino IDE 1.01, as say in http://www.arduino.cc/playground/Code/I2CEEPROM, and all run very well!!

But i have problems addapting the code for struct datatype, like apexlogic.net/code-bank/arduino/eeprom-i2c-write-anything/

My code compile and run but it stop the run when i de-comment the 3 lines of the included file.
Could anybody help me??

Code: [Select]

//-----------------------------------------------------------------------------------------------
//
// Example of i2cEEPROMAny
//
#include <Wire.h>
#include <i2cEEPROMAny.h>

struct config_t {
   long targetLat;
   long targetLon;
   float fNum;
   bool first;
   int attempts;
} config;

void setup()
{
 config.targetLat = 4957127;
 config.targetLon = 6743421;
 config.first = false;
 config.attempts = 30;
 config.fNum = 2.23;
 
 Serial.begin(9600);
 Serial.println("");
 Serial.println("Wrinting...");

 i2cEepromWrite(0,config);
 delay(1000);
 Serial.println("Writed.");

 Serial.println("");
}

void loop()
{
   config.targetLat =0;
   config.targetLon = 0;
   config.first = true;
   config.fNum = 0.0;
   config.attempts = 0;

   Serial.println("Reading...");
   i2cEepromRead(0,config);
   delay(1000);
   Serial.println("Readed.");

   Serial.println("");
   Serial.print("lat = ");
   Serial.println(config.targetLat);
   Serial.print("lon =");
   Serial.println(config.targetLon);
   Serial.print("first");
   Serial.println(config.first);
   Serial.print("float");
   Serial.println(config.fNum);
   Serial.print("attempts = ");
   Serial.println(config.attempts);

   delay(100000);
}

//--------------------------------------------------------------------
//
// i2cEEPROMAny.h
//
#include <Arduino.h>
#define DEVICE 0x50

template <class T> int i2cEepromWrite(int ee, const T& value)
{
const byte* p = (const byte*)(const void*)&value;
int i;
Serial.begin(9600);
Wire.beginTransmission(DEVICE);
Wire.write((int)(ee >> 8)); // MSB
Wire.write((int)(ee & 0xFF)); // LSB
Serial.print("LSB Writed ");
for (i = 0; i < sizeof(value); i++)
{
  Wire.write(*p++);
          delay(200);
          Serial.print(".");
}
      Serial.println(i);
      Serial.print("Wire.endTransmittion... ");
      delay(1000);
//    Wire.endTransmission();        // run stop here if uncomment this line
      delay(1000);
      Serial.println("Done. ");
      return i;
}

template <class T> int i2cEepromRead(int ee, T& value)
{
byte* p = (byte*)(void*)&value;
int i;
Wire.beginTransmission(DEVICE);
Wire.write((int)(ee >> 8)); // MSB
Wire.write((int)(ee & 0xFF)); // LSB
       Serial.print("Wire.endTransmission... ");
       delay(1000);
// Wire.endTransmission();    // run stop here if uncomment this line
       delay(1000);
       Serial.println("Done. ");
       delay(1000);
Serial.print("Wire.requestedFrom... ");
// Wire.requestFrom(DEVICE,sizeof(value));   // run stop here if uncomment this line
Serial.println("Done.");
Serial.print("Wire.read ");
       delay(1000);
for (i = 0; i < sizeof(value); i++)
{
 if(Wire.available()) {
   *p++ = Wire.read();
   delay(200);
   Serial.print(".");
 }
   }
   Serial.print(i);
   Serial.println(" Done.");
   return i;
}
//-----------------------------------------------------------------------------------------------


PaulS

The apexlogic site is blocked at work, so I can't see that code.

Quote
but it stop the run when i de-comment the 3 lines of the included file.

If the file you want us to see is only three lines, why didn't you just post it?

Quote
My code compile and run but it stop the run

Prove that, by showing your output.

Go Up