-
Notifications
You must be signed in to change notification settings - Fork 0
/
eeprom programmer.ino
156 lines (128 loc) · 4.47 KB
/
eeprom programmer.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#define SHIFT_DATA 2
#define SHIFT_CLK 3
#define SHIFT_LATCH 4
#define EEPROM_D0 5
#define EEPROM_D7 12
#define WRITE_EN 13
/*
* Output the address bits and outputEnable signal using shift registers.
*/
void setAddress(int address, bool outputEnable) {
(outputEnable ? digitalWrite(A0, LOW) : digitalWrite(A0, HIGH));
shiftOut(SHIFT_DATA, SHIFT_CLK, MSBFIRST, (address >> 8));
shiftOut(SHIFT_DATA, SHIFT_CLK, MSBFIRST, address);
digitalWrite(SHIFT_LATCH, LOW);
digitalWrite(SHIFT_LATCH, HIGH);
digitalWrite(SHIFT_LATCH, LOW);
}
/*
* Read a byte from the EEPROM at the specified address.
*/
byte readEEPROM(int address) {
for (int pin = EEPROM_D0; pin <= EEPROM_D7; pin += 1) {
pinMode(pin, INPUT);
}
setAddress(address, /*outputEnable*/ true);
byte data = 0;
for (int pin = EEPROM_D7; pin >= EEPROM_D0; pin -= 1) {
data = (data << 1) + digitalRead(pin);
}
return data;
}
/*
* Write a byte to the EEPROM at the specified address.
*/
void writeEEPROM(int address, byte data) {
setAddress(address, /*outputEnable*/ false);
for (int pin = EEPROM_D0; pin <= EEPROM_D7; pin += 1) {
pinMode(pin, OUTPUT);
}
for (int pin = EEPROM_D0; pin <= EEPROM_D7; pin += 1) {
digitalWrite(pin, data & 1);
data = data >> 1;
}
digitalWrite(WRITE_EN, LOW);
delayMicroseconds(1);
digitalWrite(WRITE_EN, HIGH);
delay(10);
}
/*
* Read the contents of the EEPROM and print them to the serial monitor.
*/
void printContents() {
for (int base = 0; base <= 255; base += 16) {
byte data[16];
for (int offset = 0; offset <= 15; offset += 1) {
data[offset] = readEEPROM(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);
}
}
// 4-bit hex decoder for common anode 7-segment display
byte digits[] = { 0x81, 0xcf, 0x92, 0x86, 0xcc, 0xa4, 0xa0, 0x8f, 0x80, 0x84, 0x88, 0xe0, 0xb1, 0xc2, 0xb0, 0xb8 };
// 4-bit hex decoder for common cathode 7-segment display
// byte data[] = { 0x7e, 0x30, 0x6d, 0x79, 0x33, 0x5b, 0x5f, 0x70, 0x7f, 0x7b, 0x77, 0x1f, 0x4e, 0x3d, 0x4f, 0x47 };
void setup() {
// put your setup code here, to run once:
pinMode(SHIFT_DATA, OUTPUT);
pinMode(SHIFT_CLK, OUTPUT);
pinMode(SHIFT_LATCH, OUTPUT);
digitalWrite(WRITE_EN, HIGH);
pinMode(WRITE_EN, OUTPUT);
pinMode(A0, OUTPUT);
Serial.begin(57600);
// Erase entire EEPROM
Serial.print("Erasing EEPROM");
for (int address = 0; address <= 8190; address += 1) {
writeEEPROM(address, 0xff);
if (address % 64 == 0) {
Serial.print(".");
}
}
Serial.println(" done");
// Program data bytes
Serial.print("Programming EEPROM");
// for (int address = 0; address < sizeof(data); address += 1) {
// writeEEPROM(address, data[address]);
//
// if (address % 64 == 0) {
// Serial.print(".");
// }
// }
Serial.println("Programming...");
for (int value = 0; value <= 255; value += 1) {
// Serial.println("Programming ones place");
writeEEPROM(value, digits[value % 10]);
// Serial.println("Programming tens place");
writeEEPROM(value + 256, digits[(value / 10) % 10]);
// Serial.println("Programming hundreds place");
writeEEPROM(value + 512, digits[(value / 100) % 10]);
// Serial.println("Programming sign");
writeEEPROM(value + 768, 0xff);
// Serial.println("Programming ones place (twos complement)");
writeEEPROM((byte)value + 1024, digits[abs(value) % 10]);
// Serial.println("Programming tens place (twos complement)");
writeEEPROM((byte)value + 1280, digits[abs(value / 10) % 10]);
// Serial.println("Programming hundreds place (twos complement)");
writeEEPROM((byte)value + 1536, digits[abs(value / 100) % 10]);
}
Serial.println("Programming sign (twos complement)");
for (int value = -128; value <= 127; value += 1) {
if (value < 0) {
writeEEPROM((byte)value + 1792, 0xfe);
} else {
writeEEPROM((byte)value + 1792, 0xff);
}
}
Serial.println(" done");
// Read and print out the contents of the EERPROM
Serial.println("Reading EEPROM");
printContents();
}
void loop() {
// put your main code here, to run repeatedly:
}