Maxim DS1307 PICAXE 28X1 i2c sample code

BCJKiwi

Senior Member
Code:
'########################################
'# For PICAXE-28X1 and MAXIM DS1307 RTC #
'########################################
#picaxe 28x1
SetFreq m8
'########################################
 
Main:
 
'DS1307 CODE
pause 2000
' Set up i2c for device 
 hi2csetup i2cmaster,%11010000,i2cslow_8,i2cbyte   '%11010000 is device i2c bus address
 
' Configure clock control register and set time/date - only required once per power up of Clock
 hi2cout 0, ($00,$09,$13,$06,$26,$10,$07,%00010000)
   'sec & oscillator enable, Min, Hr, DoW, Day, Mo, Yr, Contorl register
   'configure DS1307 with date/time and control register (%00010000)
   'ensure DOW matches Date
   '$xx == xx where xx is the number you want for the date/time
   'e.g. $07 (or $7) is the year 07, $10 is 10 for October
   'highest bit of secs must be 0 to enable oscillator - simplest to set seconds at $00
   'If the set hour is from 1 to 23 then the hours will be in 24Hr mode
   ' as bit6 needs to be set to 1 to enable 12 hr mode ($4C or %01001100 for 12 hr/12 o'clock)
 
DS1307:
pause 1500
' hi2csetup i2cmaster,%11010000,i2cslow_8,i2cbyte
       ' If using multiple devices of different speeds on the bus,
       ' then use this line and remove the [%11010000] in the next line.
 
'Presentation;  Date/Time:- Yr:xx Mo:xx Day:xx DoW:yy hr:xx min;xx sec:xx
'#rem
 hi2cin [%11010000],6,(b0)  ' Year Read from register 6.  
       ' [%11010000] ensures correct i2c is read - reqd if more than one 
       ' i2c device on bus being read in sequence
 
 bcdtoascii b0,b1,b2   ' DS1307 stores data in BCD format
 sertxd ("Date/Time:- Yr:",b1,b2) ' Write output to terminal
 
 hi2cin 5,(b0)    ' Month Read from register 5
 bcdtoascii b0,b1,b2
 sertxd ("  Mo:",b1,b2)
 
 hi2cin 4,(b0)    ' Day Read from register 4
 bcdtoascii b0,b1,b2
 sertxd ("  Day:",b1,b2)
 
 hi2cin 3,(b0)    ' DoW Read from register 3
 bcdtoascii b0,b1,b2
 If     b2="1" then sertxd ("  DoW:","Su") 'convert Ascii 1-7 Day of Week number to Text
 elseif b2="2" then sertxd ("  DoW:","Mo")
 elseif b2="3" then sertxd ("  DoW:","Tu")
 elseif b2="4" then sertxd ("  DoW:","We")
 elseif b2="5" then sertxd ("  DoW:","Th")
 elseif b2="6" then sertxd ("  DoW:","Fr")
 elseif b2="7" then sertxd ("  DoW:","Sa")
 EndIf
 
 hi2cin 2,(b0)    ' Hour Read from register 2
 bcdtoascii b0,b1,b2
 sertxd ("  Hr:",b1,b2)
 
 hi2cin 1,(b0)    ' Minute Read from register 1
 bcdtoascii b0,b1,b2
 sertxd ("  Min:",b1,b2)
 
 hi2cin 0,(b0)    ' Second Read from register 0
 bcdtoascii b0,b1,b2
 sertxd ("  Sec:",b1,b2,cr,lf)
'#endrem
 
'OR ALTERNATIVE Presentation; DoW  day/month/year  hr:min:sec
#rem
 hi2cin [%11010000],3,(b0)  ' DoW Read from register 3
       ' [%11010000] ensures correct i2c is read - reqd if more than one 
       ' i2c device on bus being read in sequence
 bcdtoascii b0,b1,b2   ' DS1307 stores data in BCD format
 If     b2="1" then sertxd ("Sunday    ") 'convert Ascii 1-7 Day of Week number to Text
 elseif b2="2" then sertxd ("Monday    ")
 elseif b2="3" then sertxd ("Tuesday   ")
 elseif b2="4" then sertxd ("Wednesday ")
 elseif b2="5" then sertxd ("Thursday  ")
 elseif b2="6" then sertxd ("Friday    ")
 elseif b2="7" then sertxd ("Saturday  ")
 EndIf
 hi2cin 4,(b0)    ' Day Read from register 4
 bcdtoascii b0,b1,b2
 sertxd (b1,b2,"/")
 hi2cin 5,(b0)    ' Month Read from register 5
 bcdtoascii b0,b1,b2
 sertxd (b1,b2,"/")
 hi2cin 6,(b0)    ' Year Read from register 6.  
 bcdtoascii b0,b1,b2   
 sertxd (b1,b2)    ' Write output to terminal
 hi2cin 2,(b0)    ' Hour Read from register 2
 bcdtoascii b0,b1,b2
 sertxd ("  At ",b1,b2)
 hi2cin 1,(b0)    ' Minute Read from register 1
 bcdtoascii b0,b1,b2
 sertxd (":",b1,b2)
 hi2cin 0,(b0)    ' Second Read from register 0
 bcdtoascii b0,b1,b2
 sertxd (":",b1,b2,cr,lf)
#endrem
 
goto DS1307
 
Last edited:
Top