You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

49 lines
963 B

6 years ago
  1. #include <Arduino.h>
  2. #include <Wire.h>
  3. #include <AT24C32.h>
  4. void AT24C32::begin()
  5. {
  6. Wire.begin();
  7. };
  8. uint8_t AT24C32::read(int address)
  9. {
  10. uint8_t result = 0;
  11. bool read = true;
  12. Wire.beginTransmission(AT24C32_I2C_ADDRESS);
  13. if (Wire.endTransmission() == 0)
  14. {
  15. Wire.beginTransmission(AT24C32_I2C_ADDRESS);
  16. Wire.write(address >> 8);
  17. Wire.write(address & 0xff);
  18. if (Wire.endTransmission() == 0)
  19. {
  20. Wire.requestFrom(AT24C32_I2C_ADDRESS, 1);
  21. while (Wire.available() > 0 && read)
  22. {
  23. result = Wire.read();
  24. read = false;
  25. }
  26. Wire.endTransmission();
  27. }
  28. }
  29. return result;
  30. }
  31. void AT24C32::write(int address, uint8_t data)
  32. {
  33. Wire.beginTransmission(AT24C32_I2C_ADDRESS);
  34. if (Wire.endTransmission() == 0)
  35. {
  36. Wire.beginTransmission(AT24C32_I2C_ADDRESS);
  37. Wire.write(address >> 8);
  38. Wire.write(address & 0xff);
  39. Wire.write(data);
  40. Wire.endTransmission();
  41. delay(20);
  42. }
  43. }