Advertisement
Tempist

EEPROM i2c

Jun 30th, 2019
308
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.13 KB | None | 0 0
  1. /* If you have any question for anything below, message me on the following:
  2. Discord: wizard#7815
  3. Instagram: @imWizardly
  4. Twitter: @itsWizardly
  5. (Old Twitter got suspended)
  6. */
  7.  
  8. /* Writing a byte of memory to the EEPROM:
  9. 1. Send the Most Significant Byte of the memory address that you want to write to.
  10. 2. Send the Least Significant Byte of the memory address that you want to write to.
  11. 3. Send the data byte that you would like to store at this location.
  12.  
  13. Reading from the EEPROM:
  14. 1. Send the Most Significant Byte of the memory address that you want to write to.
  15. 2. Send the Least Significant Byte of the memory address that you want to write to.
  16. 3. Ask for the data byte at that location.
  17.  
  18. 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.
  19.  
  20. 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 */
  21.  
  22. /* Edit (if needed) and use code below */
  23.  
  24. #include <Wire.h>
  25. //defines the base address of the EEPROM
  26. #define eeprom 0x50
  27.  
  28. void setup()  {
  29.   Wire.begin(); //creates a Wire object
  30.   Serial.begin(9600);
  31.  
  32.   unsigned int address = 0; //first address of the EEPROM
  33.   Serial.println("Just write the serial zip here");
  34.   for(address = 0; address< 5; address++)
  35.     writeEEPROM(eeprom, address, '2'); // Writes 22222 to the EEPROM
  36.  
  37.   for(address = 0; address< 5; address++) {
  38.     Serial.print(readEEPROM(eeprom, address), HEX);
  39.     }
  40.   }
  41.  
  42. void loop() {
  43.   /*there's nothing in the loop() function because we don't want the arduino to
  44.   repeatedly write the same thing to the EEPROM over and over.
  45.   We just want a one-time write, so the loop() function is avoided with EEPROMs.*/
  46. }
  47.  
  48. //defines the writeEEPROM function
  49. void writeEEPROM(int deviceaddress, unsigned int eeaddress, byte data ) {
  50.   Wire.beginTransmission(deviceaddress);
  51.   Wire.write((int)(eeaddress >> 8));      //writes the MSB
  52.   Wire.write((int)(eeaddress & 0xFF));    //writes the LSB
  53.   Wire.write(data);
  54.   Wire.endTransmission();
  55.   }
  56.  
  57. //defines the readEEPROM function
  58. byte readEEPROM(int deviceaddress, unsigned int eeaddress ) {
  59.   byte rdata = 0xFF;
  60.   Wire.beginTransmission(deviceaddress);
  61.   Wire.write((int)(eeaddress >> 8));      //writes the MSB
  62.   Wire.write((int)(eeaddress & 0xFF));    //writes the LSB
  63.   Wire.endTransmission();
  64.   Wire.requestFrom(deviceaddress,1);
  65.   if (Wire.available())
  66.     rdata = Wire.read();
  67.   return rdata;
  68.   }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement