|
|
View previous topic :: View next topic |
Author |
Message |
gs
Joined: 22 Aug 2005 Posts: 30 Location: Ioannina - Greece
|
DS1340 RTC driver |
Posted: Sun Dec 17, 2006 9:32 am |
|
|
Hello, I finally got a DS1340 and played with it the last few days, I post here the results.
The driver checks for the OSF (Oscillator Stop Flag) to check data validity so if you are reading all zeros, you have to re-adjust the time.
Please post any comments for improvements.
Code: |
///////////////////////////////////////////////////////////////////////////////////
/// DS1340.C ///
/// Driver for Real Time Clock ///
/// For Dallas/Maxim DS1340 RTC (I2C, trickle charge, etc) ///
/// Based on DS1307 driver. ///
/// ///
/// rtc_init() -Call once at start-up. To Set trickle charge state ///
/// uncomment the line you want below. No Calibration ///
/// used here Control byte is set to all 0's ///
/// ///
/// ///
/// rtc_set_date_time(day,mth,year,dow,hour,min,sec) Set the date/time ///
/// ///
/// rtc_get_date(day,mth,year,dow) Get the date* ///
/// ///
/// rtc_get_time(hr,min,sec) Get the time* ///
/// ///
/// *(if returned value's are all zeros this means that the clock ///
/// stopped for some reason and the time is invalid) ///
/// ///
///////////////////////////////////////////////////////////////////////////////////
//#define RTC_SDA PIN_C4
//#define RTC_SCL PIN_C3
//#use i2c(master, sda=RTC_SDA, scl=RTC_SCL, FORCE_HW)
BYTE bin2bcd(BYTE binary_value);
BYTE bcd2bin(BYTE bcd_value);
void rtc_check_valid();
void rtc_init(void)
{
BYTE seconds = 0;
i2c_start();
i2c_write(0xD0); // WR to RTC
i2c_write(0x00); // REG 0
i2c_start();
i2c_write(0xD1); // RD from RTC
seconds = bcd2bin(i2c_read(0)); // Read current "seconds" in DS1340
i2c_stop();
seconds &= 0x7F;
delay_us(3);
i2c_start();
i2c_write(0xD0); // WR to RTC
i2c_write(0x00); // REG 0
i2c_write(bin2bcd(seconds)); // Start oscillator with current "seconds value
i2c_start();
i2c_write(0xD0); // WR to RTC
i2c_write(0x07); // goto Control reg
i2c_write(0); // Set control byte to zer0s
i2c_start();
i2c_write(0xD0); // WR to RTC
i2c_write(0x08); // goto trickle address
// Set Trickle Charger byte (uncomment ONLY one of the following)*****************
// i2c_write(0b00100101); // disabled
// i2c_write(0b10100101); // No diode, 250Ω resistor
// i2c_write(0b10101001); // One diode, 250Ω resistor
// i2c_write(0b10100110); // No diode, 2kΩ resistor
// i2c_write(0b10101010); // One diode, 2kΩ resistor
i2c_write(0b10100111); // No diode, 4kΩ resistor
// i2c_write(0b10101011); // One diode, 4kΩ resistor
// Set Trickle Charger byte
i2c_stop();
}
void rtc_set_date_time(BYTE day, BYTE mth, BYTE year, BYTE dow, BYTE hr, BYTE min, BYTE sec)
{
sec &= 0x7F;
hr &= 0x3F;
i2c_start();
i2c_write(0xD0); // I2C write address
i2c_write(0x00); // Start at REG 0 - Seconds
i2c_write(bin2bcd(sec)); // REG 0
i2c_write(bin2bcd(min)); // REG 1
i2c_write(bin2bcd(hr)); // REG 2
i2c_write(bin2bcd(dow)); // REG 3
i2c_write(bin2bcd(day)); // REG 4
i2c_write(bin2bcd(mth)); // REG 5
i2c_write(bin2bcd(year)); // REG 6
i2c_start();
i2c_write(0xD0); // WR to RTC
i2c_write(0x09); // Set OSF address
i2c_write(0); // Clear OSF
i2c_stop();
}
void rtc_get_date(BYTE &day, BYTE &mth, BYTE &year, BYTE &dow)
{
i2c_start();
i2c_write(0xD0);
i2c_write(0x03); // Start at REG 3 - Day of week
i2c_start();
i2c_write(0xD1);
dow = bcd2bin(i2c_read() & 0x7f); // REG 3
day = bcd2bin(i2c_read() & 0x3f); // REG 4
mth = bcd2bin(i2c_read() & 0x1f); // REG 5
year = bcd2bin(i2c_read(0)); // REG 6
i2c_stop();
rtc_check_valid();
}
void rtc_get_time(BYTE &hr, BYTE &min, BYTE &sec)
{
i2c_start();
i2c_write(0xD0);
i2c_write(0x00); // Start at REG 0 - Seconds
i2c_start();
i2c_write(0xD1);
sec = bcd2bin(i2c_read() & 0x7f);
min = bcd2bin(i2c_read() & 0x7f);
hr = bcd2bin(i2c_read(0) & 0x3f);
i2c_stop();
rtc_check_valid();
}
void rtc_check_valid()
{
int validity;
i2c_start(); //read OSF bit to check data validity
i2c_write(0xD0);
i2c_write(0x09);
i2c_start();
i2c_write(0xD1);
validity=i2c_read(0);
i2c_stop();
if (validity!=0) //if data not valid make them zeros
{
sec=0;
min=0;
hour=0;
dow=0;
year=0;
mth=0;
day=0;
}
}
BYTE bin2bcd(BYTE binary_value)
{
BYTE temp;
BYTE retval;
temp = binary_value;
retval = 0;
while(1)
{
// Get the tens digit by doing multiple subtraction
// of 10 from the binary value.
if(temp >= 10)
{
temp -= 10;
retval += 0x10;
}
else // Get the ones digit by adding the remainder.
{
retval += temp;
break;
}
}
return(retval);
}
// Input range - 00 to 99.
BYTE bcd2bin(BYTE bcd_value)
{
BYTE temp;
temp = bcd_value;
// Shifting upper digit right by 1 is same as multiplying by 8.
temp >>= 1;
// Isolate the bits for the upper digit.
temp &= 0x78;
// Now return: (Tens * 8) + (Tens * 2) + Ones
return(temp + (temp >> 2) + (bcd_value & 0x0f));
}
|
_________________ www.hlektronika.gr |
|
|
YARGICH
Joined: 19 Jan 2007 Posts: 8
|
|
Posted: Wed Mar 18, 2009 7:56 pm |
|
|
Hi,
I tried to use your ds1340 driver. But when I read hour, minute and second, I always read hour=0x2D, minute=0x55, and second=0x55. what do you think about my problem ?
I am using ds1340c model. You know that, ds1340 have internal xtal in its ds1340c model. Should I define to use this xtal to my ds1340c?
best regards. |
|
|
gs
Joined: 22 Aug 2005 Posts: 30 Location: Ioannina - Greece
|
|
Posted: Thu Mar 19, 2009 4:26 pm |
|
|
I don't see any settings on the datasheet specifically for the C version, but I noticed this:
Quote: | All N.C. (no connect) pins must be connected
to ground. |
I did not read thorougly the datasheet and the last time I used this chip is some years ago. _________________ www.hlektronika.gr |
|
|
YARGICH
Joined: 19 Jan 2007 Posts: 8
|
|
Posted: Thu Mar 19, 2009 6:16 pm |
|
|
Thx for your research. I solved this problem. When I read hour, minute, and second data serially, It gave me wrong datas. after that I wrote separate routines to read these datas. Now, It gives me true datas. But in this wise, I must call different routines to take all datas.
Actually it is not important for me. Thanks a lot for DS1340 CCS driver.
Best Regards. |
|
|
sajjadonline
Joined: 07 Sep 2011 Posts: 5 Location: Banned - spammer
|
tnx for ds1307 library |
Posted: Wed Feb 27, 2013 3:22 am |
|
|
very nice this library gs |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|