25LC1024 STORING 16BIT INTEGERS

Pete,

I've decided to try and do a page write instead and it worked and with the help you have given me I've adapted it however this code did work but all of sudden stopped working.

// 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

#define SSMEMORY 9

#include <SPI.h>

void setup()
{
  Serial.begin(9600);
  pinMode(SSMEMORY, OUTPUT);
  digitalWrite(SSMEMORY, HIGH);
  
  Serial.println("--------------");
  Serial.println(SSMEMORY); // 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.
  
  for (int i = 0; i < 5; i++)
  {
  writing(i, i);  //  data value, address
  } 

  delay(100);
 
  Serial.print("\n"); 

  for(unsigned long i = 0; i < 5; i++)
  {
  unsigned int readvalue = reading(i);
  
  Serial.print("READ = ");
  Serial.println(readvalue);
  Serial.print("\n");
  }
  
}

void writing(unsigned int outval, unsigned long address)
{
  unsigned char low, high;
  address = address + address;
  
  low = outval & 0x00FF;
  high = (outval >> 8) & 0x00FF;
  
  Serial.print("Write LOW Data = ");
  Serial.println(low, DEC); 
  
  Serial.print("Write HIGH Data = ");
  Serial.println(high, DEC); 
  
  // Write One Value to One Address
  // Enable writing
  digitalWrite(SSMEMORY, LOW);
  SPI.transfer(WREN);
  digitalWrite(SSMEMORY, HIGH); 
  
  digitalWrite(SSMEMORY, LOW);
  SPI.transfer(WRITE); // write instruction
  SPI.transfer((address >> 16) & 255);
  SPI.transfer((address >> 8) & 255);
  SPI.transfer(address & 255);
  SPI.transfer(low);
  SPI.transfer(high);
  digitalWrite(SSMEMORY, HIGH);
}

unsigned int reading (unsigned long address)
{
 
  unsigned char low, high;
  address = address + address;
  unsigned int finaloutput;
 
  // Read One Value from One Address
  digitalWrite(SSMEMORY, LOW); 
  SPI.transfer(READ); // read instruction
  
  SPI.transfer((address >> 16) & 255);
  SPI.transfer((address >> 8) & 255);
  SPI.transfer(address & 255);
  low = SPI.transfer(0);
  high = SPI.transfer(0);
  digitalWrite(SSMEMORY, HIGH);
  
  Serial.print("Read LOW Data = ");
  Serial.println(low, DEC); 
  
  Serial.print("Read HIGH Data = ");
  Serial.println(high, DEC); 
  
  finaloutput = (high << 8) | low;
  
  return finaloutput;
}

void loop()
{
   
}

In my setup I am also trying to use a DAC. Once I managed to get my code working for the DAC through using digital pin 9 as its OUTPUT for chip select, the pin 10 did not work any more for the external memory.

The result given for the the read value is always 65536 at every address. WHAT IS HAPPENING? I didn't change anything. I have checked the wiring... again and again but I swear its all correct.

Do you see any bugs in my code?

Thanks again for all your help.