只接通电源,SD卡无法存储时间模块DS1307里的时机-Arduino中文社区 - Powered by Discuz!

Arduino中文社区

 找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 1828|回复: 3

[未解决] 只接通电源,SD卡无法存储时间模块DS1307里的时机

[复制链接]
发表于 2021-8-20 15:51 | 显示全部楼层 |阅读模式
       大佬们又是我,一直以来都麻烦大家啦!非常感谢大家无私的帮助。       我这里又遇到了一个问题,我这里的程序是想用SD卡记录两个传感器的数据和上传数据的时间,在电脑串口监视器里没有问题,此时SD卡的时间是现实的时间。但是,一旦编程用9V的电源供电的话,SD卡的时间就不正常了。附上我的程序,我把我用到的时间那部分标红,麻烦各位了

   

  1. #include <SoftwareSerial.h>
  2. #include <SPI.h>
  3. #include <SD.h>
  4. [color=#ff0000]#include <TimeLib.h>[/color]
  5. [color=#ff0000]#include <Wire.h>[/color]
  6. [color=#ff0000]#include <DS1307RTC.h>[/color]
  7. SoftwareSerial mySerial1(8, 9); // RX, TX
  8. SoftwareSerial mySerial2(2, 3); // RX, TX
  9. String comdata = "";
  10. unsigned char DEC1, DEC2;
  11. byte buff[10];                     
  12. byte flag;
  13. byte request_cmd[9] = {0xFF, 0x01, 0x86, 0x00, 0x00, 0x00,0x00, 0x00,0x79};
  14. //byte request_cmd[9] = [255,1,134,0,0,0,0,0,121];

  15. int Val_1_1;
  16. int Val_1_2;
  17. int Val_2_1;
  18. int Val_2_2;

  19. bool islistening;
  20. int i=0;
  21. void Ser_1();
  22. void Ser_2();

  23. const int chipSelect = 4;      

  24. unsigned long processSyncMessage();

  25. [color=#ff0000]int hous;[/color]
  26. [color=#ff0000]int mins;[/color]
  27. [color=#ff0000]int secs;[/color]
  28. [color=#ff0000]int days;[/color]
  29. [color=#ff0000]int mons;[/color]
  30. [color=#ff0000]int yeas;[/color]

  31. int buzzer = 7;
  32. void setup() {
  33.    Serial.begin(9600);
  34. //  while (!Serial){}
  35.   mySerial1.begin(9600);
  36.   mySerial2.begin(9600);
  37.   pinMode(chipSelect,OUTPUT);
  38.   pinMode(buzzer,OUTPUT);

  39.   delay(1000);
  40.   [color=#ff0000]setSyncProvider(RTC.get);   [/color]
  41. [color=#ff0000]  if (timeStatus() != timeSet) [/color]
  42. [color=#ff0000]     Serial.println("Unable to sync with the RTC");[/color]
  43. [color=#ff0000]  else[/color]
  44. [color=#ff0000]     Serial.println("RTC has set the system time");[/color]
  45.   Serial.println("Initializing SD card");
  46.   if(!SD.begin(chipSelect))
  47.   {
  48.     Serial.println("initializing failed!");
  49.     while(1);
  50.   }
  51.   Serial.println("initialization done.");
  52. }


  53. void loop() {

  54.   Ser_1();
  55.   Ser_2();
  56.   if(Val_1_1 || Val_1_2 || Val_2_1 || Val_2_2)
  57.   {
  58.     digitalWrite(buzzer,HIGH);  
  59.   }
  60. }

  61. void Ser_1(){
  62. [color=#ff0000]if (Serial.available()) {[/color]
  63. [color=#ff0000]    time_t t = processSyncMessage();[/color]
  64. [color=#ff0000]    if (t != 0) {[/color]
  65. [color=#ff0000]      RTC.set(t);   // set the RTC and the system time to the received value[/color]
  66. [color=#ff0000]      setTime(t);          [/color]
  67. [color=#ff0000]    }[/color]
  68. [color=#ff0000]  }[/color]
  69. [color=#ff0000]  [/color]
  70. [color=#ff0000]  hous = hour();[/color]
  71. [color=#ff0000]  mins = minute();[/color]
  72. [color=#ff0000]  secs = second(); [/color]
  73. [color=#ff0000]  days = day();[/color]
  74. [color=#ff0000]  mons = month();[/color]
  75. [color=#ff0000]  yeas = year();[/color]
  76.   Serial.print("----------Ser_1----------");
  77.   mySerial1.listen();
  78.   delay(500);
  79.   Serial.print("Ser_1 request start...-->");
  80.   for (i = 0; i<9;i++){
  81.     mySerial1.write(request_cmd[i]);
  82.     Serial.print(request_cmd[i], HEX);
  83.     if (i<8){
  84.       Serial.print('-');
  85.     }
  86.     delay(2);
  87.   }
  88.   Serial.println("");
  89.   buff[0]=0;
  90.   i=0;

  91.   islistening = mySerial1.isListening();
  92.   mySerial1.flush();
  93. //  delay(900);

  94.   while (mySerial1.available() > 0 && i<9) {
  95.     if (i == 0){
  96.       Serial.print("1 is listening...-->");
  97.     }
  98.     buff[i]= mySerial1.read();
  99.     i++;
  100.     delay(2);
  101.   }
  102.   if (buff[0] == 255) {
  103.     DEC1 = buff[2] & 0x1F;
  104.     DEC2 = buff[3];
  105.     Val_1_1 = DEC1 * 256 + DEC2;
  106.     DEC1 = buff[6] & 0x1F;
  107.     DEC2 = buff[7];
  108.     Val_1_2 = DEC1 * 256 + DEC2;
  109. //    Serial.print("1 is listening...-->");
  110.     for (i=0; i<9; i++){
  111.         Serial.print(buff[i], HEX);
  112.         if (i<8){
  113.           Serial.print("-");
  114.         }
  115.       }
  116.     Serial.println("");
  117.     Serial.print("1_1 value: ");
  118.     Serial.println(Val_1_1);
  119.     Serial.print("1_2 value: ");
  120.     Serial.println(Val_1_2);
  121.     Serial.println("----------Ser_1----------");
  122.     Serial.println("");
  123.   }
  124.    //如果值大于1,打开文件并将检测到的数据写入文件
  125. //  if(Val_1_1 || Val_1_2 > 0){
  126.     Serial.println("Open file and write data1");
  127.     File dataFile = SD.open("datalog.txt",FILE_WRITE);
  128.     if(dataFile)
  129.     {   
  130.       [color=#ff0000]dataFile.print(hous);[/color]
  131. [color=#ff0000]      dataFile.print(":");[/color]
  132. [color=#ff0000]      dataFile.print(mins);[/color]
  133. [color=#ff0000]      dataFile.print(":");[/color]
  134. [color=#ff0000]      dataFile.print(secs);[/color]
  135. [color=#ff0000]      dataFile.print("  ");[/color]
  136. [color=#ff0000]      dataFile.print(days);[/color]
  137. [color=#ff0000]      dataFile.print("/");[/color]
  138. [color=#ff0000]      dataFile.print(mons);[/color]
  139. [color=#ff0000]      dataFile.print("/");[/color]
  140. [color=#ff0000]      dataFile.print(yeas);[/color]
  141. [color=#ff0000]      dataFile.print("  ");[/color]
  142.       dataFile.print(Val_1_1);
  143.       dataFile.print("  ");
  144.       dataFile.print(Val_1_2);
  145.       dataFile.print("  ");
  146.       dataFile.close();
  147.       delay(500);
  148.     }
  149.     else
  150.     {
  151.       Serial.println("error opering datalog.txt");
  152.     }  
  153. //  delay(250);
  154. //  }
  155. }

  156. void Ser_2(){
  157.   Serial.print("----------Ser_2----------");
  158.   mySerial2.listen();
  159.   delay(500);
  160.   Serial.print("Ser_2 request start...-->");
  161.   for (i = 0; i<9;i++){
  162.     mySerial2.write(request_cmd[i]);
  163.     Serial.print(request_cmd[i], HEX);
  164.     if (i<8){
  165.       Serial.print('-');
  166.     }
  167.     delay(2);
  168.   }
  169.   Serial.println("");
  170.     buff[0]=0;
  171. //  flag = false;
  172.   i = 0;

  173.   islistening = mySerial2.isListening();
  174.   mySerial2.flush();
  175. //  delay(800);

  176.   while (mySerial2.available() > 0 && i<9) {
  177.     if (i == 0){
  178.       Serial.print("2 is listening...-->");
  179.     }
  180.     buff[i]= mySerial2.read();
  181.     if (i ==0){
  182.       flag = buff[i];
  183.     }
  184.     i++;
  185.     delay(2);
  186.   }
  187.   if (buff[0] == 255) {
  188.     DEC1 = buff[2] & 0x1F;
  189.     DEC2 = buff[3];
  190.     Val_2_1 = DEC1 * 256 + DEC2;
  191.     DEC1 = buff[6] & 0x1F;
  192.     DEC2 = buff[7];
  193.     Val_2_2 = DEC1 * 256 + DEC2;

  194.     for (i=0; i<9; i++){
  195.         Serial.print(buff[i], HEX);
  196.         if (i<8){
  197.           Serial.print("-");
  198.         }
  199.       }
  200.     Serial.println("");
  201.     Serial.print("2_1 value: ");
  202.     Serial.println(Val_2_1);
  203.     Serial.print("2_2 value: ");
  204.     Serial.println(Val_2_2);
  205.     Serial.println("----------Ser_2----------");
  206.     Serial.println("");
  207.   }
  208.    //如果值大于1,打开文件并将检测到的数据写入文件
  209. //  if(Val_2_1 || Val_2_2> 0){  
  210.     Serial.println("Open file and write data2");
  211.     File dataFile1 = SD.open("datalog.txt",FILE_WRITE);
  212.     if(dataFile1)
  213.     {   
  214.       dataFile1.print(Val_2_1);
  215.       dataFile1.print("  ");
  216.       dataFile1.print(Val_2_2);
  217.       dataFile1.print('\n');
  218.       dataFile1.close();
  219.       delay(500);                                             
  220.     }
  221.     else
  222.     {
  223.       Serial.println("error opering datalog.txt");
  224.     }  
  225. //    delay(250);
  226. //  }
  227. }
  228. [color=#ff0000]#define TIME_HEADER  "T"   // Header tag for serial time sync message[/color]
  229. [color=#ff0000]
  230. [/color]
  231. [color=#ff0000]unsigned long processSyncMessage() {[/color]
  232. [color=#ff0000]  unsigned long pctime = 0L;[/color]
  233. [color=#ff0000]  const unsigned long DEFAULT_TIME = 1357041600; // Jan 1 2013 [/color]
  234. [color=#ff0000]
  235. [/color]
  236. [color=#ff0000]  if(Serial.find(TIME_HEADER)) {[/color]
  237. [color=#ff0000]     pctime = Serial.parseInt();[/color]
  238. [color=#ff0000]     return pctime;[/color]
  239. [color=#ff0000]     if( pctime < DEFAULT_TIME) { // check the value is a valid time (greater than Jan 1 2013)[/color]
  240. [color=#ff0000]       pctime = 0L; // return 0 to indicate that the time is not valid[/color]
  241. [color=#ff0000]     }[/color]
  242. [color=#ff0000]  }[/color]
  243. [color=#ff0000]  return pctime;[/color]
  244. [color=#ff0000]} [/color]
  245. [color=#ff0000]
复制代码

捕获.PNG
 楼主| 发表于 2021-8-20 15:56 | 显示全部楼层
!这程序我再来一遍好了
#include <SoftwareSerial.h>
#include <SPI.h>
#include <SD.h>
#include <TimeLib.h>
#include <Wire.h>
#include <DS1307RTC.h>

SoftwareSerial mySerial1(8, 9); // RX, TX
SoftwareSerial mySerial2(2, 3); // RX, TX
String comdata = "";
unsigned char DEC1, DEC2;
byte buff[10];                     
byte flag;
byte request_cmd[9] = {0xFF, 0x01, 0x86, 0x00, 0x00, 0x00,0x00, 0x00,0x79};
//byte request_cmd[9] = [255,1,134,0,0,0,0,0,121];

int Val_1_1;
int Val_1_2;
int Val_2_1;
int Val_2_2;

bool islistening;
int i=0;
void Ser_1();
void Ser_2();

const int chipSelect = 4;      

unsigned long processSyncMessage();

int hous;
int mins;
int secs;
int days;
int mons;
int yeas;


int buzzer = 7;
void setup() {
   Serial.begin(9600);
//  while (!Serial){}
  mySerial1.begin(9600);
  mySerial2.begin(9600);
  pinMode(chipSelect,OUTPUT);
  pinMode(buzzer,OUTPUT);
  
  delay(1000);
  setSyncProvider(RTC.get);   
  if (timeStatus() != timeSet)
     Serial.println("Unable to sync with the RTC");
  else
     Serial.println("RTC has set the system time");

  Serial.println("Initializing SD card");
  if(!SD.begin(chipSelect))
  {
    Serial.println("initializing failed!");
    while(1);
  }
  Serial.println("initialization done.");
}


void loop() {

  Ser_1();
  Ser_2();
  if(Val_1_1 || Val_1_2 || Val_2_1 || Val_2_2)
  {
    digitalWrite(buzzer,HIGH);  
  }
}

void Ser_1(){
if (Serial.available()) {
    time_t t = processSyncMessage();
    if (t != 0) {
      RTC.set(t);   // set the RTC and the system time to the received value
      setTime(t);         
    }
  }
  
  hous = hour();
  mins = minute();
  secs = second();
  days = day();
  mons = month();
  yeas = year();

  Serial.print("----------Ser_1----------");
  mySerial1.listen();
  delay(500);
  Serial.print("Ser_1 request start...-->");
  for (i = 0; i<9;i++){
    mySerial1.write(request_cmd);
    Serial.print(request_cmd, HEX);
    if (i<8){
      Serial.print('-');
    }
    delay(2);
  }
  Serial.println("");
  buff[0]=0;
  i=0;
  
  islistening = mySerial1.isListening();
  mySerial1.flush();
//  delay(900);
  
  while (mySerial1.available() > 0 && i<9) {
    if (i == 0){
      Serial.print("1 is listening...-->");
    }
    buff= mySerial1.read();
    i++;
    delay(2);
  }
  if (buff[0] == 255) {
    DEC1 = buff[2] & 0x1F;
    DEC2 = buff[3];
    Val_1_1 = DEC1 * 256 + DEC2;
    DEC1 = buff[6] & 0x1F;
    DEC2 = buff[7];
    Val_1_2 = DEC1 * 256 + DEC2;
//    Serial.print("1 is listening...-->");
    for (i=0; i<9; i++){
        Serial.print(buff, HEX);
        if (i<8){
          Serial.print("-");
        }
      }
    Serial.println("");
    Serial.print("1_1 value: ");
    Serial.println(Val_1_1);
    Serial.print("1_2 value: ");
    Serial.println(Val_1_2);
    Serial.println("----------Ser_1----------");
    Serial.println("");
  }
   //如果值大于1,打开文件并将检测到的数据写入文件
//  if(Val_1_1 || Val_1_2 > 0){
    Serial.println("Open file and write data1");
    File dataFile = SD.open("datalog.txt",FILE_WRITE);
    if(dataFile)
    {   
      dataFile.print(hous);
      dataFile.print(":");
      dataFile.print(mins);
      dataFile.print(":");
      dataFile.print(secs);
      dataFile.print("  ");
      dataFile.print(days);
      dataFile.print("/");
      dataFile.print(mons);
      dataFile.print("/");
      dataFile.print(yeas);
      dataFile.print("  ");

      dataFile.print(Val_1_1);
      dataFile.print("  ");
      dataFile.print(Val_1_2);
      dataFile.print("  ");
      dataFile.close();
      delay(500);
    }
    else
    {
      Serial.println("error opering datalog.txt");
    }  
//  delay(250);
//  }
}

void Ser_2(){
  Serial.print("----------Ser_2----------");
  mySerial2.listen();
  delay(500);
  Serial.print("Ser_2 request start...-->");
  for (i = 0; i<9;i++){
    mySerial2.write(request_cmd);
    Serial.print(request_cmd, HEX);
    if (i<8){
      Serial.print('-');
    }
    delay(2);
  }
  Serial.println("");
    buff[0]=0;
//  flag = false;
  i = 0;

  islistening = mySerial2.isListening();
  mySerial2.flush();
//  delay(800);
  
  while (mySerial2.available() > 0 && i<9) {
    if (i == 0){
      Serial.print("2 is listening...-->");
    }
    buff= mySerial2.read();
    if (i ==0){
      flag = buff;
    }
    i++;
    delay(2);
  }
  if (buff[0] == 255) {
    DEC1 = buff[2] & 0x1F;
    DEC2 = buff[3];
    Val_2_1 = DEC1 * 256 + DEC2;
    DEC1 = buff[6] & 0x1F;
    DEC2 = buff[7];
    Val_2_2 = DEC1 * 256 + DEC2;
   
    for (i=0; i<9; i++){
        Serial.print(buff, HEX);
        if (i<8){
          Serial.print("-");
        }
      }
    Serial.println("");
    Serial.print("2_1 value: ");
    Serial.println(Val_2_1);
    Serial.print("2_2 value: ");
    Serial.println(Val_2_2);
    Serial.println("----------Ser_2----------");
    Serial.println("");
  }
   //如果值大于1,打开文件并将检测到的数据写入文件
//  if(Val_2_1 || Val_2_2> 0){  
    Serial.println("Open file and write data2");
    File dataFile1 = SD.open("datalog.txt",FILE_WRITE);
    if(dataFile1)
    {   
      dataFile1.print(Val_2_1);
      dataFile1.print("  ");
      dataFile1.print(Val_2_2);
      dataFile1.print('\n');
      dataFile1.close();
      delay(500);                                             
    }
    else
    {
      Serial.println("error opering datalog.txt");
    }  
//    delay(250);
//  }
}
#define TIME_HEADER  "T"   // Header tag for serial time sync message

unsigned long processSyncMessage() {
  unsigned long pctime = 0L;
  const unsigned long DEFAULT_TIME = 1357041600; // Jan 1 2013

  if(Serial.find(TIME_HEADER)) {
     pctime = Serial.parseInt();
     return pctime;
     if( pctime < DEFAULT_TIME) { // check the value is a valid time (greater than Jan 1 2013)
       pctime = 0L; // return 0 to indicate that the time is not valid
     }
  }
  return pctime;
}
 楼主| 发表于 2021-8-20 16:01 | 显示全部楼层
啥情况啊这是,真是要命了,希望大家不要嫌弃。
我一开始有拿书上的程序试过,但是上面那个tm.的函数告诉我不能用,我后来找库的时候看示例做了这个程序,虽然有点蠢,但是我之前在小屏幕上做了一个小时钟做测试,那个断电时间是不掉的
附上库的网址https://www.pjrc.com/teensy/td_libs_DS1307RTC.html
 楼主| 发表于 2021-8-23 14:15 | 显示全部楼层
问题已经解决啦
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|Archiver|手机版|Arduino中文社区

GMT+8, 2024-11-29 02:47 , Processed in 0.098229 second(s), 18 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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