You are on page 1of 15

DS1307

DS 1307

,
DS1307
, real time Clock
DS1307 I2C ,

.

:

: 2200 32.768

: 2200


,
.

;)( deley
,
,
.
;)(: millis
DELAY
,

Overflow To zero 56 !
,
,
millis .
DS1307 ,
8BIT
.

"
, , .. "



0x01
0x06 .

, 8bit 4bit
" " Ones 4bit
Tens
.

, DS1307
" Binary coded
decimal"BCD ,

.
, ,
,
" "
Binary
. Decimal

:
(1
(2 . DS1307
(3 . CR 3v
(4
(5 32.768KHz
. BreadBoard (6
(7 . 2.2K
,
32.768KHz 3V


,
.
3V
,
.
:
:
(1 Wire.h
I2C
(2 RTClib.h
.

:
Wire.h
I2C
, DS1307
slave
.
,
DS1307
.
,
.
//This code taken from bilder blog http://bildr.org/2011/03/ds1307-arduino/

//Modified By Mohannad Rawashdeh for http://genotronex.blogspot.com/

#include "Wire.h"
#define ADDRESS 0x68
byte Reset = 0x00; //workaround for issue #527

void setup(){
Wire.begin();
Serial.begin(9600);
setDateTime(); //This function where we Put Adjust the time and Date
}

void loop(){
printDate();//Print the Time and the Date on Serial Monitor
delay(1000);
}

void setDateTime(){

byte second =

00; //0-59

byte minute =

00; //0-59

byte hour =

17; //0-23

byte weekDay =

1; //1-7

byte monthDay = 11; //1-31


byte month =
byte year =

2; //1-12
12; //0-99

Wire.beginTransmission(ADDRESS);
Wire.write(Reset); //stop Oscillator

Wire.write(DecToBcd(second));

Wire.write(DecToBcd(minute));
Wire.write(DecToBcd(hour));
Wire.write(DecToBcd(weekDay));
Wire.write(DecToBcd(monthDay));
Wire.write(DecToBcd(month));
Wire.write(DecToBcd(year));

Wire.write(Reset); //start

Wire.endTransmission();

byte DecToBcd(byte val){


// Convert normal decimal numbers to binary coded decimal
return ( (val/10*16) + (val%10) );
}

byte bcdToDec(byte val) {


// Convert binary coded decimal to normal decimal numbers
return ( (val/16*10) + (val%16) );
}

void printDate(){

// Reset the register pointer


Wire.beginTransmission(ADDRESS);
Wire.write(Reset);
Wire.endTransmission();

Wire.requestFrom(ADDRESS, 7);

int second = bcdToDec(Wire.read());

int minute = bcdToDec(Wire.read());


int hour = bcdToDec(Wire.read() & 0b111111); //24 hour time
int weekDay = bcdToDec(Wire.read()); //0-6 -> sunday - Saturday
int monthDay = bcdToDec(Wire.read());
int month = bcdToDec(Wire.read());
int year = bcdToDec(Wire.read());

//print the date EG 11/2/2013 17:00:00


Serial.print(month);
Serial.print("/");
Serial.print(monthDay);
Serial.print("/");
Serial.print(year);
Serial.print(" ");
Serial.print(hour);
Serial.print(":");
Serial.print(minute);
Serial.print(":");
Serial.println(second);


DS1307
.


,
..
{)(void setDateTime


.
:
RTClib.h :
,
,
, RTClib
.
"
" ,

:
// Date and time functions using a DS1307 RTC connected via I2C and Wir
e lib

//This program Modified By : Mohannad Rawashdeh http://genotronex.blogs


pot.com/
#include <Wire.h>
#include "RTClib.h"
RTC_DS1307 RTC;
void setup () {
Serial.begin(9600);
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 midnight 1/1/1970 = ");


Serial.println(now.unixtime());
delay(3000);

:
RTC.adjust(DateTime(__DATE__,
__TIME__)):

.
DS1307
,

.
now.unixtime():

Unix
1970\1\1
, .
, Serial Monitor


". " Seven segment,LCD,Etc

You might also like