Time adjustment and display. Adjustment via Com line and Display on LCD.

Dependencies:   mbed TextLCD CheckRTC

main.cpp

Committer:
kenjiArai
Date:
2010-03-27
Revision:
0:b3d6027d4ef2
Child:
1:af3fc8a8eb57

File content as of revision 0:b3d6027d4ef2:

//
// RTC Test Program
//          Kenji Arai / JH1PJL
//          March 27th,2010  Started
//          March 27th,2010  
//
#include "mbed.h"
#include "TextLCD.h"

//#define STYLE1
#define STYLE2

Serial pc(USBTX, USBRX);
DigitalOut myled1(LED1);                                // Assign LED1 output port
TextLCD lcd(p22, p28, p27, p26, p25, p24, p23, 40, 2);  // rs,rw,e,d0,d1,d2,d3,40char's x 2 lines

int hex( char c )
{
	if( c<= '/' ) return( 0 );
	if( ( c -= '0' ) <= 9 || 10 <= ( c -= 'A' - '0' - 10 ) && c <= 15 ){
		return( (int)c );
	}
	return( 0 );
}

int conv( void )
{
    char c;
    int d;
    
    c = pc.getc();
    pc.putc(c);
    d = hex(c);
    c = pc.getc();
    pc.putc(c);
    d = d * 10 + hex(c);
    return d;
}

int main() {
    char c;
    char buf[40];
    time_t seconds;
    struct tm t;

    lcd.cls();
    lcd.locate(0, 0);
    lcd.locate(0, 0);   // 1st line top
    //          1234567890123456789012345678901234567890     
    lcd.printf("  Waiting for time adjustment via com ");
    printf("\r\n");
    while (1){
        while (!pc.readable()){
            seconds = time(NULL);
            printf("Current time is");
            strftime(buf,40, "%I:%M:%S %p (%Y/%m/%d) ", localtime(&seconds));
            printf(" = %s Is this okay? [Enter Y/y or N/n]\r", buf);
        }
        c = getchar();
        if (c == 'N' || c == 'n'){
            printf("\r\nPlese enter Year 20YY (only 2 digits)\r\n]");
            t.tm_year = 100 + conv();
            printf("\r\nPlese enter Month MM (only 2 digits)\r\n]");
            t.tm_mon = conv();
            printf("\r\nPlese enter Day DD (only 2 digits)\r\n]");
            t.tm_mday = conv();
            printf("\r\nPlese enter Hour HH (only 2 digits)\r\n]");
            t.tm_hour = conv();
            printf("\r\nPlese enter Miniut MM (only 2 digits)\r\n]");
            t.tm_min = conv();
            printf("\r\nPlese enter Second SS (only 2 digits)\r\n]");
            t.tm_sec = conv();
            printf("\r\n");
            seconds = mktime(&t);
            set_time(seconds);    
        } else {
            break;
        }
    }
    printf("\r\n  Current Time -> Plese see LCD \r\n");
    for(;;){
        while ( seconds == time(NULL)) ;
        seconds = time(NULL);
        myled1 = !myled1;       
        lcd.cls();
        lcd.locate(0, 0);   // 1st line top
        lcd.printf("It is %d sec since Jan.1,1970\n", seconds); 
        lcd.locate(0, 1);   // 2nd line top
 #ifdef STYLE1
        //                  27 Mar 2010 13:24:00
        strftime(buf,40, "%x %X ", localtime(&seconds));
 #endif
 #ifdef STYLE2
        //                 13:24:00 PM (2010/03/27)
        strftime(buf,40, "%I:%M:%S %p (%Y/%m/%d)", localtime(&seconds));
 #endif
        lcd.printf("Time = %s", buf);
        //printf("Time = %s\r", buf);
    }
}