This is part of a library I wrote forthe DS1307 RTC. I'm displaying the time on the 7segment displays using interrupt routine to multiplex them. After a random amount of time 5-10 minutes my program appears to hang up somewhere in this Ds1307_GetDateTime function. The 7Segs will continue to update via the interrupt routine, but the time never changes because this function never returns.
I call this routine once a second, when the clock pulse from the clock goes high.
Also when it hangs... The clock chip is still pulsing.
I determined it is hangin in this function by setting an output pin high right before calling it then setting it low when it returns. Watching this pin it will show it to be high all the time when the "hang occurs". It toggles otherwise.
Any ideas?
Code: Select all
const DS1307_OK = 0 ' Everything it OK
const DS1307_I2C_ERROR = 1 ' General I2C Error
const DS1307_TIME_NOT_SET = 2 ' Time not set
structure DS1307_DateTime ' Date time structure
dim Sec as byte
dim Min as byte
dim Hr as byte
dim DOW as byte
dim Day as byte
dim Mnth as byte
dim Yr as byte
end structure
'******************************************************************************
'* This procedure will get the values in the structure from the DS1307. *
'******************************************************************************
sub function DS1307_GetDateTime(dim byref ts as DS1307_DateTime) as byte
dim temp as byte ' a temporary variable
'
result = DS1307_I2C_ERROR ' Set default error
if I2C_Start = 0 then ' Start and continue if ok
if I2C_Wr(%11010000) = 0 then ' Send chip address and WRITE
if I2C_Wr(0x00) = 0 then ' Send pointer address
I2C_Repeated_Start ' Send Repeated Start
I2C_Wr(%11010001) ' Send chip address and WRITE
ts.Sec = I2C_Rd(1) ' Get seconds -- ACK
ts.Min = I2C_Rd(1) ' Get minutes -- ACK
ts.Hr = I2C_Rd(1) ' Get hours -- ACK
ts.DOW = I2C_Rd(1) ' Get day of week -- ACK
ts.Day = I2C_Rd(1) ' Get day -- ACK
ts.Mnth = I2C_Rd(1) ' Get month -- ACK
ts.Yr = I2C_Rd(0) ' Get year -- ACK
I2C_Stop ' Stop
end if '
end if '
end if '
end sub