鳕鱼天空

This is Mr Wang's Tech Blog.

Arduino驱动DS3231高精度时钟模块

很想要个时钟模块,自己焊又太麻烦,干脆在TB上买下来了,省时。

  模块参数:
  1.尺寸:38mm(长)*22mm(宽)*14mm(高)
  2.重量:8g
  3.工作电压:3.3--5.5V
  4.时钟芯片:高精度时钟芯片DS3231
  5.时钟精度:0-40℃范围内,精度2ppm,年误差约1分钟
  6.带2个日历闹钟
  7.可编程方波输出
  8.实时时钟产生秒、分、时、星期、日期、月和年计时,并提供有效期到2100年的闰年补偿
  9.芯片内部自带温度传感器,精度为±3℃
  10.存储芯片:AT24C32(存储容量32K)
  11.IIC总线接口,最高传输速度400KHz(工作电压为5V时)
  12.可级联其它IIC设备,24C32地址可通过短路A0/A1/A2修改,默认地址为0x57
  13.带可充电电池LIR2032,保证系统断电后,时钟任然正常走动

接线说明,以Arduino uno r3为例:
  SCL→A5
  SDA→A4
  VCC→5V
  GND→GND

 
 

代码部分:

#include <DS3231.h>
#include <Wire.h>

DS3231 Clock;
bool Century=false;
bool h12;
bool PM;
byte ADay, AHour, AMinute, ASecond, ABits;
bool ADy, A12h, Apm;

byte year, month, date, DoW, hour, minute, second;

void setup() {
 // 启动I2C(IIC)接口
 Wire.begin();
        //以下部分是初始化时间,每次板子通电之后都会初始化成这个时间,只是测试用,以后可以删除。
        Clock.setSecond(50);//Set the second
        Clock.setMinute(59);//Set the minute 设置分钟
        Clock.setHour(11);  //Set the hour 设置小时
        Clock.setDoW(5);    //Set the day of the week 设置星期几
        Clock.setDate(31);  //Set the date of the month 设置月份
        Clock.setMonth(5);  //Set the month of the year 设置一年中的月份
        Clock.setYear(13);  //Set the year (Last two digits of the year) 设置年份(在今年的最后两位数——比如2013年最后的13)
 // Start the serial interface
 Serial.begin(115200);
}
void ReadDS3231()
{
  int second,minute,hour,date,month,year,temperature;
  second=Clock.getSecond();
  minute=Clock.getMinute();
  hour=Clock.getHour(h12, PM);
  date=Clock.getDate();
  month=Clock.getMonth(Century);
  year=Clock.getYear();
 
  temperature=Clock.getTemperature();
 
  Serial.print("20");
  Serial.print(year,DEC);
  Serial.print('-');
  Serial.print(month,DEC);
  Serial.print('-');
  Serial.print(date,DEC);
  Serial.print(' ');
  Serial.print(hour,DEC);
  Serial.print(':');
  Serial.print(minute,DEC);
  Serial.print(':');
  Serial.print(second,DEC);
  Serial.print('\n');
  Serial.print("Temperature=");  //这里是显示温度
  Serial.print(temperature);
  Serial.print('\n');
}
void loop()
{
  ReadDS3231();
  delay(1000);  //间隔1000ms(1000ms=1秒)循环一次。
}

不允许评论