Shop OBEX P1 Docs P2 Docs Learn Events
My RTC clock set program seems to be corrupted — Parallax Forums

My RTC clock set program seems to be corrupted

I been using DS3231 RTCs for a long time but now even after a lot of checking I can't seem to make the setting program work. It just wants to read out zeros after programming. Don't know if I accidentally changed something. I had a computer crash in January and been rewriting a lot of things. Help would be appreciated. It is using 5 volts!

CON

  _clkmode = xtal1 + pll16x      '80 MHz system clock
  _xinfreq = 5_000_000

  SCL = 28
  SDA = 29

  RTC = %1101000




'Timbuf address registers
Seconds = 0
Minutes = 1
Hours   = 2
Day     = 3
Date    = 4
Month   = 5
Year    = 6

'Useful assignments

Sunday    = $01
Monday    = $02
Tuesday   = $03
Wednesday = $04
Thursday  = $05
Friday    = $06
Saturday  = $07

January   = $01
February  = $02
March     = $03
April     = $04
May       = $05
June      = $06
July      = $07
August    = $08
September = $09
October   = $10
November  = $11
December  = $12

OBJ

   PST  :   "Parallax Serial Terminal.spin"
   i2c  :   "I2C SPIN driver v1.4od"
VAR
  byte  Timbuf[7]
  byte answer

PUB  init

'  rtc.startx(sclpin, sdapin)      'startx(sclpin, sdapin)
  PST.start(115_200)
  waitcnt(clkfreq * 3 + cnt)

  I2C.init(SCL,SDA)


    PST.clear
    PST.str(string("Enter 'y' before 3 seconds if you want to set clock:"))
    answer := PST.CharIn
    if answer == "y"
      Settime
    PST.clear  'clear screen          ?????????
    waitcnt(clkfreq / 100 + cnt)
    time
PUB  settime | Idx, t, n      'probably don't need Idx or t


  PST.clear     'clear screen

  n:= 0
  PST.str(string("Enter year (00-99): "))
  repeat until n == $0D
    n := PST.CharIn
    if n => "0" and n =< "9"
      Timbuf[year] := Timbuf[year] << 4 + (n - "0")


  n := 0
  PST.str(string("Enter month (01-12): "))
  repeat until n == $0D
    n := PST.CharIn
    if n => "0" and n =< "9"
      Timbuf[month] := Timbuf[month] << 4 + (n - "0")

  n := 0
  PST.str(string("Enter date (01-31): "))
  repeat until n == $0D
    n := PST.CharIn
    if n => "0" and n =< "9"
      Timbuf[date] := Timbuf[date] << 4 + (n - "0")

  n:= 0
  PST.str(string("Enter hour (01-12) for 12 hr or (01- 23) for 24 hr): "))
  repeat until n == $0D
    n := PST.CharIn
    if n => "0" and n =< "9"
      Timbuf[hours] := (Timbuf[hours] << 4 + (n - "0"))


  PST.str(string("if 12 hour clock desired, press 'y' before 3 seconds:"))
                            'IF NOT PRESS ENTER
    answer := PST.CharIn
    if answer == "y"
      twelve_hour
    Else
      PST.newLine

  PST.str(string("if PM, press 'y' before 3 seconds:"))
 '                                 'IF NOT PRESS ENTER
    answer := PST.CharIn
    if answer == "y"
      afternoon
    Else
      PST.newLine

  n:= 0
  PST.str(string("Enter minutes (00-59): "))
  repeat until n == $0D
    n := PST.CharIn
    if n => "0" and n =< "9"
      Timbuf[minutes] := Timbuf[minutes] << 4 + (n - "0")

  n:= 0
  PST.str(string("Enter seconds (00-59): "))
  repeat until n == $0D
    n := PST.CharIn
    if n => "0" and n =< "9"
      Timbuf[seconds] := Timbuf[seconds] << 4 + (n - "0")

  n := 0
  PST.str(string("Enter Day of Week. Sun=01,M=02,T=03,W=04,TH=05,F=06,Sat=07 : "))
  repeat until n == $0D
    n := PST.CharIn
    if n => "0" and n =< "9"
      Timbuf[Day] := Timbuf[Day] << 4 + (n - "0")



  Timbuf := I2C.writebytes(RTC,Seconds,@Timbuf[seconds],7) '(device,address,dataAddress,bytes)
 ' PST.bin(timbuf,16)
  waitcnt(clkfreq/2+cnt)


PUB  time

  Repeat
    waitcnt(clkfreq/2+cnt)
    PST.clear    'clear screen
    I2C.readbytes(RTC,0,@Timbuf,7) '(device,address,dataAddress,bytes)
    PST.str(lookup(Timbuf[Day]:string("Sunday"),string("Monday"),string("Tuesday"),string("Wednesday"),string("Thursday"),string("Friday"),string("Saturday")))
    PST.str(string(" "))
    if Timbuf[Month] => $10
      Timbuf[month] -= $06
    PST.str(lookup(Timbuf[Month]:string("January"),string("February"),string("March"),string("April"),string("May"),string("June"),string("July"),string("August"),string("September"),string("October"),string("November"),string("December")))
    PST.str(string(" "))
    PST.hex(Timbuf[Date],2)
    PST.str (string(", "))
    PST.hex($2000 + (Timbuf[year]),4)
    PST.str(string(" "))
    if Timbuf[hours] & %0100_0000   'is true (ie not 0)         'if set for  12-hour time
      PST.hex(Timbuf[hours] & %0001_1111,2)
      PST.str(string(":"))
      PST.hex(Timbuf[minutes],2)
      PST.str(string(":"))
      PST.hex(Timbuf[seconds],2)
      PST.str(string(" "))
      if Timbuf[hours] & %0010_0000
        PST.str(string("pm"))
      else
        PST.str(string("am"))
    else     'if set for 23-hour time
      PST.hex(Timbuf[hours] & %0011_1111,2)
      PST.str(string(":"))
      PST.hex(Timbuf[minutes],2)
      PST.str(string(":"))
      PST.hex(Timbuf[seconds],2)

PUB  twelve_hour
  Timbuf[hours] += $40    'for 12 hour clock

PUB  afternoon
  Timbuf[hours] += $20      'for PM

 {
PRI Prompt(msg, CR)
  PST.str(msg)
  if CR
    PSTclear
 }

Comments

Sign In or Register to comment.