I've checked the wiring 3-4 times and it definitely right.
When I write one integer and to a single address the code works however when put through an array this is when the problem occurs. Trying to write an array of integers using a 'for while loop' to a number of addresses causes funny results. The value saved previously in the address is from writing one value at a time to a single address. If I haven't written something to a single address, thats when 65536 occurs? I think I've neated my code up thought but still having the same issue of writing an array one numbers and reading them back again
// 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 values[10] = {0,4,6,7,3,2,4,5,5,7};
unsigned long masteraddress = 5, time1, time2, samplerate, readtime;
float timeseconds;
unsigned int readvalue;
byte invalue, invalue2;
void setup()
{
Serial.begin(9600);
// set up to match device data sheet
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);
writing(masteraddress, 666); // address, data value
Serial.print("Read Time = ");
time1 = micros(); // Gather time in micro seconds before reading 10,00 values
for(int i = 0; i < 10000; i++)
{
readvalue = reading(i);
}
time2 = micros(); // Gather time in micro seconds after reading 10,00 values
readtime = time2 - time1; // calculates time in micro seconds to read 10,000 values
Serial.println(readtime);
timeseconds = readtime / 1000000; // Converts micro seconds into seconds
samplerate = 10000 / timeseconds; // Sample rate = Number of values read / Length of time in seconds
Serial.print("Time in seconds = ");
Serial.println(timeseconds);
Serial.print("Sample rate = ");
Serial.println(samplerate);
Serial.print("Read value = ");
Serial.println(readvalue,DEC);
}
void writing(unsigned long address, unsigned int inputval)
{
unsigned char low, high;
unsigned long lowaddress = address * 2;
unsigned long highaddress = lowaddress + 1;
low = inputval & 0x00FF;
high = (inputval >> 8) & 0x00FF;
digitalWrite(SS, LOW);
SPI.transfer(WRITE); // write instruction
// Write low byte first
SPI.transfer((lowaddress >> 16) & 255);
SPI.transfer((lowaddress >> 8) & 255);
SPI.transfer(lowaddress & 255);
SPI.transfer(low);
// Write high byte second
SPI.transfer((highaddress >> 16) & 255);
SPI.transfer((highaddress >> 8) & 255);
SPI.transfer(highaddress & 255);
SPI.transfer(high);
digitalWrite(SS, HIGH);
}
unsigned int reading(unsigned long address)
{
unsigned char low, high;
unsigned long lowaddress = address * 2;
unsigned long highaddress = lowaddress + 1;
unsigned int finaloutput;
digitalWrite(SS, LOW);
SPI.transfer(READ); // read instruction
// Read the low byte first
SPI.transfer((lowaddress >> 16) & 255);
SPI.transfer((lowaddress >> 8) & 255);
SPI.transfer(lowaddress & 255);
low = SPI.transfer(lowaddress);
// Read the high byte second
SPI.transfer((highaddress >> 16) & 255);
SPI.transfer((highaddress >> 8) & 255);
SPI.transfer(highaddress & 255);
high = SPI.transfer(highaddress);
finaloutput = (high << 8) | low;
digitalWrite(SS, HIGH);
return finaloutput;
}
void loop()
{
}