Thứ Bảy, 30 tháng 1, 2016

How to use Ds1307 with Arduino!

https://arduino-info.wikispaces.com/DS1307_RealTime_Clock_Brick
/* YourDuinoStarter Example: DS1307 Real Time Clock
 - WHAT IT DOES: Set and/or Run DS1307 Real Time Clock
 - SEE the comments after "//" on each line below
 - CONNECTIONS:
   Function  Arduino Pin  Mega Pin
   -         Gnd          Gnd
   +         +5V          +5V
   SDA       Analog A4    20
   SCL       Analog A5    21
 - V1.02 04/26/2015
   Questions: terry@yourduino.com */

/*-----( Import needed libraries )-----*/
#include <Wire.h>
#include "RTClib.h"

/*-----( Declare Constants and Pin Numbers )-----*/
// Not needed: RTC Lib expects connections as above

/*-----( Declare objects )-----*/
RTC_DS1307 rtc;    // Create a RealTimeClock object

/*-----( Declare Variables )-----*/
// NONE because the library handles this...

void setup()   /****** SETUP: RUNS ONCE ******/
{
  Serial.begin(9600); // Set up for Serial Monitor to be able to see this work
  // Set this in the lower right of the Serial Monitor screen
  
  Serial.println("YourDuino.com DS1307 Real Time Clock - Set / Run Utility");

  /*----( These lines allow code to work also on Arduino DUE )----- */
#ifdef AVR
  Wire.begin();
#else
  Wire1.begin(); // Shield I2C pins connect to alt I2C bus on Arduino Due
#endif

  rtc.begin(); // Start the RTC library code

  /*----( SET the date and time.  Comment OUT these lines after setting )----*/
  // Put these "//" in front of the line you do NOT want to use
  // following line sets the RTC to the date & time this sketch was compiled
     rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
  // This line sets the RTC with an explicit date & time, for example to set
  // May 21, 2015 at 6pm you would call: (use 24 hour time)
  //    rtc.adjust(DateTime(2015, 5, 21, 18, 0, 0));

}//--(end setup )---


void loop()   /****** LOOP: RUNS CONSTANTLY ******/
{
  DateTime now = rtc.now();  // Read data from the RTC Chip

  Serial.print(now.year(), DEC); //
  Serial.print('/');
  Serial.print(now.month(), DEC);
  Serial.print('/');
  Serial.print(now.day(), DEC);
  Serial.print(' ');
  Serial.print(now.hour(), DEC);
  Serial.print(':');
  Serial.print(now.minute(), DEC);
  Serial.print(':');
  Serial.print(now.second(), DEC);
  Serial.println();

  delay(3000);

}//--(end main loop )---

/*-----( Declare User-written Functions )-----*/
//NONE

//*********( THE END )***********

0 nhận xét:

Đăng nhận xét