DS1337 - 12h to 24h mode


Results 1 to 31 of 31

Threaded View

  1. #15
    Join Date
    May 2013
    Location
    australia
    Posts
    2,690


    Did you find this post helpful? Yes | No

    Default Re: DS1337 - 12h to 24h mode

    the code by tabsoft works fine with a ds1307 and the double write is not needed , I have not got a 1337 to test

    Code:
    ' pic16f688
     #CONFIG
           __config _INTRC_OSC_NOCLKOUT & _CP_OFF & _WDT_OFF  &  _PWRTE_ON  &  _MCLRE_ON  & _BOD_ON    &  _BOR_ON
     #ENDCONFIG
      
      
    
            DEFINE OSC 8                 
           DEFINE NO_CLRWDT 1
            CRC VAR BYTE   BANK0 SYSTEM    'CRC
            
                SDApin   var PORTC.0                'SDA
                SCLpin   var PORTC.1               'SCL 
               
                tmp var byte
                pkt var byte[11]
     rtcAMPM   var bit   ' Storage flag to indicate AM or PM ; 0=AM, 1=PM
     hourmode  var bit   ' Storage flag for RTC 12/24 hour mode ; 0=24Hr, 1=12Hr
     BCDResult var byte  ' Storage for temporary result for BCD Conversions
     RTCHour    var byte
              osccon=$70    '8 mhz
              ansel=0       'dig i/o
              CMCON0=7        ' compare off 
                     trisc=%100000
      portc=3
      pause 10
    
    
    i2cwrite  sdapin,sclpin,$D0,0,[$0,$14,$21,3,$14,1,$14]  'SANE TIME AND DATE  
    'i2cwrite  sdapin,sclpin,$D0,0,[0]
     pause 3000  
             serout2 porta.0,84, ["ready",13,10]  
    main:
    
      i2cREAD  sdapin,sclpin,$D0,0,[str pkt \3 ]
          RTCHour = pkt[2]
       if  RTCHour.6 then
       serout2 porta.0,84, [hex2 (pkt[2]&$1f),":",hex2 pkt[1],":",hex2 pkt[0]] 
        if   RTCHour.5 then serout2 porta.0,84, [" pm" ]
         serout2 porta.0,84, [13,10] 
       
       else
       serout2 porta.0,84, [hex2 pkt[2],":",hex2 pkt[1],":",hex2 pkt[0],13,10] 
       endif
        pause 5000
      gosub ampm  
       goto main
       
    ampm:
    
       I2CRead SDApin,SCLpin,$D0,$02,[RTCHour]
       hourmode = RTCHour.6
       if hourmode = 1 then          ' Is the RTC set for 12-hour mode
        rtcAMPM = RTCHour.5          ' Assign AM/PM
        RTCHour = RTCHour & %00011111  ' Bits 4-0 = Hours  0-12
       endif
      BCDResult = RTCHour
      
      gosub Bcd2Dec
      
       
      if hourmode = 1 then  '12Hr Convert to 24Hr
        hourmode = 0    ' change to 24Hr
        if rtcAMPM = 1 then 'PM 
            BCDResult = BCDResult + 12  'PM
        endif
      else  '24Hr Convert to 12Hr
        hourmode = 1
        if BCDResult > 12 then 
            BCDResult = BCDResult - 12
            rtcAMPM = 1
        else
            rtcAMPM = 0
        endif
      endif
      gosub Dec2Bcd
      RTCHour = BCDResult
      RTCHour.6 = hourmode
      RTCHour.5 = rtcAMPM 
      I2cwrite SDApin,SCLpin,$D0,$02,[RTCHour]
      'pause 10
      'I2cwrite SDApin,SCLpin,$D0,$02,[RTCHour]  ' Need to write twice because you changed the mode bit
     
     return 
             
      
     Bcd2Dec:
    ' Subroutine to convert BCD (2 hex digits) to Decimal
    ' Make sure to set BCDResult with parameter value before calling this sub  
    ' Result is stored in BCDResult
    
        BCDResult = (((BCDResult >> 4) * 10) + (BCDResult & $0f))
        
        return
        
    Dec2Bcd:
    ' Subroutine to convert Decimal to BCD (2 hex digits)
    ' Make sure to set BCDResult with parameter value before calling this sub  
    ' Result is stored in BCDResult
         
        BCDResult = (((BCDResult / 10) << 4) | (BCDResult // 10))
        
        return
    Last edited by richard; - 14th January 2015 at 06:51.

Similar Threads

  1. 12H format setting for DS1307
    By larzazral in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 23rd February 2010, 08:34
  2. example code for DS1337
    By TONIGALEA in forum mel PIC BASIC Pro
    Replies: 25
    Last Post: - 8th December 2009, 22:16
  3. Code for DS1337
    By Fernanda in forum Off Topic
    Replies: 2
    Last Post: - 21st July 2008, 21: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, 21:18
  5. DS1337 Problem
    By snow in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 9th January 2005, 13: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