Advertisement
Guest User

Untitled

a guest
Oct 30th, 2014
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.30 KB | None | 0 0
  1.  
  2. #include <Wire.h>
  3. #include <avr/pgmspace.h>
  4. #define WIRE Wire
  5. #define DS1307_ADDRESS  0x68
  6. #if (ARDUINO >= 100)
  7.  #include <Arduino.h> // capital A so it is error prone on case-sensitive filesystems
  8.  // Macro to deal with the difference in I2C write functions from old and new Arduino versions.
  9.  #define _I2C_WRITE write
  10.  #define _I2C_READ  read
  11. #else
  12.  #include <WProgram.h>
  13.  #define _I2C_WRITE send
  14.  #define _I2C_READ  receive
  15. #endif
  16.  
  17. static uint8_t bcd2bin (uint8_t val) { return val - 6 * (val >> 4); }
  18. static uint8_t bin2bcd (uint8_t val) { return val + 6 * (val / 10); }
  19.  
  20.  
  21. void setup(){
  22.   Serial.begin(9600);
  23. }
  24.  
  25. void loop(){
  26.   WIRE.beginTransmission(DS1307_ADDRESS);
  27.   WIRE._I2C_WRITE(0);  
  28.   WIRE.endTransmission();
  29.   WIRE.requestFrom(DS1307_ADDRESS, 7);
  30.   uint8_t ss = bcd2bin(WIRE._I2C_READ() & 0x7F);
  31.   uint8_t mm = bcd2bin(WIRE._I2C_READ());
  32.   uint8_t hh = bcd2bin(WIRE._I2C_READ());
  33.   WIRE._I2C_READ();
  34.   uint8_t d = bcd2bin(WIRE._I2C_READ());
  35.   uint8_t m = bcd2bin(WIRE._I2C_READ());
  36.   uint16_t y = bcd2bin(WIRE._I2C_READ()) + 2000;
  37.   Serial.print(y);
  38.   Serial.print("-");
  39.   Serial.print(m);
  40.   Serial.print("-");
  41.   Serial.print(d);
  42.   Serial.print(" ");
  43.   Serial.print(hh);
  44.   Serial.print(":");
  45.   Serial.print(mm);
  46.   Serial.print(":");
  47.   Serial.println(ss);
  48.   delay(1000);
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement