1 | #include "EEPROM_I2C.h"
|
---|
2 | #include <Wire.h>
|
---|
3 |
|
---|
4 | #if ARDUINO >= 100
|
---|
5 | #include "Arduino.h"
|
---|
6 | #else
|
---|
7 | #include "WProgram.h"
|
---|
8 | #endif
|
---|
9 |
|
---|
10 | void EEPROM_I2C::begin(){
|
---|
11 | Wire.begin();
|
---|
12 | }
|
---|
13 |
|
---|
14 | void EEPROM_I2C::writeByte(unsigned int eeaddress, byte data){
|
---|
15 | int rdata = data;
|
---|
16 | this->_beginTransmission(eeaddress);
|
---|
17 | Wire.write(rdata);
|
---|
18 | this->_endTransmission();
|
---|
19 | }
|
---|
20 |
|
---|
21 | byte EEPROM_I2C::readByte(unsigned int eeaddress){
|
---|
22 | int rdata;
|
---|
23 | this->_beginTransmission(eeaddress);
|
---|
24 | this->_endTransmission();
|
---|
25 |
|
---|
26 | Wire.requestFrom(DEVICEADDRESS,1);
|
---|
27 | if (Wire.available()) rdata = Wire.read();
|
---|
28 | return rdata;
|
---|
29 | }
|
---|
30 |
|
---|
31 | void EEPROM_I2C::writePage(unsigned int eeaddress, byte* data, byte length ){
|
---|
32 | this->_beginTransmission(eeaddress);
|
---|
33 |
|
---|
34 | byte c;
|
---|
35 |
|
---|
36 | for ( c = 0; c < length; c++)
|
---|
37 | Wire.write(data[c]);
|
---|
38 |
|
---|
39 | this->_endTransmission();
|
---|
40 |
|
---|
41 | delay(10); // need some delay
|
---|
42 | }
|
---|
43 |
|
---|
44 | void EEPROM_I2C::readBuffer(unsigned int eeaddress, byte *buffer, int length ){
|
---|
45 | this->_beginTransmission(eeaddress);
|
---|
46 | this->_endTransmission();
|
---|
47 | Wire.requestFrom(DEVICEADDRESS,length);
|
---|
48 |
|
---|
49 | for ( int c = 0; c < length; c++ )
|
---|
50 | if (Wire.available()) buffer[c] = Wire.read();
|
---|
51 | }
|
---|
52 |
|
---|
53 |
|
---|
54 |
|
---|
55 | void EEPROM_I2C::_beginTransmission(unsigned int eeaddress){
|
---|
56 | Wire.beginTransmission(DEVICEADDRESS);
|
---|
57 | Wire.write((eeaddress >> 8)); // Address High Byte
|
---|
58 | Wire.write((eeaddress & 0xFF)); // Address Low Byte
|
---|
59 | }
|
---|
60 | void EEPROM_I2C::_endTransmission(){
|
---|
61 | Wire.endTransmission();
|
---|
62 | }
|
---|