[Diao Ye learns programming] Arduino hands-on (84) --- DS1307 clock module 4

insert image description here

The reference to 37 sensors and modules has been widely circulated on the Internet. In fact, there must be more than 37 sensor modules compatible with Arduino. In view of the fact that I have accumulated some sensor and actuator modules on hand, in accordance with the concept of true knowledge (must be hands-on), for the purpose of learning and communication, here I am going to try and do more experiments one by one. Whether it is successful or not, it will be recorded ——Small progress or unsolvable problems, I hope to be able to throw bricks and spark jade.

[Arduino] 168 kinds of sensor module series experiments (data code + simulation programming + graphics programming)
Experiment 84: DS1307 clock module Tiny RTC I2C module 24C32 memory clock (with battery)

insert image description here

Experimental environment of DS1307 clock module
1. DS1307 clock module experiment hardware list
DS1307 clock module X1
TM1637 four-digit digital tube X1
Arduino Uno development board X1
DuPont lines (9 pieces are prepared)
IIC/I2C 1602 LCD LCD screen module X1
LED Light-emitting diode (blue, green) X2
Proto Shield prototype expansion board (with mini breadboard) X1

insert image description here

Program 4: Read the status of the module pins at intervals of 5 seconds, and then traverse possible values
​​(1) Arduino reference open source code

/*
  【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)
  程序四:以5秒间隔读取模块引脚的状态,然后遍历可能的值
*/

#include "RTClib.h"//导入DS1307驱动库
RTC_DS1307 rtc;
int mode_index = 0;
Ds1307SqwPinMode modes[] = {
    
     DS1307_OFF, DS1307_ON, DS1307_SquareWave1HZ, DS1307_SquareWave4kHz, DS1307_SquareWave8kHz, DS1307_SquareWave32kHz};

void print_mode() {
    
    
  Ds1307SqwPinMode mode = rtc.readSqwPinMode();

  Serial.print("Sqw 引脚模式:");
  switch (mode) {
    
    
    case DS1307_OFF:
      Serial.println("OFF");
      break;
    case DS1307_ON:
      Serial.println("ON");
      break;
    case DS1307_SquareWave1HZ:
      Serial.println("1Hz");
      break;
    case DS1307_SquareWave4kHz:
      Serial.println("4.096kHz");
      break;
    case DS1307_SquareWave8kHz:
      Serial.println("8.192kHz");
      break;
    case DS1307_SquareWave32kHz:
      Serial.println("32.768kHz");
      break;
    default:
      Serial.println("UNKNOWN");
      break;
  }
}

void setup () {
    
    
  Serial.begin(57600);

#ifndef ESP8266
  while (!Serial); // 等待串口连接
#endif

  if (! rtc.begin()) {
    
    
    Serial.println("找不到 RTC");
    Serial.flush();
    while (1) delay(10);
  }

  print_mode();
}

void loop () {
    
    
  rtc.writeSqwPinMode(modes[mode_index++]);
  print_mode();

  if (mode_index > 5) {
    
    
    mode_index = 0;
  }

  delay(5000);
}

(2) The return status of the experimental serial port

insert image description here
Program 5: Example of using non-volatile RAM storage on DS1307
(1) Arduino reference open source code

/*
  【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)
  程序五:在 DS1307 上使用非易失性 RAM 存储的示例
*/

#include "RTClib.h"//导入DS1307驱动库
RTC_DS1307 rtc;//初始化DS1307

void printnvram(uint8_t address) {
    
    
  Serial.print("地址 0x");
  Serial.print(address, HEX);
  Serial.print(" = 0x");
  Serial.println(rtc.readnvram(address), HEX);
}

void setup () {
    
    
  Serial.begin(57600);

#ifndef ESP8266
  while (!Serial); //等待串口连接
#endif

  if (! rtc.begin()) {
    
    
    Serial.println("找不到 RTC");
    Serial.flush();
    while (1)
      delay(10);
  }

  // Print old RAM contents on startup.
  Serial.println("当前 NVRAM 值:");
  for (int i = 0; i < 6; ++i) {
    
    
    printnvram(i);
  }

  // 将一些字节写入非易失性 RAM 存储。
  // 注意:只能从地址 0 到 55 读取和写入(即 56 字节值)。
  Serial.println("写入 NVRAM 值");
  // 一次写入一个字节的示例
  rtc.writenvram(0, 0xFE);
  rtc.writenvram(1, 0xED);
  // 写入多个字节的示例
  uint8_t writeData[4] = {
    
     0xBE, 0xEF, 0x01, 0x02 };
  rtc.writenvram(2, writeData, 4);

  // 从非易失性 RAM 存储中读取字节。
  Serial.println("读取 NVRAM 值:");
  // 一次读取一个字节的示例
  Serial.println(rtc.readnvram(0), HEX);
  Serial.println(rtc.readnvram(1), HEX);
  // 读取多个字节的示例
  uint8_t readData[4] = {
    
    0};
  rtc.readnvram(readData, 4, 2);
  Serial.println(readData[0], HEX);
  Serial.println(readData[1], HEX);
  Serial.println(readData[2], HEX);
  Serial.println(readData[3], HEX);
}

void loop () {
    
    
  // 在循环中什么都不做。
}

(2) The return status of the experimental serial port

insert image description here
Program 6: Calculate the date after 7 days, 12 hours, 30 minutes and 6 seconds in the future
(1) Arduino reference open source code

/*
  【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)
  程序六:计算未来 7 天 12 小时 30 分钟 6 秒后的日期
*/

#include "RTClib.h"//导入DS1307驱动库
RTC_DS1307 rtc;
char daysOfTheWeek[7][12] = {
    
    "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};

void setup () {
    
    
  Serial.begin(57600);

#ifndef ESP8266
  while (!Serial); // 等待串口连接
#endif

  if (! rtc.begin()) {
    
    
    Serial.println("找不到 RTC");
    Serial.flush();
    while (1)
      delay(10);
  }

  if (! rtc.isrunning()) {
    
    
    Serial.println("RTC 没有运行,让我们设置时间!");
    // 当需要在新设备上设置时间时,或者在断电后
    rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
    rtc.adjust(DateTime(2022, 4, 10, 15, 35, 0));
  }
  // 此行使用明确的日期和时间设置 RTC,例如设置
  // 2022 年 4 月 10 日下午 15 点,可以这样:
  rtc.adjust(DateTime(2022, 4, 10, 15, 35, 0));
}

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(daysOfTheWeek[now.dayOfTheWeek()]);
  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(" 自 1970 年 1 月 1 日午夜起 =");
  Serial.print(now.unixtime());
  Serial.print("秒 = ");
  Serial.print(now.unixtime() / 86400L);
  Serial.println("天");

  // 计算未来 7 天 12 小时 30 分钟 6 秒后的日期
  DateTime future (now + TimeSpan(7, 12, 30, 6));

  Serial.print(" now + 7d + 12h + 30m + 6s: ");
  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);
}

(2) The return status of the experimental serial port

insert image description here
Arduino experiment scene diagram
insert image description here

insert image description here

Guess you like

Origin blog.csdn.net/weixin_41659040/article/details/132353758