Subversion Repositories idreammicro-avr

Compare Revisions

Ignore whitespace Rev 61 → Rev 62

/trunk/libraries/ds1307/ds1307.h
37,6 → 37,8
 
#include <useful/datetime.h>
 
#include <stdbool.h>
 
/******************************************************************************
* Public macro definitions.
******************************************************************************/
52,19 → 54,46
******************************************************************************/
 
/**************************************************************************//**
* \enum ds1307__hour_modes.
* \brief RTC hour modes.
*
* \typedef ds1307__hour_mode_t.
* \bried RTC hour mode.
* \brief RTC hour mode.
******************************************************************************/
/**************************************************************************//**
* \enum ds1307__hour_modes.
* \bried RTC hour modes.
******************************************************************************/
typedef enum ds1307__hour_modes
{
DS1307__HOUR_MODE__24_HOUR = 0, /*!< 24-hour mode. */
DS1307__HOUR_MODE__24_HOUR = 0, /*!< 24-hour mode. */
DS1307__HOUR_MODE__12_HOUR = 1 /*!< 12-hour mode. */
} ds1307__hour_mode_t;
 
/**************************************************************************//**
* \enum ds1307__sqw_out_levels
* \brief
*
* \typedef ds1307__sqw_out_level_t
* \brief
******************************************************************************/
typedef enum ds1307__sqw_out_levels
{
DS1307__SQW_LEVEL__LOW, /*!< Low level. */
DS1307__SQW_LEVEL__HIGH /*!< High level. */
} ds1307__sqw_out_level_t;
 
/**************************************************************************//**
* \enum ds1307__sqw_out_frequencies
* \brief
*
* \typedef ds1307__sqw_out__frequency_t
* \brief
******************************************************************************/
typedef enum ds1307__sqw_out_frequencies
{
DS1307__SQW_FREQUENCY__1_HZ,
DS1307__SQW_FREQUENCY__4096_HZ,
DS1307__SQW_FREQUENCY__8192_HZ,
DS1307__SQW_FREQUENCY__32768_HZ
} ds1307__sqw_out__frequency_t;
 
/******************************************************************************
* Public function prototypes.
******************************************************************************/
140,6 → 169,36
datetime__date_t* p_date
);
 
/**************************************************************************//**
* \fn void ds1307__set_square_wave_output_level(ds1307__sqw_out_level_t level)
*
* \brief Set square wave output pin level.
*
* \param level Level to set.
******************************************************************************/
void
ds1307__set_square_wave_output_level
(
ds1307__sqw_out_level_t level
);
 
/**************************************************************************//**
* \fn void ds1307__set_square_wave_output_signal(
* ds1307__sqw_out__frequency_t frequency,
* bool enable)
*
* \brief Set Square-Wave output signal.
*
* \param frequency Frequency.
* \param enable Status.
******************************************************************************/
void
ds1307__set_square_wave_output_signal
(
ds1307__sqw_out__frequency_t frequency,
bool enable
);
 
#ifdef _cplusplus
}
#endif
/trunk/libraries/ds1307/src/ds1307.c
31,9 → 31,13
#include <ds1307/ds1307.h>
 
#include <twi/twi.h>
#include <useful/bits.h>
#include <useful/datetime.h>
 
#include <avr/io.h>
 
#include <assert.h>
#include <stdbool.h>
#include <stdlib.h>
 
/******************************************************************************
40,17 → 44,28
* Private macros.
******************************************************************************/
 
#define DS1307__ADDRESS 0xD0
#define DS1307__ADDRESS 0xD0
 
#define DS1307__REGISTER_ADDRESS__SECONDS 0x00
#define DS1307__REGISTER_ADDRESS__MINUTES 0x01
#define DS1307__REGISTER_ADDRESS__HOURS 0x02
#define DS1307__REGISTER_ADDRESS__DAY 0x03
#define DS1307__REGISTER_ADDRESS__DATE 0x04
#define DS1307__REGISTER_ADDRESS__MONTH 0x05
#define DS1307__REGISTER_ADDRESS__YEAR 0x06
#define DS1307__REGISTER_ADDRESS__CONTROL 0x07
#define DS1307__REG_SECONDS 0x00
#define DS1307__REG_MINUTES 0x01
#define DS1307__REG_HOURS 0x02
#define DS1307__REG_DAY 0x03
#define DS1307__REG_DATE 0x04
#define DS1307__REG_MONTH 0x05
#define DS1307__REG_YEAR 0x06
#define DS1307__REG_CONTROL 0x07
 
