|
| 1 | +/* |
| 2 | + Read and write settings and calibration data to an external I2C EEPROM |
| 3 | + By: Nathan Seidle |
| 4 | + SparkFun Electronics |
| 5 | + Date: December 11th, 2019 |
| 6 | + License: This code is public domain but you buy me a beer if you use this |
| 7 | + and we meet someday (Beerware license). |
| 8 | + Feel like supporting our work? Buy a board from SparkFun! |
| 9 | + https://www.sparkfun.com/products/14764 |
| 10 | +
|
| 11 | + This example demonstrates how to read and write various variables to memory. |
| 12 | +
|
| 13 | + The I2C EEPROM should have all its ADR pins set to GND (0). This is default |
| 14 | + on the Qwiic board. |
| 15 | +
|
| 16 | + Hardware Connections: |
| 17 | + Plug the SparkFun Qwiic EEPROM to an Uno, Artemis, or other Qwiic equipped board |
| 18 | + Load this sketch |
| 19 | + Open output window at 115200bps |
| 20 | +*/ |
| 21 | + |
| 22 | +#include <Wire.h> |
| 23 | + |
| 24 | +#include "SparkFun_External_EEPROM.h" // Click here to get the library: http://librarymanager/All#SparkFun_External_EEPROM |
| 25 | +ExternalEEPROM myMem; |
| 26 | + |
| 27 | +void setup() |
| 28 | +{ |
| 29 | + Serial.begin(115200); |
| 30 | + Serial.println("Qwiic EEPROM example"); |
| 31 | + |
| 32 | + Wire.begin(); |
| 33 | + |
| 34 | + if (myMem.begin() == false) |
| 35 | + { |
| 36 | + Serial.println("No memory detected. Freezing."); |
| 37 | + while (1) |
| 38 | + ; |
| 39 | + } |
| 40 | + Serial.println("Memory detected!"); |
| 41 | + |
| 42 | + uint32_t eepromSizeBytes = myMem.getMemorySizeBytes(); |
| 43 | + Serial.print("Detected EEPROM size (bytes): "); |
| 44 | + Serial.print(eepromSizeBytes); |
| 45 | + Serial.print(" - EEPROM Type: 24XX"); |
| 46 | + if (eepromSizeBytes == 16) |
| 47 | + Serial.print("00"); |
| 48 | + else |
| 49 | + { |
| 50 | + if ((eepromSizeBytes * 8 / 1024) < 10) Serial.print("0"); |
| 51 | + Serial.print(eepromSizeBytes * 8 / 1024); |
| 52 | + } |
| 53 | + Serial.println(); |
| 54 | + |
| 55 | + Serial.print("Detected number of address bytes: "); |
| 56 | + Serial.println(myMem.getAddressBytes()); |
| 57 | + |
| 58 | + Serial.print("Detected pageSizeBytes: "); |
| 59 | + Serial.println(myMem.getPageSizeBytes()); |
| 60 | + |
| 61 | + //Yes you can read and write bytes, but you shouldn't! |
| 62 | + byte myValue1 = 200; |
| 63 | + myMem.write(0, myValue1); //(location, data) |
| 64 | + |
| 65 | + byte myRead1 = myMem.read(0); |
| 66 | + Serial.print("I read (should be 200): "); |
| 67 | + Serial.println(myRead1); |
| 68 | + |
| 69 | + //You should use gets and puts. This will automatically and correctly arrange |
| 70 | + //the bytes for larger variable types. |
| 71 | + int myValue2 = -366; |
| 72 | + myMem.put(10, myValue2); //(location, data) |
| 73 | + int myRead2; |
| 74 | + myMem.get(10, myRead2); //location to read, thing to put data into |
| 75 | + Serial.print("I read (should be -366): "); |
| 76 | + Serial.println(myRead2); |
| 77 | + |
| 78 | + float myValue3 = -7.35; |
| 79 | + myMem.put(20, myValue3); //(location, data) |
| 80 | + float myRead3; |
| 81 | + myMem.get(20, myRead3); //location to read, thing to put data into |
| 82 | + Serial.print("I read (should be -7.35): "); |
| 83 | + Serial.println(myRead3); |
| 84 | + |
| 85 | + String myString = "Hi, I am just a simple test string"; |
| 86 | + unsigned long nextEEPROMLocation = myMem.putString(30, myString); |
| 87 | + String myRead4 = ""; |
| 88 | + myMem.getString(30, myRead4); |
| 89 | + Serial.print("I read: "); |
| 90 | + Serial.println(myRead4); |
| 91 | + Serial.print("Next available EEPROM location: "); |
| 92 | + Serial.println(nextEEPROMLocation); |
| 93 | +} |
| 94 | + |
| 95 | +void loop() |
| 96 | +{ |
| 97 | +} |
0 commit comments