Robofun 機器人論壇

 找回密碼
 申請會員
搜索
熱搜: 活動 交友 discuz
查看: 5826|回復: 12

ds3231 無法使用RTCDateTime dt;

[複製鏈接]
發表於 2018-5-20 20:05:53 | 顯示全部樓層 |閱讀模式
本帖最後由 比利 於 2018-5-20 20:11 編輯

我是用 即時時鐘 RTC 3231

#include <DS3231.h>
#include <Wire.h>

DS3231 clock;

RTCDateTime dt;



我載了很多不同的程式庫  ds3231.h 的  應該不是程式庫的問題
但一直跑出RTCDateTime' does not name a type  的error
刪掉之後  又跑出class DS3231' has no member named 'begin' 的error
(有參考過其他網站都需要RTCDateTime dt; 這句)

是什麼問題?
   
發表於 2018-5-20 20:26:52 | 顯示全部樓層
應該是用錯 library 了。
最簡單的方法就是從功能表中
檔案(FILE)-範例(examples)-DS3231中
隨便叫出一個範例出來看看就知道了
 樓主| 發表於 2018-5-20 20:30:13 | 顯示全部樓層
請問是要看什麼?
看裡面有沒有RTCDateTime  嗎
 樓主| 發表於 2018-5-20 20:32:53 | 顯示全部樓層
本帖最後由 比利 於 2018-5-20 20:36 編輯

/*
  DS3231: Real-Time Clock. Simple example
  Read more: www.jarzebski.pl/arduino/kompone ... ego-rtc-ds3231.html
  GIT: https://github.com/jarzebski/Arduino-DS3231
  Web: http://www.jarzebski.pl
  (c) 2014 by Korneliusz Jarzebski
*/

#include <Wire.h>
#include <DS3231.h>

DS3231 clock;
RTCDateTime dt;

void setup()
{
  Serial.begin(9600);

  // Initialize DS3231
  Serial.println("Initialize DS3231");;
  clock.begin();

  // Set sketch compiling time
  clock.setDateTime(__DATE__, __TIME__);
}

void loop()
{
  dt = clock.getDateTime();

  // For leading zero look to DS3231_dateformat example

  Serial.print("Raw data: ");
  Serial.print(dt.year);   Serial.print("-");
  Serial.print(dt.month);  Serial.print("-");
  Serial.print(dt.day);    Serial.print(" ");
  Serial.print(dt.hour);   Serial.print(":");
  Serial.print(dt.minute); Serial.print(":");
  Serial.print(dt.second); Serial.println("");

  delay(1000);
}


我把範例開出來  裡面有  編譯了  但一樣的問題
發表於 2018-5-20 21:07:02 來自手機 | 顯示全部樓層
這個人寫的library很明顯有問題,改用其他人寫的吧!
 樓主| 發表於 2018-5-20 21:10:04 | 顯示全部樓層
請問怎麼看是哪裡有問題?
 樓主| 發表於 2018-5-20 21:12:22 | 顯示全部樓層
可以推薦哪個人的library是沒問題的嗎

這是不同人的

/*
  DS3231: Real-Time Clock. Date Format
  Read more: www.jarzebski.pl/arduino/kompone ... ego-rtc-ds3231.html
  GIT: https://github.com/jarzebski/Arduino-DS3231
  Web: http://www.jarzebski.pl
  (c) 2014 by Korneliusz Jarzebski
*/

#include <Wire.h>
#include <DS3231.h>

DS3231 clock;
RTCDateTime dt;

void setup()
{
  Serial.begin(9600);

  // Initialize DS3231
  Serial.println("Initialize DS3231");;
  clock.begin();

  // Set sketch compiling time
  clock.setDateTime(__DATE__, __TIME__);

  // Set from UNIX timestamp
  // clock.setDateTime(1397408400);

  // Manual (YYYY, MM, DD, HH, II, SS
  // clock.setDateTime(2014, 4, 13, 19, 21, 00);
}

void loop()
{
  dt = clock.getDateTime();

  Serial.print("Long number format:          ");
  Serial.println(clock.dateFormat("d-m-Y H:i:s", dt));

  Serial.print("Long format with month name: ");
  Serial.println(clock.dateFormat("d F Y H:i:s",  dt));

  Serial.print("Short format witch 12h mode: ");
  Serial.println(clock.dateFormat("jS M y, h:ia", dt));

  Serial.print("Today is:                    ");
  Serial.print(clock.dateFormat("l, z", dt));
  Serial.println(" days of the year.");

  Serial.print("Actual month has:            ");
  Serial.print(clock.dateFormat("t", dt));
  Serial.println(" days.");

  Serial.print("Unixtime:                    ");
  Serial.println(clock.dateFormat("U", dt));

  Serial.println();

  delay(1000);
}
發表於 2018-5-20 21:26:43 | 顯示全部樓層
不要用從
https://github.com/jarzebski/Arduino-DS3231
下載的 library
從他寫的 ds3231.cpp來看, 至少 dateformat 那邊就寫錯了,雖然 compile 顯示是 warning
但明顯是 error
其他的寫法看起來,不像是有經驗的工程師寫的
一堆 warning
或許有部分功能是正常吧?
 樓主| 發表於 2018-5-20 21:35:37 | 顯示全部樓層
/*
DS3231_test.pde
Eric Ayars
4/11

Test/demo of read routines for a DS3231 RTC.

Turn on the serial monitor after loading this to check if things are
working as they should.

*/

#include <DS3231.h>
#include <Wire.h>

DS3231 Clock;
bool Century=false;
bool h12;
bool PM;
byte ADay, AHour, AMinute, ASecond, ABits;
bool ADy, A12h, Apm;

byte year, month, date, DoW, hour, minute, second;

