I'm trying to interface with a 23K256 SPI RAM IC using an Arduino Mega 2560. I can't use the standard SPI pins, since I'm using an ethernet shield and it doesn't play nicely when SS is set high.
So, instead, I'm trying to bit-bang it. I've tried a few methods of my own, and eventually borrowed the code from the link in this video's description, but it doesn't seem to work at all.
// hold pin is wired directly to VCC (3.3V)
int RAMClockPin = 14;
int RAMInPin = 15;
int RAMOutPin = 11;
int RAMSelectPin = 12;
// These are hard-wired in the chip
int RAM_READ_COMMAND = 3; // 00000011
int RAM_WRITE_COMMAND = 2; // 00000010
//================================================
// Send a byte into the chip's input, bit by bit
void RAMRawWrite(byte b) {
// Cycle through the 8 bits in the byte
for (int j = 0; j < 8; j++) {
// Send MSB (Most Significant Bit) to chip
if ((b & 0x80) == 0x80) digitalWrite(RAMInPin, HIGH);
else digitalWrite(RAMInPin, LOW);
// Send a clock pulse
digitalWrite(RAMClockPin, HIGH);
digitalWrite(RAMClockPin, LOW);
// Shift the rest of the bits, one to the left
b = b << 1;
}
}
//================================================
// The whole procedure of sending a data byte
void writeToRAM(unsigned int wAddr, byte wValue) {
// Pointer to a byte
byte *bp;
// Point to higher byte of address (wAddr is 2 bytes long)
bp = (byte *)&wAddr + 1;
// Start session
digitalWrite(RAMSelectPin, LOW);
// Send the "write" command
RAMRawWrite(RAM_WRITE_COMMAND);
// Send the address, one byte at a time
RAMRawWrite(*bp); // Higher byte
bp--;
RAMRawWrite(*bp); // Lower byte
// Send the data byte
RAMRawWrite(wValue);
// Close session
digitalWrite(RAMSelectPin, HIGH);
}
//================================================
// The whole procedure of reading a data byte
byte readFromRAM(unsigned int rAddr) {
byte b, currBit;
byte *bp;
// Point to higher byte of address
bp = (byte *)&rAddr + 1;
// Start session
digitalWrite(RAMSelectPin, LOW);
// Send the "read" command
RAMRawWrite(RAM_READ_COMMAND);
// Send address
RAMRawWrite(*bp); // Higher byte
bp--;
RAMRawWrite(*bp); // Lower byte
// Get bits, MSB-first
b = 0;
for (byte currBit = 0x80; currBit > 0; currBit = currBit >> 1) {
// Clock signal start
digitalWrite(RAMClockPin, HIGH);
// Get one bit of data, put it in the appropriate place
if (digitalRead(RAMOutPin) == HIGH) b += currBit;
// Clock signal end
digitalWrite(RAMClockPin, LOW);
}
// End session and return result
digitalWrite(RAMSelectPin, HIGH);
return b;
}
void setup() {
Serial.begin(9600);
pinMode(RAMClockPin, OUTPUT);
pinMode(RAMInPin, OUTPUT);
pinMode(RAMOutPin, INPUT);
pinMode(RAMSelectPin, OUTPUT);
digitalWrite(RAMSelectPin, HIGH);
Serial.println("Writing 0xA3 to 16");
writeToRAM(16, 0xA3);
Serial.println("Reading back...");
byte value = readFromRAM(16);
Serial.println(value, HEX);
}
void loop() {}
The result of the read is 0x00 every time. I've double and triple checked my pin numbers and tried reversing SI/SO, but it still isn't working. Unfortunately I don't have a scope to test the data.
Any ideas?