help on code for add alarm

yomero5560

New Member
This is my first project, I am new on picaxes, trying to add code to tun on pin 5 as the first time is meet, as soon as the second time time is meet pin 5 needs to go off. I did some modifications to a program i found in the internet, using picaxe 08m adapting to meet requirements on 08m2+ is running good. code is as follows.

Code:
; picaxe 08M2+ and 
; DS1302 Opcodes

  symbol WriteCtrl     = %10001110
  symbol ReadSecs      = %10000001
  symbol WriteSecs     = %10000000
  symbol ReadMins      = %10000011
  symbol WriteMins     = %10000010
  symbol ReadHrs       = %10000101
  symbol WriteHrs      = %10000100
  symbol WriteTrickle  = %10010000
  symbol TrickleSet    = %10100101  
  
; DS1302 Connections

  symbol CLK = 4    ' leg 3 to DS1302 leg 7
  symbol IO  = 1    ' leg 6 to DS1302 leg 6
  symbol RST = 2    ' leg 5 to DS1302 leg 5
  
; Registers

  symbol Hours     = b0
  symbol Mins      = b1
  symbol Secs      = b2
  symbol PrevSecs  = b3
  symbol Command   = b4
  symbol Data1302  = b5
  symbol ShiftData = b6
  
Main:

; Preset time here using BCD format.
; As shown, this presets the time to 12:34:56

  Hours = $12
  Mins  = $34
  Secs  = $56

  PrevSecs = 0
  
  low CLK
  
  ; Init 1302
  
  Command = WriteCtrl        ' clear write protect
  Data1302 = 0
  gosub Send1302Cmd
  
  Command = WriteTrickle      ' turn on the supercap trickle charger
  Data1302 = TrickleSet
  gosub Send1302Cmd
  
  Hours = Hours & %00111111  ' set 24-hour format, set hours
  Command = WriteHrs
  Data1302 = Hours
  gosub Send1302Cmd
  
  Command = WriteMins        ' set minutes
  Data1302 = Mins
  gosub Send1302Cmd
  
  Secs = Secs & %01111111    ' set CH bit to zero and set secs
  Command = WriteSecs
  Data1302 = Secs
  gosub Send1302Cmd
  
  
  do                         ' wait for Secs to change
  
    Command = ReadSecs
    gosub Get1302Data
    
    if Data1302 <> PrevSecs then   ' when secs change, get and show time.
      Secs = Data1302
      PrevSecs = Secs
      gosub GetTime
    endif
  
  loop
  
  
end


Send1302Cmd:

  ' Sends Command, then sends Data1302
  
  dirs = %00010110  
  high RST
  
  ShiftData = Command
  gosub Shiftout
  
  ShiftData = Data1302
  gosub Shiftout
  
  low RST

return


Get1302Data:

  ' First sends Command, then reads 1302 result to Data1302
  
  dirs = %00010110
  high RST

  ShiftData = Command
  gosub Shiftout

  dirs = %00010100
  gosub Shiftin
  
  low RST
    
return      
  
  
 Shiftout:

  ;  Shift data to 1302, LSB first

  for b13 = 1 to 8
    b12 = ShiftData % 2
    if b12 = 1 then
      high IO
    else
      low IO
    endif
    pulsout CLK,1         ' 10 uSec pulse on CLK
    ShiftDAta = ShiftData / 2
  next

return


 Shiftin:

  ;  Shifts data into Data1302 LSB first
  
  ;  This call always follows a shiftout op, so the first bit of 
  ;  the response is present on the IO pin at call.  It only takes
  ;  seven clock pulses to shift in the rest of the data byte


  ' note that this won't read the most significat bit.  but when
  ' reading the DS1302, it never has to. 
  for b13 = 1 to 7
    b12 = pin1 * 128
    Data1302 = Data1302 + b12
    Data1302 = Data1302 / 2   
    pulsout CLK,1               ' 10 uSec pulse on CLK strobes in next data bit  
  next

return


GetTime:

  ' Get Hours, Mins, Secs, convert to ASCII, send to Picaxe terminal
  
  Command = ReadHrs
  gosub Get1302Data
  Hours = Data1302 & %00111111
  bcdtoascii Hours,b12,b13
  sertxd(b12,b13,":")
  
  
  Command = ReadMins
  gosub Get1302Data
  bcdtoascii Data1302,b12,b13
  sertxd(b12,b13,":")
 
  Command = ReadSecs
  gosub Get1302Data
  bcdtoascii Data1302,b12,b13
  sertxd(b12,b13,13,10)

return
What I need is the code to turn on C.0 at first programmed or checked against ds1302 RTC and another time turn C.0 off how can I compare the times to turn on and then turn off any help on how to write the code is more than well come.
Thanks in advance to every one.
 
Last edited by a moderator:

hippy

Technical Support
Staff member
I would guess something like ...

Code:
Do

  Command = ReadSecs
  gosub Get1302Data
  Secs = Data1302

  Command = ReadMins
  gosub Get1302Data
  Mins = Data1302

  Command = ReadHrs
  gosub Get1302Data
  Hours = Data1302 & %00111111
  
  Command = ReadSecs
  gosub Get1302Data

Loop Until Secs = Data1302

If Hours = ?? And Mins = ?? And Secs = ?? Then
  ...
End If
 
Top