Rev 43 |
    Blame |
    Compare with Previous |
    Last modification |
    View Log
    | Download
    | RSS feed
  
  
/**************************************************************************//**
 * \brief DS1307 RTC library - Demonstration program
 * \author Copyright (C) 2011  Julien Le Sech - www.idreammicro.com
 * \version 1.0
 * \date 20090501
 *
 * This file is part of the iDreamMicro library.
 *
 * This library is free software: you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation, either version 3 of the License, or (at your option) any
 * later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program. If not, see http://www.gnu.org/licenses/
 ******************************************************************************/
/**************************************************************************//**
 * \file demo_ds1307.c
 ******************************************************************************/
/******************************************************************************
 * Header file inclusions.
 ******************************************************************************/
#include <deuligne/deuligne.h>
#include <ds1307/ds1307.h>
#include <twi/twi.h>
#include <usart/usart0.h>
#include <useful/datetime.h>
#include <avr/io.h>
#include <util/delay.h>
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/******************************************************************************
 * Private function prototypes.
 ******************************************************************************/
/**************************************************************************//**
 * \fn static void clock__transmit_string(const char* p_string)
 *
 * \brief Transmit a string through USART.
 *
 * \param[in]   p_string  String to transmit.
 ******************************************************************************/
static
void
clock__transmit_string
(
    const char* p_string
);
/******************************************************************************
 * Main function.
 ******************************************************************************/
/**************************************************************************//**
 * \fn int main(void)
 *
 * \brief Main function.
 *
 * \return 0
 ******************************************************************************/
int main
(void)
{
    // Initialize USART0.
    usart0__initialize
(NULL
);
    // Enable transmitter.
    usart0__enable_transmitter
();
    // Initialize TWI.
    twi__initialize
(DS1307__CLOCK_RATE
);
    // Initialize RTC.
    ds1307__initialize
();
    // Declare some variables.
    datetime__time_t 
time;
    ds1307__hour_mode_t hour_mode
;
    datetime__date_t date
;
    // Initialize Deuligne and configure display.
    deuligne__initialize
();
    deuligne__set_display
(true, false, false);
    // Switch on backlight.
    deuligne__switch_on_backlight
();
    for (;;)
    {
        // Get RTC time.
        ds1307__get_time
(&time, &hour_mode
);
        // Get RTC date.
        ds1307__get_date
(&date
);
        char str_date
[] = "dd/mm/yy";
        snprintf
        (
            str_date
,
            strlen(str_date
) + 1,
            "%02u/%02u/%02u",
            date.
date,
            date.
month,
            date.
year
        );
        char str_time
[] = "hh:mm:ss";
        snprintf
        (
            str_time
,
            strlen(str_time
) + 1,
            "%02u:%02u:%02u",
            time.
hours,
            time.
minutes,
            time.
seconds
        );
        clock__transmit_string
(&(str_date
[0]));
        usart0__transmit_byte
(' ');
        clock__transmit_string
(&(str_time
[0]));
        usart0__transmit_byte
('\n');
        deuligne__set_cursor_position
(DEULIGNE__DISPLAY_LINE_1
, 0);
        deuligne__write_string
(str_date
);
        deuligne__set_cursor_position
(DEULIGNE__DISPLAY_LINE_2
, 0);
        deuligne__write_string
(str_time
);
        _delay_ms
(1000);
    }
    return 0;
}
/******************************************************************************
 * Private function definitions.
 ******************************************************************************/
/**************************************************************************//**
 * \fn static void clock__transmit_string(const char* p_string)
 *
 * \brief Transmit a string through USART.
 *
 * \param[in]   p_string  String to transmit.
 ******************************************************************************/
static
void
clock__transmit_string
(
    const char* p_string
){
    // Check the preconditions.
    assert(NULL 
!= p_string
);
    while (*p_string 
!= '\0')
    {
        usart0__transmit_byte
(*p_string
);
        p_string
++;
    }
}