Real Time Clock 1302


Closed Thread
Results 1 to 5 of 5
  1. #1
    Join Date
    Feb 2012
    Location
    PERTH AUSTRALIA
    Posts
    838

    Default Real Time Clock 1302

    Hi guys

    Found this example on the site but could not find how the tris bits are set for for the data direction from out to in the code , is it required , if not how did the data pin change direction for the read ( shiftin)

    cheers

    sheldon

    Code:
    ' An example routines to use the DS1302 timer chip
    ' based on code provided by Omer Yildiz, Copyright (c) 2006 [2005] 
    ' http://www.picbasic.co.uk/forum/showthread.php?t=423
    ' This code uses a supercap at VCC1 to provide power when VCC2 is interupted
    ' Set initial time manually in clockinit
    ' Then reprogramme with convertTime and setClock commented out 
    
    DEFINE LOADER_USED 1    'enable bootloader
    DEFINE osc 20
    
    'defines for debug
    INCLUDE "modedefs.bas"
    
    'set-up debug serial
    DEFINE DEBUG_REG PORTB      ' Set Debug pin port
    DEFINE DEBUG_BIT 6          ' Set Debug pin bit
    DEFINE DEBUG_BAUD 9600      ' Set Debug baud rate
    DEFINE DEBUG_MODE 1         ' Set Debug mode: 0 = true, 1 = inverted
    
    LED             VAR PORTC.5
    
    'clock pin definitions--------------------------------------------
    timer_clk     var portb.3 ' DS1302 Clock Pin
    timer_dq      var portb.2 ' DS1302 Data Pin
    timer_rst     var portb.1 ' DS1302 Reset Pin
    
    'decimal variables for setting up time---------------
    second  var byte
    minute  var byte
    hour    var byte
    day     var byte
    month   var byte
    year    var byte
    
    'hex variables for actual time display--------------
    secondx     Var byte
    minutex     var byte
    hourx       var byte
    dayx        var byte
    monthx      var byte
    yearx       var byte
    RTC         var byte
    
    'Write Commands For DS1302 --------------------------
    writectrl       con $8E ' Control byte
    writechrg       CON $91 ' Write trickle charge
    tricklereg      CON $A0 ' Enable trickle charge for supercap
    writeram        con $80 ' Write to RAM
    writeprotect    con $00 ' Write-protect DS1302
    writesec        con $80 ' Write seconds
    writemin        con $82 ' Write minutes
    writehour       con $84 ' Write hour
    writedate       con $86 ' Write date
    writemonth      con $88 ' Write month
    writeyear       con $8C ' Write year
    
    'Read Commands For DS1302 -------------------------
    readsec         con $81 ' Read seconds from DS1302
    readmin         con $83 ' Read minutes from DS1302
    readhour        con $85 ' Read hours from DS1302
    readdate        con $87 ' Read date from DS1302
    readyear        con $8D ' Read year from DS1302
    readmonth       con $89 ' Read month from DS1302
    
    'Time Variables ------------------------------
    mem         var byte ' Temporary data holder
    outbyte     var byte ' Second byte to ds1302
    reg_adr     var byte ' First byte to DS1302
    decx        var byte ' A swap variable
    
    
    HIGH LED
    PAUSE 1000
    LOW LED
    
    
    'Initial Settings For Ports ------------------------
    clockInit:
        
        DEBUG "DS1302 timer with trickle charge",10,13
        
        ' set intial time and date here
        second = 20
        minute = 14
        hour = 12
        day = 10
        month = 9
        year = 7
    
        low timer_rst ' Set reset pin low
        low timer_clk ' Set clock pin low
        
        ' This enables the trickle charger
        GOSUB setTrickleCharge
        
        ' These two should be commented out after initialising the PIC
        ' with the time date above
        'GOSUB convertTime
        'GOSUB setClock
     
     GOTO loop
    
    
    'Read time at 5 secind intervals-------------------------
    loop:
            call readclk            ' go read DS1302
            pause 5000
    goto loop
    
    
    'Time Commands Subroutines --------------------------
    readclk:
    
            reg_adr = readsec   'Read seconds
            gosub timerRead
            second = mem
            
            reg_adr = readmin   'Read minutes
            gosub timerRead
            minutex = mem
            
            reg_adr = readhour  'Read Hours
            gosub timerRead
            hourx = mem
            
            reg_adr = readyear  'Read Year
            gosub timerRead
            yearx = mem
            
            reg_adr = readdate  'Read Date
            gosub timerRead
            dayx = mem
            
            reg_adr = readmonth 'Read Month
            gosub timerRead
            monthx = mem
            
            DEBUG HEX hourx,":",HEX minutex,":",HEX second," ",HEX dayx,"/",HEX monthx,"/",HEX yearx,10,13
    
    return
    
    setTrickleCharge:
    
            reg_adr = writectrl         ' Set to control byte 
            outbyte = writeprotect      ' Set turn off protection
            gosub timerWrite            ' Send both bytes
            
            reg_adr = writechrg
            outbyte = tricklereg
            gosub timerWrite
            
            reg_adr = writectrl
            outbyte = writeprotect
            gosub timerWrite
    RETURN
    
       
    setClock:
            reg_adr = writectrl         ' Set to control byte 
            outbyte = writeprotect      ' Set turn off protection
            gosub timerWrite            ' Send both bytes
            
            reg_adr = writesec          ' Set to write seconds register
            outbyte = secondx           ' Set to write 00 to seconds register in HEX format
            gosub timerWrite
            
            reg_adr = writemin
            outbyte = minutex            'Our minutex and others with x (except for monthx) are now in HEX format.
            gosub timerWrite
            
            reg_adr = writehour
            outbyte = hourx
            gosub timerWrite
            
            reg_adr = writedate
            outbyte = dayx
            gosub timerWrite
            
            reg_adr = writemonth
            outbyte = monthx
            gosub timerWrite
            
            reg_adr = writeyear
            outbyte = yearx
            gosub timerWrite
            
            reg_adr = writectrl
            outbyte = writeprotect
            gosub timerWrite
    RETURN
    
    timerRead:
            mem = reg_adr                           ' Set mem variable to reg_adr contents
            high timer_rst                          ' Activate the DS1302
            shiftout timer_dq,timer_clk,0, [mem]    ' Send control byte
            shiftin timer_dq,timer_clk,1, [mem]     ' Retrieve data in from the DS1302
            low timer_rst                           ' Deactivate DS1302
    return 
    
    timerWrite:
            mem = reg_adr                           ' Set mem variable to reg_adr contents
            high timer_rst                          ' Activate the DS1302
            shiftout timer_dq,timer_clk,0, [mem]    ' Send control byte
            mem = outbyte                           ' Set mem variable to outbyte contents
            shiftout timer_dq,timer_clk,0, [mem]    ' Send data stored in mem variable to DS1302
            low timer_rst                           ' Deactivate DS1302
    return
    
    
    'Convert initial set up times to hex---------------------------------
        
    convertTime:
    
            pause 100
            call chkLeap
            pause 20
            decx=year          
            gosub convertion
            yearx=rtc
           
            pause 100
            decx=month
            gosub convertion
            monthx=rtc
            
            pause 100
            decx=day
            gosub convertion
            dayx=rtc            
            
            pause 100
            decx=hour
            gosub convertion
            hourx=rtc
            
            pause 100
            decx=minute
            gosub convertion
            minutex=rtc            
            
            pause 100
            decx=second
            gosub convertion
            secondx=rtc
                        
    RETURN
                 
    
    'Convert DEC to HEX
    convertion:
    
    	RTC=decx DIG 1
    	RTC=RTC<<4
    	RTC=RTC+decx DIG 0
    
    return
    
    
    'Check for leap year
    chkLeap:  
    
        if year=8 or year=12 or year=16	or year=20 Then 
            write 2,29              'Years with Feb=29 days (for 50 yrs.)
            pause 20
        else
            write 2, 28   
            pause 20
        endif
      
    return
    
    
    END

  2. #2
    Join Date
    Feb 2010
    Location
    USA, New England
    Posts
    164


    Did you find this post helpful? Yes | No

    Default Re: Real Time Clock 1302

    Hello Sheldon,

    SHIFTIN and SHIFTOUT take care of setting the data directions on their own; PBP takes care of it. I checked some code where I have used these commands and (for no particular reason) initialized both the data and clock pins as outputs.

    Best Regards,
    Paul
    The way to avoid mistakes is to gain experience. The way to gain experience is to make mistakes.

  3. #3
    Join Date
    Feb 2012
    Location
    PERTH AUSTRALIA
    Posts
    838


    Did you find this post helpful? Yes | No

    Default Re: Real Time Clock 1302

    ah cool , i thought as much , but i looked for such detail in the PB3 help file for shiftin , shiftout but there is no mention of that fact , that i could see , maybe in the manual somewhere

  4. #4
    Join Date
    Jan 2005
    Location
    Montreal, Quebec, Canada
    Posts
    2,597


    Did you find this post helpful? Yes | No

    Default Re: Real Time Clock 1302

    Couldn't see anything in the manual for SHIFTIN.

    (PBP 3.0 rev. Nov 28 2011)

    Robert
    Last edited by Demon; - 30th May 2013 at 11:31.
    My Creality Ender 3 S1 Plus is a giant paperweight that can't even be used as a boat anchor, cause I'd be fined for polluting our waterways with electronic devices.

    Not as dumb as yesterday, but stupider than tomorrow!

  5. #5
    Join Date
    Feb 2012
    Location
    PERTH AUSTRALIA
    Posts
    838


    Did you find this post helpful? Yes | No

    Default Re: Real Time Clock 1302

    thanks robert ,

    i thought i look hard and long at the online help in PB3 for the info before i posted , its clear its been a feature for some time of shiftin , shiftout , but not mentioned ,
    up until now i have only had pins for shiftin/ out that were defined for in or out not one pin that was used for both

Similar Threads

  1. real time clock
    By maria in forum Code Examples
    Replies: 44
    Last Post: - 1st March 2022, 12:13
  2. Real Time Clock
    By in forum mel PIC BASIC Pro
    Replies: 7
    Last Post: - 2nd June 2012, 04:52
  3. Real time clock
    By PlantBob in forum mel PIC BASIC Pro
    Replies: 8
    Last Post: - 10th February 2011, 13:01
  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 : 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