Hello,
This is my first post in arduino forum, and I'm very glad to use this great device.
I have used arduino a couple of months, I have made a different projects using it, such as (Car robot, little Game Console with 3310 LCD ...).
Now, Searching for new project and especially in GameBoy console, I have found that some people have made programmable gameboy cartridge, changing its Rom and replace it with AM29F040 Flash Memory. So, I decided to make a one for me too.
Before I purchased this Flash memory "AM29F040B", I did some research on internet to be sure if it's possible to replace the gameboy cartridge ROM with this Flash Mem. The answer was yes.
This Flash memory has 32 pins, and arduino UNO has just 11 pins without using the 0 & 1 (for some Serial problem reasons), and A0 to A6 for the analog pins.
To come to this lack of pins, Shift registers should be used, therefore, I bought 2 shift registers (74HC595) and wire them in cascading which will give us 16 outputs
After that, the big problem has appeared: How can I Read/Write/Erase this memory using Arduino since it's so versatil, cheap and easy to use. (Programmers are so expensive).
I read some replies from people in some forums, they claimed that arduino UNO which I use, is not fast enough to drive correctly AM29F040B (because arduino can't drive 20ns or something like that), in other hand some others confirm that's possible to do.
I have read the datasheet but my weakness is the Timing Diagram (I'm not an electronic engineer) I can't understand it correctly, which means that I can't write a functionnal code.
Below, Is the circuit:
Arduino pin 10 -> Shift Register Serial Data
Arduino pin 11 -> Latch pin (SR)
Arduino pin 12 -> Serial Clock (SR)
Arduino pin 2 to 9 -> Flash memory D0 - D7
Arduino A0 -> Flash memory pin 18
Arduino A1 -> Flash memory pin 17
Arduino A2 -> Flash memory pin 16
Arduino A3 -> Flash memory WE#
Arduino A4 -> Flash memory OE#
Arduino A5 -> Flash memory CE#
Shift Register 1 Q0 - Q7 -> Flash memory A0 - A7
Shift Register 2 Q0 - Q7 -> Flash memory A8 - A15
And here is the code to read the memory (Don't know if it's correct or not):
#define SERIALDATA 10
#define SERIALCLK 12
#define STORECLK 11
#define WE A3
#define OE A4
#define CE A5
#define OUT 0
#define IN 1
unsigned int addr = 0;
byte data = 0;
int counter = 0;
boolean val = LOW;
void ChangeMode(byte io)
{
if (io) {
pinMode(2, INPUT);
pinMode(3, INPUT);
pinMode(4, INPUT);
pinMode(5, INPUT);
pinMode(6, INPUT);
pinMode(7, INPUT);
pinMode(8, INPUT);
pinMode(9, INPUT);
}
else
{
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
pinMode(7, OUTPUT);
pinMode(8, OUTPUT);
pinMode(9, OUTPUT);
}
}
void setup()
{
Serial.begin(19200);
//Data read or write D0 - D7 - Pin9 - Pin2
ChangeMode(IN);
//Shiftregister pins
pinMode(STORECLK, OUTPUT);
pinMode(SERIALCLK, OUTPUT);
pinMode(SERIALDATA, OUTPUT);
//Command register
pinMode(WE, OUTPUT);
pinMode(OE, OUTPUT);
pinMode(CE, OUTPUT);
digitalWrite(WE, LOW);
digitalWrite(CE, LOW);
digitalWrite(OE, LOW);
}
void loop()
{
Serial.println("START READING");
for (addr = 0; addr < 0xFF; addr++)
{
digitalWrite(STORECLK, LOW);
shiftOut(SERIALDATA, SERIALCLK, LSBFIRST, (addr&0xFF));
shiftOut(SERIALDATA, SERIALCLK, LSBFIRST, (addr>>8));
digitalWrite(STORECLK, HIGH);
digitalWrite(CE, HIGH); // Chip Enabled
digitalWrite(OE, HIGH); //Read Enabled
digitalWrite(WE, LOW);
for (int i = 2, j = 0; i <= 9; i++, j++)
{
val = digitalRead(i);
if (val == HIGH)
bitWrite(data, j, HIGH);
}
digitalWrite(OE, LOW);
digitalWrite(CE, LOW);
Serial.print(data, HEX);
Serial.print(" ");
if (++counter == 32) {
Serial.println("");
counter = 0;
}
}
Serial.println("");
Serial.println("Finish");
while(1);
}
And over here is the code to write the same data just for test:
#define SERIALDATA 10
#define SERIALCLK 12
#define STORECLK 11
#define WE A3
#define OE A4
#define CE A5
//Change the pinMode
#define OUT 0
#define IN 1
unsigned int addr = 0;
int count = 0;
void ChangeMode(byte io)
{
if (io) {
pinMode(2, INPUT);
pinMode(3, INPUT);
pinMode(4, INPUT);
pinMode(5, INPUT);
pinMode(6, INPUT);
pinMode(7, INPUT);
pinMode(8, INPUT);
pinMode(9, INPUT);
}
else
{
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
pinMode(7, OUTPUT);
pinMode(8, OUTPUT);
pinMode(9, OUTPUT);
}
}
void setup()
{
Serial.begin(19200);
//Data read or write D0 - D7 - Pin9 - Pin2
ChangeMode(OUT);
//Shiftregister pins
pinMode(STORECLK, OUTPUT);
pinMode(SERIALCLK, OUTPUT);
pinMode(SERIALDATA, OUTPUT);
//command pins
pinMode(WE, OUTPUT);
pinMode(OE, OUTPUT);
pinMode(CE, OUTPUT);
//Disable the command pins
digitalWrite(WE, LOW);
digitalWrite(CE, LOW);
digitalWrite(OE, LOW);
}
void loop()
{
Serial.println("START WRITING ...");
//Setup the chip
digitalWrite(WE, LOW);
digitalWrite(CE, LOW);
digitalWrite(OE, HIGH);
digitalWrite(STORECLK, LOW);
shiftOut(SERIALDATA, SERIALCLK, LSBFIRST, (0x0555&0xFF));
shiftOut(SERIALDATA, SERIALCLK, LSBFIRST, (0x0555>>8));
digitalWrite(STORECLK, HIGH);
delay(1);
digitalWrite(OE, LOW); //Read disable
delay(1);
digitalWrite(CE, HIGH); //Chip enable
delay(1);
digitalWrite(WE, HIGH); //Write Enable
delay(1);
for (addr = 0; addr <= 0x3FFF; addr++)
{
digitalWrite(STORECLK, LOW);
shiftOut(SERIALDATA, SERIALCLK, LSBFIRST, (addr&0xFF));
shiftOut(SERIALDATA, SERIALCLK, LSBFIRST, (addr>>8));
digitalWrite(STORECLK, HIGH);
delay(1);
digitalWrite(OE, LOW); //Read disable
delay(1);
digitalWrite(CE, HIGH); //Chip enable
delay(1);
digitalWrite(WE, HIGH); //Write Enable
delay(1);
//Write the same data in the block
digitalWrite(2, HIGH);
digitalWrite(3, LOW);
digitalWrite(4, HIGH);
digitalWrite(5, LOW);
digitalWrite(6, HIGH);
digitalWrite(7, LOW);
digitalWrite(8, HIGH);
digitalWrite(9, LOW);
delay(1);
digitalWrite(WE, LOW); //Write disabled
delay(1);
digitalWrite(CE, LOW); //Chip disable
delay(1);
digitalWrite(OE, HIGH); //reads enable
delay(1);
digitalWrite(2, LOW);
digitalWrite(3, LOW);
digitalWrite(4, LOW);
digitalWrite(5, LOW);
digitalWrite(6, LOW);
digitalWrite(7, LOW);
digitalWrite(8, LOW);
digitalWrite(9, LOW);
}
delay(1000);
Serial.println("");
Serial.println("WRITING COMPLETE.");
while(1)
delay(50);
}
Now, I'm very confused, I don't know if this code is working correctly or not. (The reading program always return 0xFF and Writing seems not working).
Please help me, I would like if someone can see the Timing diagram of this Flash memory and correct me in the code if somethings are wrong. And Why not, if it'll work we can make this as library for future use.
If you need any further informations I'm here.
Many thanks.