Problem with 23LC1024 RAM memory

Status
Not open for further replies.

Mr.Bean

New member
Hello friends! :D

I'm finishing a personal project but I need more RAM because I need many large temporary variables. I investigated and discovered the chip 23LC1024 so I bought it.

I tested the chip with an Arduino UNO and worked perfectly . But when I tried the chip with Teensy and it did not work. :(
The error is the below
Sin títuFFlo.jpg
Teensy always reads the value 255 for the "for" loop. I am using Teensy 3.2 at 72 Mhz.
Obviously, for the Teensy , I set the " SPI.beginTransaction " function according to this page: https://www.pjrc.com/teensy/td_libs_SPI.html

The code I used to Teensy is as follows:

Code:
#include <SPI.h>

#define SS 10
 
//Configuración SRAM 
#define RDSR        5
#define WRSR        1
#define READ        3
#define WRITE       2
  
//Byte transfer functions

byte READ_RAM(uint32_t address) {
  byte read_byte;
 
  digitalWrite(SS, LOW); // Enables SPI communication
  SPI.transfer(READ);

  SPI.transfer((byte)(address >> 16) & 0xff); 
  SPI.transfer((byte)(address >> 8) & 0xff);  // 0xff = 255 = 11111111
  SPI.transfer((byte)address);               
  read_byte = SPI.transfer(0x00);
  digitalWrite(SS, HIGH); // Disable the SPI communication
  return read_byte;
}
  
void WRITE_RAM(uint32_t address, uint32_t data_byte) {
  
  digitalWrite(SS, LOW); // Enables SPI communication
  SPI.transfer(WRITE);  // We put the RAM in write mode
  /*
  Serial.println(address,BIN);
  Serial.println(0xff,BIN);
  Serial.println(((byte)address>>16) & 0xff);
  */

  SPI.transfer((byte)(address >> 16) & 0xff);   // Select the direction
  SPI.transfer((byte)(address >> 8) & 0xff);    // in RAM where the data will be saved
  SPI.transfer((byte)address);
  SPI.transfer(data_byte);   // We send the data to the RAM
  digitalWrite(SS, HIGH); //  Disable the SPI communication
}



void setup(void) {
  uint32_t i;
  byte value;
 
  Serial.begin(9600);
  SPI.begin();
  
   
SPI.beginTransaction(SPISettings (20000000, MSBFIRST, SPI_MODE0));

delay(1000);
Serial.println(1);
delay(1000);
Serial.println(2);
delay(1000);
Serial.println(3);
delay(1000);
Serial.println(4);
delay(1000);
Serial.println(5);
delay(1000);

  for (i=0; i<40; i++) {
    
    WRITE_RAM(i, i+5);
    value = READ_RAM(i);
    Serial.println((uint16_t)value);
  }

}
 
void loop() {
}

And the code that I used with Arduino is the same buy without the function SPI.beginTransaction (because Arduino works at 16 Mhz)

The code is very simple. It waits five seconds and then type a value i + 5 in the " i " position and then read in the "i" position to send it via the serial port.

Connections that I use to connect the RAM to Teensy are as follows:


23LC1024

-- Vcc --- HOLD --- SCK -- MOSI --
|
|)
|
-- CS --- MISO --- NC --- GND --


Teensy pins

Vcc and HOLD ---- 3.3V or 5V
SCK ----------- 13
MOSI ---------- 11
MISO ---------- 12
CS ------------ 10


Also I attached the circuit in Fritzing :

Sin título-1.png

I think the problem is in the configuration of SPI. But I do not know what I'm doing wrong. I searched a lot of information but I have not found someone to explain how to communicate with this chip using Teensy.

I know the Teensy audio library can access this chip. For this reason I think that this RAM can be configured directly from your code without using the library. The reason not to use audio library for my project is because I want to declare variables and store there. And as far as I know the library does it is save a series of data to produce a delay effect in the Audo.

So after all this, what is my mistake? :confused:

