Hello,
I'm trying to interface an 32k x 8 sram chip with shift registers but somehow it just won't write or read anything. I've tested the shift registers output which seems to be fine.
I've tried the advice given here: http://arduino.cc/forum/index.php/topic,50633.0.html
I've added decoupling capacitors and simplified the reading and writing routine but to no avail.
I've based the shift register setup on this tutorial: http://www.arduino.cc/en/Tutorial/ShiftOut
Here's the code:
//Pin connected to ST_CP of 74HC595
int latchPin = 8;
//Pin connected to SH_CP of 74HC595
int clockPin = 12;
////Pin connected to DS of 74HC595
int dataPin = 11;
int bytePins[8] = {2,3,4,5,6,7,9,10};
int CE = A2;
int OE = A1;
int WE = A0;
#define CHECK_BIT(var,pos) ((var) & (1<<(pos)))
/* mode: INPUT or OUTPUT
OUTPUT: for writing out to memory
INPUT: for reading from memory
*/
void setBytePins(int mode){
for(int i=0 ; i < 8 ; i++){
pinMode(bytePins[i], mode);
}
}
void readBytes(unsigned char * buffer, unsigned short addr, int len){
unsigned char b;
setBytePins(INPUT);
digitalWrite(CE, LOW);
digitalWrite(OE, LOW);
digitalWrite(WE, HIGH);
for( int i=0 ; i < len ; i++){
b = readByte((int)addr+i);
buffer[i] = b;
}
}
unsigned char readByte(unsigned short addr){
setAddr(addr);
return getByte();
}
unsigned char getByte(){
unsigned char b=0;
int state;
for( int i=0 ; i < 8 ; i++ ){
state = digitalRead(bytePins[i]);
if( state == HIGH ){
b |= 1 << i;
}else{
b &= ~(1 << i);
}
}
return b;
}
void writeBytes(unsigned char * b, unsigned short addr, int len){
setBytePins(OUTPUT);
digitalWrite(CE, LOW);
digitalWrite(WE, LOW);
for( int i=0 ; i < len ; i++){
writeByte(b[i],(int)addr+i);
}
}
void writeByte(unsigned char b, unsigned short addr){
setAddr(addr);
setByte(b);
writePulse();
}
void setAddr(unsigned short addr){
doubleShiftOut(addr);
}
void setByte(unsigned char b){
for(int i=0 ; i < 8 ; i++){
if( bytePins[i] & (0x1 << i) ){
digitalWrite(bytePins[7-i], HIGH);
}else{
digitalWrite(bytePins[7-i], LOW);
}
}
}
void writePulse(){
digitalWrite(WE, LOW);
delay(10);
digitalWrite(WE, HIGH);
}
void setup() {
//set pins to output so you can control the shift register
Serial.begin(9600);
pinMode(latchPin, OUTPUT);
pinMode(clockPin, OUTPUT);
pinMode(dataPin, OUTPUT);
pinMode(CE, OUTPUT);
pinMode(OE, OUTPUT);
pinMode(WE, OUTPUT);
setBytePins(OUTPUT);
}
void loop() {
unsigned char in_byte[4] = { 0xf0, 0x42, 0x43, 0xf };
unsigned char out_byte[4] = { 0,0,0,0 };
writeBytes(in_byte, 0x0, 4);
readBytes(out_byte, 0x0, 4);
Serial.print(out_byte[0], BIN);
Serial.print(" ");
Serial.print(out_byte[1], BIN);
Serial.print(" ");
Serial.print(out_byte[2], BIN);
Serial.print(" ");
Serial.println(out_byte[3], BIN);
delay(500);
}
void doubleShiftOut(unsigned short b){
unsigned char b1 = (b & 0xff00) >> 8;
unsigned char b2 = b & 0xff;
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, MSBFIRST, b1);
shiftOut(dataPin, clockPin, MSBFIRST, b2);
digitalWrite(latchPin, HIGH);
}
This prints the following to the serial line (which should be what I've written to sram):
1100000 1100000 1100000 1100000
1100000 1100000 1100000 1100000
1100000 1100000 1100000 1100000
1100000 1100000 1100000 1100000
1100000 1100000 1100000 1100000
...
The schematic is attached.


