NXP_PCF2127A demo code

このページは日本語でも記載されています.日本語版はこのページ後半をご覧ください. This page is written in Japanese as well. Please find it in 2nd half of this page.

What is this?

This is a sample/demo code for NXP PCF2127A real time clock (RTC) chip.

Of course, since the mbed/LPC1768 has an internal RTC which works ultra low power consumption, you dont need to use such external RTC chip basically.

But if you need the RTC with very high accuracy or external WDT, the PCF2127A may be a good option for the system.

This sample/demo code just initilalize the RTC registers if it is not set properly and shows the date and time on the TextLCD screen which read from the chip.

Code:

NXP_PCF2127A

It uses an interrupt to update the information on the TextLCD screen. The RTC chip is set to generate periodical interrupt every seconds.

mbed gets the interrupt on pin8. When the interrupt comes, mbed read the RTC registers and those will be shown on the TextLCD.

#include "mbed.h"
#include "TextLCD.h"
#include "NXP_PCF2127A.h"

TextLCD         lcd(p24, p25, p26, p27, p28, p29, p30); // rs, rw, e, d0, d1, d2, d3
NXP_PCF2127A    rtc( p9, p10 );
InterruptIn     intr( p8 );

void time_intr( void );

int main() {
    printf( "--- PCF2127 demo started.\r\n" );

    if ( rtc.is_init_required() ) {
        lcd.locate( 0, 0 );
        lcd.printf( "please set time from terminal" );
        rtc.set_time();
        lcd.cls();
    }

    intr.fall( &time_intr );

    while ( 1 )
        ;
}


void time_intr( void ) {
    struct tm   dt, *dtp;
    time_t      t;
    char        s[ 30 ];
    dtp = &dt;

    rtc.clear_intr();

    t       = rtc.time( NULL );
    dtp     = localtime( &t );

    strftime( s, 30, "%H:%M:%S, %Y/%b/%d %a", dtp );
    printf( "%s\r\n", s );

    strftime( s, 20, "%H:%M:%S PCF2127", dtp );
    lcd.locate( 0, 0 );
    lcd.printf( "%s", s );

    strftime( s, 20, "%Y/%b/%d(%a)", dtp );
    lcd.locate( 0, 1 );
    lcd.printf( "%s", s );
}

Note:

The I2C bus and the interrupt lines should be pulled-up properly since those signals are open-drain.

Reference:

PCF2127A datasheet: http://www.nxp.com/pip/PCF2127A_2.html

Realtime clock usage: http://mbed.org/users/roen/notebook/real-time/



これは何?

NXPのリアルタイム・クロック(RTC)チップ,PCF2127Aのサンプル/デモコードです.

mbed/LPC1768は内部にRTCを持っているので,このような外付けRTCを使う必要はありません.でももし高精度のRTCが必要だったり,外付けのWDTが必要な場合には,これもひとつの候補となり得るでしょう.

このサンプル/デモコードは,もしまだRTCチップが設定されていなければ初期化し,その後,読み出した日付と時間をTextLCDに表示します.

コード:

NXP_PCF2127A

TextLCDの表示の更新に割り込みを使います.

RTCチップが毎秒の周期的な割り込みを発生するように設定します.

この割り込みはmbedの8ピンに接続され,割り込みが来るとRTCレジスタを読み,TextLCDに表示します.

#include "mbed.h"
#include "TextLCD.h"
#include "NXP_PCF2127A.h"

TextLCD         lcd(p24, p25, p26, p27, p28, p29, p30); // rs, rw, e, d0, d1, d2, d3
NXP_PCF2127A    rtc( p9, p10 );
InterruptIn     intr( p8 );

void time_intr( void );
void led_ctrl( void );

int main() {
    printf( "--- PCF2127 demo started.\r\n" );

    if ( rtc.is_init_required() ) {
        lcd.locate( 0, 0 );
        lcd.printf( "please set time from terminal" );
        rtc.set_time();
        lcd.cls();
    }

    intr.fall( &time_intr );

    while ( 1 )
        ;
}


void time_intr( void ) {
    struct tm   dt, *dtp;
    time_t      t;
    char        s[ 30 ];
    dtp = &dt;

    rtc.clear_intr();

    t       = rtc.time( NULL );
    dtp     = localtime( &t );

    strftime( s, 30, "%H:%M:%S, %Y/%b/%d %a", dtp );
    printf( "%s\r\n", s );

    strftime( s, 20, "%H:%M:%S PCF2127", dtp );
    lcd.locate( 0, 0 );
    lcd.printf( "%s", s );

    strftime( s, 20, "%Y/%b/%d(%a)", dtp );
    lcd.locate( 0, 1 );
    lcd.printf( "%s", s );
}

注意:

I2Cバスと割り込みの信号線はオープンドレインなので,プルアップ抵抗を忘れないようにしてください.

参考:

PCF2127A datasheet: http://www.nxp.com/pip/PCF2127A_2.html

Realtime clock usage: http://mbed.org/users/roen/notebook/real-time/


0 comments

Please log in to post a comment.