wordclock/libraries/AT23C32/AT24C32.cpp

50 lines
963 B
C++
Raw Permalink Normal View History

2019-02-13 19:27:22 +01:00
#include <Arduino.h>
#include <Wire.h>
#include <AT24C32.h>
void AT24C32::begin()
{
Wire.begin();
};
uint8_t AT24C32::read(int address)
{
uint8_t result = 0;
bool read = true;
Wire.beginTransmission(AT24C32_I2C_ADDRESS);
if (Wire.endTransmission() == 0)
{
Wire.beginTransmission(AT24C32_I2C_ADDRESS);
Wire.write(address >> 8);
Wire.write(address & 0xff);
if (Wire.endTransmission() == 0)
{
Wire.requestFrom(AT24C32_I2C_ADDRESS, 1);
while (Wire.available() > 0 && read)
{
result = Wire.read();
read = false;
}
Wire.endTransmission();
}
}
return result;
}
void AT24C32::write(int address, uint8_t data)
{
Wire.beginTransmission(AT24C32_I2C_ADDRESS);
if (Wire.endTransmission() == 0)
{
Wire.beginTransmission(AT24C32_I2C_ADDRESS);
Wire.write(address >> 8);
Wire.write(address & 0xff);
Wire.write(data);
Wire.endTransmission();
delay(20);
}
}