Rtc ds 1307


+ Reply to Thread
Results 1 to 3 of 3

Thread: Rtc ds 13071999 days old

  1. #1
    Pic
    Guest Pic's Avatar

    0 Not allowed!

    Default Rtc ds 1307

    Hello all congratulations on the forum.
    I'm trying to run a DS1307 RTC with Pic 18F25K20.
    I can not set it initially the DS1307.
    I see on diplay all numbers 0 and time does not update.
    I ask your help. Thank you very much.
    Under the listing.

    Device 18F25K20
    Xtal 4


    Config_Start
    FOSC = INTIO67 ;Internal oscillator block
    PLLCFG = On ;Oscillator used directly
    PRICLKEN = off ;Primary clock enabled
    FCMEN = OFF ;Fail-Safe Clock Monitor disabled
    IESO = OFF ;Oscillator Switchover mode disabled
    PWRTEN = On ;Power up timer enabled
    BOREN = NOSLP ;Brown-out Reset enabled in hardware only and disabled in Sleep mode (SBOREN is disabled)
    BORV = 250 ;VBOR set to 2.50 V nominal
    WDTEN = OFF ;Watch dog timer is always disabled. SWDTEN has no effect.
    WDTPS = 32768 ;1:32768
    CCP2MX = PORTC1 ;CCP2 input/output is multiplexed with RC1
    PBADEN = On
    CCP3MX = PORTC6 ;P3A/CCP3 input/output is mulitplexed with RC6
    HFOFST = On ;HFINTOSC output and ready status are not delayed by the oscillator stable status
    T3CMX = PORTC0 ;T3CKI is on RC0
    P2BMX = PORTC0 ;P2B is on RC0
    MCLRE = INTMCLR ;RE3 input pin enabled; MCLR disabled
    STVREN = OFF ;Stack full/underflow will not cause Reset
    LVP = OFF ;Single-Supply ICSP disabled
    XINST = OFF ;Instruction set extension and Indexed Addressing mode disabled (Legacy mode)
    Debug = OFF ;Disabled
    Cp0 = On ;Block 0 (000800-001FFFh) code-protected
    CP1 = On ;Block 1 (002000-003FFFh) code-protected
    CP2 = On ;Block 2 (004000-005FFFh) code-protected
    CP3 = On ;Block 3 (006000-007FFFh) code-protected
    CPB = On ;Boot block (000000-0007FFh) code-protected
    CPD = OFF ;Data EEPROM not code-protected
    WRT0 = OFF ;Block 0 (000800-001FFFh) not write-protected
    WRT1 = OFF ;Block 1 (002000-003FFFh) not write-protected
    WRT2 = OFF ;Block 2 (004000-005FFFh) not write-protected
    WRT3 = OFF ;Block 3 (006000-007FFFh) not write-protected
    WRTC = OFF ;Configuration registers (300000-3000FFh) not write-protected
    WRTB = OFF ;Boot Block (000000-0007FFh) not write-protected
    WRTD = OFF ;Data EEPROM not write-protected
    EBTR0 = OFF ;Block 0 (000800-001FFFh) not protected from table reads executed in other blocks
    EBTR1 = OFF ;Block 1 (002000-003FFFh) not protected from table reads executed in other blocks
    EBTR2 = OFF ;Block 2 (004000-005FFFh) not protected from table reads executed in other blocks
    EBTR3 = OFF ;Block 3 (006000-007FFFh) not protected from table reads executed in other blocks
    EBTRB = OFF ;Boot Block (000000-0007FFh) not protected from table reads executed in other blocks
    Config_End
    ;**** End of Fuse Configurator Settings ****


    TRISB=0

    TRISC= %10111000

    Declare LCD_Interface 8
    Declare LCD_DTPin PORTB.0 'collegamento diplay a 8 bit tutta la porta d
    Declare LCD_RSPin PORTC.0
    Declare LCD_RWPin PORTC.1
    Declare LCD_ENPin PORTC.2
    Declare LCD_Lines 2
    PORTC.1=0

    Declare SDA_Pin PORTC.4
    Declare SCL_Pin PORTC.3
    Declare Slow_Bus 0
    Declare bus_scl On '
    Declare Hbus_Bitrate 100 '


    Dim GP_Loop As Byte
    Dim Convert As Byte
    Dim Temp_Convert As Byte



    Dim RTC [8] As Byte

    Dim secondi As RTC#0
    Dim minuti As RTC#1
    Dim ore As RTC#2
    Dim day As RTC#3
    Dim giorno_mese As RTC#4
    Dim mese As RTC#5
    Dim anno As RTC#6
    Dim control As RTC#7




    Symbol trasmissione = %11010000
    Symbol ricezione = %11010001

    secondi=%00000000
    minuti=%00000100
    ore=%00000111
    day=%00000110
    giorno_mese =%00001110
    mese=%00000010
    anno=%00001111
    control=%00000000


    For GP_Loop = 0 To 7
    Convert = RTC[GP_Loop]
    GoSub BIN_BCD
    RTC[GP_Loop] = Convert
    Next GP_Loop
    GoSub Scrittura_RTC

    GoTo main

    '************************************************* ******************************************
    aggiornamento:


    GoSub Lettura_RTC

    For GP_Loop = 0 To 7
    Convert = RTC[GP_Loop]
    GoSub BCD_BIN
    RTC[GP_Loop] = Convert '
    Next GP_Loop
    Return


    '************************************************* ************************************************** *********************
    Lettura_RTC: ' Read 8 bytes from our RTC registers to our aliased array RTC[8]

    HBStart
    HBusIn ricezione,0, [Str RTC]
    HBStop
    Return
    '************************************************* ************************************************** ********************
    Scrittura_RTC: ' Write 8 bytes to our RTC main registers to our aliased array RTC[8]
    HBStart
    HBusOut trasmissione,0,[Str RTC]
    HBStop
    Return
    '************************************************* ************************************************** ************************


    main:

    GoSub aggiornamento

    'ore= ore & %00111111

    Print $fe, 1, Dec2 giorno_mese, "/", Dec2 mese , "/" , Dec2 anno,
    Print At 2,1, " ", Dec2 ore, ":", Dec2 minuti, ":", Dec2 secondi

    DelayMS 500 ' Ritardo di 500 mS
    GoTo main '

    '************************************************* ************************************************** ***************************
    BCD_BIN: ' Enter with value in Convert, return is also in Convert
    '
    Temp_Convert = Convert & $F0 ' take the high nibble BCD Value only by masking the low nibble with a bitwise and
    Swapf Temp_Convert , 1 ' place in the low nibble
    Temp_Convert = Temp_Convert * 10 ' make Temp_Convert = Temp_Convert * 10 , hte 10's digits
    Convert = Convert & $0F ' place the low digit of convert in the low nibble by masking with a bitwise and
    Convert = Convert + Temp_Convert ' the result of the addition of the two is the binary value of the orignal bcd


    Return
    '************************************************* ************************************************** *****************************
    BIN_BCD: ' Convert Binary (Deimal) to BCD
    ' Enter with value in Convert, return is also in Convert
    Temp_Convert = Convert//10 ' Temp_Convert has the remainder of the divide by 10 in the low nibble
    Convert = Convert / 10 ' Convert has the integer value of the divide by 10 in the low nibble
    Swapf Convert,1 ' Swap the high and low nibbles of Temp_Convert and place result back into Temp_Convert
    Convert = Convert ^ Temp_Convert ' Bitwise XOR the two results to form the BCD Value


    Return

  2. Attention

    This valuable resource relies upon the very small amount of revenue generated by displaying online advertisements to our visitors.

    The advertisements we display are relevant to this web site and your browsing history

    Please consider supporting us by disabling your ad blocker.


    Note: Some users have reported issues related to ad-blockers rendering parts of this wesite unusable,
    where possible we will rectify the issues to enable you to use this resource with adblocking enabled.

    If you can, please report issues in the forum area WebSite / Forum Issues


    Thank you for your attention.

  3. #2
    Prolific Poster John Drew's Avatar
    Join Date
    Feb 2002
    Posts
    2,954
    Thumbs Up
    Received: 94
    Given: 34
    Total Downloaded
    5.12 GB

    0 Not allowed!

    Default Re: Rtc ds 1307

    This code comes from somewhere in the WIKI and I think it was originally written by Tim.
    It works very well with a 18F4685. Your code looks similar so you may have obtained it from the same source.
    John


    Code:
    '---------------------------------------------------------------------------------------
    BCD_BIN:                              ' Enter with value in Convert, return is also in Convert            
        Temp_Convert = Convert & $F0      ' take the high nibble BCD Value only by masking the low nibble with a bitwise and
        Swapf Temp_Convert , 1            ' place in the low nibble
        Temp_Convert = Temp_Convert * 10  ' make Temp_Convert = Temp_Convert * 10 , hte 10's digits
        Convert = Convert & $0F           ' place the low digit of convert in the low nibble by masking with a bitwise and
        Convert = Convert + Temp_Convert  ' the result of the addition of the two is the binary value of the orignal bcd
        Return
    
    '---------------------------------------------------------------------------------------               
    BIN_BCD:                              ' Convert Binary (Decimal) to BCD
                                          ' Enter with value in Convert, return is also in Convert
        Temp_Convert = Convert//10        ' Temp_Convert has the remainder of the divide by 10 in the low nibble
        Convert = Convert / 10            ' Convert has the integer value of the divide by 10 in the low nibble
        Swapf Convert,1                   ' Swap the high and low nibbles of Temp_Convert and place result back into Temp_Convert
        Convert = Convert ^ Temp_Convert  ' Bitwise XOR the two results to form the BCD Value   
    Return
    
    '---------------------------------------------------------------------------------------
    Read_time:                            'bring in data one byte at a time
        BusIn RTC_In,ds1307_address,[Convert]
        GoSub BCD_BIN
        Return
    
    '-------------------------------------------------------------------------------------------
    Get_time:
        ds1307_address=0:GoSub Read_time   'read seconds data from 1307
        Second=Convert                     'convert to binary
        ds1307_address=1:GoSub Read_time   'read minute data from 1307
        Minute=Convert                     'convert to binary     
        ds1307_address=2:GoSub Read_time   'read hour data from 1307
        Hour=Convert                       'convert to binary     
        ds1307_address=3:GoSub Read_time   'read day data from 1307
        Day=Convert                        'convert to binary            'day of week
        ds1307_address=4:GoSub Read_time   'read date data from 1307
        Day=Convert                        'convert to binary             'was date 
        ds1307_address=5:GoSub Read_time   'read month data from 1307
        Month=Convert                      'convert to binary
        ds1307_address=6:GoSub Read_time   'read year data from 1307
        Year=Convert + 2000                'convert to binary
        GoSub Timeouts                     'operate the pseudo interrupt timer
        GoSub CalcLocalTime                'update the local time variables
        Return
    '------------------------------------------------------------------------------------------
    SETUPtime:                             'converts decimal time to BCD
        Convert=Second                     'convert the seconds to a bcd equiv
        GoSub BIN_BCD
        TempSec=Convert
        
        Convert=Minute                     'ditto for rest of time/date
        GoSub BIN_BCD
        TempMin=Convert
        
        Convert=Hour
        GoSub BIN_BCD
        TempHr=Convert
        
        Convert=Day                        'program day is really day of month
        GoSub BIN_BCD
        TempDay=Convert
        
        Convert=Month
        GoSub BIN_BCD
        TempMonth=Convert
        
        Convert=Year - 2000
        GoSub BIN_BCD
        'now send the initial time to the RTC
        BusOut RTC_Out,0, [TempSec, TempMin, TempHr, 1, TempDay, TempMonth, Convert,Ctrl]   'convert is Year  -was [Second, Minute, Hour, Day, Date, Month, Year,Ctrl]
        Return
    '------------------------------------------------------------------------------------------    
    
    InitialiseClock:  ' this is our clock initialisation routines, run once at change of time
        DelayMS 200                        'settle time
        '------------- initial start of RTC-oscillator
        BusIn RTC_In,0,[Second]            'read seconds + bit7 (=osc. on/off)
        DelayMS 10
        Clear Second.7                     ' make sure osc. = on
        BusOut RTC_Out,0,[Second]          'write back to RTC
        DelayMS 10
        '-----------------------  ensure 24 hour clock
        BusIn RTC_In,2,[Hour]
        Clear Hour.6                       '24 hr clock
        DelayMS 10
        BusOut RTC_Out,2,[Hour]
        DelayMS 10
        BusOut RTC_Out,7,[Ctrl]            'write the control byte
        DelayMS 10
        GoSub Get_time                     'now update time variables  
        Return
    The Busin/Busout code is set up:
    Code:
    'I2C declares
    Declare SDA_Pin PORTC.0                  'for use in Busout for RTC
    Declare SCL_Pin PORTC.1                  'for use in Busin
    Declare Slow_Bus On                      'slow down the I2C bus as xtal >8MHz
    To use the code:

    In your initialisation you must call Gosub InitialiseClock
    Then
    Gosub SetupTime
    to set the time.

    Gosub Get_Time to get the time
    Last edited by John Drew; 18th February 2015 at 05:49.

  4. Attention

    This valuable resource relies upon the very small amount of revenue generated by displaying online advertisements to our visitors.

    The advertisements we display are relevant to this web site and your browsing history

    Please consider supporting us by disabling your ad blocker.


    Note: Some users have reported issues related to ad-blockers rendering parts of this wesite unusable,
    where possible we will rectify the issues to enable you to use this resource with adblocking enabled.

    If you can, please report issues in the forum area WebSite / Forum Issues


    Thank you for your attention.

  5. #3
    Pic
    Guest Pic's Avatar

    0 Not allowed!

    Default Re: Rtc ds 1307

    Thank you Mr. John Drew responded to me.
    Now the DS1307 works, I have one problem when I insert the battery freezes and I see all 0 on diplay.

  6. Attention

    This valuable resource relies upon the very small amount of revenue generated by displaying online advertisements to our visitors.

    The advertisements we display are relevant to this web site and your browsing history

    Please consider supporting us by disabling your ad blocker.


    Note: Some users have reported issues related to ad-blockers rendering parts of this wesite unusable,
    where possible we will rectify the issues to enable you to use this resource with adblocking enabled.

    If you can, please report issues in the forum area WebSite / Forum Issues


    Thank you for your attention.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

     

Similar Threads

  1. Using the RTC on a 18F65K22
    By SimonJA in forum Proton Plus Compiler v3
    Replies: 5
    Last Post: 10th October 2015, 06:52
  2. PC time to RTC
    By fanie in forum The Lounge
    Replies: 9
    Last Post: 25th August 2012, 14:40
  3. [SOLVED !] What RTC?
    By Ashfaq in forum Proton Plus Compiler v3
    Replies: 6
    Last Post: 18th January 2010, 08:25
  4. Rtc bug ?
    By chuckieboy in forum Proton Plus Compiler v3
    Replies: 2
    Last Post: 17th June 2009, 05:48
  5. RTC Again sorry
    By chuckieboy in forum Proton Plus Compiler v3
    Replies: 6
    Last Post: 21st October 2008, 16:07

Tags for this Thread

Posting Permissions

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