Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* If you have any question for anything below, message me on the following:
- Discord: wizard#7815
- Instagram: @imWizardly
- Twitter: @itsWizardly
- (Old Twitter got suspended)
- */
- /* Writing a byte of memory to the EEPROM:
- 1. Send the Most Significant Byte of the memory address that you want to write to.
- 2. Send the Least Significant Byte of the memory address that you want to write to.
- 3. Send the data byte that you would like to store at this location.
- Reading from the EEPROM:
- 1. Send the Most Significant Byte of the memory address that you want to write to.
- 2. Send the Least Significant Byte of the memory address that you want to write to.
- 3. Ask for the data byte at that location.
- All of the bytes in a 512 Kbit EEPROM standing in a line from 0 to 64000, because there are 8 bits to a byte. There are 32000 possible places in a 256 Kbit EEPROM and because 255 is the largest number you can encode in one byte you dumbass will need to send this address in two bytes. So, first send the Most Significant Byte (MSB) aka the first 8 bits. Then send the Least Significant Byte (LSB) the second 8 bits. The EEPROM uses an internal counter that automatically increases the memory location with each following data byte it receives. Once a memory address has been sent we can follow it with up to 64 bytes of data. The EEPROM assumes (rightly) that an address of 312 followed by 10 bytes will record byte 0 at address 312, byte 1 at address 313, byte 2 at address 314, and so on.
- Most EEPROM devices have something called a "page write buffer" which allows you to write multiple bytes at a time the same way you would a single byte */
- /* Edit (if needed) and use code below */
- #include <Wire.h>
- //defines the base address of the EEPROM
- #define eeprom 0x50
- void setup() {
- Wire.begin(); //creates a Wire object
- Serial.begin(9600);
- unsigned int address = 0; //first address of the EEPROM
- Serial.println("Just write the serial zip here");
- for(address = 0; address< 5; address++)
- writeEEPROM(eeprom, address, '2'); // Writes 22222 to the EEPROM
- for(address = 0; address< 5; address++) {
- Serial.print(readEEPROM(eeprom, address), HEX);
- }
- }
- void loop() {
- /*there's nothing in the loop() function because we don't want the arduino to
- repeatedly write the same thing to the EEPROM over and over.
- We just want a one-time write, so the loop() function is avoided with EEPROMs.*/
- }
- //defines the writeEEPROM function
- void writeEEPROM(int deviceaddress, unsigned int eeaddress, byte data ) {
- Wire.beginTransmission(deviceaddress);
- Wire.write((int)(eeaddress >> 8)); //writes the MSB
- Wire.write((int)(eeaddress & 0xFF)); //writes the LSB
- Wire.write(data);
- Wire.endTransmission();
- }
- //defines the readEEPROM function
- byte readEEPROM(int deviceaddress, unsigned int eeaddress ) {
- byte rdata = 0xFF;
- Wire.beginTransmission(deviceaddress);
- Wire.write((int)(eeaddress >> 8)); //writes the MSB
- Wire.write((int)(eeaddress & 0xFF)); //writes the LSB
- Wire.endTransmission();
- Wire.requestFrom(deviceaddress,1);
- if (Wire.available())
- rdata = Wire.read();
- return rdata;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement