Advertisement
Guest User

Untitled

a guest
May 23rd, 2019
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.59 KB | None | 0 0
  1. // Written by Nick Gammon
  2. // February 2011
  3.  
  4. // Note: maximum address is 0x7FFF (32 Kbytes)
  5.  
  6. #include <Wire.h>
  7.  
  8. const byte rom = 0x50; // Address of 24LC256 eeprom chip
  9.  
  10. void setup (void)
  11. {
  12. Serial.begin (9600); // debugging
  13.  
  14. Wire.begin ();
  15.  
  16. // test: write the number 123 to address 0x1234
  17. writeEEPROM (rom, 0x1234, 123);
  18.  
  19. // read back to confirm
  20. byte a = readEEPROM (rom, 0x1234);
  21.  
  22. Serial.println (a, DEC); // display to confirm
  23.  
  24. // test: write a string to address 0x1000
  25. byte hello [] = "Hello, world!";
  26.  
  27. writeEEPROM (rom, 0x1000, hello, sizeof hello);
  28.  
  29. // read back to confirm
  30. byte test [sizeof hello];
  31. byte err = readEEPROM (rom, 0x1000, test, sizeof test);
  32.  
  33. Serial.println ((char *) test); // display to confirm
  34. } // end of setup
  35.  
  36. void loop() {} // no main loop
  37.  
  38.  
  39. // write len (max 32) bytes to device, returns non-zero on error
  40. // return code: 0xFF means buffer too long
  41. // other: other error (eg. device not present)
  42.  
  43. // Note that if writing multiple bytes the address plus
  44. // length must not cross a 64-byte boundary or it will "wrap"
  45.  
  46. byte writeEEPROM (byte device, unsigned int addr, byte * data, byte len )
  47. {
  48. byte err;
  49. byte counter;
  50.  
  51. if (len > BUFFER_LENGTH) // 32 (in Wire.h)
  52. return 0xFF; // too long
  53.  
  54. Wire.beginTransmission(device);
  55. Wire.write ((byte) (addr >> 8)); // high order byte
  56. Wire.write ((byte) (addr & 0xFF)); // low-order byte
  57. Wire.write (data, len);
  58. err = Wire.endTransmission ();
  59.  
  60. if (err != 0)
  61. return err; // cannot write to device
  62.  
  63. // wait for write to finish by sending address again
  64. // ... give up after 100 attempts (1/10 of a second)
  65. for (counter = 0; counter < 100; counter++)
  66. {
  67. delayMicroseconds (300); // give it a moment
  68. Wire.beginTransmission (device);
  69. Wire.write ((byte) (addr >> 8)); // high order byte
  70. Wire.write ((byte) (addr & 0xFF)); // low-order byte
  71. err = Wire.endTransmission ();
  72. if (err == 0)
  73. break;
  74. }
  75.  
  76. return err;
  77.  
  78. } // end of writeEEPROM
  79.  
  80. // write one byte to device, returns non-zero on error
  81. byte writeEEPROM (byte device, unsigned int addr, byte data )
  82. {
  83. return writeEEPROM (device, addr, &data, 1);
  84. } // end of writeEEPROM
  85.  
  86. // read len (max 32) bytes from device, returns non-zero on error
  87. // return code: 0xFF means buffer too long
  88. // 0xFE means device did not return all requested bytes
  89. // other: other error (eg. device not present)
  90.  
  91. // Note that if reading multiple bytes the address plus
  92. // length must not cross a 64-byte boundary or it will "wrap"
  93.  
  94. byte readEEPROM (byte device, unsigned int addr, byte * data, byte len )
  95. {
  96. byte err;
  97. byte counter;
  98.  
  99. if (len > BUFFER_LENGTH) // 32 (in Wire.h)
  100. return 0xFF; // too long
  101.  
  102. Wire.beginTransmission (device);
  103. Wire.write ((byte) (addr >> 8)); // high order byte
  104. Wire.write ((byte) (addr & 0xFF)); // low-order byte
  105. err = Wire.endTransmission ();
  106.  
  107. if (err != 0)
  108. return err; // cannot read from device
  109.  
  110. // initiate blocking read into internal buffer
  111. Wire.requestFrom (device, len);
  112.  
  113. // pull data out of Wire buffer into our buffer
  114. for (counter = 0; counter < len; counter++)
  115. {
  116. data [counter] = Wire.read ();
  117. }
  118.  
  119. return 0; // OK
  120. } // end of readEEPROM
  121.  
  122. // read one byte from device, returns 0xFF on error
  123. byte readEEPROM (byte device, unsigned int addr )
  124. {
  125. byte temp;
  126.  
  127. if (readEEPROM (device, addr, &temp, 1) == 0)
  128. return temp;
  129.  
  130. return 0xFF;
  131.  
  132. } // end of readEEPROM
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement