Having problem with DS1307 year.

captnemo

New Member
Picaxe 18m2, DS1307 (stand alone chip with crystal), communicating to display and DS1307 over 12c.

The seconds, minutes, hours, date, and month all read back properly, however the year only shows the last digit.
If I set the year as $20 I'll get one zero (lsd )read back to the display. What's strange is, if I program the year as 19 or anything under 19
it returns that year and dislay's it perfectly but, as soon as I enter a year 20 or greater I have problems.
All the data is two digits, minutes hours, etc. including the year, so why am I having a problem with only the year? The program to load the data and read the data
to the DS1307 is the same for seconds, minutes, hours, date, month, year.
 

bpowell

Senior Member
I notice you didn't mention the "day" variable; which is at 03h address ... I wonder if you've skipped that by accident, and are trying to stuff $20 into the "month" slot (05h) vs. the "Year" slot (06h). If I recall correctly, you *can* exceed the limits of some of the rtc memory locations, but performance gets very unpredictable when you do. As @Jack Burns said: Share your code.
 

inglewoodpete

Senior Member
The following code was used on a 20X2 (in one of my 2011 year projects!) Hopefully, this will assit but note that i2c command syntax is slightly different.

Rich (BB code):
   Symbol bcdSeconds    = b20 'w10  Note that numeric data is sent/received to DS1307 in BCD format.
   Symbol bcdMinutes    = b21 'w10
   Symbol bcdHours      = b22 'w11
   Symbol bcdDay        = b23 'w11
   Symbol bcdDate       = b24 'w12
   Symbol bcdMonth      = b25 'w12
   Symbol bcdYear       = b26 'w13
   Symbol Control       = b27 'w13
'
' ----- StartClock: Set Time and Start Clock -----------------------------------------
'
'Set time and date e.g. to 11:59:00 on Thurs 25/12/03
'Numbers are expressed in BCD (Coded in Hex Eg $25 = "25")
'
'   bcdSeconds = CSSSssss C: Clock Halt = 1, Run=0        SSS=0-5 ssss=0-9
'   bcdMinutes = 0MMMmmmm                                 MMM=0-5 mmmm=0-9
'   bcdHours   = 00HHhhhh 24-hour mode                     HH=0-2 hhhh=0-9
'or bcdHours   = 011ahhhh 12-hour mode  a=0 am; a=1 pm     HH=0-2 hhhh=0-9
'   bcdDay     = 00000ddd Day-of-week indicator                    ddd=1-7
'   bcdDate    = 00DDdddd                                  DD=0-3 dddd=0-9
'   bcdMonth   = 000Mmmmm                                   M=0-1 mmmm=0-9
'   bcdYear    = YYYYyyyy                                YYYY=0-9 yyyy=0-9
'   Control = %o00E00RR  Enable output pin
'              o..E..RR  RR Rate of square wave output, where...
'              |  |  |_  00 1Hz; 01 4096Hz; 10 8192Hz; 11 32768Hz
'              |  |____  Enable square wave output = 1; Disable = 0
'              |_______  Set output level of pin when square wave in DISabled 
'
' Note that programme takes about 40 seconds to download when configuring the time here
'
StartClock: bcdSeconds  = $40 ' If Bit 7 (MSB) = 1 then clock oscillator is disabled: programme takes about 30 seconds to load
            bcdMinutes  = $52 ' Bit 7 always = 0
            bcdHours    = $17 ' Bit 7 = 0; Bit 6: 0 for 24-hour, 1 for 12-hour; If 12-hour, Bit 5 is am/pm indicator 
            bcdDay      = $05 ' Bits 7 - 3 always 0 Monday = 1; Sunday = 7
            bcdDate     = $02 ' Bits 7 - 6 always 0
            bcdMonth    = $09 ' Bits 7 - 5 always 0
            bcdYear     = $11 ' BCD value range = 00 to 99
            '
            Control  = %00010000 ' Enable output at 1Hz
            '
            hi2cSetUp i2cMaster, i2cAddrDS1307, i2cslow, i2cbyte  ' set DS1307 slave address with byte addressing
            Writei2c [noparse][[/noparse]i2cAddrDS1307[noparse]][/noparse], 0,(bcdSeconds, bcdMinutes, bcdHours, bcdDay, bcdDate, bcdMonth, bcdYear, Control)
            '
            SerTxd (CR, LF, "Clock now set.", CR, LF)
            'GoSub GetTime                      'Read the time variables
            'GoSub ShowTime
            Return
 

captnemo

New Member
I skipped the "day" when setting and reading the clock. That's what screwed me up. I put it in and it's working fine.
Thanks for every ones help.
However. One last problem. I'm running 24hr format and changed the software to read in 12 hour format and that's fine.
I need a way of putting AM or PM in the display without using the AM/PM signal from the DS1307, since it's not available in the 24 hour format.
Any ideas.
 

papaof2

Senior Member
Read the hour in 24 hour format and set your AM/PM flag by the value of the hour. Less than 12 = AM. Greater than 11 = PM. I haven't touched the code for my DS1307 clock in a long time so I don't know how difficult that would be or if there's an easier way. I display the time in 24 hour format so I don't have that problem.
 
Top