|
本帖最后由 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);
- }
復制代碼
|
評分
-
查看全部評分
|