56832926 发表于 2015-1-17 10:58:07

1602LCD_1307RTC_DS18B20

本帖最后由 56832926 于 2015-1-17 12:53 编辑

今天在弘毅大牛关于   “arduino学习笔记27 - DS1307 RTC时钟芯片与DS18B20数字温度传感器实验 ”的基础上实现了1602液晶显示日期、时间、温度值。以供大家学习!
DS1307和DS18B20(注此图借用):

结果如图:

程序如下:
/*
* LCD RS pin to digital pin 12
* LCD Enable pin to digital pin 11
* LCD D4 pin to digital pin 5
* LCD D5 pin to digital pin 4
* LCD D6 pin to digital pin 3
* LCD D7 pin to digital pin 2
* LCD R/W pin to ground
* 10K resistor:
* ends to +5V and ground
* wiper to LCD VO pin (pin 3)
* DS1307 SDA pin to analog pin A4
* DS1307 SCL pin to analog pin A5
* DS18B20 DS pin to digital pin 8
*/
#include <LiquidCrystal.h>
#include <DS1307RTC.h>
#include <Time.h>
#include <Wire.h>
#include <OneWire.h>
#include <DallasTemperature.h>

#define ONE_WIRE_BUS 8
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

OneWire oneWire(ONE_WIRE_BUS);

DallasTemperature sensors(&oneWire);

tmElements_t tm;

void setup() {
sensors.begin();
lcd.begin(16, 2);   
}

void loop() {
sensors.requestTemperatures();
tmElements_t tm;

if (RTC.read(tm)) {
    lcd.setCursor(0, 0);
    print2digits(tm.Hour);
    lcd.write(':');
    print2digits(tm.Minute);
    lcd.write(':');
    print2digits(tm.Second);
    lcd.setCursor(10, 0);
    lcd.print(sensors.getTempCByIndex(0),2);
    lcd.print("C");
    lcd.setCursor(0, 1);
    lcd.print("Date:");
    lcd.print(tm.Day);
    lcd.write('/');
    lcd.print(tm.Month);
    lcd.write('/');
    lcd.print(tmYearToCalendar(tm.Year));
}
}

void print2digits(int number) {
if (number >= 0 && number < 10) {
    lcd.write('0');
}
lcd.print(number);
}

darkorigin 发表于 2015-1-18 23:33:54

不错啊 还可以把数据存储起来或者上传到上位机 这样可以绘制一条温度曲线了

56832926 发表于 2015-1-19 09:18:10

darkorigin 发表于 2015-1-18 23:33 static/image/common/back.gif
不错啊 还可以把数据存储起来或者上传到上位机 这样可以绘制一条温度曲线了

:D不错不错,可以试试:)

老实人 发表于 2015-4-5 12:31:12

不知道1602是怎样连接的
页: [1]
查看完整版本: 1602LCD_1307RTC_DS18B20