PropBasic and RTC

I have a system running the Propeller coded in "C" that includes an RTC in one COG.

Is there a PropBasic program that does the same thing...setting and reading year, month, date, day, hour, minute, and seconds?
I really like PropBasic and would like to rebuild these systems using PropBasic.

Sincerely,

Discovery

Comments

  • 8 Comments sorted by Date Added Votes
  • I was sure that I used the DS1307 with PropBasic, not for the RTC, per se but the battery-backed RAM for a parts counter.

    Can't even find the DS1307 module right now. Will dig deeper over the weekend.
    PropBASIC ROCKS!
  • ersmithersmith Posts: 2,331
    edited October 5 Vote Up0Vote Down
    Discovery wrote: »
    I have a system running the Propeller coded in "C" that includes an RTC in one COG.

    Is there a PropBasic program that does the same thing...setting and reading year, month, date, day, hour, minute, and seconds?
    By "an RTC in one COG" do you mean "one COG acting as an RTC" (counting seconds since the clock was last set) or do you mean "one COG driving an external RTC chip"?

    Making a COG count seconds is pretty simple, although you do need a bit of code to initialize the correct time at the start.
  • Yes...the code that was supplied by a Forum member was written in "C" and all the RTC functions ran in one COG. That propeller has been functioning for several years. As I mentioned, I found that PropBasic is the program code that I like best so for future systems I want to download an RTC into one propeller COG coded in PropBasic then sent to the propeller as compiled code.

    Has this been accomplished?

    Sincerely,

    Discovery
  • It really should be easy to write an RTC for PropBasic. I'm rusty on PropBasic syntax, but here is a version using fastspin Basic. It runs the "updateClock" routine in another COG to update the time, while the main program loops printing the time. In PropBasic I think you'd make updateClock a TASK.
    ''
    '' simple real time clock running in another cog
    ''
    const FREQUENCY = 80_000_000
    
    ' hours, minutes, seconds: 00-23, 00-59, 00-59
    dim as ubyte hours, mins, secs
    
    ' month, day in month: 1-12, 1-31
    dim as ubyte month, day
    
    ' year: 4 digits
    dim as integer year
    
    ' stack space for the RTC COG
    dim stack(10)
    
    ''
    '' helper subroutine; return number of days in month
    ''
    function daysInMonth() as uinteger
      ' february is a special case
      if month = 2 then
        if (year mod 4 = 0) then
          ' possible leap year
          if (year mod 100 <> 0) or (year mod 1000 = 0) then
            return 29  ' definite leap year
          endif
        endif
        return 28
      endif
      if (month = 4) or (month=6) or (month=9) or (month=11) return 30
      return 31
    end function
        
    ''
    '' routine to keep the clock up to date
    '' this is launched in another COG and just runs forever,
    '' waking once per second to update the time
    ''
    sub updateClock
      dim nextSecond
    
      nextSecond = getcnt() + FREQUENCY
      do
        waitcnt(nextSecond)
        nextSecond = nextSecond + FREQUENCY
        secs = secs + 1
        if (secs >= 60) then
          secs = 0
          mins = mins + 1
          if (mins >= 60) then
            mins = 0
    	hours = hours + 1
    	if (hours >= 24) then
    	  hours = 0
    	  day = day + 1
    	endif
          endif
        endif
        if (day > daysInMonth()) then
          day = 1
          month = month + 1
          if (month > 12) then
            month = 1
    	year = year + 1
          endif
        endif
      loop
    end sub
    
    ''
    '' main program
    ''
    
    '' initialize the time
    print "Enter year month day as YYYY-MM-DD ";
    '' read 10 characters into s$
    var s$ = input$(10)
    print
    
    '' split out the month, year, day
    year = val(left$(s$, 4))
    month = val(mid$(s$, 6, 2))
    day = val(right$(s$, 2))
    
    print "Enter time as hh:mm:ss ";
    s$ = input$(8)
    print
    
    hours = val(left$(s$, 2))
    mins = val(mid$(s$, 4, 2))
    secs = val(right$(s$, 2))
    
    ' start the RTC update thread on another COG
    var x = cpu(updateClock, @stack(1))
    
    ' now loop printing the time
    do
      print using "####_-%%_-%%  "; year, month, day;
      print using "##:%%:%%"; hours, mins, secs
      pausems(1000)
    loop
    
  • Since I'm working on the Badge WX libraries here is an RTC for the Badge WX.
    '-----------------------------------------
    ' Badge WX PropBasic Demo
    '-----------------------------------------
    
    DEVICE P8X32A, XTAL1, PLL16X
    FREQ 80_000_000
    
    ' Libraries
    LOAD "Lib_BWX_OLED.pbas"
    
    hub_time    HUB BYTE(4) = 0 ' seconds, minutes, hours, seconds
    
    RTC TASK AUTO
    
    'VARs
    seconds  VAR LONG
    minutes  VAR LONG
    hours    VAR LONG
    temp     VAR LONG
    
    
    PROGRAM Start LMM
    '{$DEFINE LMM}
    
    Start:
      WRBYTE hub_Time, 50,58,23
      OLED_Start
      DO
        ' Read until seconds is stable
        DO
          RDBYTE hub_Time, seconds, minutes, hours, temp
        LOOP UNTIL seconds = temp 
     
        OLED_Locate 0,2
        IF hours < 10 THEN
          OLED_PrintLGChar "0"
        ENDIF
        OLED_PrintLGValue hours
        OLED_PrintLGChar ":"
        IF minutes < 10 THEN
          OLED_PrintLGChar "0"
        ENDIF
        OLED_PrintLGValue minutes
        OLED_PrintLGChar ":"
        IF seconds < 10 THEN
          OLED_PrintLGChar "0"
        ENDIF
        OLED_PrintLGValue seconds
    
      LOOP
    END
    
    TASK RTC
      clocks  VAR LONG
      seconds VAR LONG
      minutes VAR LONG
      hours   VAR LONG
    
      clocks = CNT + 80_000_000
      DO
        WAITCNT clocks, 80_000_000
        RDBYTE hub_Time, seconds, minutes, hours
        INC seconds
        IF seconds = 60 THEN
          seconds = 0
          INC minutes
          IF minutes = 60 THEN
            minutes = 0
            INC hours
            IF hours = 24 THEN
              hours = 0
              ' adjust date here
            ENDIF ' hours = 24
          ENDIF ' minutes = 60
        ENDIF ' seconds = 60
        WRBYTE hub_Time, seconds, minutes, hours, seconds
      LOOP
    ENDTASK
    

    Bean
  • Excellent Bean...thank you.

    Sincerely,

    Discovery
  • Hi Bean,

    My first step in using the program information you provided is to: (1) set the clock time in the COG and (2) read the hours, minutes, and seconds from the main program to activate and deactivate propeller outputs at specific times during the course of a 24 hour day. I have no use for the display at this time.

    What additions or changes do I make to accomplish this task.

    Sincerely,

    Discovery
  • Bean,
    I figured it out...thanks.

    Discovery
Sign In or Register to comment.