Time logic


Closed Thread
Results 1 to 7 of 7

Thread: Time logic

Hybrid View

  1. #1
    Join Date
    Oct 2009
    Posts
    583

    Default Time logic

    My current project is not performing very well. It relies on matching the values in two variables to the converted BDC format from a battery backed up 1307 RTC. My idea is to use a variable to store the current time in minutes from midnight, so in theory it should have a value between 0 and 1439. The idea then is to convert the time the LEDs come on into a numerical value and the use the "if then " statement to determine if the LEDs should be on in the event of a power cut. For example if the on time was from 14:00 to 18:00 hrs then the values would be => then 840 but =< 1080, so if there was a power cut and power was resumed at say 15:00 hrs then as the time would be converted to 900 and the logic would then check the value against the programmed value and thus turn the LEDs on. This is my proposed code (with some of the RTC variables not included for simplicity)

    Code:
    Counter 1 var byte
    counter 2 var byte
    counter 3 var byte
    
    TimeH var byte      ' Variable to store current hour 
    TimeM var Byte      ' Variable to store current minutes 
    
    
    Main:
    
    I2CRead SDApin,SCLpin,$D0,$00,[RTCSec,RTCMin,RTCHour,RTCWDay,RTCDay,RTCMonth,RTCYear,RTCCtrl]  ; read DS1307 chip
    
    timeH=(RTCHour>>4)                               'convert the BCD format of the hours register and store in variable timeH
    timeH=(timeH &$03)*10
    timeH=timeH+(RTCHour&$0F)
    
    timeM=(RTCMin>>4)
    timeM=(timeM &$07)*10
    timeM=timeM+(RTCMin&$0F)                         'convert the BCD format of the mins register and store in variable timeM
    
    If TimeH = 0 and timeM = 0 then
        couner 3 = 0
    endif
    
    If TimeH <1 then counter 1 = 0
    If timeH >0 then counter 1 = Time H * 60
    Counter 3 = counter1 + TimeM
    Endif
    endif
    Would welcome comments
    Last edited by Scampy; - 26th October 2013 at 23:11.

  2. #2
    Join Date
    May 2013
    Location
    australia
    Posts
    2,631


    Did you find this post helpful? Yes | No

    Default Re: Time logic

    to get minutes from midnight.

    whats this about ????? (timeH=(timeH &$03)*10)

    would it not be :-
    timeH=(RTCHour>>4) 'convert the BCD format
    timeH=timeH *10
    timeH=timeH+(RTCHour&$0F) ' to binary hours


    timeM=(RTCMin>>4) ' bcd minutes to binary minutes

    timeM=timeM *10
    timeM=timeM+(RTCMin&$0F)



    minutes_from_midnite =(timeh*60)+timem


    i'm sure its a typo but
    Counter 1 var byte
    counter 2 var byte
    counter 3 var byte

    looks a bit dodgy to have a space in a var name

  3. #3
    Join Date
    Apr 2011
    Location
    Welches, Oregon
    Posts
    198


    Did you find this post helpful? Yes | No

    Default Re: Time logic

    I am reading / replying on phone; please understand the difficulty of studying closely your code. It would appear to me that counter variables may exceed byte value assigned? Var WORD to take values greater than 254.

    I too am recently working with clock and found it complex and clumsy to work between decimal and BCD; perhaps you too would find it simpler to make the conversion only when reading/ writing registers and therefore all other interactions are simplified by being common value without effort?
    Last edited by Amoque; - 27th October 2013 at 13:46.

  4. #4
    Join Date
    Oct 2009
    Posts
    583


    Did you find this post helpful? Yes | No

    Default Re: Time logic

    Thanks for the comments guys and yes it should be word to get the 1440 minutes in a day. The code for the conversion from BDC came from a sample code written by Melenie, which I've used in other projects so couldn't see any reason to change it.

  5. #5
    Join Date
    May 2013
    Location
    australia
    Posts
    2,631


    Did you find this post helpful? Yes | No

    Default Re: Time logic

    I have found it unnecessary to mask off the unused bits with genuine ds1307 chips , if the time is ok they are always 0.
    however PCF8563 chips need every unused bit masked off . so maybe scampy's bcd2bin conversion code is the safest option
    cheers

  6. #6
    Join Date
    Apr 2011
    Location
    Welches, Oregon
    Posts
    198


    Did you find this post helpful? Yes | No

    Default Re: Time logic

    Quote Originally Posted by Scampy View Post
    My current project is not performing very well. It relies on matching the values in two variables to the converted BDC format from a battery backed up 1307 RTC. My idea is to use a variable to store the current time in minutes from midnight, so in theory it should have a value between 0 and 1439. The idea then is to convert the time the LEDs come on into a numerical value and the use the "if then " statement to determine if the LEDs should be on in the event of a power cut. For example if the on time was from 14:00 to 18:00 hrs then the values would be => then 840 but =< 1080, so if there was a power cut and power was resumed at say 15:00 hrs then as the time would be converted to 900 and the logic would then check the value against the programmed value and thus turn the LEDs on. This is my proposed code (with some of the RTC variables not included for simplicity)

    Would welcome comments
    I thought to take another look benefited by the large screen, as I did not understand well your explanation of what was to happen here. Perhaps I still do not... curiosity begs to ask: What is the purpose of this?

    Typically I refrain from asking, but in this case I try only to understand for my own satisfaction whether I am slow to comprehend the text... or the purpose. If the clock is battery-backed, the time will be always correct then yes? Should it not then be correct to calculate the LED's status from the current time always? Why then to store the minutes from midnight (rather than calculate)? And, if the clock is not correct, then how can the minutes from midnight be the right number and the LED's status correctly resolved?

    Also, if the "start" of the counter is always midnight, could you not avoid any calculation at all by maintaining a count as the minutes pass similar to:

    If OldMinute <> NewMinute then
    MinituesSinceMidnight = MinutesSinceMidnight +1
    OldMinute = NewMinute
    Endif

    starting at midnight?

  7. #7
    Join Date
    Oct 2009
    Posts
    583


    Did you find this post helpful? Yes | No

    Default Re: Time logic

    Your logic seems sound, and I guess I could then use the logic thus:

    If MinutessinceMidnight => LightsOn and <= LightsOff then turn lights on
    Endif

    so for example, if the lighton value was 720 and lightsoff value was 800 then LEDs would be on just between these "times"

    I just need to get my DS1307 working now (see other thread !)

Similar Threads

  1. Logic Probe Recommendations
    By retepsnikrep in forum Off Topic
    Replies: 3
    Last Post: - 12th June 2011, 20:35
  2. DS1904 RTC - How to Convert Binary Time into Real Time/Date?
    By Ioannis in forum mel PIC BASIC Pro
    Replies: 7
    Last Post: - 2nd December 2010, 10:45
  3. Logic Level Triac
    By emavil in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 29th November 2006, 18:18
  4. Fuzzy logic?
    By Damir in forum mel PIC BASIC Pro
    Replies: 0
    Last Post: - 28th August 2006, 12:03
  5. Linx RF -> HSERIN time delay / time critical app...
    By batee in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 15th October 2004, 15:04

Members who have read this thread : 0

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts