Created
July 12, 2018 23:47
-
-
Save taiypeo/830acd8d63ce328af419d02e0bea5012 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Based on Ben Eater's tutorial | |
#define SHIFT_DATA 2 | |
#define SHIFT_CLOCK 3 | |
#define SHIFT_LATCH 4 | |
#define EEPROM_I0 5 | |
#define EEPROM_I7 12 | |
#define WE 13 | |
void set_address(int addr, bool OE) | |
{ | |
OE = !OE; | |
shiftOut(SHIFT_DATA, SHIFT_CLOCK, LSBFIRST, (addr << 3) | (OE << 2)); | |
shiftOut(SHIFT_DATA, SHIFT_CLOCK, LSBFIRST, (addr >> 5)); | |
digitalWrite(SHIFT_LATCH, LOW); | |
digitalWrite(SHIFT_LATCH, HIGH); | |
digitalWrite(SHIFT_LATCH, LOW); | |
} | |
byte read_eeprom(int addr) | |
{ | |
for (int pin = EEPROM_I0; pin <= EEPROM_I7; ++pin) | |
pinMode(pin, INPUT); | |
set_address(addr, true); | |
byte data = 0; | |
for (int pin = EEPROM_I7; pin >= EEPROM_I0; --pin) | |
data = (data << 1) + digitalRead(pin); | |
return data; | |
} | |
void write_eeprom(int addr, byte data) | |
{ | |
set_address(addr, false); | |
for (int pin = EEPROM_I0; pin <= EEPROM_I7; ++pin) | |
{ | |
pinMode(pin, OUTPUT); | |
digitalWrite(pin, data & 1); | |
data >>= 1; | |
} | |
digitalWrite(WE, LOW); | |
delayMicroseconds(1); | |
digitalWrite(WE, HIGH); | |
delay(10); | |
} | |
void print_contents(int max_base=64) | |
{ | |
for (int base = 0; base <= max_base; base += 16) | |
{ | |
byte data[16]; | |
for (int offset = 0; offset < 16; ++offset) | |
{ | |
data[offset] = read_eeprom(base + offset); | |
} | |
char buf[80]; | |
sprintf(buf, "%03x: %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x", | |
base, data[0], data[1], data[2], data[3], data[4], data[5], data[6], data[7], data[8], data[9], | |
data[10], data[11], data[12], data[13], data[14], data[15]); | |
Serial.println(buf); | |
} | |
} | |
void write_data(int start_addr, byte data[], int data_length) | |
{ | |
for (int offset = 0; offset < data_length; ++offset) | |
write_eeprom(start_addr + offset, data[offset]); | |
} | |
void erase_eeprom(int len, int start_addr=0) | |
{ | |
for (int offset = 0; offset < len /*8192*/; ++offset) | |
write_eeprom(start_addr + offset, 0xff); | |
} | |
void setup() { | |
pinMode(SHIFT_DATA, OUTPUT); | |
pinMode(SHIFT_CLOCK, OUTPUT); | |
pinMode(SHIFT_LATCH, OUTPUT); | |
digitalWrite(WE, HIGH); | |
pinMode(WE, OUTPUT); | |
Serial.begin(57600); | |
Serial.println("Erasing EEPROM..."); | |
erase_eeprom(16); | |
Serial.println("Done.\n"); | |
Serial.println("Programming EEPROM..."); | |
byte data[] = {0x00, 0x01, 0x02, 0x03, 0x04}; | |
write_data(1, data, sizeof(data) / sizeof(byte)); | |
Serial.println("Done.\n"); | |
Serial.println("EEPROM contents:"); | |
print_contents(); | |
} | |
void loop() { | |
// put your main code here, to run repeatedly: | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment