nr Bundaberg, Australia
Online
Tesla Member
Karma: 75
Posts: 6977
Scattered showers my arse -- Noah, 2348BC.
|
 |
« Reply #15 on: July 17, 2011, 06:53:07 pm » |
I think this looks a bit sus b |= digitalRead(bytePins[i] << i); try b |= digitalRead(bytePins[i]) << i; Also in principle I don't think it's good to leave CE low while the address lines are changing, void writeBytes(unsigned char * b, unsigned short addr, int len){ setBytePins(OUTPUT); digitalWrite(CE, LOW); <<<<<<<<<<<<<<<<<<<<<<<<<<<< for( int i=0 ; i < len ; i++){ writeByte(b[i],(int)addr+i); } digitalWrite(CE, HIGH); <<<<<<<<<<<<<<<<<<<<<<<<<<<< } void writeByte(unsigned char b, unsigned short addr){ setAddr(addr); setByte(b); digitalWrite(WE, LOW); digitalWrite(WE, HIGH); } I'd move the CE stuff to the writeByte() func void writeByte(unsigned char b, unsigned short addr){ setAddr(addr); setByte(b); digitalWrite(CE, LOW); digitalWrite(WE, LOW); digitalWrite(WE, HIGH); digitalWrite(CE, LOW); } ______ Rob
|
|
|
|
« Last Edit: July 17, 2011, 07:01:41 pm by Graynomad »
|
Logged
|
|
|
|
|
|
|
nr Bundaberg, Australia
Online
Tesla Member
Karma: 75
Posts: 6977
Scattered showers my arse -- Noah, 2348BC.
|
 |
« Reply #17 on: July 18, 2011, 02:56:02 am » |
Yep I just looked at the data sheet, it seems that it's OK to even leave CE low permanently. I still prefer to keep such things together in one function, but that's as much a style issue as anything. Meaning that a function called writeByte() should do everything involved in writing a byte. On a larger program someone may call writeByte() directly one day and wonder why it didn't work.
Admittedly if speed is an issue the current code will be faster and this is a very small program, it's easy to see what's happening as the CE code is just a few lines above.
______ Rob
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 15
|
 |
« Reply #18 on: July 18, 2011, 05:04:15 am » |
I think it's not functioning properly due to there being a drop in voltage on the bus though. Any ideas on what might be causing that?
The datasheet says it can only operate at 4.5v minimum and I'm reading 4.36v on the bus even though it's fed by a 5v source.
|
|
|
|
|
Logged
|
|
|
|
|
nr Bundaberg, Australia
Online
Tesla Member
Karma: 75
Posts: 6977
Scattered showers my arse -- Noah, 2348BC.
|
 |
« Reply #19 on: July 18, 2011, 06:14:38 am » |
Did you look at the code I think is wrong?
You also need to find out where 5v turns into 4.36v. Measure at the source, if it's good there then work down the line until it goes bad.
If it's bad at the source unplug the ram and see if things change. Do you have 5v with no ram? If so then the ram chip may be at fault. If it doesn't change then the PSU is at fault.
Do you only get this voltage when running your sketch? If you run a blank sketch does it still show 4.36v?
______ Rob
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 15
|
 |
« Reply #20 on: July 18, 2011, 06:50:20 am » |
If I run a blank sketch it stays at 4.93v (with all the components in the board).
With the ram_driver sketch: No ic's: 4.93v I add 1 shift register: 4.80v I add another shift register: 4.55v I add the ram chip: 4.05v (even lower than before)
Just to clarify: I've connected the top of the bus to the meter (vcc to incoming wire, com to the outgoing wire to the rest of the board). Is that the right way to measure?
|
|
|
|
« Last Edit: July 18, 2011, 07:23:46 am by RolfvDam »
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 15
|
 |
« Reply #21 on: July 18, 2011, 06:53:30 am » |
Did you look at the code I think is wrong?
Here's the updated 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); for( int i=0 ; i < len ; i++){ b = readByte((int)addr+i); buffer[i] = b; } digitalWrite(OE, HIGH); // Add this digitalWrite(CE, LOW); // Add this } unsigned char readByte(unsigned short addr){ setAddr(addr); return getByte(); } unsigned char getByte(){ unsigned char b=0;
for( int i=0 ; i < 8 ; i++ ){ b |= digitalRead(bytePins[i]) << i; }
return b; } void writeBytes(unsigned char * b, unsigned short addr, int len){ setBytePins(OUTPUT); 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); digitalWrite(CE, LOW); digitalWrite(WE, LOW); digitalWrite(WE, HIGH); digitalWrite(CE, HIGH); } void setAddr(unsigned short addr){ doubleShiftOut(addr); } void setByte(unsigned char b){ for(int i=0 ; i < 8 ; i++) digitalWrite(bytePins[7-i], b & (1<<i) ); } 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); digitalWrite(CE, HIGH); /// Add this pinMode(OE, OUTPUT); digitalWrite(OE, HIGH); /// Add this pinMode(WE, OUTPUT); digitalWrite(WE, HIGH); //setBytePins(OUTPUT); }
void loop() { unsigned char in_byte[4] = { 0xf1, 0x40, 0x41, 0xf0 }; unsigned char out_byte[4] = { 1,2,3,4 }; 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); }
It outputs 0 0 0 0 now. I think it's reading ok but not writing anything due to the drop in voltage but that's just a guess.
|
|
|
|
|
Logged
|
|
|
|
|
Massachusetts, USA
Offline
Tesla Member
Karma: 108
Posts: 6615
|
 |
« Reply #22 on: July 18, 2011, 07:48:09 am » |
The only strange things I see in the code now should not prevent it from working. You're probably right about the hardware (power) problem. Notes: CE was left LOW after readBytes(). Not a problem. writeByte() was using the data pins in the opposite order from readByte(). Data would come out reversed but not zero. You had a few places where a function called one function (setAddr()) or a small function was used in just one place. It's mostly a matter of style. You also had a few unnecessary temporary variables and casts. Just to consolidate code a bit: //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;
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); digitalWrite(CE, HIGH); // DISABLED (active low signal)
pinMode(OE, OUTPUT); digitalWrite(OE, HIGH); // DISABLED (active low signal)
pinMode(WE, OUTPUT); digitalWrite(WE, HIGH); // DISABLED (active low signal) }
/* 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 setAddr(unsigned short addr){ digitalWrite(latchPin, LOW); shiftOut(dataPin, clockPin, MSBFIRST, addr>>8); shiftOut(dataPin, clockPin, MSBFIRST, addr); digitalWrite(latchPin, HIGH); }
void readBytes(unsigned char * buffer, unsigned short addr, int len){ setBytePins(INPUT); digitalWrite(CE, LOW); digitalWrite(OE, LOW); for( int i=0 ; i < len ; i++){ buffer[i] = readByte(addr+i);; } digitalWrite(OE, HIGH); digitalWrite(CE, HIGH); }
unsigned char readByte(unsigned short addr){ setAddr(addr); unsigned char b=0; for( int i=0 ; i < 8 ; i++ ){ b |= digitalRead(bytePins[i]) << i; } return b; }
void writeBytes(unsigned char * b, unsigned short addr, int len){ setBytePins(OUTPUT); for( int i=0 ; i < len ; i++){ writeByte(b[i],addr+i); } }
void writeByte(unsigned char b, unsigned short addr){ setAddr(addr); for(int i=0 ; i < 8 ; i++) digitalWrite(bytePins[i], b & (1<<i) ); digitalWrite(CE, LOW); digitalWrite(WE, LOW); // Initiate write digitalWrite(WE, HIGH); digitalWrite(CE, HIGH); }
void loop() { unsigned char in_byte[4] = {0xf1, 0x40, 0x41, 0xf0}; unsigned char out_byte[4] = {1, 2, 3, 4};
writeBytes(in_byte, 0x0000, 4); readBytes(out_byte, 0x0000, 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); }
|
|
|
|
|
Logged
|
|
|
|
|
nr Bundaberg, Australia
Online
Tesla Member
Karma: 75
Posts: 6977
Scattered showers my arse -- Noah, 2348BC.
|
 |
« Reply #23 on: July 18, 2011, 08:13:36 am » |
The voltage shouldn't die like that, what are you using for a power supply?
______ Rob
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 15
|
 |
« Reply #24 on: July 18, 2011, 09:49:23 am » |
The voltage shouldn't die like that, what are you using for a power supply?
I'm using the power supplied by usb on the Arduino (Duemilanove) Could it be I've broken my chips in earlier tests? For example I didn't know it was bad to leave a chip unpowered while still having power on the data lines when I started out with this project.
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 15
|
 |
« Reply #25 on: July 19, 2011, 05:30:21 pm » |
Yeah, I broke my Arduino. Computer doesn't recognize it anymore.
edit: I was wrong. When I removed all the pins from the Arduino the computer picked it up again. I think I'll just rebuild the circuit from scratch using the schematic and try again (and use a different power source or powered usb hub).
|
|
|
|
« Last Edit: July 20, 2011, 07:43:26 pm by RolfvDam »
|
Logged
|
|
|
|
|
Rapa Nui
Offline
Edison Member
Karma: 31
Posts: 1183
Pukao hats cleaning services
|
 |
« Reply #26 on: July 31, 2011, 06:29:17 am » |
.. I'm using Ramtron's FRAM FM25H20 - serial SPI, 8pin, 256kByte, spi_clk up to 40MHz, nonvolatile, works as SRAM (full r/w speed).. P.
|
|
|
|
|
Logged
|
|
|
|
|
|