Using Microchip 25LC080 serial eeprom with an Arduino
I am working on a data logger for the wii nunchuck. I need something to record the accelerometer values that get output from the nunchuck. I had a Microchip 25LC080 eeprom handy and decided to hook it up to my Arduino.
The Arduino site has a great tutorial on working with serial eeproms http://www.arduino.cc/en/Tutorial/SPIEEPROM . I used it to get most of my information. The eeprom I am using is a just little different than the one in the tutorial.
My plan is to have the Arduino sample the accelerometer data, then store it in the eeprom for later retrieval. That could be used to measure all sorts of fun things like acceleration on a swing set, up an elevator, in your car, etc..
The 25LC080 only holds 1024bytes. The nunchuck puts out 3 bytes of data (1 for each axis), so I can only hold 333 samples. I ordered a 25AA512 which hold 64000 bytes. It is a drop in replacement.
Here is my code:
// Original program code by Heather Dewey-Hagborg, http://www.arduino.cc/en/Tutorial/SPIEEPROM
// with slight modifications to work with 25cl0l by Chad Phillips
#define DATAOUT 11 //MOSI
#define DATAIN 12 //MISO
#define SPICLOCK 13 //sck
#define SLAVESELECT 10 //ss
//opcodes
#define WREN 6
#define WRDI 4
#define RDSR 5
#define WRSR 1
#define READ 3
#define WRITE 2
byte eeprom_output_data;
byte eeprom_input_data = 0;
byte clr;
int address = 0;
//data buffer
char buffer[128];
void
fill_buffer ()
{
int a = 0;
for (int I = 0; I < 128; I++)
{
buffer[a] = I;
a++;
}
}
char
spi_transfer (volatile char data)
{
SPDR = data; // Start the transmission
while (!(SPSR & (1 << SPIF))) // Wait the end of the transmission
{
};
return SPDR; // return the received byte
}
void
setup ()
{
Serial.begin (19200);
pinMode (DATAOUT, OUTPUT);
pinMode (DATAIN, INPUT);
pinMode (SPICLOCK, OUTPUT);
pinMode (SLAVESELECT, OUTPUT);
digitalWrite (SLAVESELECT, HIGH); //disable device
// SPCR = 01010000
//interrupt disabled,spi enabled,msb 1st,master,clk low when idle,
//sample on leading edge of clk,system clock/4 rate (fastest)
SPCR = (1 << SPE) | (1 << MSTR);
clr = SPSR;
clr = SPDR;
delay (10);
//fill buffer with data, that will be used to write our test data to eeprom
fill_buffer ();
for (int I = 0; I < 128; I++)
{
start_transfer (buffer[I], I);
delay (50);
}
delay (3000);
Serial.print ("All data sent to EEPROM");
Serial.println ();
Serial.print ("Start reading EEPROM");
Serial.println ();
delay (1000);
}
void
start_transfer (char data, int address)
{
Serial.print ("Write value, ");
Serial.print (data, DEC);
Serial.print (" , to address ");
Serial.print (address, DEC);
Serial.println ();
digitalWrite (SLAVESELECT, LOW);
spi_transfer (WREN); //write enable
digitalWrite (SLAVESELECT, HIGH);
delay (10);
digitalWrite (SLAVESELECT, LOW);
spi_transfer (WRITE); //write instruction
spi_transfer ((char) (address >> 8)); //send MSByte address first
spi_transfer ((char) (address)); //send LSByte address
// start spi transfer
spi_transfer (data); //write data byte
digitalWrite (SLAVESELECT, HIGH); //release chip
}
byte
read_eeprom (int EEPROM_address)
{
//READ EEPROM
int data;
digitalWrite (SLAVESELECT, LOW);
spi_transfer (READ); //transmit read opcode
spi_transfer ((char) (EEPROM_address >> 8)); //send MSByte address first
spi_transfer ((char) (EEPROM_address)); //send LSByte address
data = spi_transfer (0xFF); //get data byte
digitalWrite (SLAVESELECT, HIGH); //release chip, signal end transfer
return data;
}
void
loop ()
{
eeprom_output_data = read_eeprom (address);
Serial.print ("EEPROM address ");
Serial.print (address, DEC);
Serial.print (" contains value ");
Serial.print (eeprom_output_data, DEC);
Serial.println ();
address++;
// This eeprom only holds 1024 bytes
// Set back to zero when we hit the end
// In this example we only initialed the
// first 128 bytes. If this is a new eeprom, bytes 129 to 1024 should contain 255
if (address > 1023)
{
address = 0;
Serial.print (" reset address to 0\n");
}
delay (20); //pause for readability
}
Comments
billigflüge:
Using Microchip 25LC080 serial eeprom with an Arduino - Thanks, very great article.
Zoran:
Awesome! thanks for the code.


Anonymous66:
film indir
film izle SpooExi
film izle
dizi izle
online mp3
Post new comment