Arduino I2C + DS1307实时时钟

时间:2023-03-09 15:29:15
Arduino I2C + DS1307实时时钟

主要特性

DS1307是Maxim的串行、I2C实时时钟芯片。主要特性有:

  • 工作电压:主电源电压4.5~5.5V,电池电压2.0~3.5V
  • 功耗:电池供电、备份模式时<500nA
  • 接口:I2C,最大速率100kbps
  • 可编程方波输出
  • 电源自动切换、失效检测
  • 内置56字节大小、支持电池备份的RAM
  • 封装:8-Pin SO/PDIP

管脚定义

Arduino I2C + DS1307实时时钟

  • X1、X2: 接32.768kHz晶体,要求晶体负载电容12.5pF
  • Vcc:主电源,范围4.5~5.5V。当需要对DS1307读写时,需要接Vcc。
  • VBAT:接电池,范围2.0~3.5V。
  • GND:地
  • SDA、SCL:I2C接口数据线、时钟线。
  • SQW/OUT:方波输出脚、通过写寄存器来使能。

与Arduino的连接

采用I2C接口与Arduino连接。SQW/OUT脚亦为开漏(open-drain)设计,需要接上拉电阻。

Arduino I2C + DS1307实时时钟

DS1307的VCC脚接Arduino UNO的5V;GND脚接Arduino UNO的GND;SDA脚接Arduino UNO的A4(SDA);SCL脚接Arduino UNO的A5(SCL)。

功能调试

1. 写寄存器时,先写入寄存器指针(Register pointer),之后依次写入寄存器内容。每写入一个字节,Register pointer都自动加一。

2. 读寄存器时,也是先写入Register pointer,之后发Sr(Repeated start),依次读出寄存器内容。同样的,每读出一个字节,Register pinter都自动加一。

测试代码

1. 首先设置DS1307时间。代码中timeDec数组保存的是当前时间,根据实际调整。

 /*
real time clock using DS1307
function: set the time
*/ #include <Wire.h> #define ADDRESS_DS1307 0x68 byte timeDec[] = {, , , , , , };
byte timeBcd[] = {, , , , , , };
//time = {year, month, date, day, hours, minutes, seconds}; void setup()
{
Wire.begin();
Serial.begin(); //convert decimal to BCD code
int i;
for (i = ; i < ; i++)
{
timeBcd[i] = DecToBcd(timeDec[i]);
} //set the time
Wire.beginTransmission(ADDRESS_DS1307);
Wire.write(0x00);
for (i = ; i < ; i++)
{
Wire.write(timeBcd[-i]);
}
Wire.endTransmission(); Serial.println("Time has been set."); } void loop()
{
delay();
} // Convert normal decimal numbers to binary coded decimal
byte DecToBcd(byte val)
{
byte res;
if ((val <= ) && (val >= ))
{
res = ((val/)<<) | (val%);
}
else
{
res = ;
Serial.println("Error");
}
return res;
}
// Convert binary coded decimal to normal decimal numbers
byte BcdToDec(byte val)
{
byte res;
if (val <= 0x99)
{
res = (val >> )* + (val & 0x0F);
}
else
{
res = ;
Serial.println("Error");
}
return res;
}

2. 之后可以向DS1307获取实时时间。

 /*
real time clock using DS1307
function: read the time
*/ #include <Wire.h> #define ADDRESS_DS1307 0x68 byte timeBcd[] = {, , , , , , };
//time = {year, month, date, day, hours, minutes, seconds}; void setup()
{
Wire.begin();
Serial.begin();
} void loop()
{
//read the time
Wire.beginTransmission(ADDRESS_DS1307);
Wire.write(0x00);
Wire.endTransmission();
Wire.requestFrom(ADDRESS_DS1307, );
if (Wire.available() >= )
{
for (int i = ; i < ; i++)
{
timeBcd[-i] = Wire.read();
}
} //print
Serial.print(""); Serial.print(timeBcd[], HEX); Serial.print("/");
Serial.print(timeBcd[], HEX); Serial.print("/"); Serial.print(timeBcd[], HEX);
Serial.print(" "); Serial.print(BcdToDay(timeBcd[])); Serial.print(" ");
Serial.print(timeBcd[], HEX); Serial.print(":");
Serial.print(timeBcd[], HEX); Serial.print(":");
Serial.print(timeBcd[], HEX); Serial.println(); delay();
} // Convert binary coded decimal to day
String BcdToDay(byte val)
{
String res;
switch(val)
{
case : res = "Sunday"; break;
case : res = "Monday"; break;
case : res = "Tuesday"; break;
case : res = "Wednesday"; break;
case : res = "Thursday"; break;
case : res = "Friday"; break;
case : res = "Saturday"; break;
default: res = "Error!";
}
return res;
}

程序运行后,通过串口打印当前时间如下图。

Arduino I2C + DS1307实时时钟

3. 通过设置控制寄存器,可以设置SQW/OUT管脚输出高电平、低电平或某个频率的方波。方波支持的频率有1Hz、4.096kHz、8.192kHz、32.768kHz。以下代码设置的频率为32.768kHz。

 /*
real time clock using DS1307
function: output the square-wave
*/ #include <Wire.h> #define ADDRESS_DS1307 0x68
#define CONTROL_REGISTER 0x07 void setup()
{
Wire.begin();
Serial.begin(); //set the time
Wire.beginTransmission(ADDRESS_DS1307);
Wire.write(CONTROL_REGISTER);
Wire.write(0b00010011); //frequency = 32.768kHz
Wire.endTransmission(); Serial.println("Square-wave output is enabled.");
} void loop()
{
delay();
}

与示波器测试的信号图形相符。

Arduino I2C + DS1307实时时钟

参考资料

Maxim - DS1307 64x8、串行、I²C实时时钟
Tutorial – Using DS1307 and DS3231 Real-time Clock Modules with Arduino
Digital Clock with Arduino and DS1307
Assemble an Adafruit DS1307 Real Time Clock Kit