void setup() {
        // Start the I2C interface
        Wire.begin();
        Clock.setSecond(50);//Set the second
        Clock.setMinute(59);//Set the minute
        Clock.setHour(11);  //Set the hour
        Clock.setDoW(5);    //Set the day of the week
        Clock.setDate(31);  //Set the date of the month
        Clock.setMonth(5);  //Set the month of the year
        Clock.setYear(13);  //Set the year (Last two digits of the year)
        // Start the serial interface
        Serial.begin(115200);
}
void ReadDS3231()
{
  int second,minute,hour,date,month,year,temperature;
  second=Clock.getSecond();
  minute=Clock.getMinute();
  hour=Clock.getHour(h12, PM);
  date=Clock.getDate();
  month=Clock.getMonth(Century);
  year=Clock.getYear();
  
  temperature=Clock.getTemperature();
  
  Serial.print("20");
  Serial.print(year,DEC);
  Serial.print('-');
  Serial.print(month,DEC);
  Serial.print('-');
  Serial.print(date,DEC);
  Serial.print(' ');
  Serial.print(hour,DEC);
  Serial.print(':');
  Serial.print(minute,DEC);
  Serial.print(':');
  Serial.print(second,DEC);
  Serial.print('\n');
  Serial.print("Temperature=");
  Serial.print(temperature);
  Serial.print('\n');
}
void loop() {ReadDS3231();delay(1000);
        // send what's going on to the serial monitor.
        // Start with the year
/*        Serial.print("2");
        if (Century) {                        // Won't need this for 89 years.
                Serial.print("1");
        } else {
                Serial.print("0");
        }
        Serial.print(Clock.getYear(), DEC);
        Serial.print('-');
        // then the month
        Serial.print(Clock.getMonth(Century), DEC);
        Serial.print('-');
        // then the date
        Serial.print(Clock.getDate(), DEC);
        Serial.print(' ');*/
        // and the day of the week
        /*Serial.print(Clock.getDoW(), DEC);
        Serial.print(' ');*/
        // Finally the hour, minute, and second
        /*Serial.print(Clock.getHour(h12, PM), DEC);
        Serial.print(':');
        Serial.print(Clock.getMinute(), DEC);
        Serial.print(':');
        Serial.print(Clock.getSecond(), DEC);
        // Add AM/PM indicator
        if (h12) {
                if (PM) {
                        Serial.print(" PM ");
                } else {
                        Serial.print(" AM ");
                }
        } else {
                Serial.print(" 24h ");
        }
        // Display the temperature
        Serial.print("T=");
        Serial.print(Clock.getTemperature(), 2);
        // Tell whether the time is (likely to be) valid
        if (Clock.oscillatorCheck()) {
                Serial.print(" O+");
        } else {
                Serial.print(" O-");
        }*/
        // Indicate whether an alarm went off
        /*if (Clock.checkIfAlarm(1)) {
                Serial.print(" A1!");
        }
        if (Clock.checkIfAlarm(2)) {
                Serial.print(" A2!");
        }*/
        // New line on display
        //Serial.print('\n');
       // delay(1000);
        // Display Alarm 1 information
/*        Serial.print("Alarm 1: ");
        Clock.getA1Time(ADay, AHour, AMinute, ASecond, ABits, ADy, A12h, Apm);
        Serial.print(ADay, DEC);
        if (ADy) {
                Serial.print(" DoW");
        } else {
                Serial.print(" Date");
        }
        Serial.print(' ');
        Serial.print(AHour, DEC);
        Serial.print(' ');
        Serial.print(AMinute, DEC);
        Serial.print(' ');
        Serial.print(ASecond, DEC);
        Serial.print(' ');
        if (A12h) {
                if (Apm) {
                        Serial.print('pm ');
                } else {
                        Serial.print('am ');
                }
        }
        if (Clock.checkAlarmEnabled(1)) {
                Serial.print("enabled");
        }
        Serial.print('\n');
        // Display Alarm 2 information
        Serial.print("Alarm 2: ");
        Clock.getA2Time(ADay, AHour, AMinute, ABits, ADy, A12h, Apm);
        Serial.print(ADay, DEC);
        if (ADy) {
                Serial.print(" DoW");
        } else {
                Serial.print(" Date");
        }
        Serial.print(' ');
        Serial.print(AHour, DEC);
        Serial.print(' ');
        Serial.print(AMinute, DEC);
        Serial.print(' ');
        if (A12h) {
                if (Apm) {
                        Serial.print('pm');
                } else {
                        Serial.print('am');
                }
        }
        if (Clock.checkAlarmEnabled(2)) {
                Serial.print("enabled");
        }*/
        /* display alarm bits
        Serial.print('\n');
        Serial.print('Alarm bits: ');
        Serial.print(ABits, DEC);
        */
/*
        Serial.print('\n');
        Serial.print('\n');
        delay(1000);

        // Display the time once more as a test of the getTime() function
        Clock.getTime(year, month, date, DoW, hour, minute, second);
       
        Serial.print(year, DEC);
        Serial.print("/");
        Serial.print(month, DEC);
        Serial.print("/");
        Serial.print(date, DEC);
        Serial.print("day of the week :");
        Serial.println(DoW, DEC);
        Serial.print(hour, DEC);
        Serial.print(":");
        Serial.print(minute, DEC);
        Serial.print(":");
        Serial.println(second, DEC);*/
}


這個會比較好嘛?  
網址是https://makerpro.cc/2017/03/use- ... ow-the-time-on-lcd/
發表於 2018-5-20 21:36:15 | 顯示全部樓層
不過如果你要用,我猜部份基本功能是ok的
它的基本範例雖然一堆 warning
但我還是可以正常編譯完並下載
並沒有你說的錯誤
如果你不能下載,你可能有其他問題待解決
發表於 2018-5-20 21:37:37 | 顯示全部樓層
只要可以下載成功,應該都可以用吧?
有問題再進一步看是什麼問題
您需要登錄後才可以回帖 登錄 | 申請會員

本版積分規則

小黑屋|手機版|Archiver|機器人論壇 from 2005.07

GMT+8, 2024-3-29 22:02 , Processed in 0.114462 second(s), 6 queries , Apc On.

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回復 返回頂部 返回列表