找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 5923|回复: 4
打印 上一主题 下一主题
收起左侧

Arduino温度记录仪带SD卡保存 DS1302+18b20在LCD1602上显示

  [复制链接]
跳转到指定楼层
楼主
ID:237671 发表于 2017-10-7 18:38 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
从DS1302读取时间,从18b20读取温度,在LCD1602上显示,每隔十分钟把数据写入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);//设置时间
  41. //rtc.time(t);//写入时间
  42.   while (!Serial) {;}
  43.   Serial.print("初始化SD卡...");
  44.   if (!SD.begin(chipSelect)) {
  45.     Serial.println("SD卡故障或无卡");
  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("   数据成功写入SD卡");
  86.   }
  87.   else {
  88.     Serial.println("   数据写入SD卡失败");
  89.   }
  90.   delay(1000);
  91. }
复制代码

所有资料51hei提供下载:
Arduino温度记录仪.zip (1.13 KB, 下载次数: 71)


评分

参与人数 1黑币 +2 收起 理由
碌碌无为 + 2 赞一个!

查看全部评分

分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏6 分享淘帖 顶 踩
回复

使用道具 举报

沙发
ID:230374 发表于 2017-10-16 13:10 | 只看该作者
谢谢分享
回复

使用道具 举报

板凳
ID:87193 发表于 2017-10-18 14:19 | 只看该作者
附件是什么内容呢,黑币不多,要省着点用
回复

使用道具 举报

地板
ID:422781 发表于 2018-11-24 09:22 | 只看该作者
收藏备用,SD卡上是什么文件格式,txt?csv?
回复

使用道具 举报

5#
ID:373610 发表于 2018-11-24 16:37 | 只看该作者
收藏备用,SD卡上是什么文件格式,txt?csv?
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表