// ------------------------------------------------------------------------------
// ds1337_driver.c
//
// Provides interface to DS1337 Real TimeClock IC.
//
// ------------------------------------------------------------------------------
#include "i2c_8051.h"
#include "..\common_51\Com.h"
//#include "..\common_51\Timing.h"
//#include "..\common_51\hex_functions.h"
#include "ds1337_driver.h"
#include "51_flash.h"
code char *weekday_names[ ] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
date_time_t xdata *dtg;
// ==========================================================================
void ds1337_printDateTime( void )
{
pprintf( "\r\nTime: %02X", (int)dtg->hours );
pprintf( ":%02X:%02X %s ", (int)dtg->minutes, (int)dtg->seconds, weekday_names[ (int)dtg->day ] );
pprintf( "%02X/%02X/%02X\r\n\n", (int)dtg->month, (int)dtg->date, (int)dtg->year );
}
// ==========================================================================
void ds1337_fprintfDT( void )
{
DS1337_read_datetime( );
flprintf( "%02X:%02X:%02X ", (int)dtg->hours, (int)dtg->minutes, (int)dtg->seconds );
flprintf( " am %s ", weekday_names[ (int)dtg->day ] );
flprintf( "%02X/%02X/%02X ", (int)dtg->month, (int)dtg->date, (int)dtg->year );
}
// ==========================================================================
void DS1337_set_datetime( )
{
// Convert the input date/time into BCD values that are formatted for
// the DS1337 registers.
ds1337_printDateTime( );
delay10mS( 2 );
// Write to the date and time registers. Disable interrupts so they
// can't disrupt the i2c operations.
EA = 0;
I2C_Send_Start( );
I2C_Send_Byte( DS1337_I2C_WRITE_ADDR );
I2C_Send_Byte( DS1337_SECONDS_REG ); // Start at seconds register
I2C_Send_Byte( ( dtg->seconds ));
I2C_Send_Byte( ( dtg->minutes ));
I2C_Send_Byte( ( dtg->hours ));
I2C_Send_Byte( ( dtg->day ));
I2C_Send_Byte( ( dtg->date ));
I2C_Send_Byte( ( dtg->month ));
I2C_Send_Byte( ( dtg->year ));
I2C_Send_Stop( );
EA = 1; // re-enable interrupts
}
//----------------------------------------------
// Read the Date and Time from the hardware registers
// in the DS1337.
void DS1337_read_datetime( void )
{
uchar xdata dtBuf[ 8 ];
delay10mS( 2 );
// Disable interrupts so the i2c process is not disrupted.
EA = 0;
I2C_SeqReadByte( DS1337_I2C_WRITE_ADDR, DS1337_SECONDS_REG, 7, &dtBuf );
dtg->seconds = dtBuf[ 0 ];
dtg->minutes = dtBuf[ 1 ];
dtg->hours = dtBuf[ 2 ];
dtg->day = dtBuf[ 3 ];
dtg->date = dtBuf[ 4 ];
dtg->month = dtBuf[ 5 ];
dtg->year = dtBuf[ 6 ];
delay10mS( 2 );
EA = 1; // re-enable interrupts
delay10mS( 2 );
}
//----------------------------------------------
// If there has been an oscillator failure since last
// init, then init DS1337. A default time and date is set as well.
void DS1337_init( void )
{
EA = 0; // disable interrupts
I2C_Send_Start( );
I2C_Send_Byte( DS1337_I2C_WRITE_ADDR );
I2C_Send_Byte( DS1337_SECONDS_REG );
I2C_Send_Byte( 0x00 ); // seconds
I2C_Send_Byte( 0x00 ); // minutes
I2C_Send_Byte( 0x40 ); // hours & select 12 hr mode
I2C_Send_Byte( 0x00 ); // day
I2C_Send_Byte( 0x01 ); // date
I2C_Send_Byte( 0x01 ); // month
I2C_Send_Byte( 0x16 ); // year
I2C_Send_Byte( 0x00 ); // alm1 seconds
I2C_Send_Byte( 0x00 ); // alm1 minutes
I2C_Send_Byte( 0x00 ); // alm1 hours
I2C_Send_Byte( 0x00 ); // alm1 day/date
I2C_Send_Byte( 0x00 ); // alm2 minutes
I2C_Send_Byte( 0x00 ); // alm2 hours
I2C_Send_Byte( 0x00 ); // alm2 day/date
I2C_Send_Byte( DS1337_CTRL_REG_INIT_VAL ); // Turn off the squarewave output pin.
I2C_Send_Byte( DS1337_CLEAR_STATUS_VAL ); // Clear the status registers
I2C_Send_Stop( );
EA = 1;
}
/*
//----------------------------------------------
// If there has been an oscillator failure since last
// init, then init DS1337. A default time and date is set as well.
void DS1337_init_on_osc_fail( void )
{
uchar xdata temp = 0;
// Read the status register to see if the oscillator has failed.
I2C_ReadByte( DS1337_I2C_WRITE_ADDR, DS1337_STATUS_REG, &temp );
// Unpack OSF bit
temp = temp >> 7;
// If oscillator has failed then init DS1337
if ( temp )
{
DS1337_init( );
}
} */