real time clock


Closed Thread
Results 1 to 40 of 45

Thread: real time clock

Hybrid View

  1. #1
    blainecf's Avatar
    blainecf Guest


    Did you find this post helpful? Yes | No

    Talking Straight-forward RTC bits conversion algorithm

    For all you old timers, here's an alternate way of calculating Human Date/Time from the RTC bits:

    (Also, because all info is read in one shot, there is less liklihood that the seconds won't match the minutes.)

    I2CREAD DPIN,CPIN,RTC,I2C_Adr_B,[Rd_seconds, rd_minutes, rd_hours, Rd_DOW, Rd_DAy, RD_month, Rd_Year] ' in bcd code

    Seconds = Rd_Seconds.0 + (Rd_Seconds.1 * 2) + (rd_seconds.2 * 4) + (rd_Seconds.3 * 8) + (rd_seconds.4 * 10) + (rd_seconds.5 * 20) + (Rd_Seconds.6 * 40)

    Minutes = Rd_Minutes.0 + (Rd_Minutes.1 * 2) + (rd_Minutes.2 * 4) + (rd_Minutes.3 * 8) + (rd_Minutes.4 * 10) + (rd_Minutes.5 * 20) + (Rd_Minutes.6 * 40)

    Hours = RD_Hours.0 + (Rd_hours.1 * 2) + (rd_hours.2 * 4) + (rd_hours.3 * 8) + (rd_hours.4 * 10)

    am_pm = rd_hours.5
    dow = rd_dow

    day = rd_day.0 + (rd_day.1 * 2) + (rd_day.2 * 4) + (rd_day.3 * 8) + (rd_day.4 * 10) + (rd_day.5 * 20)

    Month = rd_month.0 + (rd_month.1 * 2) + (rd_month.2 * 4) + (rd_month.3 * 8) + (rd_month.4 * 10)

    Year = rd_year.0 + (rd_year.1 * 2) + (rd_year.2 * 4) + (rd_year.3 * 8) + (rd_year.4 * 10) + (rd_year.5 * 20) + (rd_year.6 * 40) + (rd_year.7 * 80)

  2. #2
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    I suppose that might work. But it wouldn't be my first choice.

    Only 30 or 40 times the needed code.

    DT

  3. #3
    jessey's Avatar
    jessey Guest


    Did you find this post helpful? Yes | No

    Default How do I set the 12 hr mode for DS1302

    Hello,

    I added some code to Omer Yildiz's ds1302_LCD_setup.bas program that was modified from CocaColaKids original code example posted here & I added a timer and have a few questions if anyone could answer me.

    I have read the DS1302 datasheet but couldn't figure out how to get the clock to show in the 12 hour mode for am & pm time. According to the datasheet I have to set bit 7 of the hours register to a high (1) but I don't understand Omer's code to do that. Also can anyone here explain what this kind of look up table is all about?

    Quote from Omer's code.............................................. ...............
    Lets burn the eeprom for something useful. A kind of lookup table.
    eeprom 1,[31,28,31,30,31,30,31,31,30,31,30,31]

    I've included the program here if anyone wants to look at it, I'm sure its not very efficient but it works.

    These are the modifications below that I made to Omer's ds1302_LCD_setup.bas program. A possible application could be as a kitchen timer.

    I added visual Lcd screens that shows you what your setting when setting the calendar and the clock. I also programmed the menu led to be used as a visual flashing indicator that the alarm time has elapsed. I added an up-count timer that increments each time the second variable of the DS1302 increments. I also added another 2 push buttons and followed Omer's nice example of his 2 button set and increment push button routine for setting the Seconds, Minutes and Hours times for the alarm. On initial power up the up-count timer is disabled, once your finished setting the times for the alarm and press the Set_Alarm_Timer_Push_Button then the up-counter starts incrementing. When the
    set time equals the up-count time then the alarm buzzer sounds for approx 10 beeps then shuts off while the red led continues to flash until the Set_Timer_Push_Button is pressed to shut it off. Eventually I'll change it again to add more buttons, one for each of the Seconds, Minutes and Hours settings so the times can be set quicker but it works great as is.

    There is one thing I noticed after getting the timer set up to work, is that when the up-count timer starts after your finished setting the alarm times, its not always accurate to the second. Because, depending on the time when your finished setting your times and then when you return to start the up-count timer, it very seldom starts exactly on a second change and can be out by as much as 900 ms. If this is only used as a kitchen timer then its not a big deal but in a time critical application it could be.

    Thanks
    jessey
    Attached Files Attached Files

  4. #4
    Join Date
    Jan 2006
    Location
    Istanbul
    Posts
    1,185


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by jessey
    ... Also can anyone here explain what this kind of look up table is all about?
    Quote from Omer's code.............................................. ...............
    Lets burn the eeprom for something useful. A kind of lookup table.
    eeprom 1,[31,28,31,30,31,30,31,31,30,31,30,31]

    Hi jessey,

    Omer is me.

    That code was so old and just for an example. I have modified it several times afterwards.

    For the eeprom look up table, it works while the user is in setup menu. It is not used in any other place in the code.

    While the user is changing the day, the table matches the number of days for the stored month.

    If the year is one of those with 29 days, then the "EEPROM location 2" will be written by 29 so that when user gets into "changing the day" and if it is in February then the max days on LCD will be 29.

    Examples:
    If the month is 1 then LCD shows the max day as day31 then starts from day1.

    If the month is 2 then LCD shows the max day as day28 or day29 depending on the year and then starts from day1.

    .
    ..
    ...

    if the month is 11 then LCD shows the max day as day30 then starts from day1.

    etc...
    Last edited by sayzer; - 29th August 2006 at 09:02.
    "If the Earth were a single state, Istanbul would be its capital." Napoleon Bonaparte

  5. #5
    jessey's Avatar
    jessey Guest


    Did you find this post helpful? Yes | No

    Default Do I have this right?

    Hi sayzer,

    Thanks for the explanation of your eeprom code.

    I went over the DS1302 data sheet again (actually many times) and I think I got it now, I'm hoping. I finally realized that you wrote your code for the reads and writes of the Registers in hex which threw me off. When I converted them over to binary for the hours then it made some sense. I set bit 7 and bit 5 in the binary number then converted them back to hex. I haven't tried it yet but I think this might work.

    for am 12 hr mode
    writehour = 00100011 = $C4
    readhour = 10100011 = $A3

    for pm 12 hr mode
    readhour = 10101011 = $AB
    writehour = 00101011 = $D4

    Do you think this will do it? If I have this part right then I don't think I'll have any problems converting over the rest of the code to display it with the am and pm.

    Thanks
    jessey

  6. #6
    Join Date
    Jan 2006
    Location
    Istanbul
    Posts
    1,185


    Did you find this post helpful? Yes | No

    Default

    Take a look at this.
    You should get nice ideas from Melanie's extreme (ly easy for her) work here.


    http://www.melabs.com/resources/samp...ted/MN1307.txt


    ------------------
    "If the Earth were a single state, Istanbul would be its capital." Napoleon Bonaparte

  7. #7
    jessey's Avatar
    jessey Guest


    Did you find this post helpful? Yes | No

    Default Nothing but questions

    Hi sayzer,

    Thanks for sending me the link to Melanie's code.

    Yea its way over my head, I don't understand the BCD system at all. I checked out BCD on the net and it says it can be used to save RAM space which can result in using half the storage space as compared to storing the same value in hex. If I read and understood that correctly then it would make sense to use it. This is the site where I read that: http://www.danbbs.dk/~erikoest/bcd.htm#top its got links to a lot of useful stuff that you didn't want to know! But I guess its all relevant if you want to learn programming.

    This is the BCD code that I didn't understand when I first looked at Melanie's code:
    Code:
     ' Subroutine Converts back to BCD
     ' -------------------------------
      ' CounterA is the entry variable
      ' CounterB holds the converted BCD result on exit
    ConvertBCD:
     CounterB=CounterA DIG 1
     CounterB=CounterB<<4
     CounterB=CounterB+CounterA DIG 0
     Return
    It looks a little similar to this routine below that I found on the link that I posted above:
    Code:
    SUBROUTINE PACKIT
    On Entry: 
    
    Register B contain the upper byte of the BCD number. 
    Register C contains the lower byte of the BCD number. 
    
    On Exit: 
    
    Register A contains the Packed BCD Number. 
    
    To pack the number we must: 
    
    A <= (B) 
    Rotate A Left 4 times 
    A <= (A) + (C) 
    
    That's all. We could enhance our routine by checking to see if 
    the first number is zero. If it is, we do not need to rotate
    4 times.
    I have another question for you if you understand Melanie's ConvertBCD: subroutine above, what does the DIG 1 & DIG 0 do? I've seen it used in some of the code posted here on the forum but I don't understand what its function is?

    Oh, and another question, you didn't confirm or not if my last post was a correct way to set the hours registry?

    Thanks
    jessey

  8. #8
    Join Date
    Jan 2006
    Location
    Istanbul
    Posts
    1,185


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by jessey
    ....
    for am 12 hr mode
    writehour = 00100011 = $C4
    readhour = 10100011 = $A3

    for pm 12 hr mode
    readhour = 10101011 = $AB
    writehour = 00101011 = $D4
    hi jessey,

    When reading or writing you change the register address command.

    Reading: $85
    Writing : $84

    If you want to "Write Hour" to the chip, then you first send $84 command and prepare the chip to accept the incoming data for "hour". Then right after that you send your byte variable consisting your "hour data".

    If sending 12Hr AM mode then the Bit7 of your byte should be "0" indicating 12Hr mode, and Bit5 should be "1 " indicating "AM".

    Sending $85 command to the chip will be for "read hour" command and incoming data byte will consist of "hour data". Thus for reading, you do not set a byte to write to the chip; just read the incoming data.

    In this case, I need to correct your arrangement as below.

    12AM hr mode
    writehour = 00100011
    Bit7=0 12hr mode.
    Bit5=1 AM mode.
    Correct

    readhour = 10100011
    No need.
    Just send $85 and read the incoming data.


    12PM hr mode
    writehour = 00101011
    Bit7=0 12hr mode.
    bit5=1 AM mode.
    incorrect

    The correct byte arrangement:
    writehour = 00001011
    Bit7=0 12Hr mode.
    bit5=0 PM mode.


    readhour = 10101011
    No need.
    Just send $85 and read the incoming data.



    Quote Originally Posted by jessey
    I have another question for you if you understand Melanie's ConvertBCD: subroutine above, what does the DIG 1 & DIG 0 do?

    In a byte or word, the digits counts from right to left (just like Arabic).

    Say we have a byte and it is set to Decimal 245.

    ex:
    Test VAR BYTE
    Apple VAR BYTE

    Test = 245

    In this case, from right to left,
    Test Dig 0 is 5
    Test Dig 1 is 4
    Test Dig 2 is 2

    to extract any of these numbers and use in somewhere else, we use;

    Apple = Test Dig 1

    Apple is now 4.



    How is it now ?
    ---------------------
    Last edited by sayzer; - 1st September 2006 at 16:13.
    "If the Earth were a single state, Istanbul would be its capital." Napoleon Bonaparte

  9. #9
    jessey's Avatar
    jessey Guest


    Did you find this post helpful? Yes | No

    Default seems simple enough?

    Hi sayzer,

    I can't seem to figure out what I'm doing wrong here. This is what I did to change to the 12 hour am & pm mode. I got rid of the "writehour CON $84" statement, as its not required anymore. Then I created these four con's and variable below:
    Code:
    Mode VAR BIT
    Mode = 0   ' starts in am mode on initial power-up
    writehour_am    con $C4
    writehour_pm    con $D0
    am CON 0
    pm CON 1
    Then in your setup: subroutine I added this code block to replace your code block for the hourx:
    Code:
            IF Mode = am THEN
              reg_adr = writehour_am   ' = 00100011
            ELSE
              reg_adr = writehour_pm   ' = 00001011
            ENDIF
            outbyte = hourx
            gosub w_out
    Then in your in your sethour: subroutine I added this code block to replace yours for setting the hour:
    Code:
            IF CHNG = 0 THEN        'change the hour on display
                pause 100
                hour = hour + 1
                IF hour > 12 THEN
                  IF Mode = am THEN
                     Mode = pm
                  ELSE
                     Mode = am
                  ENDIF
                     hour = 1
                ENDIF
              call send
            ENDIF
    And that's it. Seems simple enough but it won't work. After I finish setting the clock and return to the start: subroutine it shows two zero's instead of what I set it to. I added these modifications to your original code without my added timer counter routines to rule out any of the other changes I made to your code. I used a converter that is here: http://www.itlocation.com/en/softwar...,download,.htm to calculate the HEX values. Its a great converter because it allows copping and pasting the values into and out of the converter.

    I found a discrepancy in the manual for what you said about bit 7.

    You said:
    Code:
    If sending 12Hr AM mode then the Bit7 of your byte should be 
    "0" indicating 12Hr mode, and Bit5 should be "1 "
    indicating "AM".
    And the manual says:
    Code:
                            AM-PM/12-24 MODE 
    Bit 7 of the hours register is defined as the 12- or 24-hour 
    mode select bit. When high, the 12-hour mode is selected.
    In the 12-hour mode, bit 5 is the AM/PM bit with logic high 
    being PM.
    I changed the bits to what the manual says but still ended up with the same results. I'll keep plugging away at it to see if I can figure it out but in the meantime would you have any ideas on what I've done wrong here?

    Thanks
    jessey

Similar Threads

  1. Real Time Clock
    By in forum mel PIC BASIC Pro
    Replies: 7
    Last Post: - 2nd June 2012, 04:52
  2. real time clock - PCF8583
    By maria in forum mel PIC BASIC Pro
    Replies: 14
    Last Post: - 15th April 2010, 15:41
  3. Real time clock ICs
    By Eng4444 in forum mel PIC BASIC Pro
    Replies: 66
    Last Post: - 20th October 2008, 16:05
  4. Real Time Clock
    By savnik in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 8th December 2006, 02:02
  5. Real time clock... what a headache!
    By Eng4444 in forum mel PIC BASIC
    Replies: 2
    Last Post: - 8th June 2006, 21:56

Members who have read this thread : 5

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