极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 10886|回复: 0

我做了一个时钟,液晶屏是i2c的,但是这个代码只显示第二位

[复制链接]
发表于 2016-2-16 17:35:35 | 显示全部楼层 |阅读模式
[pre lang=""]#include "RTClib.h"
#include <Wire.h>
#include <LiquidCrystal_I2C.h>


RTC_DS1307  DS1307; //给DS1307的库设置对象,对象名DS1307
LiquidCrystal_I2C lcd(0x27,16,2);
int h;
int m;
int s;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  lcd.begin(16,2);
  Wire.begin();
  lcd.clear();
  lcd.init(); // 给LCD的I2C通讯初始化,需要执行两次
  delay(20);
  lcd.init(); // 给LCD的I2C通讯初始化
  delay(20);
  lcd.backlight();//点亮LCD背光灯
  DS1307.begin(); //初始化时钟
  DS1307.set(RTC_SECOND, 00);  //初始化时钟的时间
  DS1307.set(RTC_MINUTE, 30);
  DS1307.set(RTC_HOUR, 15);
  DS1307.set(RTC_DAY, 16);
  DS1307.set(RTC_MONTH, 2);
  DS1307.set(RTC_YEAR, 16);
}

void loop() {
DateTime now = DS1307.now();// 获取当前时间给now实例
  h = now.hour(); // 获取当前时间小时
  m = now.minute(); // 获取当前时间给分钟
  s = now.second(); // 获取当前时间给秒
  /*把信息输出到LCD上*/
  lcd.clear();
  lcd.print("T");
  lcd.print("i");
  lcd.print("m");
  lcd.print("e");
  lcd.print(":");
  lcd.setCursor(0, 1) ; //将光标显示在第二行
  lcd.print(" ");
  lcd.print(" ");
  lcd.print(" ");
  lcd.print(" ");
  lcd.print(" ");
  lcd.print(h);//在LCD第二行上输出hour
  lcd.print(':');
  lcd.print(m);//在LCD上输出minute
  lcd.print(':');
  lcd.print(s);//在LCD上输出second
  lcd.print("     ");//在LCD上输出两个空格
    delay(1);
}
void printDateTime(DateTime dateTime) {
    //传送年份
    Serial.print(dateTime.year(), DEC);
    Serial.print('/');
    //传送月份
    Serial.print(dateTime.month(), DEC);
    Serial.print('/');
    //传送月份中的第几天
    Serial.print(dateTime.day(), DEC);
    Serial.print(' ');
    //传送小时
    Serial.print(dateTime.hour(), DEC);
    Serial.print(':');
    //传送分钟
    Serial.print(dateTime.minute(), DEC);
    Serial.print(':');
    //传送秒
    Serial.print(dateTime.second(), DEC);
    Serial.println();
}
[/code]
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|联系我们|极客工坊

GMT+8, 2026-6-16 19:42 , Processed in 0.039696 second(s), 17 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表