#define DS1307__RS0 0x00
#define DS1307__RS1 0x01
#define DS1307__SQWE 0x04
#define DS1307__OUT 0x07
 
/**************************************************************************//**
* \def DS1307__REG_RAM
* \brief Start address of RAM registers (locations 0x08 to 0x3F).
******************************************************************************/
#define DS1307__REG_RAM 0x08
 
/******************************************************************************
* Public function definitions.
******************************************************************************/
92,7 → 107,7
 
twi__start();
twi__send_slaw(DS1307__ADDRESS);
twi__send_data(DS1307__REGISTER_ADDRESS__SECONDS);
twi__send_data(DS1307__REG_SECONDS);
twi__stop();
 
twi__start();
139,7 → 154,7
 
twi__start();
twi__send_slaw(DS1307__ADDRESS);
twi__send_data(DS1307__REGISTER_ADDRESS__SECONDS);
twi__send_data(DS1307__REG_SECONDS);
 
uint8_t seconds = ((p_time->seconds / 10) << 4) + (p_time->seconds % 10);
uint8_t minutes = ((p_time->minutes / 10) << 4) + (p_time->minutes % 10);
179,7 → 194,7
 
twi__start();
twi__send_slaw(DS1307__ADDRESS);
twi__send_data(DS1307__REGISTER_ADDRESS__DAY);
twi__send_data(DS1307__REG_DAY);
twi__stop();
 
twi__start();
219,7 → 234,7
 
twi__start();
twi__send_slaw(DS1307__ADDRESS);
twi__send_data(DS1307__REGISTER_ADDRESS__DAY);
twi__send_data(DS1307__REG_DAY);
 
uint8_t date = ((p_date->date / 10) << 4) + (p_date->date % 10);
uint8_t month = ((p_date->month / 10) << 4) + (p_date->month % 10);
232,3 → 247,75
 
twi__stop();
}
 
/**************************************************************************//**
* \fn void ds1307__set_square_wave_output_level(ds1307__sqw_out_level_t level)
*
* \brief Set square wave output pin level.
*
* \param level Level to set.
******************************************************************************/
void
ds1307__set_square_wave_output_level
(
ds1307__sqw_out_level_t level
){
twi__start();
twi__send_slaw(DS1307__ADDRESS);
twi__send_data(DS1307__REG_CONTROL);
uint8_t register_value = (level == DS1307__SQW_LEVEL__LOW) ? 0x00 : 0x80;
twi__send_data(register_value);
twi__stop();
}
 
/**************************************************************************//**
* \fn void ds1307__set_square_wave_output_signal(
* ds1307__sqw_out__frequency_t frequency,
* bool enable)
*
* \brief Set Square-Wave output signal.
*
* \param frequency Frequency.
* \param enable Status.
******************************************************************************/
void
ds1307__set_square_wave_output_signal
(
ds1307__sqw_out__frequency_t frequency,
bool enable
){
uint8_t register_value = 0;
 
// Rate select.
switch (frequency)
{
case DS1307__SQW_FREQUENCY__4096_HZ:
register_value = _BV(DS1307__RS0);
break;
 
case DS1307__SQW_FREQUENCY__8192_HZ:
register_value = _BV(DS1307__RS1);
break;
 
case DS1307__SQW_FREQUENCY__32768_HZ:
register_value = _BV(DS1307__RS1) | _BV(DS1307__RS0);
break;
 
case DS1307__SQW_FREQUENCY__1_HZ:
default:
register_value = 0;
break;
}
 
// Square-Wave enable.
if (enable)
{
BIT__SET(register_value, DS1307__SQWE);
}
 
twi__start();
twi__send_slaw(DS1307__ADDRESS);
twi__send_data(DS1307__REG_CONTROL);
twi__send_data(register_value);
twi__stop();
}