I'm trying to write to external eeprom using the extEEPROM library. In the eeWRITE function I cannot get the last three bytes to write, the following is what I get on the monitor
Writing...
data[0]@write = 1100101
data[1]@write = 0
data[2]@write = 0
data[3]@write = 0
writing[0] = 1100101
writing[1] = 0
writing[2] = 0
writing[3] = 0
totalCOUNTS @ write = 234597
I have exhausted my knowledge base and would appreciate your help
Attached is the complete code.
Thanks
Bill
//Test extEEPROM library.
//Writes the EEPROM full of 32-bit integers and reads them back to verify.
//Wire a button from digital pin 6 to ground, this is used as a start button
//so the sketch doesn't do unnecessary EEPROM writes every time it's reset.
//Jack Christensen 09Jul2014
//Paolo Paolucci 17Mar2016 (fix 28Jun2017)
//2/2/2018 - Modified by WJL for testing
#include <extEEPROM.h> //https://github.com/PaoloP74/extEEPROM
//One 24LC256 EEPROMs on the bus
const uint32_t totalKBytes = 32; //for read and write test functions
extEEPROM eep(kbits_256, 1, 64, 0x50); //device size, number of devices, page size
uint8_t data[4];
int RAIN = 2;
int RAIN_C; //Rain counts stored in EEPROM and sent to xbee
int COUNT;
int lastCOUNT;
uint32_t totalCOUNTS;
int reset_rain = 9;
int reset_rain_counter;
uint8_t btst[4];
uint32_t addr;
void setup(void)
{
Serial.begin(115200);
uint8_t eepStatus = eep.begin(eep.twiClock400kHz); //go fast!
pinMode(RAIN , INPUT_PULLUP);
pinMode(reset_rain , INPUT_PULLUP);
if (eepStatus)
{
Serial.print(F("extEEPROM.begin() failed, status = "));
Serial.println(eepStatus);
while (1);
}
Serial.println("FILE NAME = WJL_eepromTest");
eeRead();
Serial.print("totalCOUNTS[]@setup = ");Serial.println(totalCOUNTS);
}
void loop(void)
{
//Serial.println("STARTING");
reset_rain_counter = digitalRead(reset_rain);
if (reset_rain_counter == LOW)
{
Serial.println(reset_rain_counter);
reset_counter();
}
COUNT = digitalRead(RAIN);
if (COUNT != lastCOUNT)
{
if (COUNT == LOW)
{
totalCOUNTS = totalCOUNTS + 1;
Serial.print("TOTAL COUNTS@COUNT = ");Serial.println(totalCOUNTS);
eeWrite();
delay(200);
}
else
{
}
delay(50);
}
lastCOUNT = COUNT;
}
//write test data (32-bit integers) to eeprom, "chunk" bytes at a time
void eeWrite()
{
Serial.println(F("Writing..."));
data[0] = (totalCOUNTS & 0xFF);
Serial.print("data[0]@write = ");Serial.println(data[0],BIN);
data[1] = (totalCOUNTS & 0xFF >> 8);
Serial.print("data[1]@write = ");Serial.println(data[1],BIN);
data[2] = (totalCOUNTS & 0xFF >> 16);
Serial.print("data[2]@write = ");Serial.println(data[2],BIN);
data[3] = (totalCOUNTS & 0xFF >> 24);
Serial.print("data[3]@write = ");Serial.println(data[3],BIN);
eep.write(0, data[0], 1);
Serial.print("writing[0] = ");Serial.println(data[0],BIN);
eep.write(1, data[1], 1);
Serial.print("writing[1] = ");Serial.println(data[1],BIN);
eep.write(2, data[2], 1);
Serial.print("writing[2] = ");Serial.println(data[2],BIN);
eep.write(3, data[3], 1);
Serial.print("writing[3] = ");Serial.println(data[3],BIN);
Serial.print("totalCOUNTS @ write = ");Serial.println(totalCOUNTS);
}
//read test data (32-bit integers) from eeprom, "chunk" bytes at a time
void eeRead()
{
Serial.println(F("Reading..."));
for(addr = 0;addr < 4; addr++)
{
eep.read(addr, btst, 4);
}
Serial.print("btst([0] = ");Serial.println(btst[0],BIN);
Serial.print("btst([1] = ");Serial.println(btst[1],BIN);
Serial.print("btst([2] = ");Serial.println(btst[2],BIN);
Serial.print("btst([3] = ");Serial.println(btst[3],BIN);
totalCOUNTS = uint32_t(btst[0]) | uint32_t(btst[1] << 8) | uint32_t(btst[2] << 16) | uint32_t (btst[3] <<24);
Serial.print("totalCOUNTS@read = ");Serial.println(totalCOUNTS);
}
void reset_counter()
{
Serial.println("AT RESET COUNTER");
totalCOUNTS = 234567;
Serial.print("totalCOUNTS = ");Serial.println(totalCOUNTS,BIN);
eeWrite();
delay(2000);
}