DS3231 Code examples

marks

Senior Member
24 hour or 12 hour modes.
We can write or read Time or Calenda information from the appropiate register bytes which is in BCD format.
both programs will display in a 12 hour format using the terminal window .

Using 24 hour mode we enter for
hours $00 to $23
mins $00 to $59
secs $00 to $59
Code:
 # no_data
 #terminal 9600
 SETFREQ M8
 
   SYMBOL secs          = b2
   SYMBOL mins          = b3
   SYMBOL hours         = b4
   SYMBOL day           = b5
   SYMBOL date          = b6
   SYMBOL month         = b7
   SYMBOL year          = b8
   SYMBOL PM_AM         = b9
   SYMBOL D0            = b10
   SYMBOL D1            = b11
   SYMBOL D2            = b12
   SYMBOL D3            = b13
   SYMBOL D4            = b14
   SYMBOL D5            = b15
             
Initialize:            
 HI2Csetup I2Cmaster, %11010000, I2Cfast_8, I2Cbyte       ' Set to 400kbps 
'      HI2Cout $0 , ( $14, $59, $11 ,day, $11, $01 , $15) ' Uncomment to Program DS3231  example (11.59.00 AM)
;Progam  Registers  (secs,mins,hours,day,date,month,year) ' Enter in BCD           hours example ( $0 to $23 )

'      HI2Cout $0E, ($0)                                  ' Control   (turn On 1hz $0)  output Pin3  ( Off $4)
Main: 
 PAUSE 1000
  ReadRegisters:     
       HI2Cin  $0 , (secs,mins,hours,day,date,month,year) ' Read from DS3231 
      
   ClockDisplay:
   PM_AM ="P" : IF hours < $12 THEN :PM_AM = "A" :ENDIF       'Indicate P or A 
   IF hours =$20 OR hours =$21 THEN :hours = hours -$6 :ENDIF
   hours = hours //$12 : IF hours =$0 THEN: hours =$12 :ENDIF '24 to 12 hour format
    
   BcdTOASCII hours,D5,D4 : IF D5 = "0" THEN: D5 = " " :ENDIF 'Zero blanking 
   BcdTOASCII mins ,D3,D2
   BcdTOASCII secs ,D1,D0   
     sertxd (CR,LF,D5,D4,".",D3,D2,".",D1,D0," ",PM_AM,"M  ") '(11.59.00 PM )
   
   BcdTOASCII date ,D5,D4 
   BcdTOASCII month,D3,D2
   BcdTOASCII year ,D1,D0   
     sertxd (D5,D4,"/",D3,D2,"/20",D1,D0)                     '( 28/06/2011 )
                
  GOTO Main
Using 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:
 # no_data 
 #terminal 9600
 SETFREQ M8
 
   SYMBOL secs          = b2
   SYMBOL mins          = b3
   SYMBOL hours         = b4	
   SYMBOL day           = b5
   SYMBOL date          = b6
   SYMBOL month         = b7
   SYMBOL year          = b8
   SYMBOL AM_PM         = b9
   SYMBOL D0            = b10
   SYMBOL D1            = b11
   SYMBOL D2            = b12
   SYMBOL D3            = b13
   SYMBOL D4            = b14
   SYMBOL D5            = b15
   		 
Initialize:            
   HI2Csetup I2Cmaster, %11010000, I2Cfast_8, I2Cbyte       ' Set to 100kbps 
'      HI2Cout $0 , ( $14, $59, $51 ,day, $11, $01 , $15) ' Uncomment to Program DS3231  example (11.59.00 AM )
;Progam  Registers  (secs,mins,hours,day,date,month,year) ' Enter in BCD   example hours AM 1-12 ( $41 to $52 )
                                                          '                example hours PM 1-12 ( $61 to $72 )
 
