DS1307 Code Examples

marks

Senior Member
24 hour or 12 hour modes.
code was quickly cut for the 20x2 hopefully it will help ! lol
both programs will display in the terminal window 12 hour format.

To program the ds1307 in 24 hour mode we enter for
hours $00 to $23
mins $00 to $59
secs $00 to $59
Code:
        # picaxe20x2
        # no_data
        # no_table
       
        SYMBOL character1    = B2
	SYMBOL character2    = B3  
	SYMBOL character3    = B4  
	SYMBOL character4    = B5  
	SYMBOL character5    = B6  
	SYMBOL character6    = B7
	SYMBOL hours         = B8
        SYMBOL mins          = B9
	SYMBOL secs          = B10
	SYMBOL day           = B11 
	SYMBOL date          = B12
	SYMBOL month         = B13
	SYMBOL year          = B14
	SYMBOL PM_AM         = B15 
	 
Program:            
   HI2Csetup I2Cmaster, %11010000, I2Cslow, I2Cbyte      ' set to 100kbps 
       HI2Cout $0, ( $00,$58 ,$23  ,day,date,month,year) ' write time to DS1307  example (11.58.00 PM)
'prog time         (secs,mins,hours,day,date,month,year) ' enter hours in 24 hour format ($0 to $23)
       
Main: 
     	 HI2Csetup I2Cmaster, %11010000, I2Cslow, I2Cbyte        
	 HI2Cin $0,  (secs,mins,hours,day,date,month,year) ' read time DS1307
	 
ClockDisplay:
    PM_AM ="P" : IF hours < $12 then :PM_AM = "A" : ENDIF       'indicate PM or AM
    
    IF hours = $20 OR hours = $21 THEN : LET hours = hours - $6 : ENDIF
    IF hours > $12 THEN : LET hours = hours - $12 : ENDIF       '24 to 12 hour format
    IF hours = $0 THEN  : hours = $12 : ENDIF
    

   BcdTOASCII hours,character1,character2 : IF character1 = "0" THEN  :  character1 = " "  :  ENDIF' zero blanking character1
   BcdTOASCII mins ,character3,character4
   BcdTOASCII secs ,character5,character6
   
 sertxd (CR, LF,character1,character2,".",character3,character4,".",character5,character6," ",PM_AM,"M", CR, LF)'(11.58.00 PM)

pause 1000 : goto main
To program the ds1307 in 12 hour mode we enter for
hours AM $41 to $52 (1-12)
hours PM $61 to $72 (1-12)
mins $00 to $59
secs $00 to $59
Code:
        # picaxe20x2
        # no_data
        # no_table
       
        SYMBOL character1    = B2
	SYMBOL character2    = B3  
	SYMBOL character3    = B4  
	SYMBOL character4    = B5  
	SYMBOL character5    = B6  
	SYMBOL character6    = B7
	SYMBOL hours         = B8
        SYMBOL mins          = B9
	SYMBOL secs          = B10
	SYMBOL day           = B11 
	SYMBOL date          = B12
	SYMBOL month         = B13
	SYMBOL year          = B14
	SYMBOL AM_PM         = B15 
	 
Program:            
 HI2Csetup I2Cmaster, %11010000, I2Cslow, I2Cbyte          ' set to 100kbps 
     HI2Cout $0, ( $00,$58 , $71 ,day,date,month,year)     ' write time to DS1307  example (11.58.00 PM)
 ' prog time     (secs,mins,hours,day,date,month,year)     ' hours AM 1-12 ($41 to $52)  
                                                           ' hours PM 1-12 ($61 to $72)       
Main: 
     HI2Csetup I2Cmaster, %11010000, I2Cslow, I2Cbyte        
     HI2Cin $0  ,(secs,mins,hours,day,date,month,year)     ' read time DS1307
	 
ClockDisplay:
     LET hours = hours - $40 :AM_PM = "A"                              ' 12 hour format   AM 
     IF hours > $20 THEN : LET hours = hours - $20 : AM_PM ="P": ENDIF ' 12 hour format   PM 

   BcdTOASCII hours,character1,character2 : IF character1 = "0" THEN  :  character1 = " "  :  ENDIF' zero blanking character1
   BcdTOASCII mins ,character3,character4
   BcdTOASCII secs ,character5,character6
   
 sertxd (CR, LF,character1,character2,".",character3,character4,".",character5,character6," ",AM_PM,"M", CR, LF)'(11.58.00 PM)

