Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Second attempt writing data to X28C256 EEPROM using arduino mega2560
/* Address pins */
#define A0 30
#define A1 31
#define A2 32
#define A3 33
#define A4 34
#define A5 35
#define A6 36
#define A7 37
#define A8 38
#define A9 39
#define A10 40
#define A11 41
#define A12 42
#define A13 43
#define A14 44
/* I/O pins */
#define IO0 22
#define IO1 23
#define IO2 24
#define IO3 25
#define IO4 26
#define IO5 27
#define IO6 28
#define IO7 29
/* Control signal pins */
#define CE_LOW 51
#define OE_LOW 52
#define WE_LOW 53
char const * test = "This is a 64 byte string that I'm hoping to write to my ROM.";
/* Helper to set a bunch of pins' modes at once */
void pinModes(int pin0, int pinN, int mode) {
for (int i = pin0; i <= pinN; ++i) {
pinMode(i, mode);
}
}
/* Like shiftOut but for a range of pins */
void shiftPinsOut(int pin0, int pinN, uint32_t data) {
for (int i = pin0; i <= pinN; ++i) {
uint32_t out = (data >> (i - pin0)) & 1;
digitalWrite(i, out);
}
}
/* Like shiftOut but parallel */
uint32_t shiftPinsIn(int pin0, int pinN) {
uint32_t data = 0;
for (int i = pin0; i <= pinN; ++i) {
uint32_t in = digitalRead(i);
data |= in << (i - pin0);
}
return data;
}
/* Write a byte to ROM */
void writeByte(size_t address, uint8_t data) {
pinModes(IO0, IO7, OUTPUT);
shiftPinsOut(A0, A14, address);
shiftPinsOut(IO0, IO7, data);
digitalWrite(WE_LOW, 0);
delayMicroseconds(1);
digitalWrite(WE_LOW, 1);
delayMicroseconds(10000); /* 10 millis; this will eventually be 1 micro to support page writes */
}
/* Read a byte from ROM */
uint8_t readByte(size_t address) {
pinModes(IO0, IO7, INPUT);
shiftPinsOut(A0, A14, address);
uint32_t data;
digitalWrite(OE_LOW, 0);
delayMicroseconds(1);
data = shiftPinsIn(IO0, IO7);
digitalWrite(OE_LOW, 1);
delayMicroseconds(1);
return data;
}
void setup() {
shiftPinsOut(CE_LOW, WE_LOW, 0xFF);
pinModes(CE_LOW, WE_LOW, OUTPUT);
digitalWrite(CE_LOW, 0);
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, 0);
pinModes(A0, A14, OUTPUT);
for (size_t i = 0; i < 64; ++i) {
writeByte(i, test[i]);
}
delay(1000);
for (size_t i = 0; i < 64; ++i) {
char val = readByte(i);
if (val != test[i]) {
digitalWrite(LED_BUILTIN, 1);
}
}
}
void loop() {
// put your main code here, to run repeatedly:
delay(1000);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment