ritchie
Joined: 13 Sep 2003 Posts: 87
|
DS1307 Driver Inquiry |
Posted: Sun Jun 12, 2005 7:53 pm |
|
|
Hi,
I got a DS1307 driver from this forum. I guess this is from PCM Programmer.
The code below from http://www.ccsinfo.com/forum/viewtopic.php?t=19926&highlight=ds1307
Code: |
void ds1307_set_date_time(void)
{
char i;
// Convert the binary ds1307 data, which is passed in a global array,
// into bcd data. Store it in the same array.
for(i = 0; i < 7; i++)
{
gca_ds1307_regs[i] = bin2bcd(gca_ds1307_regs[i]);
}
// There are two control bits embedded in the following data.
// The Clock Halt bit is in bit 7 of the DS1307 Seconds register.
// We need to make sure that it's = 0, to make the clock run.
// The other bit is the 24/12 hour clock format bit, in bit 6 of
// the DS1307 Hours register. We need 24 hour mode, so set it = 0.
gca_ds1307_regs[DS1307_SECONDS_REG] &= 0x7f;
gca_ds1307_regs[DS1307_HOURS_REG] &= 0x3f;
// Now write the 7 bytes of BCD data to the ds1307,
// using inline code for speed.
disable_interrupts(GLOBAL);
i2c_start();
i2c_write(DS1307_I2C_WRITE_ADDR);
// Start reading at the Seconds register.
i2c_write(DS1307_SECONDS_REG);
// Write 7 bytes, to registers 0 to 6.
for(i = 0; i < 7; i++)
{
i2c_write(gca_ds1307_regs[i]);
}
// After setting the time in registers 0-6, also set the
// Control register. (index = 7)
// This just turns off the squarewave output pin.
// Doing it here, every time we set the clock registers,
// seems less risky than setting it near the
// start of the program, every time the unit powers-up.
i2c_write(DS1307_CONTROL_REG_INIT_VALUE);
i2c_stop();
enable_interrupts(GLOBAL);
}
|
I notice that the code above does not need to wait for an ACK from the DS1307 chip. According to the datasheet their is an ACK for ervery data send.
Is this possible without waiting for an ACK?
If I put a wait for an ACK routine, would it be advisable?
Code: |
while (TRUE) {
i2c_start();
// look for the ACK from the device to ensure the
// devide is redy
if (i2c_write(F_WR_ADDRESS) == 0)
break;
}
|
The code above check if their is an ACK from the device.
I need your comments and suggestions.
Thank you. |
|