Estoy tratando de interactuar con un 23K256 SPI RAM IC utilizando un Arduino Mega 2560 . No puedo usar los pines SPI estándar, ya que estoy usando un escudo de Ethernet y no funciona bien cuando el SS está alto.
Entonces, en cambio, estoy tratando de golpearlo. He intentado algunos métodos propios y, finalmente, tomé prestado el código del enlace en descripción de este video , pero no parece funcionar en absoluto.
// 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() {}
El resultado de la lectura es 0x00
cada vez. He comprobado el doble y el triple de mis números de pin y he intentado revertir SI
/ SO
, pero aún no funciona. Desafortunadamente no tengo un alcance para probar los datos.
¿Alguna idea?