pause 1000 : goto main
 
Last edited:

westaust55

Moderator
Although you mention in the covering words that you are using a 20X2 for which the default clock frequency is 8 MHz, in the code examples you have keywords based upon 16 MHz.

Code:
HI2CSETUP I2CMASTER, %11010000, [B]I2Cslow[COLOR="Red"]_16[/COLOR][/B], I2CBYTE
To include the extra lines given below needs to be considered/mentioned

Code:
#PICAXE 20X2
SETFREQ m16
or remove the _16 portion of the keyword for operation at the default PICAXE clock speed.
 

marks

Senior Member
I like your red marking pen.
thanks westaust55

I also know i have been a bit spoilt using the 20x2 others which i'm not too familiar with!
and just realising the other chips lack space and those extra commands
 
Last edited:

ptribbey

New Member
Simple clock / date good untill 2099. I used code I found at PH Anderson, as well as my own.
Code:
'using ph anderson 117 serial lcd driver and 18x picaxe
symbol LCD=7 'output 7 to serial LCD driver
symbol Number = b8
symbol Digit = b9

pause 2000
i2cslave %11010000, i2cslow, i2cbyte

goto readclock 'comment out after first time through
start_clock:'all in BCD format
let b0 = $00'seconds
let b1 = $29'minutes 
let b2 = $18'hours 
let b3 = $05'day 
let b4 = $02'date 
let b5 = $02'month
let b6 = $12'year - 2012
let b7 = %01110000 'as found in the DS1307 datasheet 
writei2c 0,(b0,b1,b2,b3,b4,b5,b6,b7)
serout LCD,T2400,("?f") 'commands for ph anderson 117 lcd driver

readClock:
i2cslave %11010000, i2cslow, i2cbyte
readi2c 0, (b0,b1,b2,b3,b4,b5,b6)
serout LCD,T2400,(254,128)
serout LCD,T2400,("?a")
Number = b2 'hours
Gosub Print2DigitNbr
serout LCD,T2400,(":")
Number = b1 'minutes
Gosub Print2DigitNbr
serout LCD,T2400,(":")
Number = b0 'seconds
Gosub Print2DigitNbr

serout LCD,T2400,(" ")
Number = b5 'month
Gosub Print2DigitNbr
serout LCD,T2400,("/")
Number = b4 'date
Gosub Print2DigitNbr
serout LCD,T2400,("/")
serout LCD,T2400,("20")
Number = b6 'year
Gosub Print2DigitNbr

serout LCD,T2400,("?a")
goto readClock

