Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Wire.h>
- #include <avr/pgmspace.h>
- #define WIRE Wire
- #define DS1307_ADDRESS 0x68
- #if (ARDUINO >= 100)
- #include <Arduino.h> // capital A so it is error prone on case-sensitive filesystems
- // Macro to deal with the difference in I2C write functions from old and new Arduino versions.
- #define _I2C_WRITE write
- #define _I2C_READ read
- #else
- #include <WProgram.h>
- #define _I2C_WRITE send
- #define _I2C_READ receive
- #endif
- static uint8_t bcd2bin (uint8_t val) { return val - 6 * (val >> 4); }
- static uint8_t bin2bcd (uint8_t val) { return val + 6 * (val / 10); }
- void setup(){
- Serial.begin(9600);
- }
- void loop(){
- WIRE.beginTransmission(DS1307_ADDRESS);
- WIRE._I2C_WRITE(0);
- WIRE.endTransmission();
- WIRE.requestFrom(DS1307_ADDRESS, 7);
- uint8_t ss = bcd2bin(WIRE._I2C_READ() & 0x7F);
- uint8_t mm = bcd2bin(WIRE._I2C_READ());
- uint8_t hh = bcd2bin(WIRE._I2C_READ());
- WIRE._I2C_READ();
- uint8_t d = bcd2bin(WIRE._I2C_READ());
- uint8_t m = bcd2bin(WIRE._I2C_READ());
- uint16_t y = bcd2bin(WIRE._I2C_READ()) + 2000;
- Serial.print(y);
- Serial.print("-");
- Serial.print(m);
- Serial.print("-");
- Serial.print(d);
- Serial.print(" ");
- Serial.print(hh);
- Serial.print(":");
- Serial.print(mm);
- Serial.print(":");
- Serial.println(ss);
- delay(1000);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement