|
| 1 | + |
| 2 | +#include <Arduino.h> |
| 3 | +#include "ProgrammerAT28.h" |
| 4 | + |
| 5 | +#include "ProgData.h" |
| 6 | + |
| 7 | + |
| 8 | +// Procedure to set and display registers: |
| 9 | +// STCP low |
| 10 | +// For each address bit (high to low): SHCP low, write bit, SHCP high |
| 11 | +// STCP high to transfer to the storage register (and the output) |
| 12 | +// |
| 13 | +void writeShiftRegister(long val) |
| 14 | +{ |
| 15 | + digitalWrite(STCP_Pin, LOW); |
| 16 | + |
| 17 | + // From highest bit to lowest bit |
| 18 | + for(int i = 15; i >= 0; i--) |
| 19 | + { |
| 20 | + digitalWrite(SHCP_Pin, LOW); |
| 21 | + digitalWrite(SER_Pin, (val >> i) & 1); // Write the next highest bit |
| 22 | + digitalWrite(SHCP_Pin, HIGH); // Left shift the register 1 bit |
| 23 | + |
| 24 | + } |
| 25 | + digitalWrite(STCP_Pin, HIGH); |
| 26 | +} |
| 27 | + |
| 28 | + |
| 29 | +// Light up the LEDs using each valid address |
| 30 | +void testShiftRegister() |
| 31 | +{ |
| 32 | + // Just light up the LEDs in order. |
| 33 | + writeShiftRegister(0); |
| 34 | + delay(100); |
| 35 | + for (int i=0; i < numOfRegisterPins; i++) |
| 36 | + { |
| 37 | + writeShiftRegister(1 << i); |
| 38 | + delay(50); |
| 39 | + } |
| 40 | + // Light them all up, then turn them off. |
| 41 | + writeShiftRegister(8191); |
| 42 | + delay(50); |
| 43 | + writeShiftRegister(0); |
| 44 | +} |
| 45 | + |
| 46 | + |
| 47 | +void printValue(int address, byte value, int debug) |
| 48 | +{ |
| 49 | + if (address % 32 == 0 || debug) |
| 50 | + { |
| 51 | + Serial.println(""); |
| 52 | + Serial.print("$"); |
| 53 | + if (address < 4096) Serial.print("0"); |
| 54 | + if (address < 256) Serial.print("0"); |
| 55 | + if (address < 16) Serial.print("0"); |
| 56 | + Serial.print(address, HEX); |
| 57 | + Serial.print(":"); |
| 58 | + } |
| 59 | + |
| 60 | + Serial.print(" "); |
| 61 | + if (value < 16) Serial.print("0"); |
| 62 | + Serial.print(value, HEX); |
| 63 | +} |
| 64 | + |
| 65 | +byte readValue(int address) |
| 66 | +{ |
| 67 | + // Now read the value. |
| 68 | + // Set the AT28 for "READ", WE high, OE low |
| 69 | + // Set the data pins for input |
| 70 | + digitalWrite(AT28_WE_Pin, HIGH); |
| 71 | + for (int i=2; i <= 9; i++) pinMode(i, INPUT); |
| 72 | + |
| 73 | + writeShiftRegister(address); |
| 74 | + digitalWrite(AT28_OE_Pin, LOW); |
| 75 | + |
| 76 | + int actualValue = 0; |
| 77 | + // the (9 - i) translates from bit # to pin # |
| 78 | + for (int i=0; i <= 7; i++) { |
| 79 | + if (digitalRead(9 - i)) actualValue += (1 << i); |
| 80 | + } |
| 81 | + |
| 82 | + digitalWrite(AT28_OE_Pin, HIGH); |
| 83 | + return actualValue; |
| 84 | +} |
| 85 | + |
| 86 | +// Procedure to write to the AT28: |
| 87 | +// Set OE and WE high |
| 88 | +// For each address, write the address and data, then pulse WE low |
| 89 | +int writeEEPROM() |
| 90 | +{ |
| 91 | + Serial.println(""); |
| 92 | + Serial.println("***** WRITE *****"); |
| 93 | + digitalWrite(AT28_CE_Pin, LOW); |
| 94 | + byte currentValue; |
| 95 | + |
| 96 | + for (int address=0; address < ADDRESS_MAX; address++) |
| 97 | + { |
| 98 | + byte value = pgm_read_byte_near(values + address); |
| 99 | + printValue(address, value, 0); |
| 100 | + |
| 101 | + currentValue = readValue(address); |
| 102 | + if (currentValue != value) |
| 103 | + { |
| 104 | + |
| 105 | + for (int l=0; l < 10; l++) |
| 106 | + { |
| 107 | + for (int i=2; i <= 9; i++) pinMode(i, OUTPUT); |
| 108 | + digitalWrite(AT28_OE_Pin, HIGH); |
| 109 | + digitalWrite(AT28_WE_Pin, HIGH); |
| 110 | + writeShiftRegister(address); |
| 111 | + |
| 112 | + // the (9 - i) translates from bit # to pin # |
| 113 | + for (int i=0; i <= 7; i++) { |
| 114 | + if (value & (1 << i)) digitalHigh(9 - i); else digitalLow(9 - i); |
| 115 | + } |
| 116 | + |
| 117 | + // Send a pulse to the AT28 to write the data |
| 118 | + digitalLow(AT28_WE_Pin); |
| 119 | + delay(1); |
| 120 | + digitalHigh(AT28_WE_Pin); |
| 121 | + currentValue = readValue(address); |
| 122 | + if (currentValue == value) break; |
| 123 | + } |
| 124 | + |
| 125 | + for (int i=0; i <= 7; i++) { |
| 126 | + digitalLow(9 - i); |
| 127 | + } |
| 128 | + currentValue = readValue(address); |
| 129 | + if (currentValue == value) |
| 130 | + { |
| 131 | + currentValue = readValue(address); |
| 132 | + if (currentValue != value) |
| 133 | + { |
| 134 | + Serial.println(""); |
| 135 | + Serial.println("Error writing value!"); |
| 136 | + return 0; |
| 137 | + } |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + } |
| 142 | + |
| 143 | + writeShiftRegister(0); |
| 144 | + return 1; |
| 145 | +} |
| 146 | + |
| 147 | + |
| 148 | +// Procedure to read from the AT28: |
| 149 | +// Set OE low and WE high |
| 150 | +// For each address, write the address, then read the data |
| 151 | +void verifyEEPROM() |
| 152 | +{ |
| 153 | + Serial.println(""); |
| 154 | + Serial.println("***** READ *****"); |
| 155 | + for (int i=2; i <= 9; i++) pinMode(i, INPUT); |
| 156 | + digitalWrite(AT28_CE_Pin, LOW); |
| 157 | + |
| 158 | + for (int address=0; address < ADDRESS_MAX; address++) |
| 159 | + { |
| 160 | + int value = readValue(address); |
| 161 | + |
| 162 | + byte expectvalue = pgm_read_byte_near(values + address); |
| 163 | + |
| 164 | + if (expectvalue != value) { |
| 165 | + printValue(address, value, 1); |
| 166 | + Serial.print(" <-- Wrong value, should be: "); |
| 167 | + if (expectvalue < 16) Serial.print("0"); |
| 168 | + Serial.println(expectvalue, HEX); |
| 169 | + } |
| 170 | + else |
| 171 | + { |
| 172 | + printValue(address, value, 0); |
| 173 | + } |
| 174 | + delay(1); |
| 175 | + } |
| 176 | + |
| 177 | + writeShiftRegister(0); |
| 178 | + Serial.println(""); |
| 179 | +} |
| 180 | + |
| 181 | + |
| 182 | +// Procedure to read from the AT28: |
| 183 | +// Set OE low and WE high |
| 184 | +// For each address, write the address, then read the data |
| 185 | +void readEEPROM() |
| 186 | +{ |
| 187 | + Serial.println(""); |
| 188 | + Serial.println("***** READ *****"); |
| 189 | + for (int i=2; i <= 9; i++) pinMode(i, INPUT); |
| 190 | + digitalWrite(AT28_CE_Pin, LOW); |
| 191 | + |
| 192 | + for (int address=0; address < ADDRESS_MAX; address++) |
| 193 | + { |
| 194 | + if (address % 32 == 0) Serial.println(""); |
| 195 | + int value = readValue(address); |
| 196 | + Serial.print(value); |
| 197 | + Serial.print(","); |
| 198 | + |
| 199 | + delay(1); |
| 200 | + } |
| 201 | + |
| 202 | + writeShiftRegister(0); |
| 203 | + Serial.println(""); |
| 204 | +} |
| 205 | + |
| 206 | + |
| 207 | +// Initial setup of all of the AT28C16/C64 pins. |
| 208 | +void setupAT28() |
| 209 | +{ |
| 210 | + |
| 211 | + pinMode(SER_Pin, OUTPUT); |
| 212 | + pinMode(STCP_Pin, OUTPUT); |
| 213 | + pinMode(SHCP_Pin, OUTPUT); |
| 214 | + |
| 215 | + pinMode(AT28_CE_Pin, OUTPUT); |
| 216 | + pinMode(AT28_WE_Pin, OUTPUT); |
| 217 | + pinMode(AT28_OE_Pin, OUTPUT); |
| 218 | + |
| 219 | + // Disable the AT28 |
| 220 | + digitalWrite(AT28_CE_Pin, HIGH); |
| 221 | + digitalWrite(AT28_WE_Pin, HIGH); |
| 222 | + digitalWrite(AT28_OE_Pin, HIGH); |
| 223 | + |
| 224 | + for (int i=2; i <= 9; i++) pinMode(i, INPUT); |
| 225 | + |
| 226 | + if (ADDRESS_MAX > 2048) |
| 227 | + { |
| 228 | + Serial.println("***** Is the switch in the UP position?! *****"); |
| 229 | + } |
| 230 | + else |
| 231 | + { |
| 232 | + Serial.println("***** Is the switch in the DOWN position?! *****"); |
| 233 | + } |
| 234 | + |
| 235 | +} |
0 commit comments