arduino 读取当前时间_Arduino使用DS1302读取时间
在科技爱好者博客原来的文章中,使用树莓派和DS1302链接,来读取实时时间,感兴趣的可以点击查看树莓派使用DS1302实现实时时钟功能。本文教大家在Arduino 上使用DS1302来读取当前时间。
一、DS1302 芯片介绍
DS1302实时时钟模块,内含有一个实时时钟/日历和31 字节静态RAM ,通过简单的串行接口与单片机进行通信。实时时钟/日历电路提供秒、分、时、日、周、月、年的信息,每月的天数和闰年的天数可自动调整。时钟操作可通过AM/PM 指示决定采用24 或12 小时格式。DS1302 与单片机之间能简单地采用同步串行的方式进行通信,仅需用到三个口线:(1)RST 复位(2)I/O 数据线(3)SCLK串行时钟。时钟/RAM 的读/写数据以一个字节或多达31 个字节的字符组方式通信。DS1302 工作时功耗很低保持数据和时钟信息时功率小于1mW。
更加详细的 DS1302实时时钟模块 介绍可以查看这篇文章: 树莓派使用DS1302实现实时时钟功能 。
二、Arduino 与DS1302硬件连接
我使用的是DS1302的模块,这样方便连接和使用。
DS1302时钟模块
VCC连接Arduino 3.3V接口
GND连接 Arduino 接地接口
CLK连接 Arduino 11接口
DAT 连接 Arduino 10接口
RST 连接 Arduino 9接口
三、程序设计
在编写程序前,首先要下载DS1302的库,点击这里下载。
下载完成后,将库文件放在Ardunio的libraries文件夹。
之后打开IDE,编写程序,代码如下。
#include
#include
namespace {
// Set the appropriate digital I/O pin connections. These are the pin
// assignments for the Arduino as well for as the DS1302 chip. See the DS1302
// datasheet:
//
// http://datasheets.maximintegrated.com/en/ds/DS1302.pdf
const int kCePin = 9; // Chip Enable
const int kIoPin = 10; // Input/Output
const int kSclkPin = 11; // Serial Clock
// Create a DS1302 object.
DS1302 rtc(kCePin, kIoPin, kSclkPin);
String dayAsString(const Time::Day day) {
switch (day) {
case Time::kSunday: return "Sunday";
case Time::kMonday: return "Monday";
case Time::kTuesday: return "Tuesday";
case Time::kWednesday: return "Wednesday";
case Time::kThursday: return "Thursday";
case Time::kFriday: return "Friday";
case Time::kSaturday: return "Saturday";
}
return "(unknown day)";
}
void printTime() {
// Get the current time and date from the chip.
Time t = rtc.time();
// Name the day of the week.
const String day = dayAsString(t.day);
// Format the time and date and insert into the temporary buffer.
char buf[50];
snprintf(buf, sizeof(buf), "%s %04d-%02d-%02d %02d:%02d:%02d",
day.c_str(),
t.yr, t.mon, t.date,
t.hr, t.min, t.sec);
// Print the formatted string to serial so we can see the time.
Serial.println(buf);
}
} // namespace
void setup() {
Serial.begin(9600);
// Initialize a new chip by turning off write protection and clearing the
// clock halt flag. These methods needn't always be called. See the DS1302
// datasheet for details.
rtc.writeProtect(false);
rtc.halt(false);
// Make a new time object to set the date and time.
// Sunday, September 22, 2020 at 01:38:50.
Time t(2020, 9, 22, 1, 38, 50, Time::kSunday);
// Set the time and date on the chip.
rtc.time(t);
}
// Loop and print the time every second.
void loop() {
printTime();
delay(1000);
}
完成后编译下载,打开串口调试工具,可以看到实时时间。
实时时间
arduino 读取当前时间_Arduino使用DS1302读取时间相关推荐
- arduino 读取当前时间_Arduino 101/Genuino 101 时间控制函数
时间控制函数 运行时间函数 使用运行时间函数millis(),能获取Arduino通电后(或复位后)到现在的时间. [mw_shl_code=cpp,true]millis()[/mw_shl_cod ...
- 【原创】数据库中时间字段的值读取到DataSet中后,毫秒部分丢失。
数据库中时间字段的值读取到DataSet中后,毫秒部分丢失. 在网上查了些解决方案,说读取时间时,先转化为DateTime类型,然后在ToString("yyyy-MM-dd HH:mm:s ...
- (2)arduino读取电压相对值(模拟读取传感器数值)
arduino读取电压相对值 准备工作 接线图 读取电压值代码 准备工作 需要准备的材料有:电位器一个.arduino uno单片机.杜邦线.面包板 接线图 电位器与滑动变阻器相似,若将其两端连接5V ...
- Arduino ESP32 Web服务器从microSD卡读取
Arduino ESP32 Web服务器从microSD卡读取 将网页内容放到SD卡中,当有客户端数据请求时,从SD卡读取信息进行发送,比起SPIFFS文件系统,更节约板上的硬件资源. SD卡接线说明 ...
- Arduino提高篇22—实时时钟DS1302
很多电子爱好者都热衷制作电子时钟来练手,这些时钟会使用数码管.点阵屏.LCD液晶屏.OLED屏.TFT屏等显示模块,所使用的RTC时钟芯片也各不相同,DS1302.DS3231.DS12C887等.通 ...
- arduino自带程序_arduino代码运行时间测试函数,代码性能运行时间测试方法
先感谢"皮皮学电子"同学的帮助,秒回完美快速的解决了我的问题(难得遇到明白人).我在GitHub上找了半天,以为需要额外的库函数调用定时器才能实现代码运行时间的测试功能,没想到ar ...
- 基于STM32F103——DS1302日期时间+串口打印
DS1302时钟模块串口打印 DS1302时钟模块相关介绍 基本介绍 概述 特点 各引脚功能 相关寄存器 时序图 单字节写时序 单字节读时序 时钟/日历多字节(Burst)方式 BCD转十进制和十进制 ...
- arduino平衡车超声波_Arduino Nano DIY超声波测距平衡小车源码与资料分享
采用Arduino Nano控制模块制作的超声波测距平衡小车 1.项目概述 因为第一次制作自平衡小车,心里没底,也就没有投入过多的资金,一切按照最小配置进行,所以选择"TT马达", ...
- DS1302的时间明显快很多的原因
DS1302是一款常用的时钟芯片,原来也经常使用,不过最近使用过程中发现了一个问题,就是DS1302的时间走得太快,难道是晶振的原因? 很显然,晶振的误差不可能导致时钟芯片运行快很多,所以最大的可能就 ...
- DS1302输出时间,在lcd上显示
完整电路图: 完整代码: #include <reg51.h> #include <intrins.h> //包含_nop_( )空函数指令的头文件#define uchar ...
最新文章
- petshop4.0 详解之一(系统架构设计)
- 全血微量元素团标今起实施 李喜贵:两项标准填补国内外空白
- php鼠标悬停字体变大代码CS6,Dreamweaver cs6设置代码字体大小的方法
- mysql 有ntext_深入char、varchar、text和nchar、nvarchar、ntext的区别详解
- 如何在Windows 10上安装MySQL数据库服务器8.0.19
- 《SOA In the real world》第一章译稿(含下载)
- 卸载python会删除pip安装的包吗_python 使用pip安装,卸载,升级和查看包
- C++中的error C2662,const的this指针问题
- Python爬取wfxnews 小说网站,实现批量下载小说
- 地下停车场的车内与车外,揭露了中年男人内心最沉重的痛
- 【前端框架】Element UI Dialog 组件中执行 DOM 操作异常问题的分析与处理
- 压缩算法——FLAC (Free Lossless Audio Codec)编码原理
- 上拉、下拉电阻的原理和作用
- 互联网日报 | 腾讯地图上线聚合打车服务;瑞幸咖啡等公司被罚6100万元;中通快递下周二香港上市...
- arm linux 识别新硬盘_嵌入式Linux 的NTFS移动硬盘支持
- 最厉害的面试技巧都有哪些?
- Python全栈 Linux基础之1.Linux初章
- 名悦集团:开车从不追尾,老司机分享驾驶避免事故小知识
- 简单示例mock的使用 (Junit结合Jmock)
- 软件测试定义/过程/分类