找回密碼
 立即注冊(cè)

QQ登錄

只需一步,快速開(kāi)始

搜索
查看: 5871|回復(fù): 4
打印 上一主題 下一主題
收起左側(cè)

Arduino溫度記錄儀帶SD卡保存 DS1302+18b20在LCD1602上顯示

  [復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
ID:237671 發(fā)表于 2017-10-7 18:38 | 只看該作者 回帖獎(jiǎng)勵(lì) |倒序?yàn)g覽 |閱讀模式
從DS1302讀取時(shí)間,從18b20讀取溫度,在LCD1602上顯示,每隔十分鐘把數(shù)據(jù)寫(xiě)入SD卡保存。

Arduino源程序如下:
  1. #include <OneWire.h>
  2. #include <DallasTemperature.h>
  3. #include <Wire.h>
  4. #include <LiquidCrystal_I2C.h>
  5. LiquidCrystal_I2C lcd(0x27,16,2);
  6. #define ONE_WIRE_BUS 2
  7. OneWire oneWire(ONE_WIRE_BUS);
  8. DallasTemperature sensors(&oneWire);

  9. #include <stdio.h>
  10. #include <DS1302.h>
  11. namespace {
  12. const int kCePin   = 5;
  13. const int kIoPin   = 6;
  14. const int kSclkPin = 7;
  15. DS1302 rtc(kCePin, kIoPin, kSclkPin);
  16. String dayAsString(const Time::Day day) {
  17.   switch (day) {
  18.     case Time::kSunday: return "";
  19.     case Time::kMonday: return "";
  20.     case Time::kTuesday: return "";
  21.     case Time::kWednesday: return "";
  22.     case Time::kThursday: return "";
  23.     case Time::kFriday: return "";
  24.     case Time::kSaturday: return "";
  25.   }
  26.   return "(unknown day)";
  27. }
  28. }
  29. #include <SPI.h>
  30. #include <SD.h>
  31. const int chipSelect = 4;

  32. void setup(void)
  33. {
  34.   Serial.begin(9600);
  35.   sensors.begin();
  36.   lcd.init();
  37.   lcd.backlight();
  38.   rtc.writeProtect(false);
  39.   rtc.halt(false);
  40. //Time t(2017, 9,21,13,55, 0, Time::kThursday);//設(shè)置時(shí)間
  41. //rtc.time(t);//寫(xiě)入時(shí)間
  42.   while (!Serial) {;}
  43.   Serial.print("初始化SD卡...");
  44.   if (!SD.begin(chipSelect)) {
  45.     Serial.println("SD卡故障或無(wú)卡");
  46.     return;
  47.   }
  48.   Serial.println("SD卡初始化完成.");
  49. }
  50. void loop(void)
  51. {
  52.   Time t = rtc.time();
  53.   const String day = dayAsString(t.day);
  54.   char buf[50];
  55.   snprintf(buf, sizeof(buf), "%s%04d%02d%02d %02d%02d %02d",
  56.            day.c_str(),
  57.            t.yr, t.mon, t.date,
  58.            t.hr, t.min, t.sec);
  59.   Serial.print(buf);
  60.   Serial.print("   溫度");
  61.   sensors.requestTemperatures();
  62.   Serial.print(sensors.getTempCByIndex(0));
  63.   Serial.print("℃");
  64.   lcd.setCursor(0,0);
  65.   lcd.print(buf);
  66.   lcd.setCursor(0,1);
  67.   lcd.print(sensors.getTempCByIndex(0));
  68.   lcd.print((char)223);
  69.   lcd.print("C");
  70.     String dataString = "";
  71.   for (int analogPin = 0; analogPin < 3; analogPin++) {
  72.     int sensor = analogRead(analogPin);
  73.     dataString += String(sensor);
  74.     if (analogPin < 2) {
  75.       dataString += ",";
  76.     }
  77.   }
  78.   File dataFile = SD.open("Arduino.txt", FILE_WRITE);
  79.   if (dataFile) {
  80.     dataFile.print(buf);
  81.     dataFile.print("   溫度");
  82.     dataFile.print(sensors.getTempCByIndex(0));
  83.     dataFile.println("℃");
  84.     dataFile.close();
  85.     Serial.println("   數(shù)據(jù)成功寫(xiě)入SD卡");
  86.   }
  87.   else {
  88.     Serial.println("   數(shù)據(jù)寫(xiě)入SD卡失敗");
  89.   }
  90.   delay(1000);
  91. }
復(fù)制代碼

所有資料51hei提供下載:
Arduino溫度記錄儀.zip (1.13 KB, 下載次數(shù): 71)


評(píng)分

參與人數(shù) 1黑幣 +2 收起 理由
碌碌無(wú)為 + 2 贊一個(gè)!

查看全部評(píng)分

分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏6 分享淘帖 頂 踩
回復(fù)

使用道具 舉報(bào)

沙發(fā)
ID:230374 發(fā)表于 2017-10-16 13:10 | 只看該作者
謝謝分享
回復(fù)

使用道具 舉報(bào)

板凳
ID:87193 發(fā)表于 2017-10-18 14:19 | 只看該作者
附件是什么內(nèi)容呢,黑幣不多,要省著點(diǎn)用
回復(fù)

使用道具 舉報(bào)

地板
ID:422781 發(fā)表于 2018-11-24 09:22 | 只看該作者
收藏備用,SD卡上是什么文件格式,txt?csv?
回復(fù)

使用道具 舉報(bào)

5#
ID:373610 發(fā)表于 2018-11-24 16:37 | 只看該作者
收藏備用,SD卡上是什么文件格式,txt?csv?
回復(fù)

使用道具 舉報(bào)

本版積分規(guī)則

手機(jī)版|小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機(jī)教程網(wǎng)

快速回復(fù) 返回頂部 返回列表