DS1337 - 12h to 24h mode


Closed Thread
Results 1 to 31 of 31

Hybrid View

  1. #1
    Join Date
    Sep 2006
    Posts
    747

    Default DS1337 - 12h to 24h mode

    Hello,

    I am trying to convert from 12h mode to 25H mode and vice versa with a Ds1337 chip. Not getting great results for now.

    What I understand Bit 5 and 6 or the hours plays a role in converting. Her is a clip from the datasheet:
    The DS1337 can be run in either 12-hour or 24-hour mode. Bit 6 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. In the 24-hour mode, bit 5 is the second 10-hour bit (20–23 hours).
    I am configuring it so that when two buttons are pressed down for over 1.5 seconds, the conversion between 12h mode to 24h mode is done. From what I understan, I also need to convert the time myself example: from 20H to 8H. The code for me should work but keeps giving me random values, any ideas ?
    thanks

    Code:
            If (PortC.5 = 1) & (PortC.6 = 1) then Pause 1500
    	     If (PortC.5 = 1) & (PortC.6 = 1) then
                'IF hourly mode = AM
                I2CRead SDApin,SCLpin,$D0,$00,[RTCSec,RTCMin,RTCHour]  
                Pause 10
                     if RTCHour.6 = 1 then      'turn into 24h mode   therefore 1 to 11 converts to 13 to 23
                     point =2
                     gosub bcd_to_bin
                     RTCHour = TempVal + 12 
                     RTCHour.6 = 0
                     endif
                     if RTCHour.6 = 0 then      'turn into 12h mode   therefore 13 to 23 converts to 1 to 11
                     point =3
                     gosub bcd_to_bin
                     RTCHour = TempVal - 12 
                     RTCHour.6 = 1
                     endif
                I2cwrite SDApin,SCLpin,$D0,$00,[RTCSec,RTCMin,RTCHour] 
                Pause 100
              endif

  2. #2
    Join Date
    Jan 2013
    Location
    Texas USA
    Posts
    229


    Did you find this post helpful? Yes | No

    Default Re: DS1337 - 12h to 24h mode

    I could see a couple of issues here, but without additional information it's a guess.

    1. You do not post your RTC Initialization routine so how are you setting up the RTC 12 Hour or 24 Hour mode initially?
    1a. After initialization does your project display the correct values before you enter your 12/24 Hour configuration routine?

    Note: If you change the 12/24 hour mode at any time with a write to the RTC, you will need to perform a second write to the RTCHour register (noted in the DS Pg 9 last paragraph under "Clock and Calendar").

    2. I see that you are looking at the 12/24 hour bit, but I do not see that you are checking/setting the AM/PM bit (RTCHour.5). When the RTC is set for 12 hour mode you might want to set this or the calendar values of the RTC will be off.

    3. You did not include your bcd_bin routine so there may be issues there, can't tell without seeing it.
    3a. You seem to be converting from BCD to BIN (gosub bcd_bin) when you read and are wanting to change to the other mode (12/24 Hr) but...
    I do not see where you are converting from BIN back to BCD before you write back to the RTC.
    Unless you are using the "point" variable in a IF/Then or Select Case structure in the bcd_bin routine to select BcdToBin or BinToBcd, again hard to tell without seeing the code. I also assume the TempVar variable holds the converted value from the bcd_bin routine??

    Also, if you are using and LCD or other output device, try and display out the variables at different stages of your configuration routine to make sure that the values are what you expect.

    Perhaps you might want to post more of your code to see if we can help further.

    Just my thoughts.

    Regards,
    Regards,
    TABSoft

  3. #3
    Join Date
    May 2013
    Location
    australia
    Posts
    2,632


    Did you find this post helpful? Yes | No

    Default Re: DS1337 - 12h to 24h mode

    I think your becoming a good case for the snippets are us award
    but if you intention is to just to set/reset 24 h mode on the chip

    Code:
     If(PortC.5 = 1) & (PortC.6 = 1) then Pause 1500
      If (PortC.5 = 1) & (PortC.6 = 1) then
       'IF hourly mode = AM
       I2CRead SDApin,SCLpin,$D0,$02,[RTCHour]  
       RTCHour.6 = ! RTCHour.6
       I2cwrite SDApin,SCLpin,$D0,$02,[RTCHour]     
      endif
     endif

  4. #4
    Join Date
    Sep 2006
    Posts
    747


    Did you find this post helpful? Yes | No

    Default Re: DS1337 - 12h to 24h mode

    Here is the full code.
    The display gosub is not being used, will be used when display in a 7 segment display.
    Now I am using a LCDout command to view my numbers

    Code:
    '////////////////////////////////////////////////////
    '////////////////// PROGRAM /////////////////////////
    '////////////////////////////////////////////////////
     I2CWRITE SDApin,SCLpin,$D0,$00,[$56,$59,$20,$00,$00,$00,$00,$00] ' Write to DS1337 to initialize RTC 
     pause 10
                 I2CRead SDApin,SCLpin,$D0,$00,[RTCSec,RTCMin,RTCHour]     ' bit 5 of hour is 1 for PM  , 0 for AM
                 pause 10
                 'RTCHour.5 = 1      'bit 5 is the AM/PM bit with logic high being PM   AND NOT USED
                ' RTCHour.6 = 0   '24h selected    ' for 12 or 24 h  mode ,  when high 12h mode is selected
                 'in 24 hour mode (=0)  bit 5 is used for hours 20 to 23
                 
                      I2CwRITE SDApin,SCLpin,$D0,$00,[RTCSec,RTCMin,RTCHour]  
                       pause 10
                           
    Mainloop:
            I2CRead SDApin,SCLpin,$D0,$00,[RTCSec,RTCMin,RTCHour]  
            gosub bcd_to_bin 
            
            If (PortC.5 = 1) & (PortC.6 = 1) then Pause 1500
    	     If (PortC.5 = 1) & (PortC.6 = 1) then
                'IF hourly mode = AM
                I2CRead SDApin,SCLpin,$D0,$00,[RTCSec,RTCMin,RTCHour]  
                Pause 10
                     if RTCHour.6 = 1 then      'turn into 24h mode   therefore 1 to 11 converts to 13 to 23
                     point =2
    
                     RTCHour.6 = 0
                     endif
                     if RTCHour.6 = 0 then      'turn into 12h mode   therefore 13 to 23 converts to 1 to 11
                     point =3
                     RTCHour.6 = 1
                     endif
                I2cwrite SDApin,SCLpin,$D0,$00,[RTCSec,RTCMin,RTCHour] 
                Pause 100
              endif
       
            temp1_min = RTCMin_temp1  
            m = temp1_min
            'PortA.0 =1
            'gosub display 
          '  PortA.0 =0
            
            temp2_min = RTCMin_temp2  
            n = temp2_min
          '  PortA.1 =1
          '  gosub display 
         '   PortA.1 =0  
             
            temp1_sec = RTCSec_temp1  
            o = temp1_sec
           ' PortA.2 =1
           ' gosub display 
           ' PortA.2 =0
            
            temp2_sec = RTCSec_temp2  
            p = temp2_sec
           ' PortA.4 =1
            'gosub display 
            'PortA.4 =0
    
            		lcdout $FE,1, "Time: ",  HEX2 RTCHour, ":", hex2 RTCmin ,":", HEX2 RTCsec 'in decimals... wow
    				lcdout $FE,$C0, dec TempVal , " :" ,dec RTCHour_temp1,":", dec RTCHour_temp2,":", dec point
    				Pause 150 
    				
        goto Mainloop
        end
    
        bcd_to_bin:
                      
         if (RTCHour.6 = 1 ) then      'bit 5 is used if RTCHour.6 = 0   ; 24h selected
           RTCHour.6 = 0
           RTCHour.5 = 0
         RTCHour_temp2 = $0F & RTCHour
         RTCHour_temp1= RTCHour >> 4 
         endif
         if (RTCHour.6 = 0 ) then 
         RTCHour_temp2 = %00001111 & RTCHour
         RTCHour_temp1= RTCHour >> 4                        ' Shift down four to read 2 BCD value
         TempVal = RTCHour_temp1 * 10 + RTCHour_temp2       ' this calculate the hours in decima
        
          if ( TempVal > 19 ) then 
                return
            else 
             RTCHour.5 = 0
             RTCHour_temp2 = $0F & RTCHour
             RTCHour_temp1= RTCHour >> 4 
             endif
          endif
             
         RTCMin_temp2 = $0F & RTCMin               ' Clear off the top four bits
         RTCMin_temp1= RTCMin >> 4                       ' Shift down four to read 2 BCD value
         
         RTCSec_temp2 = $0F & RTCSec                     ' Clear off the top four bits
         RTCSec_temp1= RTCSec >> 4                       ' Shift down four to read 2 BCD value
    
        return
    Last edited by lerameur; - 13th January 2015 at 21:28.

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


    Did you find this post helpful? Yes | No

    Default Re: DS1337 - 12h to 24h mode

    this may be better


    Code:
    If(PortC.5 = 1) & (PortC.6 = 1) then Pause 1500
      If (PortC.5 = 1) & (PortC.6 = 1) then
       'IF hourly mode = AM
       I2CRead SDApin,SCLpin,$D0,$02,[RTCHour] 
       temp1 = ((RTCHour>> 4) * 10) + (RTCHour& $0f)    , to binary
    
       if RTCHour.6=1  then   ' 12h mode 
            if RTCHour.5=1then  temp1=temp1+12  ' pm
       
       else  
             if temp1 > 12 then
             temp1=temp1-12
       endif
    
      rtcHOUR = ((temp1 / 10) << 4) + (tEmp1 // 10)  ' to bcd
      
    
     RTCHour.6 = ! RTCHour.6  '  set/reset   24h mode
    
       I2cwrite SDApin,SCLpin,$D0,$02,[RTCHour]     
      endif
     endif
    Last edited by richard; - 13th January 2015 at 22:48. Reason: add som comments

  6. #6
    Join Date
    May 2013
    Location
    australia
    Posts
    2,632


    Did you find this post helpful? Yes | No

    Default Re: DS1337 - 12h to 24h mode

    no I'm still wrong, why is time so complicated

    try

    Code:
    
    
    Code:
    If(PortC.5 = 1) & (PortC.6 = 1) then Pause 1500
      If (PortC.5 = 1) & (PortC.6 = 1) then
       I2CRead SDApin,SCLpin,$D0,$02,[RTCHour]
      if RTCHour.6=1 then
           temp1 = (((RTCHour&$1f)>> 4) * 10) + (RTCHour& $0f)    ' to binary  mask off am/pm
         else
           temp1 = ((RTCHour>> 4) * 10) + (RTCHour& $0f)    ' to binary
         endif
       if RTCHour.6=1  then   ' 12h mode 
            if RTCHour.5=1then  temp1=temp1+12  ' pm
       
       else  
             if temp1 > 12 then
             temp1=temp1-12
       endif
    
      rtcHOUR = ((temp1 / 10) << 4) + (temp1 // 10)  ' to bcd
      
    
     RTCHour.6 = ! RTCHour.6  '  set/reset   24h mode
    
       I2cwrite SDApin,SCLpin,$D0,$02,[RTCHour]     
      endif
     endif

  7. #7
    Join Date
    Sep 2006
    Posts
    747


    Did you find this post helpful? Yes | No

    Default Re: DS1337 - 12h to 24h mode

    Hi Richard,

    Trie both code with same error,
    It does seem to go from 19h to 7h.
    When I attempt to go back to 19H I get a four appearing before the hours .. like so : 7:01:01 to 47:01:01 then the four disappear to come back to 7:01:01 ( yes minutes and seconds are still incrementing. But this is weird that the four would just leave by its own !

    Also the datasheet specifies that the bit 5 hour.5 is used for the hours between the 20 to 23 hours.
    Last edited by lerameur; - 13th January 2015 at 23:18.

  8. #8
    Join Date
    May 2013
    Location
    australia
    Posts
    2,632


    Did you find this post helpful? Yes | No

    Default Re: DS1337 - 12h to 24h mode

    the "4 " probably comes from the fact that temp1 is used in you display routine and its getting messed with by my code

    make a new byte var temp12 for use in the 12/24 routine in place of temp1


    but it should go to 19h not stay at 7h check if the am/pm is getting cleared when 24h mode is selected
    I have to go to work for a while

Similar Threads

  1. 12H format setting for DS1307
    By larzazral in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 23rd February 2010, 07:34
  2. example code for DS1337
    By TONIGALEA in forum mel PIC BASIC Pro
    Replies: 25
    Last Post: - 8th December 2009, 21:16
  3. Code for DS1337
    By Fernanda in forum Off Topic
    Replies: 2
    Last Post: - 21st July 2008, 20:18
  4. PBPro for 16-bit PICs (24F/24H/DSPICs)?
    By Sergeant in forum mel PIC BASIC Pro
    Replies: 14
    Last Post: - 20th April 2007, 20:18
  5. DS1337 Problem
    By snow in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 9th January 2005, 12:17

Members who have read this thread : 1

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