Modifications

Sauter à la navigation Sauter à la recherche
3 807 octets ajoutés ,  20 janvier 2013 à 19:00
Ligne 16 : Ligne 16 :     
[[Fichier:ADF-RTC-DS1307-USE-01.jpg]]
 
[[Fichier:ADF-RTC-DS1307-USE-01.jpg]]
 +
 +
If you set analog pin 3 (digital 17) to an OUTPUT and HIGH and analog pin 2 (digital 16) to an OUTPUT and LOW you can power the RTC directly from the pins!
 +
 +
[[Fichier:ADF-RTC-DS1307-USE-02.jpg]]
 +
 +
== Premier test RTC ==
 +
 +
The first thing we'll demonstrate is a test sketch that will read the time from the RTC once a second. We'll also show what happens if you remove the battery and replace it since that causes the RTC to halt. So to start, remove the battery from the holder while the Arduino is not powered or plugged into USB. Wait 3 seconds and then replace the battery. This resets the RTC chip. Now load up the following sketch (which is also found in Examples->RTClib->ds1307) and upload it to your Arduino with the datalogger shield on!
 +
 +
<nowiki>// Date and time functions using a DS1307 RTC connected via I2C and Wire lib
 +
 +
#include <Wire.h>
 +
#include "RTClib.h"
 +
 +
RTC_DS1307 RTC;
 +
 +
void setup () {
 +
    Serial.begin(57600);
 +
    Wire.begin();
 +
    RTC.begin();
 +
 +
  if (! RTC.isrunning()) {
 +
    Serial.println("RTC is NOT running!");
 +
    // following line sets the RTC to the date & time this sketch was compiled
 +
    //RTC.adjust(DateTime(__DATE__, __TIME__));
 +
  }
 +
 +
}
 +
 +
void loop () {
 +
    DateTime now = RTC.now();
 +
   
 +
    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();
 +
   
 +
    Serial.print(" since 1970 = ");
 +
    Serial.print(now.unixtime());
 +
    Serial.print("s = ");
 +
    Serial.print(now.unixtime() / 86400L);
 +
    Serial.println("d");
 +
   
 +
    // calculate a date which is 7 days and 30 seconds into the future
 +
    DateTime future (now.get() + 7 * 86400L + 30);
 +
   
 +
    Serial.print(" now + 7d + 30s: ");
 +
    Serial.print(future.year(), DEC);
 +
    Serial.print('/');
 +
    Serial.print(future.month(), DEC);
 +
    Serial.print('/');
 +
    Serial.print(future.day(), DEC);
 +
    Serial.print(' ');
 +
    Serial.print(future.hour(), DEC);
 +
    Serial.print(':');
 +
    Serial.print(future.minute(), DEC);
 +
    Serial.print(':');
 +
    Serial.print(future.second(), DEC);
 +
    Serial.println();
 +
   
 +
    Serial.println();
 +
    delay(3000);
 +
}
 +
</nowiki>
 +
 +
Now run the Serial terminal and make sure the baud rate is set correctly at 57600
 +
 +
bps you should see the following:
 +
 +
[[Fichier:ADF-RTC-DS1307-USE-03.jpg]]
 +
 +
Whenever the RTC chip loses all power (including the backup battery) it will report the time as 0:0:0 and it won't count seconds (its stopped). Whenever you set the time, this will kickstart the clock ticking. So basically the upshot here is that you should never ever remove the battery once you've set the time. You shouldn't have to and the battery holder is very snug so unless the board is crushed, the battery wont 'fall out'
 +
 +
== Fixer l'heure ==
 +
 +
With the same sketch loaded, uncomment the line that starts with ''RTC.adjust'' like so:
 +
 +
<nowiki>// following line sets the RTC to the date & time this sketch was compiled
 +
RTC.adjust(DateTime(__DATE__, __TIME__));</nowiki>
 +
 +
This line is very cute, what it does is take the Date and Time according the computer you're using (right when you compile the code) and uses that to program the RTC. If your computer time is not set right you should fix that first. Then you must press the Upload button to compile and then immediately upload. If you compile and then upload later, the clock will be off by that amount of time.
 +
 +
Then open up the Serial monitor window to show that the time has been set
 +
 +
[[Fichier:ADF-RTC-DS1307-USE-04.jpg]]
 +
 +
From now on, you wont have to ever set the time again: the battery will last 5 or more years
 +
 +
== Lire l'heure ==
 +
 +
xxx
    
{{ADF-RTC-DS1307-TRAILER}}
 
{{ADF-RTC-DS1307-TRAILER}}
29 861

modifications

Menu de navigation