|
大佬们又是我,一直以来都麻烦大家啦!非常感谢大家无私的帮助。 我这里又遇到了一个问题,我这里的程序是想用SD卡记录两个传感器的数据和上传数据的时间,在电脑串口监视器里没有问题,此时SD卡的时间是现实的时间。但是,一旦编程用9V的电源供电的话,SD卡的时间就不正常了。附上我的程序,我把我用到的时间那部分标红,麻烦各位了
- #include <SoftwareSerial.h>
- #include <SPI.h>
- #include <SD.h>
- [color=#ff0000]#include <TimeLib.h>[/color]
- [color=#ff0000]#include <Wire.h>[/color]
- [color=#ff0000]#include <DS1307RTC.h>[/color]
- 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();
- [color=#ff0000]int hous;[/color]
- [color=#ff0000]int mins;[/color]
- [color=#ff0000]int secs;[/color]
- [color=#ff0000]int days;[/color]
- [color=#ff0000]int mons;[/color]
- [color=#ff0000]int yeas;[/color]
- int buzzer = 7;
- void setup() {
- Serial.begin(9600);
- // while (!Serial){}
- mySerial1.begin(9600);
- mySerial2.begin(9600);
- pinMode(chipSelect,OUTPUT);
- pinMode(buzzer,OUTPUT);
- delay(1000);
- [color=#ff0000]setSyncProvider(RTC.get); [/color]
- [color=#ff0000] if (timeStatus() != timeSet) [/color]
- [color=#ff0000] Serial.println("Unable to sync with the RTC");[/color]
- [color=#ff0000] else[/color]
- [color=#ff0000] Serial.println("RTC has set the system time");[/color]
- 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(){
- [color=#ff0000]if (Serial.available()) {[/color]
- [color=#ff0000] time_t t = processSyncMessage();[/color]
- [color=#ff0000] if (t != 0) {[/color]
- [color=#ff0000] RTC.set(t); // set the RTC and the system time to the received value[/color]
- [color=#ff0000] setTime(t); [/color]
- [color=#ff0000] }[/color]
- [color=#ff0000] }[/color]
- [color=#ff0000] [/color]
- [color=#ff0000] hous = hour();[/color]
- [color=#ff0000] mins = minute();[/color]
- [color=#ff0000] secs = second(); [/color]
- [color=#ff0000] days = day();[/color]
- [color=#ff0000] mons = month();[/color]
- [color=#ff0000] yeas = year();[/color]
- Serial.print("----------Ser_1----------");
- mySerial1.listen();
- delay(500);
- Serial.print("Ser_1 request start...-->");
- for (i = 0; i<9;i++){
- mySerial1.write(request_cmd[i]);
- Serial.print(request_cmd[i], 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[i]= 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[i], 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)
- {
- [color=#ff0000]dataFile.print(hous);[/color]
- [color=#ff0000] dataFile.print(":");[/color]
- [color=#ff0000] dataFile.print(mins);[/color]
- [color=#ff0000] dataFile.print(":");[/color]
- [color=#ff0000] dataFile.print(secs);[/color]
- [color=#ff0000] dataFile.print(" ");[/color]
- [color=#ff0000] dataFile.print(days);[/color]
- [color=#ff0000] dataFile.print("/");[/color]
- [color=#ff0000] dataFile.print(mons);[/color]
- [color=#ff0000] dataFile.print("/");[/color]
- [color=#ff0000] dataFile.print(yeas);[/color]
- [color=#ff0000] dataFile.print(" ");[/color]
- 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[i]);
- Serial.print(request_cmd[i], 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[i]= mySerial2.read();
- if (i ==0){
- flag = buff[i];
- }
- 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[i], 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);
- // }
- }
- [color=#ff0000]#define TIME_HEADER "T" // Header tag for serial time sync message[/color]
- [color=#ff0000]
- [/color]
- [color=#ff0000]unsigned long processSyncMessage() {[/color]
- [color=#ff0000] unsigned long pctime = 0L;[/color]
- [color=#ff0000] const unsigned long DEFAULT_TIME = 1357041600; // Jan 1 2013 [/color]
- [color=#ff0000]
- [/color]
- [color=#ff0000] if(Serial.find(TIME_HEADER)) {[/color]
- [color=#ff0000] pctime = Serial.parseInt();[/color]
- [color=#ff0000] return pctime;[/color]
- [color=#ff0000] if( pctime < DEFAULT_TIME) { // check the value is a valid time (greater than Jan 1 2013)[/color]
- [color=#ff0000] pctime = 0L; // return 0 to indicate that the time is not valid[/color]
- [color=#ff0000] }[/color]
- [color=#ff0000] }[/color]
- [color=#ff0000] return pctime;[/color]
- [color=#ff0000]} [/color]
- [color=#ff0000]
复制代码
|
-
|