I am trying to work with an eeprom, CAT25160. Here is the link to its datasheet for reference.
For now, I am working on a simple test project to right 5 bytes to the eprom, and then read them back. I write the following, or at least I think I am.
Address Value
0 10
1 11
2 12
3 13
4 14
I write them one at a time, with a 5 second delay in between each write to make sure that the writes have time to complete.
In the read routine, whatever value I pass to the read routine seems to come back to me sometimes, but not sure why? I am not sure if the write or read routine is wrong, but could use some help.
This is the output of the program, and I will paste the complete source below. Any help is appreciated.
Writing 0
Writing 1
Writing 2
Writing 3
Writing 4
Address 0
Value 0
Address 1
Value 85
Address 2
Value 85
Address 3
Value 85
Address 4
Value 255
Thank you,
Tim
#include <SPI.h>
#define DATAOUT 11//MOSI
#define DATAIN 12//MISO
#define SPICLOCK 13//sck
#define SLAVESELECT 10//ss
//for chip CAT25160
const byte WRITE_ENABLE = 0b00000110; // enable write operations
const byte WRITE_DISABLE = 0b00000100; // disable write operations
const byte READ_STATUS_REGISTER = 0b00000101; //read status register
const byte WRITE_STATUS_REGISTER = 0b00000001; //write status register
const byte READ = 0b00000011; // read command
const byte WRITE = 0b00000010; // write command
byte eeprom_output_data;
byte eeprom_input_data=0;
int address=0;
int I;
int value;
void setup() {
// set the slaveSelectPin as an output:
pinMode (SLAVESELECT, OUTPUT);
// initialize SPI:
SPI.begin();
Serial.begin(9600);
digitalWrite(SLAVESELECT,LOW);
// send in the address and value via SPI:
SPI.transfer(WRITE_ENABLE);
// take the SS pin high to de-select the chip:
digitalWrite(SLAVESELECT,HIGH);
for (int counter = 0; counter < 5; counter++) {
address = counter;
value = counter+10;
write_eeprom(address, value);
// 'short delay to give write time to finish
delay(5000);
Serial.print("Writing ");
Serial.println(address,DEC);
}
}
void loop() {
for (int counter = 0; counter < 5; counter++) {
read_eeprom(counter);
delay(1000);
}
//delay so numbers are flying by over and over too much
delay(20000);
}
int write_eeprom(int address, int value) {
// take the SS pin low to select the chip:
digitalWrite(SLAVESELECT,LOW);
// send in the address and value via SPI:
SPI.transfer(WRITE);
SPI.transfer(address);
SPI.transfer(value);
// take the SS pin high to de-select the chip:
digitalWrite(SLAVESELECT,HIGH);
}
int read_eeprom(int address) {
// take the SS pin low to select the chip:
byte retValue;
digitalWrite(SLAVESELECT,LOW);
// send in the address and value via SPI:
SPI.transfer(READ);
SPI.transfer(address);
retValue = SPI.transfer(0x55);
Serial.print("Address ");
Serial.println(address,DEC);
Serial.print("Value ");
Serial.println(retValue, DEC);
// take the SS pin high to de-select the chip:
digitalWrite(SLAVESELECT,HIGH);
return retValue;
}