'      HI2Cout $0E, ($0)                                  ' Control   (turn On 1hz $0)  output Pin3  ( Off $4)
Main: 
pause 1000
  ReadRegisters:     
	 HI2Cin $0  , (secs,mins,hours,day,date,month,year) ' Read from DS3231 
  ClockDisplay:
    AM_PM = "A" : IF hours >$60 THEN : AM_PM ="P": ENDIF  ' 12 hour format   PM 
  
   D5 = hours  &$10 /$10 +$30 : IF D5 = "0" THEN : D5 = " " : ENDIF 'Zero blanking    
   D4 = hours //$10
   D3 = mins   /$10
   D2 = mins  //$10
   D1 = secs   /$10
   D0 = secs  //$10
        sertxd (CR,LF,D5,#D4,".",#D3,#D2,".",#D1,#D0," ",AM_PM,"M  ")'(11.59.00 PM )
  
   D5 = date   /$10
   D4 = date  //$10
   D3 = month  /$10
   D2 = month //$10
   D1 = year   /$10
   D0 = year  //$10
        sertxd (#D5,#D4,"/",#D3,#D2,"/20",#D1,#D0)        '(11/01/2015)
  
  GOTO Main
 
Last edited:

marks

Senior Member
Temperature Celsius -40.00C to +85.00C
Code:
        SYMBOL character1    = B2
	SYMBOL character2    = B3  
	SYMBOL character3    = B4  
	SYMBOL character4    = B5  
	SYMBOL character5    = B6  
	SYMBOL character6    = B7
	SYMBOL Temperature   = W10 : SYMBOL TempMsb = B21 : SYMBOL TempLsb = B20
	
Main: HI2Csetup I2Cmaster, %11010000, I2Cslow, I2Cbyte        
	   HI2Cin $11,(TempMsb,TempLsb) ' read temperature DS3231SN  - 40.00 C  to  + 85.00 C 
	 	  
Convert: LET character1 = "+"   ' character1 (Sign) Display +        
         IF TempMsb > 127 THEN : Temperature = - Temperature : LET character1 = "-" : ENDIF ' character1 (Sign) Display -      
         Temperature = Temperature **25600

              
Display: BinTOASCII Temperature,character2,character3,character4,character5,character6
               IF character2 = "0" THEN : character2 = " "            ' zero blanking 
                 IF character3 = "0" THEN : character3 = " " : ENDIF  ' zero blanking 
               ENDIF
             
   SERTXD (CR,LF,character1,character2,character3,character4,".",character5,character6," degrees Celsius",CR,LF) 'display resolution to 0.25

PAUSE 1000 : GOTO main
Temperature Fahrenheit -40.00F to +185.00C
Code:
        SYMBOL character1    = B2
	SYMBOL character2    = B3  
	SYMBOL character3    = B4  
	SYMBOL character4    = B5  
	SYMBOL character5    = B6  
	SYMBOL character6    = B7
	SYMBOL Temperature   = W10 : SYMBOL TempMsb = B21 : SYMBOL TempLsb = B20
	 
 
Main:HI2Csetup I2Cmaster, %11010000, I2Cslow, I2Cbyte        
	   HI2Cin $11,(TempMsb,TempLsb) ' read temperature DS3231SN  - 40.00 F  to  + 185.00 F 	 	 	 
 

Convert: Temperature = Temperature +14080 **46080 -6700                  
            
         LET character1 = " " ' character1 (Sign) Display +
        
         IF TempMsb > 127 THEN : Temperature = - Temperature : LET character1 = "-" : ENDIF ' character1 (Sign) Display -

    
                
Display: BinTOASCII Temperature,character2,character3,character4,character5,character6
               IF character2 = "0" THEN : character2 = " "            ' zero blanking 
                 IF character3 = "0" THEN : character3 = " " : ENDIF  ' zero blanking 
               ENDIF
                        
   SERTXD (CR,LF,character1,character2,character3,character4,".",character5,character6," degrees Fahrenheit",CR,LF) 'display resolution to 0.25C

PAUSE 1000 : GOTO main
 
Last edited:

marks

Senior Member
Its Great to see recent events Curiosity spark interest in Astronomy!
Recently I received a vfd display 20x2 and thinking it would make a good astronomy clock
based on the JulianDate ,Time is always interesting ,I wish I could slow down the ds3231 a bit lol!
Example code we can Simulate giving our 7 digit day and 5 digit decimal (which is close to seconds)
and if you have an RTC connected look in the terminal and hopefully time will move
with better accuracy than the Taurdis hopefully lol.


JulianDate
Code:
	SYMBOL hours         = B11  'b0 to b10 used too
      SYMBOL mins          = B12
	SYMBOL secs          = B13
	SYMBOL day           = B14 
	SYMBOL date          = B15
	SYMBOL month         = B16
	SYMBOL year          = B17
	
	SYMBOL YY            = b18
	SYMBOL MM            = b19
	SYMBOL DD            = b20
	SYMBOL H             = b21
	SYMBOL S             = w11
	SYMBOL JDN           = w12
	SYMBOL JD            = w13
	
'ENTER marks UNIVERSAL TIME  
 YEAR  = $12          '$00 - $99 (2000 - 2099)
 MONTH = $09          '$01 - $12
 DATE  = $28          '$01 - $31
 HOURS = $12          '$00 - $23 
 MINS  = $30          '$00 - $59
 SECS  = $00          '$00 - $59
Program:            
   HI2Csetup I2Cmaster, %11010000, I2Cslow, I2Cbyte       ' set to 100kbps 
       HI2Cout $0,  (secs,mins,hours,day,date,month,year) ' write time to ds3231   
Main: 
     	 HI2Csetup I2Cmaster, %11010000, I2Cslow, I2Cbyte        
	 HI2Cin $0,   (secs,mins,hours,day,date,month,year) ' read time 
	 
UniversalTime: 
   MM  = month / 16 * 250 + month                         'convert BCD to Binary  
   YY  = year  / 16 * 250 + year
   DD  = date  / 16 * 250 + date 
   BcdTOASCII year  ,b7,b6
   BcdTOASCII hours ,b5,b4
   BcdTOASCII mins  ,b3,b2
   BcdTOASCII secs  ,b1,b0
   
   Sertxd (CR,LF," 20",b7,b6," / ",#MM," / ",#DD,"    ",b5,b4,":",b3,b2,":",b1,b0,"  UT",CR,LF)
   
JulianDate:                                        
     H = hours /$12                                       
   JDN = MM + 9/12 + YY * 7/4
   JDN = 367 * YY - JDN
   JDN = MM * 275/9 + DD + JDN + 1513 + H
   
  BinTOASCII JDN,b10,b9,b8,b7,b6 : b10=b10+197     'JulianDayNumber epoch 2451545
   
    S  = hours /16 * 65530 + hours
    S  = -12 *H + S * 3600 
    S  = mins  /16 * 65530 + mins * 60 + S
    S  = secs  /16 * 65530 + secs + S   
    JD = S /864 * 100
     S = S//864 * 25
    JD = S /216 + JD
     S = S//216 * 5/108
     
  BinTOASCII JD,b5,b4,b3,b2,b1  :b4=1//H*5+b4      'JulianDate decimal

  Sertxd (CR,LF,"   JD ",#b10,b9,b8,b7,b6," . ",b4,b3,b2,b1," ",#S,CR,LF)
  
pause 1000 : goto main
 

marks

Senior Member
I've seem to aquired werewolf like features trying to number crunch the Lunar Phase
at least i'll never miss a FullMoon Fieast again lol !
I think the code does quite well out to .3 steps a day hopefully i've
understood the julian date and lunar phase correctly.

Will indicate all 8 phases , the days increment in .1 steps with time
Phases have been rotated example..
FullMooon Rising lol! falls in the middle of a phase (mooons age 14.7 days)
marks the phase FULL from 12.9 to 16.5 days werewolfly.

Nasa http://eclipse.gsfc.nasa.gov/phase/phase2001gmt.html

LunarPhase
Code:
	SYMBOL hours         = B11  'b0 to b10 used too
      SYMBOL mins          = B12
	SYMBOL secs          = B13
	SYMBOL day           = B14 
	SYMBOL date          = B15
	SYMBOL month         = B16
	SYMBOL year          = B17
	
	SYMBOL YY            = b18
	SYMBOL MM            = b19
	SYMBOL DD            = b20
	SYMBOL H             = b21
	SYMBOL S             = w11
	
	SYMBOL JDN           = w12
	SYMBOL JD            = w13
	SYMBOL AGE           = w14
	SYMBOL PHASE         = w15
	
'ENTER marks UNIVERSAL TIME  
 YEAR  = $12          '$00 - $99 (2000 - 2099)
 MONTH = $10          '$01 - $12
 DATE  = $30          '$01 - $31
 HOURS = $03          '$00 - $23 
 MINS  = $19          '$00 - $59
 SECS  = $00          '$00 - $59
 
Program: 'Uncomment initialy and program  UniversalTime then comment out and reprogram picaxe          
   HI2Csetup I2Cmaster, %11010000, I2Cslow, I2Cbyte       ' set to 100kbps 
       HI2Cout $0,  (secs,mins,hours,day,date,month,year) ' write time to ds3231   
Main: 
     	 HI2Csetup I2Cmaster, %11010000, I2Cslow, I2Cbyte        
	 HI2Cin $0,   (secs,mins,hours,day,date,month,year) ' read time 
	 
UniversalTime: 
   MM  = month / 16 * 250 + month                         ' convert BCD to Binary  
   YY  = year  / 16 * 250 + year                          ' byte limit $99
   DD  = date  / 16 * 250 + date 
   BcdTOASCII year  ,b7,b6
   BcdTOASCII hours ,b5,b4
   BcdTOASCII mins  ,b3,b2
   BcdTOASCII secs  ,b1,b0
   
   Sertxd (CR,LF," 20",b7,b6," / ",#MM," / ",#DD,"    ",b5,b4,":",b3,b2,":",b1,b0,"  UT",CR)
   
JulianDate:                ' JD =  2451545.0    2012/JAN/01 at 12:00:00                                  
     H = hours /$12                                       
   JDN = MM + 9/12 + YY * 7/4
   JDN = 367 * YY - JDN
   JDN = MM * 275/9 + DD + JDN + 1513 + H
     
  BinTOASCII JDN,b10,b9,b8,b7,b6 : b10=b10+197      ' JulianDayNumber  
   
    S  = hours /16 * 65530 + hours
    S  = -12 *H + S * 3600 
    S  = mins  /16 * 65530 + mins * 60 + S
    S  = secs  /16 * 65530 + secs + S 
    JD = S /864 * 100
     S = S//864 * 25
    JD = S /216 + JD
     S = S//216 * 5/108
     
  BinTOASCII JD,b5,b4,b3,b2,b1  :b4=1//H*5+b4       ' JulianDate decimal

  Sertxd (CR,LF," JD ",#b10,b9,b8,b7,b6," . ",b4,b3,b2,b1," ",#S,CR)
  
LunarPhase:  ' Moons age 25101/850 = 29.530588235 days from JD 2451550.1
 b4=b4-48*10
 age = JDN-1550//2894*10**22193//100*443/15+b4+b3-48  
 IF age > 2953 THEN : age = age - 2953 : ENDIF
 phase = age +184 /369
 IF age > 2770 THEN :phase=0 : ENDIF
 
BinTOASCII age,b9,b9,b8,b7,b6 :IF b9="0" THEN : b9=" " : ENDIF
Sertxd (CR,LF," MOONS AGE ",b9,b8,".",b7," days ",CR)

ON phase GOTO phase0,phase1,phase2,phase3,phase4,phase5,phase6,phase7

phase0:Sertxd (" NEW            ") : goto delay1
phase1:Sertxd (" Waxing Crescent") : goto delay1
phase2:Sertxd (" First Quarter  ") : goto delay1
phase3:Sertxd (" Waxing Gibbous ") : goto delay1
phase4:Sertxd (" FULL"           ) : goto delay1
phase5:Sertxd (" Waning Gibbous ") : goto delay1
phase6:Sertxd (" Last Quarter   ") : goto delay1
phase7:Sertxd (" Waning Crescent") : goto delay1

Delay1: pause 1000 : goto main
 
Last edited:

marks

Senior Member
Some Old Calendar's use to show a number for each Day of the Year you dont see many around anymore.

(Day 317 of 2013)
Rich (BB code):
      SYMBOL date          = B5 
      SYMBOL month         = B6
      SYMBOL year          = B7
      SYMBOL CommonYear    = b10
      SYMBOL DayNumber     = W6
      
Initialize:           
   HI2Csetup I2Cmaster, %11010000, I2Cslow, I2Cbyte       ' Set to 100kbps
       HI2Cout $0 , ( $00, $59, $23 , 0 , $13, $11 , $13) ' Uncomment to Program         example (11.59.00 PM)
;Progam  Registers  (secs,mins,hours,day,date,month,year) ' Enter in BCD           hours example ( $0 to $23 )
     
Main:     
     HI2Csetup I2Cmaster, %11010000, I2Cslow, I2Cbyte       
      HI2Cin $4,   (date,month,year) ' read date DS3231 or DS1307
     
      FOR bptr = 5 TO 7  
        @bptr = @bptr/16*250+@bptr   ' convert BCD to Binary 
     NEXT
    
    DayNumberOfTheYear:                
     CommonYear = year //4 +3 /4     'CommonYear =1 so leapYear =0
      DayNumber = month +9 /12  
      DayNumber = CommonYear + DayNumber * DayNumber
      DayNumber = month *275 /9 +date -30 -DayNumber
     
       Pause 900    
     Sertxd ("  Day ",#DayNumber," of 20",#year,CR,LF)     
      GOTO Main
edit change
Code:
 too [code="rich"]
 
Last edited:

marks

Senior Member
Count Down To A Special Event.

First we need to work out our SpecialEvent DayNumber on a LeapYear here's an example program to simulate
Code:
     SYMBOL date          = B5  
     SYMBOL month         = B6
     SYMBOL year          = B7
     SYMBOL CommonYear    = b10
     SYMBOL DayNumber     = W6 

 Main: ' Enter date and month to calculate DayNumber for a LeapYear	
     date  = 13
     month = 11
     
    DayNumberOfTheYear:                 
     CommonYear = year //4 +3 /4     'CommonYear =1 so leapYear =0 
      DayNumber = month +9 /12   
      DayNumber = CommonYear + DayNumber * DayNumber
      DayNumber = month *275 /9 +date -30 -DayNumber
      
       Pause 900     
     Sertxd ("  Day ",#DayNumber,CR,LF)      
      GOTO Main
We can then use our SpecialEvent Daynumber in the following program
for example
SpecialEvent =360 'ChristmasDay
SpecialEvent =1 'NewYearsDay
Code:
   SYMBOL secs          = B2
   SYMBOL mins          = B3
   SYMBOL hours         = B4	
   SYMBOL date          = B5	
   SYMBOL month         = B6	 
   SYMBOL year          = B7
   SYMBOL hour12        = b8
   SYMBOL PM_AM         = b9 
   SYMBOL CommonYear    = b10 
   SYMBOL DayNumber     = W6 
   SYMBOL SpecialEvent  = W7 : SYMBOL DaysUntilEvent  = W7
	
Initialize:            
   HI2Csetup I2Cmaster, %11010000, I2Cslow, I2Cbyte       ' Set to 100kbps 
       HI2Cout $0 , ( $00, $59, $23 , 0 , $31, $12 , $13) ' Uncomment to Program         example (11.59.00 PM)
;Progam  Registers  (secs,mins,hours,day,date,month,year) ' Enter in BCD           hours example ( $0 to $23 )

Main: SpecialEvent= 1  ' Change to required DayNumber     ' example ChristmasDay  SpecialEvent= 360     

  ReadRegistersConvertToBinary:     
	 HI2Cin $0,   (secs,mins,hours,date,date,month,year)' Read from DS3231 or DS1307 skip day
	 
	   FOR bptr = 2 TO 7   
          @bptr = @bptr/16*250+@bptr                      ' Convert BCD to Binary  
         NEXT
         
  ClockDisplay:  
    hour12 =hours //12 : IF hour12 = 0 THEN : hour12 = 12 : ENDIF ' 24 to 12 hour format
     PM_AM =hours  /12 *15 +65                             ' Indicate  "P"=80  or "A"=65
     
    IF hour12 <10 THEN  sertxd (" ") :ENDIF : sertxd (#hour12,".") 
    IF  mins  <10 THEN  sertxd ("0") :ENDIF : sertxd (#  mins,".")
    IF  secs  <10 THEN  sertxd ("0") :ENDIF : sertxd (#  secs," ",PM_AM,"M  ")
    IF  date  <10 THEN  sertxd (" ") :ENDIF : sertxd (#  date,"/")                             
    IF month  <10 THEN  sertxd (" ") :ENDIF : sertxd (# month,"/20")
    IF  year  <10 THEN  sertxd ("0") :ENDIF : sertxd (#  year,CR,LF,CR,LF)' (11.59.00 PM  12/11/2013) 
   
    DayNumberOfYear:                 
     CommonYear = year //4 +3 /4                           ' CommonYear=1 so leapYear=0 
      DayNumber = month +9 /12  
      DayNumber = CommonYear + DayNumber * DayNumber
      DayNumber = month *275 /9 +date -30 -DayNumber
 
    DaysUntilSpecialEvent: 
     IF SpecialEvent > 28 THEN : SpecialEvent=SpecialEvent-CommonYear :ENDIF
 
     DaysUntilEvent = SpecialEvent -DayNumber
    
      IF DaysUntilEvent > 65169 THEN                       ' Remaining Days after Event
          CommonYear = year +1 //4 +3 /4                   ' Check NextYear is a CommonYear
          DaysUntilEvent = 366 +DaysUntilEvent -CommonYear ' Days until NextYear Event
      ENDIF
    
    CountdownDisplay:
      DaysUntilEvent =DaysUntilEvent -1 
      IF DaysUntilEvent = 65535 THEN message
       hours = 23 -hours
        mins = 59 -mins
        secs = 59 -secs
       pause 900
     sertxd (#DaysUntilEvent," Days   ",#hours," Hours   ",#Mins," Mins   ",#secs," Secs",CR,LF,CR,LF)
                                                                  ' (0 Days  0 Hours  0 Mins  59 Secs)                                                       
     GOTO Main
     
      Message:
     sertxd ("Happy New Year !!!  ") ' Change as required 
     
     GOTO Main
 

marks

Senior Member
New Oled 1602 using axe132 and DS3231 board for PI

Wow its been quiet here lately lol. Got myself an OLED1602 for Chrissy!!
and it's a bit different from the Oled 2004 I once had.
- first surprise were the pinouts were a bit different
(and now when using it with an axe132 board the programming socket is hidden)
- seems to use a boostbuck regulator so from 3v to 5v brightness remains the same
will draw about 18ma with 4 to 4.8v a sweetspot so this is really on par with a backlit display.
this is also 1/3 of the power that an Oled 2004 uses
-like every one says they look better pixels on the 1602 are slighty larger than the 2004 oled too
all these things make the Oled 1602 a preffered buy!!

The other hardware I couldnt resist one of those RTC DS3231 boards for PI less than $5 on ebay.
made cheaply but even come with a 3v battery there seem to be two versions some are fitted with
5600 resistors and others with 4700 resistors.

Just need too make a little extensionboard and daughterboard for the axe132.
-The extension board will make it easier to modify for different projects.
Best to first solder the pin headers to the axe132 before your add any other components
inserted slightly higher once all pins are soldered the plastic strip can be removed from the
pins easily lifted with a screwy(ie we left a gap underneath when we soldered lol)
then solderin the extension board this way the pins will be long enough to use with headerleads too.
-The daughterboard was going to be full length but snapped in the vise but was still long enough
so I used it.Like many RTC boards they expect to be plugged into a working I2C bus so two
3300 ohm terminating resistors are used as a supply as low as 3v upto 5v can then be used.
-The only addition to the axe132 board a 47uf tant used where the pot usually sits.
 

Attachments

marks

Senior Member
I guess we need some code to try with our new bling hardware.
Sorry but for this we're going to have to use the new PICAXE Edition 6.0.7.5
as we need this to be able collect the date and time to program the DS3231.
-We are still using the axe132 as standard 8bitmode we are just going to
share the Hi2C lines.
Instead of displaying seconds we are using an hour mins flashing seperator.
code snippets started of so people could check there hardware with some working code,
was just going to show 12hour mode working with an oled and later add 1button for programing
but thought this would be interesting best to program just after the new minute.
Code:
#picaxe 18m2 '  AXE132 8Bit marks
'           DB7  = B.7
'           DB6  = B.6
'           DB5  = B.5
'     SCL   DB4  = B.4
'           DB3  = B.3
'           DB2  = B.2
'     SDA   DB1  = B.1
'           DB0  = B.0
      SYMBOL Rx  = C.5
      SYMBOL  E  = C.6
      SYMBOL RS  = C.7       
      
 SYMBOL senddata = pinsb
 SYMBOL index    = b0       : SYMBOL halfsecond  =b0
 SYMBOL dot      = b1       : SYMBOL CRC         =b1
 SYMBOL hours    = b2
 SYMBOL mins     = b3
 SYMBOL secs     = b4  
 SYMBOL day      = b5       ' not used 
 SYMBOL date     = b6  
 SYMBOL month    = b7   
 SYMBOL year     = b8
 SYMBOL AM_PM    = b9
 SYMBOL D0       = b10       
 SYMBOL D1       = b11
 SYMBOL D2       = b12 
 SYMBOL D3       = b13
 SYMBOL D4       = b14
 SYMBOL D5       = b15      : SYMBOL Sign       = b15
 SYMBOL Temperature   = W10 : SYMBOL TempMsb    = B21 : SYMBOL TempLsb = B20 
 
      SETFREQ M32
 	dirsB = %11111111
 	dirsC = %11001111
	
 ;;;;   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
 month = @bptrinc*$10+@bptrinc-$30 :inc bptr
 date  = @bptrinc*$10+@bptrinc-$30 :inc bptr
 hours = @bptrinc*$10+@bptrinc-$30 :inc bptr          ' $0 to $23
 mins  = @bptrinc*$10+@bptrinc-$30 :inc bptr
 secs  = @bptrinc*$10+@bptrinc-$30 +$16               ' add 16 seconds (how long it takes for PC to program)         
     
InitialiseOLED: 	
	FOR  index = 0 to 6 
          LOOKUP index, ($38,$38,$38,$0C,$01,$02,$06),senddata : PULSOUT E,1 ' Initialise LCD/OLED
          '(8bit/2line/5x8)*3(Display On)(Clear Display)(Return Home)(Entry Mode Set)
 	NEXT index : PAUSE 10 

InitialiseTime:  
   hours = hours /$10 *250 +hours                            ' Convert ($00 to $23) BCD to Decimal
    
   AM_PM = hours /12 *$20 +$40                               ' Convert to 12hour 
   hours = hours //12 : IF hours =0 THEN : hours =12 : ENDIF ' Convert to 12hour
   hours = hours /10 *6 +hours +AM_PM                        ' hoursAM 1-12 ($41 to $52) hoursPM 1-12 ($61 to $72)
    
    READ  255,CRC : IF CRC <> 0 THEN Main                    ' Skip if time already programmed 
     HI2Csetup I2Cmaster, %11010000, I2Cfast_32, I2Cbyte     ' Set  DS3231 to 400kbps 
      HI2Cout $0 , (secs,mins,hours,day,date,month,year)     ' Write to DS3231 
    WRITE 255,53                                             ' we can make use of an existing eeprom entry if any


  MAIN:
      HI2Csetup I2Cmaster, %11010000, I2Cfast_32, I2Cbyte     ' Set  DS3231 to 400kbps   
       HI2Cin  $0 , (secs,mins,hours,day,date,month,year)     ' Read DS3231
        HI2CIN $11, (TempMsb,TempLsb)                         ' Read DS3231 Temperature -40 C to +85 C
      HI2Csetup OFF : dirsB = %11111111                       ' Restore Outputs after using HI2Cin
      
  ClockDisplay:
        AM_PM = "A" : IF hours > $60 THEN : AM_PM ="P": ENDIF '  Indicate AM or PM 
  
   D5 = hours  &$10 /$10 +$30 : IF D5 = "0" THEN : D5 = " " : ENDIF 'Zero blanking    
   D4 = hours //$10 +$30
   D3 = mins   /$10 +$30
   D2 = mins  //$10 +$30
  
   halfsecond = time //2 
          dot ="." : IF halfsecond =0 THEN : dot =" " : ENDIF ' Dot idicating second seperator
  
    LOW  RS : senddata = 128 : PULSOUT E,1              	  ' CommandMode (128-147)  Line 1 Cursor Position  
    HIGH RS                                                   ' CharacterMode
      FOR  index = 0 TO 7                                     ' sending characters to LCD line one 
 	  LOOKUP index,(D5,D4,dot,D3,D2," ",AM_PM,"M"),senddata : PULSOUT E,1            
 	NEXT index	
 	
  TemperatureDisplay:	
     Sign = " " : IF TempMsb >127 THEN : Temperature = -Temperature : Sign = "-" : ENDIF 
           
    D4 = TempMsb  /10 +48 : IF D4 = "0" THEN : D4 = " " : ENDIF 'Zero blanking    
    D3 = TempMsb //10 +48
 
    LOW  RS : senddata = 203 : PULSOUT E,1              	  ' CommandMode (192-211)  Line 2 Cursor Position     
    HIGH RS                                                   ' CharacterMode
      FOR  index = 0 TO 4                                     ' sending characters to LCD line one 
 	  LOOKUP index,(Sign,D4,D3,223,"C"),senddata : PULSOUT E,1            
 	NEXT index
	 
GOTO Main
 
Hi,

Iam a very new member with picaxe systems.
How i can use this code with AXE133Y?

I try many ways with serout but nothing at the moment!

Any help?

Many Thanks
Manuel
 

marks

Senior Member
Hi Manuel Pinto,
In the example posts #7 #8 we are using the ax133y as a standalone project
replacing the original driver firmware.
(there is no need for a second picaxe using SEROUT.)

To once again use the axe133y as original you would need to replace the firmware
A Copy can be found on your picaxe editor
Click on OPEN and under Picaxe editor
Click on SAMPLES / BASIC / AXE133-Serial OLED Display / 18M2
using the latest Picaxe Editor is recommended 6.0.8.1
http://www.picaxe.com/Software/PICAXE/PICAXE-Editor-6/
 
Picaxe 40x2 with ds1307 problem

Hi again.

Sorry, but i try use this code with my 40x2, but not work!
In attach have the schemaforum.jpg of my small project.

Any help?

Thanks

Manuel
Code:
 #PICAXE 40X2 
 SYMBOL secs          = B2
   SYMBOL mins          = B3
   SYMBOL hours         = B4  
   SYMBOL date          = B5  
   SYMBOL month         = B6   
   SYMBOL year          = B7
   SYMBOL hour12        = b8
   SYMBOL PM_AM         = b9 
   SYMBOL CommonYear    = b10 
   SYMBOL DayNumber     = W6 
   SYMBOL SpecialEvent  = W7 : SYMBOL DaysUntilEvent  = W7
	
Initialize:            
   HI2Csetup I2Cmaster, %11010000, I2Cslow, I2Cbyte       ' Set to 100kbps 
       HI2Cout $0 , ( $00, $59, $23 , 0 , $31, $12 , $13) ' Uncomment to Program         example (11.59.00 PM)
;Progam  Registers  (secs,mins,hours,day,date,month,year) ' Enter in BCD           hours example ( $0 to $23 )

Main: SpecialEvent= 1  ' Change to required DayNumber     ' example ChristmasDay  SpecialEvent= 360     

  ReadRegistersConvertToBinary:     
	 HI2Cin $0,   (secs,mins,hours,date,date,month,year)' Read from DS3231 or DS1307 skip day
	 
	   FOR bptr = 2 TO 7   
          @bptr = @bptr/16*250+@bptr                      ' Convert BCD to Binary  
         NEXT
         
  ClockDisplay:  
    hour12 =hours //12 : IF hour12 = 0 THEN : hour12 = 12 : ENDIF ' 24 to 12 hour format
     PM_AM =hours  /12 *15 +65                             ' Indicate  "P"=80  or "A"=65
     
    IF hour12 <10 THEN  sertxd (" ") :ENDIF : sertxd (#hour12,".") 
    IF  mins  <10 THEN  sertxd ("0") :ENDIF : sertxd (#  mins,".")
    IF  secs  <10 THEN  sertxd ("0") :ENDIF : sertxd (#  secs," ",PM_AM,"M  ")
    IF  date  <10 THEN  sertxd (" ") :ENDIF : sertxd (#  date,"/")                             
    IF month  <10 THEN  sertxd (" ") :ENDIF : sertxd (# month,"/20")
    IF  year  <10 THEN  sertxd ("0") :ENDIF : sertxd (#  year,CR,LF,CR,LF)' (11.59.00 PM  12/11/2013) 
   
    DayNumberOfYear:                 
     CommonYear = year //4 +3 /4                           ' CommonYear=1 so leapYear=0 
      DayNumber = month +9 /12  
      DayNumber = CommonYear + DayNumber * DayNumber
      DayNumber = month *275 /9 +date -30 -DayNumber
 
    DaysUntilSpecialEvent: 
     IF SpecialEvent > 28 THEN : SpecialEvent=SpecialEvent-CommonYear :ENDIF
 
     DaysUntilEvent = SpecialEvent -DayNumber
    
      IF DaysUntilEvent > 65169 THEN                       ' Remaining Days after Event
          CommonYear = year +1 //4 +3 /4                   ' Check NextYear is a CommonYear
          DaysUntilEvent = 366 +DaysUntilEvent -CommonYear ' Days until NextYear Event
      ENDIF
    
    CountdownDisplay:
      DaysUntilEvent =DaysUntilEvent -1 
      IF DaysUntilEvent = 65535 THEN message
       hours = 23 -hours
        mins = 59 -mins
        secs = 59 -secs
       pause 900
     sertxd (#DaysUntilEvent," Days   ",#hours," Hours   ",#Mins," Mins   ",#secs," Secs",CR,LF,CR,LF)
                                                                  ' (0 Days  0 Hours  0 Mins  59 Secs)                                                       
     GOTO Main
     
      Message:
     sertxd ("Happy New Year !!!  ") ' Change as required 

serout c.0, n2400, (254,1)
	pause 30
	serout c.0,N2400,(254,128,HOURS,":",MINS,":",SECS)
      ;serout c.0,N2400,(254,128,date,"/",month,"/",year)

     
     GOTO Main
 

marks

Senior Member
Hi Manuel Pinto,
Its good you have gone to the trouble of providing both code and circuit.
Hardware I2C uses specific pins on picaxe chips perhaps check that these are correct.
(on the 40x2 SCL C.3 pin18, SDA C.4 pin23)
I usually start with some simple code to prove hardware is working,
the following code should display the time/date using the Serial Terminal
and display the countdown using the Oled/LCD.
Code:
#PICAXE 40X2 
#TERMINAL 9600
'  SerIn   = RXD    (pin 6 )
'  SerOut  = A.4    (pin 7 )
'  SerOled = C.0    (pin 15) 'TXserial to standard AXE133Y  
'     SCL  = C.3    (pin 18) 
'     SDA  = C.4    (pin 23)

   SYMBOL secs          = B2
   SYMBOL mins          = B3
   SYMBOL hours         = B4  
   SYMBOL date          = B5  
   SYMBOL month         = B6   
   SYMBOL year          = B7
   SYMBOL hour12        = b8
   SYMBOL PM_AM         = b9 
   SYMBOL CommonYear    = b10 
   SYMBOL DayNumber     = W6 
   SYMBOL SpecialEvent  = W7 : SYMBOL DaysUntilEvent  = W7
	
Initialize:            
   HI2Csetup I2Cmaster, %11010000, I2Cslow, I2Cbyte       ' Set to 100kbps 
       HI2Cout $0 , ( $00, $59, $23 , 0 , $31, $12 , $13) ' Uncomment to Program         example (11.59.00 PM)
;Progam  Registers  (secs,mins,hours,day,date,month,year) ' Enter in BCD           hours example ( $0 to $23 )
serout c.0, n2400, (254,1) ' clear OLED screen
Main: SpecialEvent= 1  ' Change to required DayNumber     ' example ChristmasDay  SpecialEvent= 360     

  ReadRegistersConvertToBinary:     
	 HI2Cin $0,   (secs,mins,hours,date,date,month,year)' Read from DS3231 or DS1307 skip day
	 
	   FOR bptr = 2 TO 7   
          @bptr = @bptr/16*250+@bptr                      ' Convert BCD to Binary  
         NEXT
         
  ClockDisplay:  
    hour12 =hours //12 : IF hour12 = 0 THEN : hour12 = 12 : ENDIF ' 24 to 12 hour format
     PM_AM =hours  /12 *15 +65                             ' Indicate  "P"=80  or "A"=65
     
    IF hour12 <10 THEN  sertxd (" ") :ENDIF : sertxd (#hour12,".") 
    IF  mins  <10 THEN  sertxd ("0") :ENDIF : sertxd (#  mins,".")
    IF  secs  <10 THEN  sertxd ("0") :ENDIF : sertxd (#  secs," ",PM_AM,"M  ")
    IF  date  <10 THEN  sertxd (" ") :ENDIF : sertxd (#  date,"/")                             
    IF month  <10 THEN  sertxd (" ") :ENDIF : sertxd (# month,"/20")
    IF  year  <10 THEN  sertxd ("0") :ENDIF : sertxd (#  year,CR,LF,CR,LF)' (11.59.00 PM  12/11/2013) 
   
    DayNumberOfYear:                 
     CommonYear = year //4 +3 /4                           ' CommonYear=1 so leapYear=0 
      DayNumber = month +9 /12  
      DayNumber = CommonYear + DayNumber * DayNumber
      DayNumber = month *275 /9 +date -30 -DayNumber
 
    DaysUntilSpecialEvent: 
     IF SpecialEvent > 28 THEN : SpecialEvent=SpecialEvent-CommonYear :ENDIF
 
     DaysUntilEvent = SpecialEvent -DayNumber
    
      IF DaysUntilEvent > 65169 THEN                       ' Remaining Days after Event
          CommonYear = year +1 //4 +3 /4                   ' Check NextYear is a CommonYear
          DaysUntilEvent = 366 +DaysUntilEvent -CommonYear ' Days until NextYear Event
      ENDIF
    
    CountdownDisplay:
      DaysUntilEvent =DaysUntilEvent -1 
      IF DaysUntilEvent = 65535 THEN message
       hours = 23 -hours
        mins = 59 -mins
        secs = 59 -secs
    
serout c.0,N2400,(254,130,#hours," : ",#mins," : ",#secs,"   ")
     GOTO Main
      Message:
     
serout c.0,N2400,(254,128,"Happy New Year !")
     GOTO Main
 

Attachments

Last edited:
Hi Marks

Thanks for your help.
I try your code and the pinning is correct, i see the information on serial terminal but nothing on OLED LCD.
If i test the LCD direct with (hello world), he works perfect.

I ready change the DS1307 for a new one, and nothing.
It is a first time i work with I2c, and this want put me crazy!

Any ideia?

Many thanks for your time.
 

DAC90

New Member
Greetings Marks,
I have been attempting to adapt your code to a picaxe 20M2, outputting to a low end ( budget serial ) LCD via a DS1307 RTC. I am outputting data through pin B.6 in this case. after some alterations I am getting a good readout when running the simulation. However, when loading into the picaxe the display reads the following 114 : 150 : 18. I am wondering if there is something in the code which is causing this unusual output or whether perhaps the DS1307 is not operating properly? Basically I am trying to make a clock which eventually I want to build into a nixie clock once I have tested it with an LCD. I should also say that I am fairly new at this so my apologies for a high degree of ignorance at the moment. Any ideas to resolve my current problem would be much appreciated.

Much thanks indeed!!
 

marks

Senior Member
Hi DAC90,
welcome to the forum.
If the seconds are changing the RTC should be ok. If not try lowering the pullup values 2.2k is ideal !

I've tried to show a few different examples perhaps this has added some confusion.
The easiest method is using BCDtoASCII and checking the results with the Terminal.
It seems there may be many trying to output to an AXE133 and LCD/Oled so I've added the extra code
hopefully you'll have some success!
Code:
#picaxe 20m2
#terminal 4800 
      SYMBOL index         = B1    
      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)
       serout B.6, N2400, (254,1)                        ' Clear LCD/OLED   
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
   
   IF secs <> index THEN 
 sertxd (CR, LF,character1,character2,".",character3,character4,".",character5,character6," ",PM_AM,"M", CR, LF)'to Terminal
    ENDIF : index = secs
                   
 serout B.6,N2400,(254,130,character1,character2,":",character3,character4,":",character5,character6," ",PM_AM,"M")'to LCD/OLED

 goto main
 

marks

Senior Member
Using an RTC for simple Alarms and control.

Rich (BB code):
#picaxe 18m2
#terminal 19200
SETFREQ M16
dirsB = %11111111
dirsC = %11001111
SYMBOL ATime         = W5  : SYMBOL hours         = b11 : SYMBOL mins         = b10
SYMBOL secs          = B12 
SYMBOL CRC           = B13

Atime = $0659     'Set Initial Time of DS3231 or DS1307 enter BCD $0000 to $2359 hrs

SetTimeOnce:
READ  255,CRC : IF CRC <> 0 THEN Main                        ' Skip if time already programmed
     HI2Csetup I2Cmaster, %11010000, I2Cslow_16, I2Cbyte     ' Set  DS3231 to 100kbps
      HI2Cout $0 , ( $00,mins,hours,$1  )                    ' Write to DS3231
                 ' (secs,mins,hours,date)
    WRITE 255,53                                             ' we can make use of an existing eeprom entry if any

Main:
      HI2CSETUP I2CMASTER, %11010000, I2Cslow_16, I2CBYTE    ' set to 100kbps default /change to I2Cfast_16 for 400kbp
      HI2CIN 0,(secs,mins,hours)                             ' read time ds3231
           
  Alarms:          
      IF Atime = $0700 THEN                                              'at 07:00 am
            IF secs < $2  THEN : HIGH B.2 :  ELSE LOW B.2 : ENDIF        'open doors 
                 IF secs < $20 THEN : HIGH B.7 : ELSE LOW B.7 : ENDIF    'alarm for 20secs       
      ENDIF
         
  DisplayStatus:
    BcdTOASCII hours ,b5,b4
    BcdTOASCII mins  ,b3,b2
    BcdTOASCII secs  ,b1,b0
    IF secs <> CRC THEN                                       ' wait for change of secs
     Sertxd (CR,LF,b5,b4,":",b3,b2,":",b1,b0)                 ' (23.59.10) 24hr
      IF outpinb.7 = 1 THEN : SERTXD("    ALARM") : ENDIF
       IF outpinb.2 = 1 THEN : SERTXD("    Door Opening") :ENDIF
    ENDIF : CRC = secs
GOTO Main
edit change
Code:
 too [code='rich"]
 
Last edited:

marks

Senior Member
DST DayLight Savings Time

We still don't have it here in WA I guess we're still behind the times LOL!

This is for those of you using the UK time zone.
Just program your RTC with the Standard Time (GMT)
and during daylight savings (BST) it will adjust the hours, date and month for you.

Code:
#picaxe 18m2
#terminal 38400 'marks
SYMBOL index    = b0   
SYMBOL secs     = b1
SYMBOL mins     = b2
SYMBOL hours    = b3
SYMBOL date     = b4 
SYMBOL month    = b5  
SYMBOL year     = b6
SYMBOL day      = b7     ' Sun=1 Mon=2 Tue=3 Wed=4 Thr=5 Fr1=6 Sat=7
SYMBOL DST      = b8
SYMBOL D0            = b10
SYMBOL D1            = b11
SYMBOL D2            = b12
SYMBOL D3            = b13
SYMBOL D4            = b14
SYMBOL D5            = b15

SETFREQ M32     

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

BritishSummerTime:                                      ' United Kingdom 
      IF month < 3 or month > 10 THEN GMT  
      DST = 31 -date +day
    IF day =1 and hours <1 THEN : DST =8 : ENDIF       ' DST starts and ends at GMT 0100
    IF month =3 and DST >7  THEN GMT                   ' Last SunDay in March
      IF month =10 and DST <8 THEN GMT                   ' Last SunDay in October
     
       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 (CR,LF,"BST  ") :  GOTO DisplayTimeDate  ' (UTC+1) DST      
     GMT:Sertxd (CR,LF,"GMT  ")                          ' ( UTC ) StandardTime
    
DisplayTimeDate:               
   D5 = hours  /10 
   D4 = hours //10
   D3 = mins   /10
   D2 = mins  //10
   D1 = secs   /10
   D0 = secs  //10
        sertxd (#D5,#D4,".",#D3,#D2,".",#D1,#D0)         '( 00.59.30 ) 24hr time hh/mm/ss
   D5 = date   /10
   D4 = date  //10
   D3 = month  /10
   D2 = month //10
   D1 = year   /10
   D0 = year  //10
        sertxd ("  ",#D5,#D4,"/",#D3,#D2,"/20",#D1,#D0)  '( 11/01/2015 ) Date dd/mm/20yy

  Goto Main
 
Last edited:

westaust55

Moderator
We still don't have it here in WA I guess we're still behind the times LOL!
A little off topic to actual RTC programming . . . .

But does Western Australia really need Daylight Saving (DLS)?

In say NSW (east coast) I believe the real time line is somewhere a couple of 100 kilometres west of Sydney. So Sydney and east coast is something like 20 mins behind and with the Blue Mountains helping to block the evening light the DLS makes sense.
I worked south of Sydney for a time and know it gets dark earlier in the day.

In WA, DLS has been trialed. For WA the time line is around 300 km east of Perth so in Perth we are in effect already 30 mins ahead.
People getting home in the heat of the day meant more home air conditioning on earlier and thus more load on the electricity grid.
I found it was hot to do anything at home in Summer outside in the heat of summer on a 40+ degC day (Different story when I was paid to work outside in temps up to 50+ degC).

Back on to RTC codes,

Great that you have made your code available for others where DLS is practiced.
 

marks

Senior Member
Sunrise and Sunset

The attached .pdf file is actually an .xls rename and hopefully you should be able to open an excel spreadsheet
and create your own data for your specific location below is an example picaxe code for Perth when simulated should return
sunrise and sunset times for your given day.

Code:
SYMBOL month         = B6
      SYMBOL year          = B7
      SYMBOL CommonYear    = b8
	SYMBOL DayLight      = b9
	SYMBOL D0            = b10
      SYMBOL D1            = b11
      SYMBOL D2            = b12
      SYMBOL D3            = b13
      SYMBOL D4            = b14
      SYMBOL D5            = b15
     
      SYMBOL DayNumber     = W10 
	SYMBOL SunRise       = W11
      SYMBOL SunSet        = W12
	
	Table   0,( 74, 78, 83, 89, 94,100,106,111,116,121) ' Perth Sunrise data +240mins   
	Table  10,(126,131,135,139,143,147,151,155,159,164) ' Latitude  -31.96 (S)
	Table  20,(168,172,176,180,184,188,191,193,196,197) ' Longitude 115.86 (E)
	Table  30,(198,197,196,194,191,188,183,178,172,165) ' TimeZone   +8  (GMT)
	Table  40,(158,151,143,135,127,119,112,104, 97, 90) ' data plot interval every sixth day of year
	Table  50,( 84, 78, 73, 69, 66, 64, 63, 63, 65, 67) 
	Table  60,( 70, 74)                                      
	Table 100,(176,177,176,175,173,170,166,161,155,149) ' SunSet data +990mins
	Table 110,(142,135,127,120,112,104, 97, 89, 82, 76)
	Table 120,( 70, 65, 60, 56, 53, 51, 49, 49, 49, 50)
	Table 130,( 52, 55, 58, 61, 65, 68, 72, 76, 80, 84)
	Table 140,( 87, 91, 95, 98,102,106,110,114,118,122)
	Table 150,(127,132,137,143,148,153,159,163,168,171)
	Table 160,(174,176)
	
Initialize:            
   HI2Csetup I2Cmaster, %11010000, I2Cslow, I2Cbyte       ' Set to 100kbps 
       HI2Cout $0 , ( $00, $59, $23 , 0 , $30, $08 , $17) ' Uncomment to Program         example (11.59.00 PM)
;Progam  Registers  (secs,mins,hours,day,date,month,year) ' Enter in BCD           hours example ( $0 to $23 )
      
 Main:      
     HI2Csetup I2Cmaster, %11010000, I2Cslow, I2Cbyte        
      HI2Cin $4,   (date,month,year) ' read date DS3231 or DS1307
      
      FOR bptr = 5 TO 7   
        @bptr = @bptr/16*250+@bptr   ' convert BCD to Binary  
     NEXT
     
    DayNumberOfTheYear:                 
     CommonYear = year //4 +3 /4     'CommonYear =1 so leapYear =0 
      DayNumber = month +9 /12   
      DayNumber = CommonYear + DayNumber * DayNumber
      DayNumber = month *275 /9 +date -30 -DayNumber
     
     SunriseToSunset:
     Daylight = Daynumber /6 : READtable Daylight,D0 : INC Daylight :  READtable Daylight,D1
     DayLight = DayLight +99 : READtable Daylight,D2 : INC Daylight :  READtable Daylight,D3
     
     DayLight = DayNumber //6 : IF D0 > D1 THEN : SWAP D0,D1 : DayLight = 6 -DayLight : ENDIF
      SunRise = D1 -D0 * Daylight /6 +D0 +240
     DayLight = DayNumber //6 : IF D2 > D3 THEN : SWAP D2,D3 : DayLight = 6 -DayLight : ENDIF
       SunSet = D3 -D2 * Daylight /6 +D2 +990
	 
	d0=sunrise/60
	d1=sunrise//60 /10
	d4=sunrise//60//10
	d2=sunset/60
	d3=sunset//60 /10
	d5=sunset//60//10
	
	Sertxd ("  Day ",#DayNumber,"  Year 20",#year,"    Sunrise ",#d0,":",#d1,#d4,"    Sunset ",#d2,":",#d3,#d5,CR,LF) 	
     
     END
      GOTO Main
 

Attachments

lbenson

Senior Member
marks,

I downloaded the excel spreadsheet, thank you. I changed Long and Lat to that of Liverpool, Nova Scotia, 44.0348° N, 64.7209° W, Atlantic time (-4). Everything looked good.
sunrise.jpg
I wrote a VB macro to output the sunrise/sunset PICAXE table code:
Code:
Sub buildTimeTable()

  Dim sCell As String, sRow As String, outFileName As String
  Dim A As String, B As String, C As String, D As String
  Dim i As Integer, j As Integer, k As Integer, l As Integer, m As Integer, n As Integer
  Dim iTableVal(200) As Integer, iRow As Integer
  
  For iRow = 2 To 368 Step 6
    sCell = "AB" & Trim(iRow)
    Index = Range(sCell)            ' get sunrise index
    sCell = "AC" & Trim(iRow)
    iTableVal(Index) = Range(sCell) ' get sunrise value

    sCell = "AF" & Trim(iRow)
    Index = Range(sCell)            ' get sunset index
    sCell = "AG" & Trim(iRow)
    iTableVal(Index) = Range(sCell) ' get sunset value
  Next iRow
  
  outFileName = "c:\dl\picaxe\sunrise.txt"
  Open outFileName For Output As #1
  j = 0 ' count to 10 values per line
  k = 0
  A = "TABLE " & CStr(k) & ",("
  For i = 0 To 61                ' sunrise times
    A = A + CStr(iTableVal(i)) & ","
    j = j + 1
    If j = 10 Then
      j = 0
      A = Mid(A, 1, Len(A) - 1) & ")"
      Print #1, A
      k = k + 10
      A = "TABLE " & CStr(k) & ",("
    End If
  Next i
  A = Mid(A, 1, Len(A) - 1) & ")"
  Print #1, A

  j = 0 ' count to 10 values per line
  k = 100
  A = "TABLE " & CStr(k) & ",("
  For i = 100 To 161               ' sunset times
    A = A + CStr(iTableVal(i)) & ","
    j = j + 1
    If j = 10 Then
      j = 0
      A = Mid(A, 1, Len(A) - 1) & ")"
      Print #1, A
      k = k + 10
      A = "TABLE " & CStr(k) & ",("
    End If
  Next i
  A = Mid(A, 1, Len(A) - 1) & ")"
  Print #1, A

  Close #1
    
  MsgBox ("Done")

End Sub
Here's the output. All looks good (if I have understood correctly). Haven't run tests yet.
Code:
TABLE 0,(234,233,232,229,224,218,211,204,195,185)
TABLE 10,(175,165,154,143,133,122,111,100,90,81)
TABLE 20,(72,63,56,49,44,40,37,36,35,37)
TABLE 30,(39,42,47,52,58,64,71,77,84,91)
TABLE 40,(98,105,111,118,125,132,139,146,154,161)
TABLE 50,(169,177,185,193,201,208,215,221,226,230)
TABLE 60,(233,234)
TABLE 100,(21,26,33,40,48,56,64,73,81,89)
TABLE 110,(97,105,112,120,127,134,142,149,156,163)
TABLE 120,(171,178,185,191,197,203,208,212,214,216)
TABLE 130,(216,215,212,208,203,197,189,181,172,163)
TABLE 140,(152,142,131,120,109,98,87,76,65,56)
TABLE 150,(46,38,30,24,18,14,12,11,11,13)
TABLE 160,(16,21)
Thanks again. If you note anything wrong in the table, please let me know.
 
Last edited:

marks

Senior Member
Hi Ibenson,
your data looks fine to me (I'll just have to try and learn how to use your macro's one day)
the day of year is worked out from leapyears so i would prefer to use a year like 2020
but excel does round up so the mid year 2018 may be a better fit there should only be a 1 minute difference.
if a value is too high for a variable ie >255 or -15 you can also try to tweek the offset values of 240 and 990 to fit.
I havent done much of my own testing yet lol but comparing to some web pages their the same or 1 minute different even they vary.
But the spreadsheet should be quite accurate and is also in seconds.

Code:
TABLE 0,(234,234,232,229,225,219,212,204,196,186)  '  44.0348   Sunrise +240
TABLE 10,(176,166,155,144,133,122,112,101,91,81)   '  -64.7209
TABLE 20,(72,64,56,50,44,40,37,36,35,36)           '  -4 StandardTime
TABLE 30,(39,42,46,51,57,63,70,77,84,90)
TABLE 40,(97,104,111,118,125,132,139,146,153,161)  '  2020 (leapyear) 
TABLE 50,(168,176,184,192,200,208,214,221,226,230)
TABLE 60,(233,234)
TABLE 100,(21,26,32,39,47,55,64,72,80,88)          '  Sunset +990 
TABLE 110,(96,104,112,119,126,134,141,148,156,163)
TABLE 120,(170,177,184,191,197,203,207,211,214,216)
TABLE 130,(216,215,212,209,204,197,190,182,173,163)
TABLE 140,(153,143,132,121,110,98,87,77,66,56)
TABLE 150,(47,38,31,24,19,15,12,11,11,13)
TABLE 160,(16,21)
 

AllyCat

Senior Member
Hi,

Since the UK was mentioned a few posts back (with reference to DST, which we call BST), it's worth noting that the program as written cannot be used in the UK because, above about Latitude 50, the Sunrise and Sunset times change by more than 255 over the course of a year. The easy solution would be to work in two minute increments, which gets up to about 60 degrees Latitude, but three minute increments would be needed for our "Northern Isles" of Scotland (Orkneys, Shetlands, etc..).

However, the variation is made up of two parts, firstly the "Equation of Time" which causes Noon to swing by just over 30 minutes during a year, producing a "Common Mode" variation of Sunrise and Sunset. Then, the inclination of the Earth's axis causes the Day Length (or strictly half of it) to swing the Sunrise and Sunset times in a "Differential Mode". So an alternative method is to consider the two Table bytes as a single word and split it as 6 bits (for the EoT common mode value) and 10 bits for the differential part, up to 1023 minutes (of which about 800 may be needed in some extreme locations) to add and subtract from the "noon" value for Sunset and Sunrise times. But I won't clutter up your thread with any more detail, which in not needed for many parts of the World.

Cheers, Alan.
 

hippy

Technical Support
Staff member
These time and date calculations, day of week, sunrise and sunset times, phase of moon, when Easter falls, etc, would probably be better taken out of RTC specific threads and put into their own separate threads.

What would be useful for people like me, who are interested but not familiar with the subjects, would be links to the algorithm the code is based upon, and also details of how the PICAXE code implements that algorithm, what data in tables and any 'magic numbers' represent or achieve.

Things like "this approximates sin(x)*100" is far better than just a READ and a table of initially unfathomable data, or incomprehensible expressions. It helps that jump from what it's doing to why it's doing it.

For me, 'this is what we are trying to achieve, and this is how we get there', is far more useful, informative and engaging than 'this is what I ended up with'.

Being able to give someone enough that they can understand what is being done and how, can rewrite or rework the same from scratch themselves, is the ideal ultimate goal, though it is appreciated that detailed documentation and explanation can take a lot of time and effort. Having 'got there' it is additional work to then explain the how and why which will have mostly been in one's own head or on scraps of paper.
 

AllyCat

Senior Member
Hi,

Yes, this "finished projects" thread only went Off Topic at about post #19, perhaps as a "mistake" when replying to newplumber's current active thread around post #20 here, which includes the same material. I don't know if the latter posts could/should be moved there (or used to create a new "Active" thread), but as neither thread is "mine", I'll leave it to the relevant OP's (or hippy) to suggest whether anything might be moved . ;)

However, I'll just give a quick "advert" for my own thread on a related topic, which I intentionally didn't post as a finished project, because it's still a "work in progress". After more than 4 years, I'm discovering myself where the comments might be improved. ;)

Cheers, Alan.
 

marks

Senior Member
Hi AllyCat,
Its always an interesting challenge what a new location may bring lol.
I guess its my fault the thread is off topic like most do many have asked for a tutorial section
but requires alot of effort and time from members,i often thought about redoing but theres just so many other things todo
ive added code which can be used with an rtc some of the code i upload is in a ruff state
when i add toit i like to do things slightly different so i'm not always reposting the same thing. i think is relevent tho..
I sometimes put code I've wrote for others back in my own threads so i can find it later.

after having a quick look at your related topic I can see you prefer an Extreme challenge
quite afew years ago I also looked at a basic program on nasa site that was quite invovled ( i didnt start that thankfully).
interesting i noticed NOAA spread sheet and time .com is what i recently found on google too.
perhaps pm the lat long required and timezone to make it easy for me and i can add it later as example when i get time..
i'll just manualy edit the data table(proberly a third of it )add another offset hopefully just a few lines of code ..
and still try and keep it to 1 minute resolution.
here in perth ive found the the spread sheet to be more accurate than time.com

for others the data is just the minutes of the day at sunrise 61 entries and 61 entries for sunset
so its every 6 days over 366 days the code also works out the values for days inbetween (6) 7,8,9,10,11 (12)
so could also use data from time .com if sunrise is 0604am this 364minutses take offset 240 then the table dataentry becomes 124

the JulianDate is calcalated at midday the startday is given in the post which is Saturday Jan1 2000
i believe the originator first published it in skymagazine many years before this
and have seen it in many basic nasa programs that look like they came out in the 70's
it is quite common, many people know about and have reused it like myself
unfortunately i dont think a lot of those links exist so may be hard to now find.
I just added the decimal seconds part to it.
Ive reworked this code many times so am familiar like using it to get the daynumber for the year etc.

I remember seeing the lunar phase average quoted in library book being rounded off at about 16 decimal places long.
no i didnt read it ijust tookdown this figure.
from that spent much time working out word values for the picaxe luckily i recorded that in the post and settled on
25101 /850 = 29.530588235 days its 6 decimal places accurate, embarassingly i also remember the next four decimal numbers
which i subconsciously used as apassword for a while so will only tell you the next one should be a 5 not a 2 lol.
I did spend a bit of time to get to this maybe its it why we dont have so many trees left today .this was still difficult to use.
because i had all this pencil data on this number i stuck with it then took a different approach
in 2894 days found we have 98 moons (that was the best number i found i could work with)
mod 2894 / 29.530588235 =98.000079 with this smaller number we just scale up again with slight correction and divide again
by a not so accurate 29.53 add the remainder back in and the .1 day correction that part i did intend to go back and improve
so its a bit of an experimental approach to attemp get accuracy , posted and never got back toit
at that time nasa provided a lot of info chart you could look up a future date you could check against now i guess we have google.
altho i did post lunar phase average which i was trying to emulate so it could be checked with a calculator
The use of 2894 would have every1 stumped ,altho i did find smaller numbers they only went to 2 or 3 decimal places.

this sure took some time its far easier coding then posting!
 
Top