oled_sh1106+ds1307 实现

  • 前言
  • OLED_SH1106
  • DS1307时钟模块
  • 时间显示

前言

Arduino 有个很强大的开源的库 u8g2 ,它支持了现在绝大多数的OLED屏幕,至于它的基本用法可以参考我的另一篇笔记,当然这只是我自己的学习笔记写的不是太好,我在文章里贴了官方参考链接可以感兴趣可以去看看。当然,你也可以参考这个博主的单片机菜鸟哥

OLED_SH1106

sh1106是一款1.3寸大小的OLED屏幕,一般有I2C接口和SPI接口,可以将其连接到Arduino Board 的硬件I2C或SPI接口,也可以用软件模拟的方式接到数字I/O口

DS1307时钟模块

硬件介绍我这里就略过了
引脚连接:
SDA------>Arduino SDA
SCL------>Arduino SCL
VCC------>Arduino 5V
GND----->Arduino GND
我使用的是,Arduino的 RTClib 库,可以自己下载,首先我们来看看它的示例程序

// Date and time functions using a DS1307 RTC connected via I2C and Wire lib
#include "RTClib.h"RTC_DS1307 rtc;char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};void setup () {Serial.begin(57600);#ifndef ESP8266while (!Serial); // wait for serial port to connect. Needed for native USB
#endifif (! rtc.begin()) {Serial.println("Couldn't find RTC");Serial.flush();abort();}if (! rtc.isrunning()) {Serial.println("RTC is NOT running, let's set the time!");// When time needs to be set on a new device, or after a power loss, the// following line sets the RTC to the date & time this sketch was compiledrtc.adjust(DateTime(F(__DATE__), F(__TIME__)));// This line sets the RTC with an explicit date & time, for example to set// January 21, 2014 at 3am you would call:// rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));}// When time needs to be re-set on a previously configured device, the// following line sets the RTC to the date & time this sketch was compiled// rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));// This line sets the RTC with an explicit date & time, for example to set// January 21, 2014 at 3am you would call:// rtc.adjust(DateTime(2014, 1, 21, 3, 0, 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(" since midnight 1/1/1970 = ");Serial.print(now.unixtime());Serial.print("s = ");Serial.print(now.unixtime() / 86400L);Serial.println("d");// calculate a date which is 7 days, 12 hours, 30 minutes, and 6 seconds into the futureDateTime 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);
}

语句解释:
<1> RTC_DS1307 rtc---->定义一个trc模块
<2> DateTime now = rtc.now()------> 调用时间日期函数存入 now 中
<3> now.year() now.month() now.day() now.hour() now.minute() now.second()------> 获取当前具体时间或日期
<4> noww.unixtime()------>时间求和

时间显示

最后只需要将两个模块的代码缝合整理到一起便可以实现实时时间显示了

#include "RTClib.h"
#include <Arduino.h>
#include <U8g2lib.h>
#include <SPI.h>
U8G2_SH1106_128X64_NONAME_1_4W_HW_SPI u8g2(U8G2_R0, /* cs=*/ 10, /* dc=*/ 9, /* reset=*/ 8);
RTC_DS1307 rtc;    //Setup the ds1307
//char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
// TO made the WEEK
void setup() {// put your setup code here, to run once:Serial.begin(115200);u8g2.begin();u8g2.enableUTF8Print();if(! rtc.begin()){Serial.println("Could't fine RTC");Serial.flush();abort();}if (! rtc.isrunning()){Serial.println("RTC is not running, let's set the time!");rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));}
}void loop() {// put your main code here, to run repeatedly:DateTime now = rtc.now();u8g2.setFontDirection(0);u8g2.firstPage();do{u8g2.setFont(u8g2_font_ncenB10_tr);u8g2.setCursor(20,10);u8g2.print("Welcome!");u8g2.setCursor(0,30);u8g2.print("Date:");u8g2.setCursor(50,30);u8g2.print(now.year());u8g2.print('/');u8g2.print(now.month());u8g2.print('/');u8g2.println(now.day());u8g2.setCursor(0,50);u8g2.print("Time:");u8g2.setCursor(50,50);u8g2.print(now.hour());u8g2.print(':');u8g2.print(now.minute());u8g2.print(':');u8g2.println(now.second()+10);}while (u8g2.nextPage());delay(1000);}

ps:如果没有判断if (! trc.begin()),要记得使用rtc.begin()语句初始化rtc模块
最后:实验效果图

在昨晚时间显示这个实验之后一天,当我打算再次重复这个实验时,我打开了1307的示例程序连接硬件,上传后却发现用不了,打开串口,串口输出的是“Cound’t find RTC”,我去Arduino的各种英文论坛找了很久,试着把下面这一段放到 loop 里面

if (! rtc.begin()) {Serial.println("Couldn't find RTC");Serial.flush();abort();}if (! rtc.isrunning()) {Serial.println("RTC is NOT running, let's set the time!");rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));}

为了使循环可以进行,我删除了

    Serial.flush();abort();

这两句代码,结果就是时钟模块输出一直卡在一个不正常的值,类似于这种:

