1.3とりあえず使ってみる。(RTC編)

Real Time Clock(RTC)を使う

mbedとPCだけでできることを学んでいます。
ここではNXP1768のRTC機能を学びましょう。

RTCは1970年1月1日からの秒をセットして使用します。
でも、これだと毎回計算するのは大変なので用意されている関数を使用します。
time.hに入っていると思われるのだけどここはmbed.hで含まれているらしい
下の例は2011/5/5 10:00:00をセットする。月は1、引いて指定しないといけないらしい。

time構造体を作成し、mktimeで1970年からの秒に変換する。
set_timeでRTCに入力し、rtcの設定完了。
あとはデータの呼び出し
wait(1)は1秒待ち

rtc_test.cpp

#include "mbed.h"

int main() {
    // setup time structure for Thu, 5 May 2011 10:00:00
    struct tm t;
    t.tm_sec = 00;    // 0-59
    t.tm_min = 00;    // 0-59
    t.tm_hour = 10;   // 0-23
    t.tm_mday = 5;   // 1-31
    t.tm_mon = 4;     // 0-11
    t.tm_year = 111;  // year since 1900

    time_t seconds = mktime(&t);
    printf("Time as seconds since January 1, 1970 = %d\n", seconds);
    
    set_time(seconds);
    //Read RTC Data
    for(int i=0;i<10;i++){
        seconds = time(NULL);
        printf("Time as a string = %s", ctime(&seconds));
        wait(1);
    }
}

実行結果
/media/uploads/yueee_yt/rtc_test_1.jpg


Please log in to post comments.