Real Time Clock Module with I2C Interface  0.3
⇒ back to Project Page
 All Data Structures Files Functions Variables Typedefs Macros Pages
RTC Module
Revision:
0.3

This documentation describes the source code for the Real Time Clock Module with an I2C interface. This source code was written and tested for an ATmega168 controller.
The RTC module can be accessed via I2C, at the address defined by I2C_SLAVE_ADDRESS in i2c.h. The following registers are available for reading and/or writing:

 Address         Bit                Value                               Range           Access

  0x00           0...7             current register address           [see i2c.h]         R/W

  0x01            0                current_dcf_signal                   [0, 1]             R
                  1                dcf_enabled                          [0, 1]            R/W
                 2...6             dcf_state                          [see dcf77.h]        R
                  7                set_time_command                     [0, 1]             W

  0x02           0...5             seconds                             [0...59]           R/W
                 6...7             minutes (0...1)                     [0...59]           R/W

  0x03           0...3             minutes (2...5)                     [0...59]           R/W
                 4...7             month                               [1...12]           R/W

  0x04           0...4             hours                               [0...24]           R/W
                 5...7             day of week                        [see dcf77.h]       R/W

  0x05           0...4             day                                 [0...31]           R/W

  0x06           0...6             year                                [0...99]           R/W

For example, a master can read the time information (using the provided I2C interface function i2c_master_io in i2c.c) from this module in this way:

#define I2C_RTC_SLAVE_ADDRESS 0xaa //Slave Address for RTC on I2C Bus (including Write Bit)
//(7 Bit address + Write Bit)
uint8_t rtc_data[I2C_MASTER_BUFFER_LENGTH];
uint8_t i2c_retry_counter;
real_time_data real_time; //see dcf77.h
uint8_t return_value;
//set address///////////////////////////////////////////////////
rtc_data[0] = I2C_RTC_SLAVE_ADDRESS; //SLA+W
rtc_data[1] = 0x00;
//I2C communication
i2c_retry_counter = I2C_MAX_RETRYS;
return_value = I2C_IO_ERROR;
while (return_value != I2C_IO_SUCC && i2c_retry_counter > 0) {
return_value = i2c_master_data_io(rtc_data, 2, FALSE);
i2c_retry_counter--;
_delay_us(I2C_RETRY_WAIT_US);
}
//read from data registers if setting address was successful////
if (return_value == I2C_IO_SUCC) {
rtc_data[0] = I2C_RTC_SLAVE_ADDRESS | 0x01; //SLA+R
//I2C communication
i2c_retry_counter = I2C_MAX_RETRYS;
return_value = I2C_IO_ERROR;
while (return_value != I2C_IO_SUCC && i2c_retry_counter > 0) {
return_value = i2c_master_data_io(rtc_data, 7, FALSE);
i2c_retry_counter--;
_delay_us(I2C_RETRY_WAIT_US);
}
}
//extract time/date information/////////////////////////////////
if (return_value == I2C_IO_SUCC) {
//extract real time from I2C registers
real_time.current_second = rtc_data[2] & 0x3f;
real_time.current_minute = (rtc_data[2] >> 6) & 0x03;
real_time.current_minute = real_time.current_minute | ((rtc_data[3] & 0x0f) << 2);
real_time.current_month = (rtc_data[3] >> 4) & 0x0f;
real_time.current_hour = rtc_data[4] & 0x1f;
real_time.current_weekday = (rtc_data[4] >> 5) & 0x07;
real_time.current_day = rtc_data[5] & 0x1f;
real_time.current_year = rtc_data[6] & 0x7f;
}

If enabled, the current time and date is also printed on a LCD (currently 8x2 characters). Therefore, the definition RTC_LCD_TIMEDEBUG should be set to "TRUE". See also the file "port_definitions.h" for the pin/port settings.

If the definition of RTC_SAA1064_OUTPUT is set to "1", the module sends the time data to three SAA1064 chips for displaying the time via a 7 segment LED displays (each SAA1064 controls two 7 segment LED displays separately for hours, minutes or seconds). The interface to the SAA1064 chips is the same I2C bus, the I2C adresses of these chips are defined in the file saa1064.h (I2C_SAA1064_ADDRESS1 for hours, I2C_SAA1064_ADDRESS2 for minutes and I2C_SAA1064_ADDRESS3 for seconds).

For a detailed description of the functions, see the function description, starting with main()...