经过几个小时的不屑努力查找,终于我明白了:在上传代码的时候记得拔掉硬件 I2C 通信先!!!,因为它也属于串口通信,这就跟你在上传程序时不能占用 TX RX 两个引脚的硬件串口通信是一样的道理。


2021/10/19 更新
关于我前面说的时钟模块的问题,经过我几天的摸索发现,在使用前你只需要对时钟模块的时间校准(设置)一次就够了,之后将电池装上后模块会自己运行,每次你要使用时调用RTClib库的**begin()**就行了。

Arduino时钟显示相关推荐

  1. Android消息机制——时钟显示和异步处理工具类(AsyncTask)

    1. 时钟显示 定义布局文件--activity_my_analog_clock_thread_demo.xml <?xml version="1.0" encoding=& ...

  2. 使用串口发送实现ACX720开发板时钟显示

    时钟显示模块控制 digital_clock顶层模块控制 串口发送模块 hex_top hex_display模块 hex_hc595模块 TB文件展示 digital_clock顶层模块控制 用于用 ...

  3. JavaScript实现动态时钟显示

    目录 动态时钟显示效果 代码实现 1.创建html文件(时钟显示.html) 2.设置html标签 3.设置html标签的CSS样式 4.设置JavaScript 1)创建函数和Date 2)获取da ...

  4. linux桌面环境调整时钟,小技巧:Linux个性化面版时钟显示

    默认情况下,GNOME桌面的面版的时钟小程序只有少许的可控制选项,我们只能选择12/24制式,或者选择日期/秒针的显示. 现在,这则小技巧让你完全自由的控制你的时钟显示. 首先打开终端或按Alt+F2 ...

  5. html时显示当前时间的时钟,javascript实现页面的实时时钟显示示例

    时钟实现 实现这个时钟时间需要解决以下三个问题: 获得当前时间,并格式化 如何可以在页面中显示时间 让时间动起来 1.获得当前时间,并格式化 要获得当前时间,可以使用JavaSctipt的Date对象 ...

  6. 基于FPGA的数字钟——(三)时钟显示模块(数码管)

    基于FPGA的数字钟--(三)数码管显示模块 一.硬件原理 本设计中使用 6 个共阳数码管,可以显示 6 个数字(包含小数点) .电路用 PNP管来反向驱动并且控制列扫描信号来选择哪个数码管.而且所有 ...

  7. win7任务栏时钟显示秒_如何使Windows 10的任务栏时钟显示秒数

    win7任务栏时钟显示秒 Windows 10's taskbar clock can display the precise time down to the second. This featur ...

  8. 基于Arduino的显示测量环境数据设计

    题目: 基于Arduino的显示测量环境数据设计 目录 基于Arduino的显示测量环境数据设计... 3 第一章 课题任务... 4 1.1课题任务... 4 1.2任务分工... 4 1.3设计条 ...

  9. linux将时钟放在桌面上的,小技巧:Linux个性化面版时钟显示

    默认情况下,GNOME桌面的面版的时钟小程序只有少许的可控制选项,我们只能选择12/24制式,或者选择日期/秒针的显示. 现在,这则小技巧让你完全自由的控制你的时钟显示. 首先打开终端或按Alt+F2 ...

最新文章

  1. 【Unity教程】创建一个完整的驾驶游戏
  2. JavaScript初学者编程题(13)
  3. 陈立杰再获FOCS 2019最佳学生论文奖
  4. Java与JavaScript 完美实现字符串拆分(利用数组存储)与合并的互逆操作
  5. 二叉树的层序遍历和二叉树的线索化
  6. c++ 把数字和中文字符分开_C语言中的字符常量与变量
  7. Vue.js的复用组件开发流程
  8. 计算机用户域怎么删除,如何查找并删除AD域中多余的计算机帐号?
  9. WebKit 分析–for android【new】
  10. Python之路-4
  11. uniapp 小程序列表懒加载
  12. Objective-C 函数
  13. 哔哩哔哩中缓存的视频和音频如何合并
  14. 图片调整大小后变得不清晰了怎么办?
  15. 智能车OS照搬安卓没有出路,特别是在中国
  16. android studio官方教程 pdf,android studio教程pdf
  17. ctf ddos数据包 杂项 流量_抗DDoS攻击设备化解危机于无形
  18. 干货笔记,数据仓库工具箱
  19. CDH6.3.1监控界面Hive修改参数
  20. 事件回滚 rollback

热门文章

  1. 从零开始学习SEO的基础概念
  2. ArcGIS打开影像图显示全黑色解决办法
  3. 「To B端增长黑客」 获客矩阵
  4. CAN通信----电路图
  5. 【转】葡萄酒的干型、半干型、半甜型、甜型是什么意思?
  6. 充电口 米兔积木机器人_米兔积木机器人怎么充电
  7. 英文版xp系统下载ghost xp sp3英文版(双语纯净版、可随意转换)
  8. matlab怎么画两个自变量的图_眼线液的画步骤图 眼线怎么画好看图解
  9. 关于redis服务的代码编码
  10. minio服务器在win10的上传与下载,以及修改头像Minio速看免费本地文件服务器