Pages: [1]   Go Down
Author Topic: How to write and read to external memory using SPI (Chip used: 25LC1024)  (Read 1088 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello, hope someone can help me with this as I've been trying this for over two weeks and its driving me crazy!

These is what I am trying to achieve:

-- write *one* value to a *single* address on the EEPROM
-- read the contents of that address on the EEPROM
-- print the value that you read with Serial.println() such that you can see if it has worked.

Once this works I hope to store more value to the external memory but just wanna do the simple write and read of one value. This my code so far if anyone can help me out?

All help would be my appreciated, thanks!

* SIMPLE.ino (1.78 KB - downloaded 13 times.)
Logged

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 201
Posts: 8701
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
  // Write One Value to One Address
  digitalWrite(SS, LOW);
  SPI.transfer(WRITE); // write instruction
 
  SPI.transfer(inVal >> 8);  // send MSByte address first
  SPI.transfer(0x00);
  digitalWrite(SS, HIGH);

You forgot the second byte of the address.  Your data byte (I assume the 0x00 is your data byte even though you didn't say so) is being taken as the second byte of the address and since nothing follows it, nothing is written.

Change it to:
Code:
  // Write One Value to One Address
  digitalWrite(SS, LOW);
  SPI.transfer(WRITE); // write instruction
 
  SPI.transfer(inVal >> 8);  // send MSByte address first
  SPI.transfer(inVal);         // send LSByte address second
  SPI.transfer(0x00);         // Data to write
  digitalWrite(SS, HIGH);

Similarly, your READ doesn't specify a full address.  You send the MSByte and then 0x00.  It also fails to turn off SS when done.

Change it to:
Code:
// Read One Value from One Address
  digitalWrite(SS, LOW);
  SPI.transfer(READ); // read instruction
  SPI.transfer(inVal >> 8);  // Address MSByte
  SPI.transfer(inVal);         // Address LSByte
 
  // SPI functions reads 8 bits at a time
  data = SPI.transfer(0xFF); // get data byte
  digitalWrite(SS, HIGH);   // Done with the READ
Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Offline Offline
Newbie
*
Karma: 0
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks so much for your help. There was a couple of accidental typos in there, my fault. Looking at the data sheet for this micro chip though, it appears to be a 24 bit shift so I've adapted the code a little with what you have said but it only delivers a 'Read Data = 0' every time... Any clues?

Code:
// 25LC1024 1Mbit SPI interface
//
// Device pin connections:
// 1 = CS (Chip Select) (from Arduino) 
// 2 = SO (Master In Slave Out) (from Arduino)
// 3 = +5V (WP) (from Arduino)
// 4 = 0V (Ground) (from Arduino)
// 5 = SI (Master Out Slave In) (from Arduino)
// 6 = SCK (from Arduino)
// 7 = +5V HOLD (from Arduino)
// 8 = +5V Vcc (from Arduino)
//
// This file uses the SPI library and produces higher oscillation rates than the manual methods
// for the Uno: SS/CS = pin 10, MOSI/SI = pin 11, SCK/clock = pin 13, MISO/SO = pin = 12
//

#define WRITE 2
#define READ  3
#define WREN  6

#include <SPI.h>

unsigned int data;
unsigned int address = 0;
unsigned int outval = 23;
byte invalue;

void setup()
{
  Serial.begin(9600);
  Serial.println("--------------");
  Serial.println(SS); // chip select
  Serial.println(MOSI); // master out, slave in
  Serial.println(MISO); // master in, slave out
  Serial.println(SCK); // clock
  Serial.println("--------------");
 
  // set up to match device datasheet
  SPI.setBitOrder(MSBFIRST);
  SPI.setDataMode(SPI_MODE0);
  SPI.setClockDivider(SPI_CLOCK_DIV2); // max clock is 20MHz, so can set high speed
  SPI.begin(); // sets up pin modes etc.
 
  // Enable writing
  digitalWrite(SS, LOW);
  SPI.transfer(WREN);
  digitalWrite(SS, HIGH);
 
  // Write One Value to One Address
  digitalWrite(SS, LOW);
  SPI.transfer(WRITE); // write instruction
  SPI.transfer((address >> 16) & 255);
  SPI.transfer((address >> 8) & 255);
  SPI.transfer(address & 255);
  SPI.transfer(outval);
  digitalWrite(SS, HIGH);
 
  delay(1000);
 
  // Read One Value from One Address
  digitalWrite(SS, LOW);
  SPI.transfer(READ); // read instruction
  SPI.transfer((address >> 16) & 255);
  SPI.transfer((address >> 8) & 255);
  invalue = SPI.transfer(address & 255);
 
  Serial.print("Read Data = ");
  Serial.println(invalue,DEC);
 
  digitalWrite(SS, HIGH);
}

void loop()
{
   
}
Logged

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 201
Posts: 8701
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
invalue = SPI.transfer(address & 255);

There is no way this can work because in order to shift out the data the chip needs the full address.  Since the same clock is use for input and output it won't have the full address until this transaction is done, at which time it has already sent the output bits.  In Figure 2-1 you see that all 24 address bits go in before the data bits start to come out.

Change it to:

Code:

       SPI.transfer(address & 255);   // 3rd byte of address
       invalue = SPI.transfer(0);        // Clock out the data
Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Offline Offline
Newbie
*
Karma: 0
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

IT WORKS.... ITS ALIVE.... Ooooh Geee THANKS smiley This has made me so very happy, Ahhhh all the stress of it not working can all go away.

Thanks again for all your help, it was SO helpful
Logged

Pages: [1]   Go Up
Jump to: