Advertisement
Guest User

Untitled

a guest
Jan 4th, 2018
389
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.48 KB | None | 0 0
  1. /* Code for using the I2C bus with 24LC16B EEPROM chip.
  2.  
  3. MyEEPROM.cpp - Code to interface with external EEPROM on the Arduino
  4.  
  5. Adapted from http://www.fact4ward.com/blog/ic-if/arduino-24c16/
  6.  
  7. Current version by Neal Pisenti, 2013
  8. JQI - Strontium - UMD
  9.  
  10.    This program is free software: you can redistribute it and/or modify
  11.    it under the terms of the GNU General Public License as published by
  12.    the Free Software Foundation, either version 3 of the License, or
  13.    (at your option) any later version.
  14.  
  15.    This program is distributed in the hope that it will be useful,
  16.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  17.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  18.    GNU General Public License for more details.
  19.  
  20.    You should have received a copy of the GNU General Public License
  21.    aunsigned long with this program.  If not, see <http://www.gnu.org/licenses/>.
  22.  
  23.  
  24. */
  25.  
  26. #include "Arduino.h"
  27. #include "MyEEPROM.h"
  28. #include "Wire.h"
  29.  
  30. // Write to EEPROM. The 24LC16B has 8 blocks of 256 bytes.
  31. // Thus, the memAddress needs 3 bits to specify the block (MSB),
  32. // plus another 8 bits to specify the address within that block.
  33. // Thus, memAddress should be 11 bits.
  34. // Here, we're giving it a type int; you should pass it a hex value, where the
  35. // first digit is the block number (0-8), and the next two digits form the
  36. // byte specifying the memory address. For example,
  37. // 0x020 -- block 0, memory address 32
  38. // 0x500 -- block 5, memory address 0
  39. // etc.
  40.  
  41. // The *device* address is 1010 (no way to change on 24LC16B) -- these need to
  42. // be in MSB position --> use 0x50.
  43. // I'm leaving this in as a #define parameter for now.
  44.  
  45. #define deviceAddress 0x50
  46.  
  47. // data: an array of bytes of data you'd like to write!
  48. // length: how many bytes you're writing...
  49. // NOTE!! currently limited to reads and writes of (up to) 16 bytes at a time
  50. void MyEEPROMClass::write(int memAddress, const byte * data, byte length){
  51.     byte devAddr = deviceAddress | ((memAddress >> 8) & 0x07); // extracts block, bitwise OR onto device addr.
  52.                                                                 // this is the 7-bit address arduino Wire wants.
  53.  
  54.     Wire.beginTransmission(devAddr); // begin I2C transfer
  55.     Wire.write(memAddress); // specify which memory address you're starting on
  56.  
  57.     // loops through and writes data; note, will cut you short if you try to write more than
  58.     // 16 bytes! This preserves the data integrity of what you've already written on the chip.
  59.     for(int i = 0; i < length && i < 16; i++){
  60.         Wire.write(data[i]); // write data
  61.     }
  62.     Wire.endTransmission();
  63.     delay(10); // delay so EEPROM chip can persist buffer to memory
  64.  
  65.  
  66. }
  67.  
  68. byte MyEEPROMClass::read(int memAddress, byte * buffer, byte length){
  69.     byte devAddr = deviceAddress | ((memAddress >> 8) & 0x07); // extracts block, bitwise OR onto device addr.
  70.  
  71.     // make like you're going to do a write, so EEPROM moves pointer to beginning of readout block
  72.     Wire.beginTransmission(devAddr);
  73.     Wire.write(memAddress);
  74.     Wire.endTransmission();
  75.  
  76.     // request bytes from EEPROM
  77.     Wire.requestFrom(devAddr, length);
  78.  
  79.     // while bytes left && bytes in Wire RX buffer, add them to the readout buffer.
  80.     // returns the number of bytes read.
  81.     int i;
  82.     for(i = 0; i < length && Wire.available(); i++){
  83.         buffer[i] = Wire.read();
  84.     }
  85.     return i;
  86. }
  87.    
  88. // instantiate class, to use as MyEEPROM in arduino sketches.
  89. MyEEPROMClass MyEEPROM;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement