Yah, it seems to be working fine, except every time it gets powered on it will read 255 for all the values in the for loop in the setup function. Then it continues to the loop function and works fine. I have a potentiometer on the analog pin to change the values that are written to make sure the values are actually changing when getting written. When I power it off and back on I would expect those values that were written previously to be read out right away, but that's not happening.
// Written by Nick Gammon
// 10th March 2011
#include <SPI.h>
#define CHIP_SELECT 10 // for EEPROM
// AT25DF041A EEPROM commands
// reading
#define ReadArray 0x0B
#define ReadArrayLowFrequency 0x03
// programming
#define BlockErase4Kb 0x20
#define BlockErase32Kb 0x52
#define BlockErase64Kb 0xD8
#define ChipErase 0x60
#define ByteProgram 0x02
#define SequentialProgram 0xAD
// protection
#define WriteEnable 0x06
#define WriteDisable 0x04
#define ProtectSector 0x36
#define UnProtectSector 0x39
#define ReadSectorProtection 0x3C
// status
#define ReadStatus 0x05
#define WriteStatus 0x01
// miscellaneous
#define ReadManufacturer 0x9F
#define DeepPowerDown 0xB9
#define ResumeFromPowerDown 0xAB
int address = 0;
int analogInPin = A6;
int sensorValue = 0;
int reading;
// wait until chip not busy
void notBusy ()
{
digitalWrite (CHIP_SELECT, LOW);
SPI.transfer (ReadStatus);
// wait until busy bit cleared
while (SPI.transfer (0) & 1)
{}
digitalWrite (CHIP_SELECT, HIGH);
} // end notBusy
// enable writing
void writeEnable ()
{
notBusy ();
digitalWrite (CHIP_SELECT, LOW);
SPI.transfer (WriteEnable);
digitalWrite (CHIP_SELECT, HIGH);
} // end of writeEnable
// read device status
byte readStatus (void)
{
digitalWrite (CHIP_SELECT, LOW);
SPI.transfer (ReadStatus);
byte status = SPI.transfer (status);
digitalWrite (CHIP_SELECT, HIGH);
return status;
} // end of readStatus
// write status register
void writeStatus (const byte status)
{
writeEnable ();
notBusy (); // wait until ready
digitalWrite (CHIP_SELECT, LOW);
SPI.transfer (WriteStatus);
SPI.transfer (status);
digitalWrite (CHIP_SELECT, HIGH);
} // end of writeStatus
// send a command to the EEPROM followed by a 3-byte address
void sendCommandAndAddress (const byte command, const unsigned long addr)
{
SPI.transfer (command);
SPI.transfer ((addr >> 16) & 0xFF);
SPI.transfer ((addr >> 8) & 0xFF);
SPI.transfer (addr & 0xFF);
} // end of sendCommandAndAddress
// write len (max 256) bytes to device
// Note that if writing multiple bytes the address plus
// length must not cross a 256-byte boundary or it will "wrap"
void writeEEPROM (const unsigned long addr, byte * data, byte len)
{
// now write to it
writeEnable ();
notBusy (); // wait until ready
digitalWrite (CHIP_SELECT, LOW);
sendCommandAndAddress (ByteProgram, addr);
for ( ; len ; --len)
SPI.transfer (*data++);
digitalWrite (CHIP_SELECT, HIGH);
notBusy ();
} // end of writeEEPROM
// write one byte to device
void writeEEPROM (unsigned long addr, byte data)
{
writeEEPROM (addr, &data, 1);
} // end of writeEEPROM
// read len bytes from device
void readEEPROM (const unsigned long addr, int * data, unsigned int len)
{
notBusy (); // wait until ready
digitalWrite (CHIP_SELECT, LOW);
sendCommandAndAddress (ReadArray, addr);
SPI.transfer (0); // clock in "don't care" byte
for ( ; len ; --len)
*data++ = SPI.transfer (0);
digitalWrite (CHIP_SELECT, HIGH);
} // end of readEEPROM
// erase a 4Kb block of bytes which contains addr
void eraseEEPROM (const unsigned long addr)
{
writeEnable ();
notBusy (); // wait until ready
digitalWrite (CHIP_SELECT, LOW);
sendCommandAndAddress (BlockErase4Kb, addr);
digitalWrite (CHIP_SELECT, HIGH);
} // end of eraseEEPROM
// show device info and status
void info ()
{
notBusy (); // wait until ready
digitalWrite (CHIP_SELECT, LOW);
SPI.transfer (ReadManufacturer);
Serial.print ("Manufacturer: ");
Serial.println (SPI.transfer (0), HEX);
Serial.print ("Device ID Part 1: ");
Serial.println (SPI.transfer (0), HEX);
Serial.print ("Device ID Part 2: ");
Serial.println (SPI.transfer (0), HEX);
Serial.print ("Extended Information Length: ");
Serial.println (SPI.transfer (0),HEX);
digitalWrite (CHIP_SELECT, HIGH);
Serial.print ("Status: ");
Serial.println (readStatus (), HEX);
} // end of info
void setup ()
{
Serial.begin (115200);
SPI.begin ();
// global unprotect
writeStatus (0);
Serial.print('\n',BYTE);
Serial.println ("Status:");
info ();
for (int I=0;I<100;I=I+2)
{
readEEPROM (I, &reading, sizeof reading);
Serial.println (reading);
}
} // end of setup
void loop ()
{
eraseEEPROM (address);
sensorValue = analogRead (analogInPin);
writeEEPROM (address, (byte *) &sensorValue, sizeof sensorValue);
readEEPROM (address, &reading, sizeof reading);
Serial.println (reading);
address += sizeof sensorValue;
} // end of loop