Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* ------------------------------ EEPROMduino.cpp ------------------------------ */
- #include "EEPROMduino.h"
- EEPROMduino::EEPROMduino(uint8_t _a0, uint8_t _a1, uint8_t _a2)
- {
- a0 = _a0;
- a1 = _a1;
- a2 = _a2;
- EEPROM_address = buildAddress(_a0, _a1, _a2);
- Wire.begin(EEPROM_address);
- }
- void EEPROMduino::write(unsigned int address, byte data)
- {
- Wire.beginTransmission(EEPROM_address);
- Wire.write((byte)address);
- Wire.write(data);
- Wire.endTransmission();
- }
- byte EEPROMduino::read(unsigned int address)
- {
- byte data = 0xFF;
- Wire.beginTransmission(EEPROM_address);
- Wire.write((byte)address);
- Wire.endTransmission();
- Wire.requestFrom(EEPROM_address, (byte)0x1);
- if(Wire.available())
- {
- data = Wire.read();
- }
- return data;
- }
- /* ------------------------------ EEPROMduino.h ------------------------------ */
- #ifndef _EEPROMduino_H_
- #define _EEPROMduino_H_
- #include <Wire.h>
- #include <Arduino.h>
- typedef unsigned char uint8_t ;
- typedef uint8_t byte;
- #define buildAddress(a0, a1, a2) (0b1010000 | (a2<<2) | (a1<<1) | (a0))
- class EEPROMduino
- {
- public:
- EEPROMduino(uint8_t _a0, uint8_t _a1, uint8_t _a2);
- void write(unsigned int address, byte data);
- byte read(unsigned int address);
- private:
- uint8_t a0, a1, a2;
- uint8_t EEPROM_address;
- };
- #endif
- /* ------------------------------ SAMPLE CODE ------------------------------ */
- #include <Wire.h>
- #include "EEPROMduino.h"
- EEPROMduino ee(1, 1, 1);
- void setup()
- {
- Serial.begin(9600);
- Serial.println("Inited.");
- ee.write(2, 0x89);
- delay(100);
- Serial.print(ee.read(2), HEX);
- }
- void loop()
- {
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement