#include <SPI.h>
//SRAM Status Register opcodes
#define BYTE_MODE B00000000 // 0 dec
#define PAGE_MODE B10000000 // 128 dec
#define SEQUENTIAL_MODE B01000000 // 64 dec
//SRAM Status Register opcodes disabling HOLD pin
#define BYTE_MODE_NO_HW_HOLD B00000001 // 1 dec
#define PAGE_MODE_NO_HW_HOLD B10000001 // 129 dec
#define SEQUENTIAL_MODE_NO_HW_HOLD B01000001 // 65 dec
//SRAM opcodes
#define READ_STATUS_REGISTER B00000101 // 5 dec
#define WRITE_STATUS_REGISTER B00000001 // 1 dec
#define READ B00000011 // 3 dec
#define WRITE B00000010 // 2 dec
#define ZERO ((uint8_t) 0x00) // 0 dec
#define MAX_VALUE ((uint8_t) 0xFF) // 255 dec
#define MAX_ADDR ((uint16_t) 0x7FFF) // 32767 dec
#define MAX_PAGE ((uint8_t) 0x0400) // 1024 dec
#define MAX_WORD_PER_PAGE ((uint8_t) 0x20) // 32 dec
#define SET_SS_LOW() (PORTB &= ~(1<<PORTB2)) //set SPI_SS low
#define SET_SS_HIGH() (PORTB |= (1<<PORTB2)) //set SPI_SS high
uint8_t SpiRAMReadStatusRegister() {
uint8_t read_status;
//SET_SS_LOW();
digitalWrite(53, LOW);
SPI.transfer(READ_STATUS_REGISTER);
read_status = SPI.transfer(0xFF);
//SET_SS_HIGH();
digitalWrite(53, HIGH);
return read_status;
}
void SpiRAMWriteStatusRegister(uint8_t command) {
digitalWrite(53, LOW);
//SET_SS_LOW();
SPI.transfer(WRITE_STATUS_REGISTER);
SPI.transfer(command);
//SET_SS_HIGH();
digitalWrite(53, HIGH);
}
byte SpiRAMReadByte(uint16_t address) {
Serial.println("*********** FASE DI LETTURA ***************");
byte read_byte;
digitalWrite(53, LOW);
SPI.transfer(READ);
SPI.transfer((char) (address >> 8));
SPI.transfer((char) address);
read_byte = SPI.transfer(0xFF);
Serial.print("INDIRIZZO DI LETTURA = ");
Serial.println(address, DEC);
delay(200);
Serial.print("VALORE LETTO = ");
Serial.println(read_byte, DEC);
digitalWrite(53, HIGH);
return read_byte;
}
void SpiRAMWriteByte(uint16_t address, byte data_byte) {
Serial.println("*********** FASE DI SCRITTURA ***************");
digitalWrite(53, LOW);
SPI.transfer(WRITE);
SPI.transfer((char) (address >> 8));
SPI.transfer((char) address);
SPI.transfer(data_byte);
Serial.print("INDIRIZZO DI SCRITTURA = ");
Serial.println(address, DEC);
delay(200);
Serial.print("DATO SCRITTO = ");
Serial.println(data_byte, DEC);
digitalWrite(53, HIGH);
}
byte x = 1;
void setup() {
Serial.begin(9600);
SPI.begin();
pinMode(53, OUTPUT); // pin CS
SpiRAMWriteStatusRegister(BYTE_MODE);
if(SpiRAMReadStatusRegister() != BYTE_MODE) {
Serial.println("Errory while setting status register (byte operative mode)!");
}
else {
Serial.println("Byte operative mode correctly set!");
}
}
void loop() {
SpiRAMWriteByte(0, x);
delay(2000);
if(SpiRAMReadByte(0) <= 10) {
delay(8000);
x = x + 1;
}
else {
Serial.println("FINITO");
}
//delay(10);
Hello everyone and good Easter
I wanted to ask you a question about RAM memory 23k256.
I wrote the code I entered.
I wrote it that way because I wanted to check if I could overwrite the value of a fixed RAM lease and then increase it until I had interest, obviously increasing it to a maximum number that can be expressed with 8 bits. I say this last thing because I use writing and reading a byte.
What I ask you is because in images 1 and 2 attached I get those results circled in red.
Until the writing of the fourth value all ok then I write the value 5 but I read the value 6, then I write the value 9 but read the 10, and here should have appeared the finished inscription, instead it appears when I write the 10 and read the 12.
Can you help me?
Thank you.