|
本帖最后由 Plan3t 于 2018-4-30 19:52 编辑
电路引脚:
DS18B20:
VCC——电源正极
GND——地
OUT——D10
DS1302:
VCC——正极
GND——地
CLK——D7
DAT——D6
RST——D5
IIC:
VCC——正极
GND——地
SDA——A4
SCK——A5
连接好之后可能会不能显示,需要在LCD1602引脚A0端接一个电位计或者电阻,用来调整显示屏对比度调整时间,在串口监视器连续输入16个数,如2018042801170406,就是2018年4月28日 1点17分4秒 星期6
下面是整个程序,还有需要的库- //#########################################
- /* 接口定义
- CE(DS1302 pin5) -> D5
- IO(DS1302 pin6) -> D6
- SCLK(DS1302 pin7) -> D7
- */
- //################################
- #include <stdio.h>
- #include <string.h>
- #include <DS1302.h>
- #include <DallasTemperature.h> // DS18B20 库
- #include <LiquidCrystal_I2C.h> // I2C 1602
- #include <Wire.h> // I2C 库
- #include <OneWire.h>
- #define ONE_WIRE_BUS 10 // DS18B20 连接arduino D10引脚
- // 初始连接在单总线上的单总线设备
- OneWire oneWire(ONE_WIRE_BUS);
- DallasTemperature sensors(&oneWire);
- LiquidCrystal_I2C lcd(0x27,16,2); //设置LCD1602的I2C地址为0x27
- uint8_t CE_PIN = 5;
- uint8_t IO_PIN = 6;
- uint8_t SCLK_PIN = 7;
- byte nian[8] =
- {
- 0b01000,
- 0b01111,
- 0b10010,
- 0b01111,
- 0b01010,
- 0b11111,
- 0b00010,
- 0b00000
- };
- byte yue[8] =
- {
- 0b01111,
- 0b01001,
- 0b01111,
- 0b01001,
- 0b01111,
- 0b01001,
- 0b10011,
- 0b00000,
- };
- byte ri[8] =
- {
- 0b11111,
- 0b10001,
- 0b10001,
- 0b11111,
- 0b10001,
- 0b10001,
- 0b11111,
- 0b00000
- };
- byte temp[8]=
- {
- 0b10000,
- 0b01111,
- 0b01000,
- 0b01000,
- 0b01000,
- 0b01000,
- 0b01111,
- 0b00000, //温度标志— —摄氏度
- };
- // 日期变量缓存
- char buf[50];
- char day[10];
- // 串口数据缓存
- String comdata = "" ;
- int numdata[7] = {0}, j = 0, mark = 0;
- // 创建 DS1302 对象
- DS1302 rtc(CE_PIN, IO_PIN, SCLK_PIN);
- Time t;
- void print_time()
- {
- // 从 DS1302 获取当前时间
- t = rtc.time();
- // 将星期从数字转换为名称
- memset(day, 0, sizeof(day));
- switch (t.day)
- {
- case 7:
- strcpy(day, "Sunday");
- break;
- case 1:
- strcpy(day, "Monday");
- break;
- case 2:
- strcpy(day, "Tuesday");
- break;
- case 3:
- strcpy(day, "Wednesday");
- break;
- case 4:
- strcpy(day, "Thursday");
- break;
- case 5:
- strcpy(day, "Friday");
- break;
- case 6:
- strcpy(day, "Saturday");
- break;
- }
- // 将日期代码格式化凑成buf等待输出
- snprintf(buf, sizeof(buf), "%s %04d-%02d-%02d %02d:%02d:%02d", day, t.yr, t.mon, t.date, t.hr, t.min, t.sec);
- // 输出日期到串口
- Serial.println(buf);
- }
- void setup()
- {
- Serial.begin(9600);
- Serial.println("Temperature Project");
- rtc.write_protect(false);
- rtc.halt(false);
- lcd.init(); // 给LCD的I2C通讯初始化,需要执行两次
- delay(20);
- lcd.init(); // 给LCD的I2C通讯初始化
- delay(20);
- lcd.backlight();//点亮LCD背光灯
- rtc.write_protect(false);
- rtc.halt(false);
- lcd.begin(16, 2);
- lcd.createChar(0, nian);
- lcd.createChar(1, yue);
- lcd.createChar(2, ri);
- lcd.createChar(3, temp);
-
- }
- void loop()
- {
- // 当串口有数据的时候,将数据拼接到变量comdata
- Serial.print("Requesting temperatures..."); // 串口发送字符
- sensors.requestTemperatures(); // 传感器发送命令获取温度
- Serial.println("DONE"); // 串口发送字符并换行
- Serial.print("Temperature for the device 1 (index 0) is: ");
- Serial.println(sensors.getTempCByIndex(0));
- while (Serial.available() > 0)
- {
- comdata += char(Serial.read());
- delay(2);
- mark = 1;
- }
- //以逗号分隔分解comdata的字符串,分解结果变成转换成数字到numdata[]数组
- if (mark == 1)
- {
- Serial.print("You inputed : ");
- Serial.println(comdata);
- t.yr = (comdata[0] - '0') * 1000 + (comdata[1] - '0')*100 + (comdata[2] - '0') * 10 + (comdata[3] - '0'); //year
- t.mon = (comdata[4] - '0') * 10 + (comdata[5] - '0'); //month
- t.date = (comdata[6] - '0') * 10 + (comdata[7] - '0'); //date
- t.hr = (comdata[8] - '0') * 10 + (comdata[9] - '0'); //hour
- t.min = (comdata[10] - '0') * 10 + (comdata[11] - '0'); //minute
- t.sec = (comdata[12] - '0') * 10 + (comdata[13] - '0'); //second
- t.day = (comdata[14] - '0') * 10 + (comdata[15] - '0'); //week
- // 将转换好的numdata凑成时间格式,写入DS1302
- rtc.time(t);
- mark = 0;
- j = 0;//清空 comdata 变量,以便等待下一次输入
- comdata = String("");// 清空 numdata
- for (int i = 0; i < 7 ; i++) numdata = 0;
- }
- //打印当前时间
- print_time();
- lcd.setCursor(0, 0);
- lcd.print(t.yr);
- lcd.write(byte(0));
- lcd.print(t.mon);
- lcd.write(byte(1));
- lcd.print(t.date);
- lcd.write(byte(2));
- lcd.print(" ");
- lcd.setCursor(15, 0);
- lcd.print(t.day);
- lcd.setCursor(0, 1);
- lcd.print(t.hr);
- lcd.print(':');
- lcd.print(t.min);
- lcd.print(':');
- lcd.print(t.sec);
- lcd.print(" ");
- lcd.setCursor(10, 1); // 定位光标到第二行靠中位置
- lcd.print(sensors.getTempCByIndex(0)); // 显示温度值,来源DallasTemperature.h的函数
- lcd.write(byte(3)); // 不定位光标则继续前面语句继续写字符
- delay(1000);
- }
复制代码
|
评分
-
查看全部评分
|