Subversion Repositories idreammicro-avr

Rev

Rev 42 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
19 jlesech 1
/**************************************************************************//**
2
 * \brief DS1307 RTC library - Demonstration program
3
 * \author Copyright (C) 2011  Julien Le Sech - www.idreammicro.com
4
 * \version 1.0
5
 * \date 20090501
6
 *
7
 * This file is part of the iDreamMicro library.
8
 *
9
 * This library is free software: you can redistribute it and/or modify it under
10
 * the terms of the GNU Lesser General Public License as published by the Free
11
 * Software Foundation, either version 3 of the License, or (at your option) any
12
 * later version.
13
 *
14
 * This library is distributed in the hope that it will be useful, but WITHOUT
15
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
17
 * details.
18
 *
19
 * You should have received a copy of the GNU Lesser General Public License
20
 * along with this program. If not, see http://www.gnu.org/licenses/
21
 ******************************************************************************/
22
 
23
/**************************************************************************//**
24
 * \file demo_ds1307.c
25
 ******************************************************************************/
26
 
27
/******************************************************************************
28
 * Header file inclusions.
29
 ******************************************************************************/
30
 
37 jlesech 31
#include <deuligne/deuligne.h>
19 jlesech 32
#include <ds1307/ds1307.h>
37 jlesech 33
#include <twi/twi.h>
19 jlesech 34
#include <usart/usart0.h>
35
#include <useful/datetime.h>
36
 
37
#include <avr/io.h>
38
#include <util/delay.h>
39
 
42 jlesech 40
#include <assert.h>
19 jlesech 41
#include <stdio.h>
24 jlesech 42
#include <stdlib.h>
19 jlesech 43
#include <string.h>
44
 
45
/******************************************************************************
42 jlesech 46
 * Private function prototypes.
47
 ******************************************************************************/
48
 
49
/**************************************************************************//**
50
 * \fn static void clock__transmit_string(const char* p_string)
51
 *
52
 * \brief Transmit a string through USART.
53
 *
54
 * \param[in]   p_string  String to transmit.
55
 ******************************************************************************/
56
static
57
void
58
clock__transmit_string
59
(
60
    const char* p_string
61
);
62
 
63
/******************************************************************************
19 jlesech 64
 * Main function.
65
 ******************************************************************************/
66
 
67
/**************************************************************************//**
68
 * \fn int main(void)
69
 *
70
 * \brief Main function.
71
 *
72
 * \return 0
73
 ******************************************************************************/
74
int main(void)
75
{
76
    // Initialize USART0.
77
    usart0__initialize(NULL);
78
 
37 jlesech 79
    // Enable transmitter.
19 jlesech 80
    usart0__enable_transmitter();
81
 
37 jlesech 82
    // Initialize TWI.
83
    twi__initialize(100000);
84
 
19 jlesech 85
    // Initialize RTC.
42 jlesech 86
    ds1307__initialize();
19 jlesech 87
 
42 jlesech 88
    // Declare some variables.
43 jlesech 89
    datetime__time_t time;
90
    ds1307__hour_mode_t hour_mode;
91
    datetime__date_t date;
19 jlesech 92
 
42 jlesech 93
    // Initialize Deuligne and configure display.
37 jlesech 94
    deuligne__initialize();
43 jlesech 95
    deuligne__set_display(true, false, false);
37 jlesech 96
 
97
    // Switch on backlight.
98
    deuligne__switch_on_backlight();
99
 
42 jlesech 100
    for (;;)
101
    {
102
        // Get RTC time.
103
        ds1307__get_time(&time, &hour_mode);
19 jlesech 104
 
42 jlesech 105
        // Get RTC date.
106
        ds1307__get_date(&date);
19 jlesech 107
 
42 jlesech 108
        char str_date[] = "dd/mm/yy";
37 jlesech 109
        snprintf
110
        (
111
            str_date,
112
            strlen(str_date) + 1,
113
            "%02u/%02u/%02u",
114
            date.date,
115
            date.month,
116
            date.year
117
        );
19 jlesech 118
 
37 jlesech 119
        char str_time[] = "hh:mm:ss";
120
        snprintf
121
        (
122
            str_time,
123
            strlen(str_time) + 1,
124
            "%02u:%02u:%02u",
125
            time.hours,
126
            time.minutes,
127
            time.seconds
128
        );
19 jlesech 129
 
42 jlesech 130
        clock__transmit_string(&(str_date[0]));
37 jlesech 131
        usart0__transmit_byte(' ');
42 jlesech 132
        clock__transmit_string(&(str_time[0]));
37 jlesech 133
        usart0__transmit_byte('\n');
134
 
135
        deuligne__set_cursor_position(DEULIGNE__DISPLAY_LINE_1, 0);
136
        deuligne__write_string(str_date);
137
 
138
        deuligne__set_cursor_position(DEULIGNE__DISPLAY_LINE_2, 0);
139
        deuligne__write_string(str_time);
140
 
42 jlesech 141
        _delay_ms(1000);
142
    }
19 jlesech 143
 
42 jlesech 144
    return 0;
19 jlesech 145
}
42 jlesech 146
 
147
/******************************************************************************
148
 * Private function definitions.
149
 ******************************************************************************/
150
 
151
/**************************************************************************//**
152
 * \fn static void clock__transmit_string(const char* p_string)
153
 *
154
 * \brief Transmit a string through USART.
155
 *
156
 * \param[in]   p_string  String to transmit.
157
 ******************************************************************************/
158
static
159
void
160
clock__transmit_string
161
(
162
    const char* p_string
163
){
164
    // Check the preconditions.
165
    assert(NULL != p_string);
166
 
167
    while (*p_string != '\0')
168
    {
169
        usart0__transmit_byte(*p_string);
170
        p_string++;
171
    }
172
}