Print2DigitNbr:
Digit=Number & %11110000
Digit=Digit/16
serout LCD,T2400,(#Digit)

Digit=Number & %00001111 
serout LCD,T2400,(#Digit)
return
 

marks

Senior Member
Credit goes to Randy5140 from his interesting idea of wanting to
CountingDown To Christmas!

Code:
      SYMBOL character1    = B2
	SYMBOL character2    = B3  
	SYMBOL character3    = B4  
	SYMBOL character4    = B5  
	SYMBOL character5    = B6  
	SYMBOL character6    = B7
	SYMBOL hours         = B8
      SYMBOL mins          = B9
	SYMBOL secs          = B10
	SYMBOL day           = B11 
	SYMBOL date          = B12  SYMBOL daysRemaining = B12
	SYMBOL month         = B13
	SYMBOL year          = B14
	SYMBOL PM_AM         = B15 
	 
Program:            
   HI2Csetup I2Cmaster, %11010000, I2Cslow, I2Cbyte       ' set to 100kbps 
       HI2Cout $0,  ( $00, $58, $23 ,day, $24, $12 , $13) ' write time to ds3231 or ds1307  example (11.58.00 PM)
'prog time          (secs,mins,hours,day,date,month,year) ' enter hours in 24 hour format ( $0 to $23 )

Main: 
pause 1000
     	 HI2Csetup I2Cmaster, %11010000, I2Cslow, I2Cbyte        
	 HI2Cin $0,   (secs,mins,hours,day,date,month,year) ' read time 
	 
ClockDisplay:
    PM_AM ="P" : IF hours < $12 then :PM_AM = "A" : ENDIF       'indicate PM  or AM
    
    IF hours = $20 OR hours = $21 THEN : LET hours = hours - $6 : ENDIF
    IF hours > $12 THEN : LET hours = hours - $12 : ENDIF       '24 to 12 hour format
    IF hours = $0 THEN  : hours = $12 : ENDIF
    
   BcdTOASCII hours,character1,character2 : IF character1 = "0" THEN : character1 = " " : ENDIF ' zero blanking character1
   BcdTOASCII mins ,character3,character4
   BcdTOASCII secs ,character5,character6 
 sertxd (CR,LF,character1,character2,".",character3,character4,".",character5,character6," ",PM_AM,"M    ") '(11.58.00 PM)
 
DateMonthYearDisplay:
   BcdTOASCII date ,character1,character2
   BcdTOASCII month,character3,character4
   BcdTOASCII year ,character5,character6
 sertxd (character1,character2,"/",character3,character4,"/20",character5,character6,CR,LF)' (05/11/13)
   

   
HI2Csetup I2Cmaster, %11010000, I2Cslow, I2Cbyte        
	HI2Cin $0,   (secs,mins,hours,day,date,month,year) ' read time	 
IF month <> $12 THEN Main

 ChristmasCountdown:
     IF date = $25 THEN Message
     IF date > $25 THEN Main  
     date  = date  / 16 * 250 + date  ' convert BCD to Binary 
     daysRemaining  = 24 - date
     hours  = hours  / 16 * 250 + hours  ' convert BCD to Binary
     hours = 23-hours
     mins  = mins  / 16 * 250 + mins  ' convert BCD to Binary
     mins = 59-mins
     secs  = secs  / 16 * 250 + secs  ' convert BCD to Binary
     secs = 60-secs   
  sertxd (#daysremaining," Days   ",#hours," Hours   ",#Mins," Mins   ",#secs," Secs",CR,LF)      
     goto main
     
 Message: 
  sertxd ("Merry Christmas EveryOne! lol") 
     goto main
 

Randy5140

Member
Marks,,,

Thanks,, but you did the work! I was totally lost on how to refer to the clock and then,, well I do think that 2 + 2 = 6 ;)


again,
Thanks

From Cabot and Garrett too!

Merry Christmas to all
 

marks

Senior Member
Here's an example program where we take advantage of the new PICAXE Editor 6.0.7.5 (beta)
using the preprocessor command (ppp_datetime) to enter into our DS1307.
Because we add 16secs , time for it to program , best to doit just after the new minute.
the code is similiar to that used on the DS3231 so more examples to be found there also
http://www.picaxeforum.co.uk/showthread.php?18694-DS3231-Code-examples

Rich (BB code):
#picaxe 18m2
#terminal 38400 'marks
SYMBOL index    = b0  :  SYMBOL CRC      = b0
SYMBOL hours    = b1
SYMBOL mins     = b2
SYMBOL secs     = b3 
SYMBOL day      = b4       ' not used
SYMBOL date     = b4 
SYMBOL month    = b5  
SYMBOL year     = b6

SETFREQ M32   
;;;;   Collect  date and time from the PICAXE Editor 6.0.7.5 (BETA)
          bptr=28
          FOR  index = 2 TO 18     
            LOOKUP index,(ppp_datetime),@bptrinc      '  ("2015-01-31 10:00:25")
          NEXT index
;;;;    Entering date and time in $BCD  convert from ASCII  
bptr =28
year  = @bptrinc*$10+@bptrinc-$30 :inc bptr          ' $0 to $99
month = @bptrinc*$10+@bptrinc-$30 :inc bptr          ' $1 to $12
date  = @bptrinc*$10+@bptrinc-$30 :inc bptr          ' $1 to $31
hours = @bptrinc*$10+@bptrinc-$30 :inc bptr          ' $0 to $23
mins  = @bptrinc*$10+@bptrinc-$30 :inc bptr          ' $0 to $59
secs  = @bptrinc*$10+@bptrinc-$30                    ' $0 to $59
     
InitialiseTime:
     secs = secs + $16                                   ' add 16 seconds (how long it takes for PC to program)
    READ  255,CRC : IF CRC <> 0 THEN DisplayDateTime     ' Skip if time already programmed
     HI2Csetup I2Cmaster, %11010000, I2Cslow_32, I2Cbyte ' Set  DS1307 to 100kbps
      HI2Cout $0 , (secs,mins,hours,day,date,month,year) ' Write to DS1307
    WRITE 255,53                                         ' we can make use of an existing eeprom entry if any

Main:
DisplayDateTime:
    HI2Csetup I2Cmaster, %11010000, I2Cslow_8, I2Cbyte   ; Set  DS1307 to 100kbps
      HI2Cin  $0 , (secs,mins,hours,day,date,month,year) ' Read DS1307
       HI2Csetup off                                     ; 
        FOR bptr = 1 TO 6  
          @bptr = @bptr/16*250+@bptr                     ' Convert BCD to Binary 
         NEXT

  sertxd ("20",#year,"/",#month,"/",#date,"    ")        ' date
    index = mins/10 :mins = mins//10                 
    sertxd (#hours,".",#index,#mins)                     ' 24hr time         
      index = secs/10 :secs = secs//10
      sertxd (".",#index,#secs,CR,LF)
pause 2000                                               '
GOTO Main:
edit change
Code:
 too [code="rich"]
 
Last edited:

marks

Senior Member
US Day Light Savings
Luckily we don't have it.
But we do get the occasional mention on the forum
most popular seem to be after the US and UK time zones.
just program the DS1307 with the standard time
and the code will adjust date and time 1 hour during the daylight saving days.
Rich (BB code):
#picaxe 18m2
#terminal 38400 'marks
SYMBOL index    = b0  
SYMBOL hours    = b1
SYMBOL mins     = b2
SYMBOL secs     = b3
SYMBOL day      = b7     ' not used
SYMBOL date     = b4
SYMBOL month    = b5
SYMBOL year     = b6
SYMBOL CommonYear  = b8
SYMBOL DayNumber   = W6
SYMBOL DSTstart    = W7
SYMBOL DSTend      = W8  : SYMBOL DayS = W8
SETFREQ M32   
InitialiseTime:
    HI2Csetup I2Cmaster, %11010000, I2Cslow_32, I2Cbyte  ' Set  DS1307 to 100kbps
      HI2Cout $0 , ( $30, $59, $1,day, $01 , $11 , $15) ' program DS1307 with GMT
Main: pause 8000
    HI2Csetup I2Cmaster, %11010000, I2Cslow_32, I2Cbyte  ' Set  DS1307 to 100kbps
      HI2Cin  $0 , (secs,mins,hours,day,date,month,year) ' Read DS1307

        FOR bptr = 1 TO 6
          @bptr = @bptr/$10*250+@bptr                     ' Convert BCD to Decimal
        NEXT
EasternDaylightTime:                       ' United States    
     CommonYear = year //4 +3 /4            ' CommonYear =1            
      DayNumber = month +9 /12
      DayNumber = CommonYear + DayNumber * DayNumber
      DayNumber = month *275 /9 +date -30 -DayNumber
           DayS = year *368 **65046         ' year =1 to 99  
       DSTstart = Days +72//7              
       DSTstart = 74 -CommonYear -DSTstart  ' Second SunDay in March
        DSTend = Days +310 //7              
        DSTend = 312 -CommonYear -DSTend    ' First Sunday in November
 
IF DayNumber < DSTstart OR DayNumber > DSTend THEN Display
IF DayNumber = DSTstart AND hours <2 THEN Display   ' DST starts at 0200
IF DayNumber = DSTend   AND hours >1 THEN Display    ' DST ends at 0200
    IF hours =23 THEN : INC date      
      IF date =31 AND month =4 OR month =6 OR month =9 THEN : date =1 INC month: ENDIF
      IF date =32 THEN : date =1 INC month: ENDIF   
    ENDIF : hours =hours +1//24        
   Sertxd ("EDT  ") :  GOTO DateTime                 ' DST +1
Display:Sertxd ("EST  ")                                   ' StandardTime
DateTime:
   sertxd ("20",#year,"/",#month,"/",#date,"    ")    ' Date 20yy/mm/dd
         index = mins/10 :mins = mins//10                
          sertxd (#hours,".",#index,#mins)                 ' 24hr time hh/mm/ss        
         index = secs/10 :secs = secs//10
          sertxd (".",#index,#secs,CR,LF)
   goto main
UK Day Light Savings
Rich (BB code):
#picaxe 18m2
#terminal 38400 'marks
SYMBOL index    = b0  
SYMBOL hours    = b1
SYMBOL mins     = b2
SYMBOL secs     = b3
SYMBOL day      = b7     ' not used
SYMBOL date     = b4
SYMBOL month    = b5
SYMBOL year     = b6
SYMBOL CommonYear  = b8
SYMBOL DayNumber   = W6
SYMBOL DSTstart    = W7
SYMBOL DSTend      = W8  : SYMBOL DayS = W8
SETFREQ M32   

InitialiseTime:
    HI2Csetup I2Cmaster, %11010000, I2Cslow_32, I2Cbyte  ' Set  DS1307 to 100kbps
      HI2Cout $0 , ( $30, $59, $0,day, $25 , $10 , $15)  ' program DS1307 with GMT
Main: pause 8000
    HI2Csetup I2Cmaster, %11010000, I2Cslow_32, I2Cbyte  ' Set  DS1307 to 100kbps
      HI2Cin  $0 , (secs,mins,hours,day,date,month,year) ' Read DS1307
 
        FOR bptr = 1 TO 6
          @bptr = @bptr/$10*250+@bptr                     ' Convert BCD to Decimal
        NEXT

BritishSummerTime:                         ' United Kingdom    
     CommonYear = year //4 +3 /4            ' CommonYear =1           
      DayNumber = month +9 /12
      DayNumber = CommonYear + DayNumber * DayNumber
      DayNumber = month *275 /9 +date -30 -DayNumber
           DayS = year *368 **65046         ' year =1 to 99
       DSTstart = Days +89 //7              
       DSTstart = 91 -CommonYear -DSTstart  ' Last SunDay in March     
         DSTend = Days +303 //7               
         DSTend = 305 -CommonYear -DSTend    ' Last SunDay in October
    IF DayNumber < DSTstart OR DayNumber > DSTend THEN Display
    IF DayNumber = DSTstart AND hours <1 THEN Display ' DST starts at GMT 0100
    IF DayNumber = DSTend   AND hours >0 THEN Display  ' DST ends at GMT 0100
       IF hours =23 THEN : INC date         
         IF date =31 AND month =4 OR month =6 OR month =9 THEN : date =1 INC month: ENDIF
         IF date =32 THEN : date =1 INC month: ENDIF   
       ENDIF : hours =hours +1//24        
      Sertxd ("BST  ") :  GOTO DateTime               ' DST +1   
Display:Sertxd ("GMT  ")                                 ' StandardTime
DateTime:  bptr=35      
           @bptrdec=month//10  :  @bptrdec=month/10
          @bptrdec=date//10  :  @bptr=date/10
      Sertxd (#@bptrinc,#@bptrinc,"/",#@bptrinc,#@bptr,"/20",#year,"  ")          ' Date dd/mm/20yy   
          @bptrdec=secs//10  : @bptrdec=secs/10
           @bptrdec=mins//10  : @bptrdec=mins/10
          @bptrdec=hours//10 : @bptr=hours/10 +$30 : IF @bptr="0" then : @bptr=" "  : endif
      Sertxd (@bptrinc,#@bptrinc,":",#@bptrinc,#@bptrinc,":",#@bptrinc,#@bptr,cr) ' 24hr time hh/mm/ss
   
   goto main
 
Last edited:

newplumber

Senior Member
Thanks Marks for the awesome codes your put on forums and everyone else for that matter
I am building a project and since i am in usa we have day light savings so your code rocks!
I don't understand the daylight code but it works awesome :cool: to much adding and @bptr stuff for me
I also want to mention that i have my clock ds1307 from some ebay seller that sells stuff for free and have been
running it for 2 years and its right on time to the second but thanks again
your friend who's always in last place
ME :)
 

marks

Senior Member
Day Light Savings for Australia and New Zealand
Program the DS1307 with standard time and date and it will Adjust to your DST and also display day.
just change the symbol value AuNzDST at top of program.
Rich (BB code):
#picaxe 18m2
#terminal 38400 'marks
  SYMBOL AuNzDST  =1  ' =1 for AuDST      =0 for NzDST
  SYMBOL index    = b0
  SYMBOL year     = b1
  SYMBOL month    = b2
  SYMBOL date     = b3
  SYMBOL hours    = b4
  SYMBOL mins     = b5
  SYMBOL secs     = b6
  SYMBOL Day        = b7
  SYMBOL CommonYear = b8
  SYMBOL DayNumber  = W6
  SYMBOL DSTend     = W7
  SYMBOL DSTstart   = W8 : SYMBOL DayS = W8

    SETFREQ M32    
InitialiseTime:
    HI2Csetup I2Cmaster, %11010000, I2Cslow_32, I2Cbyte  ' Set  DS1307 to 100kbps
      HI2Cout $0 , ( $30, $59, $01,day, $07 , $10 , $18) ' program DS1307 with StandardTime
Main: pause 8000
    HI2Csetup I2Cmaster, %11010000, I2Cslow_32, I2Cbyte  ' Set  DS1307 to 100kbps
      HI2Cin  $0 , (secs,mins,hours,date,date,month,year)' day not read calculated from DayOfWeek.
        FOR bptr = 1 TO 6
          @bptr = @bptr/$10*250+@bptr                    ' Convert BCD to Decimal
        NEXT
AustralianNewZealandDST:
     CommonYear = year //4 +3 /4           'CommonYear =1
     DayNumberOfTheYear:              
      DayNumber = month +9 /12
      DayNumber = CommonYear + DayNumber * DayNumber
      DayNumber = month *275 /9 +date -30 -DayNumber
     DayOfWeek:
       DayS = year *368 **65046            'year01-99
       Day = days + DayNumber //7          'Sat=0 Sun=1 Mon=2 Tue=3 Wed=4 Thu=5 Fri=6
     DayLightSavings:
      DSTend = Days +96 //7              
      DSTend = 98 -CommonYear -DSTend      'First Sunday in April      
       DSTstart = AuNzDST *7 +Days +272 //7              
        DSTstart = AuNzDST *7 +274 -CommonYear -DSTstart  'First Sunday in October Au /Last Sunday in September Nz
 
IF DayNumber > DSTend AND DayNumber < DSTstart THEN StandardTime
IF DayNumber = DSTstart AND hours <2 THEN StandardTime  'AuNz DST starts at 0200
IF DayNumber = DSTend   AND hours >1 THEN StandardTime    'AuNz DST ends at 0200
       IF hours =23 THEN
         INC date : day=day+1//7
          IF date =32 THEN AdjustDate
          IF date =31 and month =11 or month=9 THEN AdjustDate
          IF date =30 and month =2  THEN AdjustDate
          IF date =29 and month =2  and CommonYear =1 THEN AdjustDate
       ENDIF
     GOTO AdjustHour
        AdjustDate:date =1 : month=month//12+1 :IF month=1 THEN INC year : ENDIF
          AdjustHour:hours =hours +1//24 : Let index =AuNzDST
            IF index =0 THEN sertxd ("Nz"):ENDIF
            IF index =1 THEN sertxd ("Au"):ENDIF
             Sertxd ("DST  ")
StandardTime:
IF day =0 THEN sertxd ("SAT"):ENDIF
IF day =1 THEN sertxd ("SUN"):ENDIF
IF day =2 THEN sertxd ("MON"):ENDIF
IF day =3 THEN sertxd ("TUE"):ENDIF
IF day =4 THEN sertxd ("WED"):ENDIF
IF day =5 THEN sertxd ("THR"):ENDIF
IF day =6 THEN sertxd ("FRI"):ENDIF
   sertxd (" ",#date,"/",#month,"/20",#year,"  ")  ' Date dd/mm/20yy
         index = mins/10 :mins = mins//10                
          sertxd (#hours,".",#index,#mins)              ' 24hr time hh/mm/ss        
         index = secs/10 :secs = secs//10
          sertxd (".",#index,#secs,CR,LF)
       
   goto main
 
Last edited:
Top