|
本帖最后由 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
連接好之后可能會(huì)不能顯示,需要在LCD1602引腳A0端接一個(gè)電位計(jì)或者電阻,用來(lái)調(diào)整顯示屏對(duì)比度調(diào)整時(shí)間,在串口監(jiān)視器連續(xù)輸入16個(gè)數(shù),如2018042801170406,就是2018年4月28日 1點(diǎn)17分4秒 星期6
下面是整個(gè)程序,還有需要的庫(kù)- //#########################################
- /* 接口定義
- 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 庫(kù)
- #include <LiquidCrystal_I2C.h> // I2C 1602
- #include <Wire.h> // I2C 庫(kù)
- #include <OneWire.h>
- #define ONE_WIRE_BUS 10 // DS18B20 連接arduino D10引腳
- // 初始連接在單總線上的單總線設(shè)備
- OneWire oneWire(ONE_WIRE_BUS);
- DallasTemperature sensors(&oneWire);
- LiquidCrystal_I2C lcd(0x27,16,2); //設(shè)置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, //溫度標(biāo)志— —攝氏度
- };
- // 日期變量緩存
- char buf[50];
- char day[10];
- // 串口數(shù)據(jù)緩存
- String comdata = "" ;
- int numdata[7] = {0}, j = 0, mark = 0;
- // 創(chuàng)建 DS1302 對(duì)象
- DS1302 rtc(CE_PIN, IO_PIN, SCLK_PIN);
- Time t;
- void print_time()
- {
- // 從 DS1302 獲取當(dāng)前時(shí)間
- t = rtc.time();
- // 將星期從數(shù)字轉(zhuǎn)換為名稱(chēng)
- 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通訊初始化,需要執(zhí)行兩次
- delay(20);
- lcd.init(); // 給LCD的I2C通訊初始化
- delay(20);
- lcd.backlight();//點(diǎn)亮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()
- {
- // 當(dāng)串口有數(shù)據(jù)的時(shí)候,將數(shù)據(jù)拼接到變量comdata
- Serial.print("Requesting temperatures..."); // 串口發(fā)送字符
- sensors.requestTemperatures(); // 傳感器發(fā)送命令獲取溫度
- Serial.println("DONE"); // 串口發(fā)送字符并換行
- 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;
- }
- //以逗號(hào)分隔分解comdata的字符串,分解結(jié)果變成轉(zhuǎn)換成數(shù)字到numdata[]數(shù)組
- 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
- // 將轉(zhuǎn)換好的numdata湊成時(shí)間格式,寫(xiě)入DS1302
- rtc.time(t);
- mark = 0;
- j = 0;//清空 comdata 變量,以便等待下一次輸入
- comdata = String("");// 清空 numdata
- for (int i = 0; i < 7 ; i++) numdata = 0;
- }
- //打印當(dāng)前時(shí)間
- 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); // 定位光標(biāo)到第二行靠中位置
- lcd.print(sensors.getTempCByIndex(0)); // 顯示溫度值,來(lái)源DallasTemperature.h的函數(shù)
- lcd.write(byte(3)); // 不定位光標(biāo)則繼續(xù)前面語(yǔ)句繼續(xù)寫(xiě)字符
- delay(1000);
- }
復(fù)制代碼
|
評(píng)分
-
查看全部評(píng)分
|