|
| 1 | + |
| 2 | +#include <Wire.h> |
| 3 | + |
| 4 | +/* I2C Test read/write AT24C02 */ |
| 5 | + |
| 6 | +void i2c_eeprom_read_page(int deviceaddress, unsigned int eeaddress, int8_t len) { |
| 7 | + uint8_t rdata = 0xFF; |
| 8 | + uint8_t eeoff = (eeaddress*len); |
| 9 | + |
| 10 | + eeaddress = eeoff; |
| 11 | + |
| 12 | + Wire.beginTransmission(deviceaddress); |
| 13 | + //Wire.write((int)(eeaddress >> 8)); // MSB |
| 14 | + Wire.write((int)(eeaddress & 0xFF)); // LSB |
| 15 | + Wire.endTransmission(); |
| 16 | + for (int i = 0; i < len; i++) { |
| 17 | + Wire.requestFrom(deviceaddress,1); |
| 18 | + if (Wire.available()) { |
| 19 | + rdata = Wire.read(); |
| 20 | + Serial.printf("%02x ",rdata); |
| 21 | + } |
| 22 | + } |
| 23 | + |
| 24 | + Serial.println (); |
| 25 | +} |
| 26 | + |
| 27 | +void i2c_eeprom_write_page(int deviceaddress, unsigned int eeaddress, int8_t len) { |
| 28 | + uint8_t rdata = 0; |
| 29 | + uint8_t eeoff = (eeaddress*len); |
| 30 | + |
| 31 | + eeaddress = eeoff; |
| 32 | + |
| 33 | + for (int i = 0; i < len; i++) { |
| 34 | + rdata = eeoff + i; |
| 35 | + //rdata = 255-rdata; |
| 36 | + Serial.printf("%02x ",rdata); |
| 37 | + |
| 38 | + Wire.beginTransmission(deviceaddress); |
| 39 | + //Wire.write((int)(eeaddress >> 8)); // MSB |
| 40 | + Wire.write((int)(eeaddress & 0xFF)); // LSB |
| 41 | + Wire.write(rdata); |
| 42 | + Wire.endTransmission(); |
| 43 | + |
| 44 | + eeaddress+=1; |
| 45 | + } |
| 46 | + |
| 47 | + Serial.println (); |
| 48 | +} |
| 49 | + |
| 50 | +void setup() { |
| 51 | + Serial.begin (115200); |
| 52 | + |
| 53 | + // wait for serial port to connect |
| 54 | + while (!Serial) |
| 55 | + { |
| 56 | + } |
| 57 | + |
| 58 | + Serial.println (); |
| 59 | + Wire.begin(); |
| 60 | + |
| 61 | + Serial.println ("I2C Test writing ..."); |
| 62 | + for (int p = 0; p < 16; p++) { |
| 63 | + i2c_eeprom_write_page(0x50, p, 16); |
| 64 | + } |
| 65 | + |
| 66 | + Serial.println ("I2C Test reading ..."); |
| 67 | + for (int p = 0; p < 16; p++) { |
| 68 | + i2c_eeprom_read_page(0x50, p, 16); |
| 69 | + } |
| 70 | + |
| 71 | + Serial.println ("Done."); |
| 72 | + |
| 73 | +} // end of setup |
| 74 | + |
| 75 | +void loop() {} |
0 commit comments