Subversion Repositories idreammicro-avr

Rev

Rev 37 | Rev 43 | 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.
89
    datetime__time_t time =
90
    {
91
        .seconds    = 00,
92
        .minutes    = 29,
93
        .hours      = 19,
94
        .meridiem   = 0
95
    };
96
    ds1307__hour_mode_t hour_mode = DS1307__HOUR_MODE__24_HOUR;
19 jlesech 97
 
42 jlesech 98
    datetime__date_t date =
99
    {
100
        .day    = 3,
101
        .date   = 5,
102
        .month  = 5,
103
        .year   = 9
104
    };
19 jlesech 105
 
42 jlesech 106
    // Initialize Deuligne and configure display.
37 jlesech 107
    deuligne__initialize();
108
    deuligne__set_display(true, true, true);
109
 
110
    // Switch on backlight.
111
    deuligne__switch_on_backlight();
112
 
42 jlesech 113
    // Set time and hour mode.
114
    //ds1307__set_time(&time, hour_mode);
19 jlesech 115
 
42 jlesech 116
    // Set date.
117
    //ds1307__set_date(&date);
19 jlesech 118
 
42 jlesech 119
    for (;;)
120
    {
121
        // Get RTC time.
122
        ds1307__get_time(&time, &hour_mode);
19 jlesech 123
 
42 jlesech 124
        // Get RTC date.
125
        ds1307__get_date(&date);
19 jlesech 126
 
42 jlesech 127
        char str_date[] = "dd/mm/yy";
37 jlesech 128
        snprintf
129
        (
130
            str_date,
131
            strlen(str_date) + 1,
132
            "%02u/%02u/%02u",
133
            date.date,
134
            date.month,
135
            date.year
136
        );
19 jlesech 137
 
37 jlesech 138
        char str_time[] = "hh:mm:ss";
139
        snprintf
140
        (
141
            str_time,
142
            strlen(str_time) + 1,
143
            "%02u:%02u:%02u",
144
            time.hours,
145
            time.minutes,
146
            time.seconds
147
        );
19 jlesech 148
 
42 jlesech 149
        clock__transmit_string(&(str_date[0]));
37 jlesech 150
        usart0__transmit_byte(' ');
42 jlesech 151
        clock__transmit_string(&(str_time[0]));
37 jlesech 152
        usart0__transmit_byte('\n');
153
 
154
        deuligne__set_cursor_position(DEULIGNE__DISPLAY_LINE_1, 0);
155
        deuligne__write_string(str_date);
156
 
157
        deuligne__set_cursor_position(DEULIGNE__DISPLAY_LINE_2, 0);
158
        deuligne__write_string(str_time);
159
 
42 jlesech 160
        _delay_ms(1000);
161
    }
19 jlesech 162
 
42 jlesech 163
    return 0;
19 jlesech 164
}
42 jlesech 165
 
166
/******************************************************************************
167
 * Private function definitions.
168
 ******************************************************************************/
169
 
170
/**************************************************************************//**
171
 * \fn static void clock__transmit_string(const char* p_string)
172
 *
173
 * \brief Transmit a string through USART.
174
 *
175
 * \param[in]   p_string  String to transmit.
176
 ******************************************************************************/
177
static
178
void
179
clock__transmit_string
180
(
181
    const char* p_string
182
){
183
    // Check the preconditions.
184
    assert(NULL != p_string);
185
 
186
    while (*p_string != '\0')
187
    {
188
        usart0__transmit_byte(*p_string);
189
        p_string++;
190
    }
191
}