If someone also has the same problem (or a similar problem) I would like to comment on it. Hopefully, together, we solve the problem :)

Regards!
 
Teensy pins

Vcc and HOLD ---- 3.3V or 5V
SCK ----------- 13
MOSI ---------- 11
MISO ---------- 12
CS ------------ 10


Also I attached the circuit in Fritzing :
https://forum.pjrc.com/attachment.php?attachmentid=7958

Try connecting the black wire to GND not the AGND (Analog GND).

EDIT:
Oh you were also missing SPI.beginTransaction and SPI.endTransaction();
Here is my sketch you can try out, keep in mind this was not tested on any hardware.
Code:
#include <SPI.h>

const int SS_PIN = 10;

// set up the speed in hertz, mode and endianness of each device
SPISettings SPI23LC1024_RAM(30000000, MSBFIRST, SPI_MODE0 ); // Teensy 3.1 can only generate 30 MHz SPI when running at 120 MHz (overclock)

//Configuración SRAM
/*
#define RDSR        5
#define WRSR        1
#define READ        3
#define WRITE       2
*/
#define  OPCODE_RDSR   B00000101     /*05h Read Status Register */
#define  OPCODE_WRSR   B00000001     /*01h Write Status Register */
#define  OPCODE_READ   B00000011     /*03h Read Memory  | Addr Bytes 3 | 1 to ∞ */
#define  OPCODE_WRITE  B00000010     /*02h Write Memory | Addr Bytes 3 | 1 to ∞ */

//Byte transfer functions

uint8_t READ_RAM(uint32_t address) {
  uint8_t read_byte;

  SPI.beginTransaction(SPI23LC1024_RAM);
  digitalWriteFast(SS_PIN, LOW); // Enables SPI communication
  SPI.transfer(OPCODE_READ);

  SPI.transfer((uint8_t)(address >> 16)); // 24bit addr
  SPI.transfer((uint8_t)(address >> 8));
  SPI.transfer((uint8_t)(address & 0xFF));
  read_byte = SPI.transfer(0x00);
  digitalWriteFast(SS_PIN, HIGH); // Disable the SPI communication
  SPI.endTransaction();
  return read_byte;
}

void WRITE_RAM(uint32_t address, uint8_t data_byte) {
  SPI.beginTransaction(SPI23LC1024_RAM);
  digitalWriteFast(SS_PIN, LOW); // Enables SPI communication
  SPI.transfer(OPCODE_WRITE);  // We put the RAM in write mode
  SPI.transfer((uint8_t)(address >> 16)); // 24bit addr
  SPI.transfer((uint8_t)(address >> 8));
  SPI.transfer((uint8_t)(address & 0xFF));
  SPI.transfer(data_byte);   // We send the data to the RAM
  digitalWriteFast(SS_PIN, HIGH); //  Disable the SPI communication
  SPI.endTransaction();
}

void setup(void) {
  pinMode(SS_PIN, OUTPUT); // SPI 23LC1024_RAM / Set pin to Output.
  digitalWrite(SS_PIN, HIGH); // SPI 23LC1024_RAM / Set pin HIGH Disable.

  uint32_t i;
  uint8_t value;

  Serial.begin(9600);
  SPI.begin();

  while (!Serial) {
    ; // wait for serial port to connect.
  }
  delay(500);
  Serial.println(1);
  delay(500);
  Serial.println(2);
  delay(500);
  Serial.println(3);
  delay(500);
  Serial.println(4);
  delay(500);
  Serial.println(5);
  delay(500);

  for (i = 0; i < 40; i++) {

    WRITE_RAM(i, i + 5);
    value = READ_RAM(i);
    Serial.println((uint16_t)value);
  }
}

void loop() {
}
 
Last edited:
Great! :) :) :) :) Now my code works. I had forgotten to put SPI.beginTransaction and SPI.endTransaction (); THANKS!!!!
 
Status
Not open for further replies.
Back
Top