大体24fc1025用
Revision 0:b8909823d506, committed 2014-12-28
- Comitter:
- kb10uy
- Date:
- Sun Dec 28 04:02:46 2014 +0000
- Commit message:
- initial commit;
Changed in this revision
I2CEEPROM.cpp | Show annotated file Show diff for this revision Revisions of this file |
I2CEEPROM.h | Show annotated file Show diff for this revision Revisions of this file |
diff -r 000000000000 -r b8909823d506 I2CEEPROM.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/I2CEEPROM.cpp Sun Dec 28 04:02:46 2014 +0000 @@ -0,0 +1,28 @@ +#include "I2CEEPROM.h" + +I2CEEPROM::I2CEEPROM(PinName sda, PinName scl, int address, int freq) : i2c(sda,scl) { + i2c.frequency(freq); + memadd = address; +} + +void I2CEEPROM::write(int bank, unsigned short address, const char *data, int count) { + char *bytes = new char[count+3]; + bytes[0] = 0xa1 | (bank << 3); + bytes[1] = address >> 8; + bytes[2] = address & 0xff; + for(int i = 0; i < count; i++) bytes[i + 3]=data[i]; + i2c.start(); + i2c.write(memadd, bytes, count + 3); + delete bytes; +} + +void I2CEEPROM::read(int bank, unsigned short address, char *data, int count) { + char *info = new char[3]; + info[0] = 0xa1 | (bank << 3); + info[1] = address >> 8; + info[2] = address & 0xff; + i2c.start(); + i2c.write(memadd, info, count + 3); + for(int i = 0; i < count; i++) data[i]=i2c.read(i < (count - 1)); + delete info; +} \ No newline at end of file
diff -r 000000000000 -r b8909823d506 I2CEEPROM.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/I2CEEPROM.h Sun Dec 28 04:02:46 2014 +0000 @@ -0,0 +1,13 @@ +#pragma once +#include "mbed.h" + +class I2CEEPROM { + public: + I2CEEPROM(PinName sda,PinName scl,int address, int freq=400000); + void write(int bank, unsigned short address, const char *data, int count); + void read(int bank, unsigned short address, char *data, int count); + + private: + I2C i2c; + int memadd; +};