Don't confuse commands used for the internal EEPROM in the Arduino. An AT24C32 is an external device and uses I2C to communicate with. You might want to look at the extEEPROM library by Jack Christensen.
Code: [Select]
mem.write(12, 42);
what is the starting and ending address in this EEPROM i am not sure.
The conceptual view for the memory location, location address, and location content of a AT24C32 EEPROM are depicted in the above figure.
The command (taken from AT24CX.h Library) to write a data byte (say, 42 = 0x2A) into an EEPROM location (aka location address = locAdr) (say, 12 = 000C):
mem.write(locAdr, dataByte)
==> mem.write(12, 42); //one byte enters into one memory location;
==> mem.write(0x000C, 0x2A);
The following example file taken from the same header file contains all possible commands:
/*
*
* Read and write demo of the AT24CX library
* Written by Christian Paul, 2014-11-24
*
*
*/
// include libraries
#include <Wire.h>
#include <AT24CX.h>
// EEPROM object
AT24CX mem;
// setup
void setup() {
// serial init
Serial.begin(115200);
Serial.println("AT24CX read/write demo");
Serial.println("----------------------");
}
// main loop
void loop() {
// read and write byte
Serial.println("Write 42 to address 12");
mem.write(12, 42);
Serial.println("Read byte from address 12 ...");
byte b = mem.read(12);
Serial.print("... read: ");
Serial.println(b, DEC);
Serial.println();
// read and write integer
Serial.println("Write 65000 to address 15");
mem.writeInt(15, 65000);
Serial.println("Read integer from address 15 ...");
unsigned int i = mem.readInt(15);
Serial.print("... read: ");
Serial.println(i, DEC);
Serial.println();
// read and write long
Serial.println("Write 3293732729 to address 20");
mem.writeLong(20, 3293732729UL);
Serial.println("Read long from address 20 ...");
unsigned long l = mem.readLong(20);
Serial.print("... read: ");
Serial.println(l, DEC);
Serial.println();
// read and write long
Serial.println("Write 1111111111 to address 31");
mem.writeLong(31, 1111111111);
Serial.println("Read long from address 31 ...");
unsigned long l2 = mem.readLong(31);
Serial.print("... read: ");
Serial.println(l2, DEC);
Serial.println();
// read and write float
Serial.println("Write 3.14 to address 40");
mem.writeFloat(40, 3.14);
Serial.println("Read float from address 40 ...");
float f = mem.readFloat(40);
Serial.print("... read: ");
Serial.println(f, DEC);
Serial.println();
// read and write double
Serial.println("Write 3.14159265359 to address 50");
mem.writeDouble(50, 3.14159265359);
Serial.println("Read double from address 50 ...");
double d = mem.readDouble(50);
Serial.print("... read: ");
Serial.println(d, DEC);
Serial.println();
// read and write char
Serial.print("Write chars: '");
char msg[] = "This is a message";
Serial.print(msg);
Serial.println("' to address 200");
mem.writeChars(200, msg, sizeof(msg));
Serial.println("Read chars from address 200 ...");
char msg2[30];
mem.readChars(200, msg2, sizeof(msg2));
Serial.print("... read: '");
Serial.print(msg2);
Serial.println("'");
Serial.println();
// write array of bytes
Serial.println("Write array of 80 bytes at address 1000");
byte xy[] = {0,0,0,1,1,1,2,2,2,3,3,3,4,4,4,5,5,5,6,6,6,7,7,7,8,8,8,9,9,9, // 10 x 3 = 30
10,11,12,13,14,15,16,17,18,19, // 10
120,121,122,123,124,125,126,127,128,129, // 10
130,131,132,133,134,135,136,137,138,139, // 10
200,201,202,203,204,205,206,207,208,209,210,211,212,213,214,215,216,217,218,219}; // 20
mem.write(1000, (byte*)xy, sizeof(xy));
// read bytes with multiple steps
Serial.println("Read 80 single bytes starting at address 1000");
for (int i=0; i<sizeof(xy); i++) {
byte sb = mem.read(1000+i);
Serial.print("[");
Serial.print(1000+i);
Serial.print("] = ");
Serial.println(sb);
}
Serial.println();
// read bytes with one step
Serial.println("Read 80 bytes with one operation at address 1000");
byte z[80];
memset(&z[0], 32, sizeof(z));
mem.read(1000, z, sizeof(z));
for (int i=0; i<sizeof(z); i++) {
Serial.print("[");
Serial.print(1000+i);
Serial.print("] = ");
Serial.println(z[i]);
}
// stop
while (1==1) {}
}
Data Logger Tutorial using: UNO + BMP180 + DS1307 + 24C32 + SM A: System in Block Diagram
B: Operating Procedures (1) Altitude is computed in every 5-sec interval. (2) The altitude is shown on Serial Monitor at every 5-sec interval with Day, Date, and Time Stamp. (3) The altitude is recorded in EEPROM at every 5-sec interval with Day, Date, and Time Stamp. (4) When K1 button (Stop Logging) is held down for more than 5-sec, the logging is stopped. The very 1st sample of the logged data is read back from EEPROM and is shown on the Serial Monitor.
C: Complete Program Codes (Extensive help and references are taken from IDE Examples)
#include <SFE_BMP180.h>
#include <Wire.h>
#include "RTClib.h"
#include <AT24CX.h>
SFE_BMP180 pressure;
AT24CX mem;
RTC_DS1307 rtc;
char msg[100];
unsigned int x;
char daysOfTheWeek[7][12] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
unsigned int locAdr = 0x0200;
#define ALTITUDE 4.0//Dhaka 1655.0 // Altitude of SparkFun's HQ in Boulder, CO. in meters
String string = "";
void setup()
{
Serial.begin(9600);
pinMode(8, INPUT_PULLUP);
if (pressure.begin()) //calibration values are read from onboard eeprom of sensor
Serial.println("BMP180 init success");
else
{
Serial.println("BMP180 init fail\n\n");
while(1); // Pause forever.
}
if (! rtc.begin())
{
Serial.println("Couldn't find RTC");
while (1);
}
if (! rtc.isrunning())
{
Serial.println("RTC is NOT running!");
}
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));//auto update from computer time
//rtc.adjust(DateTime(2014, 1, 21, 3, 10, 17));// to set the time manualy
}
void loop()
{
dayDateTime();
computedAltitude();
if(digitalRead(8) == LOW)
{
Serial.println();
Serial.println("OK! Logging is Terminated!! Reading Back EEPROM's Data!!!");
locAdr = 0x0200;
mem.readChars(locAdr, msg, x);
Serial.println(msg);
HERE: goto HERE;
}
logToEeprom();
delay(5000);
}
void computedAltitude()
{
char status;
double T,P,p0,a;
status = pressure.startTemperature();
if (status != 0)
{
// Wait for the measurement to complete:
delay(status);
status = pressure.getTemperature(T);
if (status != 0)
{
status = pressure.startPressure(3);
if (status != 0)
{
// Wait for the measurement to complete:
delay(status);
status = pressure.getPressure(P,T);
if (status != 0)
{
p0 = pressure.sealevel(P,ALTITUDE); // we're at 1655 meters (Boulder, CO)
a = pressure.altitude(P,p0);
string += " ";
string += "Computed Altitude: ";
string += a;
string += " meters";
Serial.println(string);
x = string.length();
string.getBytes(msg, x+1);
string = "";
}
else
Serial.println("error retrieving pressure measurement\n");
}
else
Serial.println("error starting pressure measurement\n");
}
else
Serial.println("error retrieving temperature measurement\n");
}
else
Serial.println("error starting temperature measurement\n");
}
void dayDateTime()
{
DateTime nowTime = rtc.now();//now() reads current Date and Time from RTC
string += daysOfTheWeek[nowTime.dayOfTheWeek()];
string += " : ";
string += nowTime.day();
string += "/";
string += nowTime.month();
string += "/";
string += nowTime.year();
string += " ";
string += nowTime.hour();
string += " : ";
string += nowTime.minute();
string += " : ";
string += nowTime.second();
}
void logToEeprom()
{
mem.writeChars(locAdr, msg, x+1); //extra 1 is to count the null character (x is without null character)
locAdr = locAdr+x;
}
That's an interesting attitude to memory conservation.
This is not mine; it belongs to IDE's Example.
But then, you're also using String, so, meh.
Yes! There have been a lot of complains from the Forum about the use of String object (one auto complain will come soon from @Robin2); but, the Arduino Reference Manual supports its use!!
So why bother reposting it here, if you know it is sub-optimal?
There have been a lot of complains from the Forum about the use of String object (one auto complain will come soon from @Robin2); but, the Arduino Reference Manual supports its use!!
My car speedometer goes to waaaay over the national speed limit, but it doesn't mean to say I have to use all of it.
So why bother reposting it here, if you know it is sub-optimal?
I have used the resource (char daysOfTheWeek[7][12]) for my purposes without noticing its special feature of memory conservation. I have to declare my position as to whom this property belongs so that I don't confuse others to offer me something which I don't deserve.
My car speedometer goes to waaaay over the national speed limit, but it doesn't mean to say I have to use all of it.
In this practical world, we have to (indeed we do) live with safety hazards; but, we are safe (does not mean protected) as long as we are aware about these hazards which could explode!
I have used the resource (char daysOfTheWeek[7][12]) for my purposes without noticing its special feature of memory conservation
I read that as " I can't count past four".
as long as we are aware about these hazards which could explode!
I've seen a very small old petrol engine push a rod through the block, but can't imagine a modwrn two litre turbo diesel blowing up. I should try harder.
I have to declare my position as to whom this property belongs so that I don't confuse others to offer me something which I don't deserve.
That's an interesting attitude to memory conservation.
Will you please, explain with example in what way the above declaration saves memory space. And also, why does the declaration contain [7][12] instead of [7][4]?
GolamMostafa:
Will you please, explain with example in what the above declaration saves memory space. And also, why does the declaration contain [7][12] instead of [7][4]?
Sarcasm hasn't yet made it to your corner of the world, I take it.