Welcome! Log In Create A New Profile

Advanced

DS1302 Real Time Clock, Coding

Posted by kev1953 
DS1302 Real Time Clock, Coding
August 09, 2014 09:07AM
Hi Has anyone written code to R/W to the DS1302 ?
Re: DS1302 Real Time Clock, Coding
August 17, 2014 10:11AM
OK so no ones done it, here is my code, hope this may help someone?
Using PB1 & DS1302 , I/O bits RB6, RB5 & RB14
Load code, run DS1302_init() then DStime() will display Time,Date etc
No timings tested, and code may be naff, but it works so better than nothing

dim p,v,p1,v1
dim h,m,s,dd,mm,yy,wd
dim d$,s$
dim dow$
    dow$ = "   MonTueWedThuFriSatSun"


function DS1302_init()
    io_pinMode(PORTB,6,OUT,WOFF)  // CLK    
    io_pinMode(PORTB,5,OUT,WOFF)  // I/O    
    io_pinMode(PORTB,14,OUT,WOFF)  // CE    
      
    io_write(PORTB, 6, LOW)   // CLK
    io_write(PORTB, 5, LOW)   // I/O
    io_write(PORTB, 14, LOW)   // CE
    print "DS1302 Init"  
endf

function DS1302_out(v1)    
        v = v1 
        dim b   
        for b=0 to 7                                // write byte
                                                    // set up data bit on I/O line        
            p1 = v & 0x01                           // bit 0
            if (p1) = 0; io_write(PORTB, 5, LOW);endif    // I/O  O=Low
            if (p1) = 1; io_write(PORTB, 5, HIGH);endif  // I/O  O=High
            io_write(PORTB, 6, HIGH)                // CLK  High  do clock pulse
            io_write(PORTB, 6, LOW)                 // CLK  Low        
            v = v >> 1                              // move left 1 bit
        next                                        // loop for next bits
                                               
endf

function DS1302_in() 
    io_pinMode(PORTB,5,IN,WPU)             // I/O  Set to INPUT  PU
    v = 0
    dim b 
    for b=0 to 7                            // write date value
        v = v >> 1                          // move right 1 bit                                    
        if (io_read(PORTB, 5)) = 1; v = v + 128; endif     // read data bit on I/O line     
        io_write(PORTB, 6, HIGH)                // CLK HIGH do clock pulse
        io_write(PORTB, 6, LOW)                 // CLK LOW                 
    next 
    io_pinMode(PORTB,5,OUT,WOFF)             // I/O  Set to OUTPUT
    if (v) < 10; d$=d$+"0"; endif
    d$=d$ + hex$(v)
endf

function DS1302_write(port,value)
    io_write(PORTB, 14, HIGH)                    // CE     
    DS1302_out(port)                            // write port value    
    DS1302_out(value)                           // write data value 
    io_write(PORTB, 14, LOW)                     // CE    
endf

function DS1302_read(port)
    io_write(PORTB, 14, HIGH)                    // CE     
    DS1302_out(port)                            // write port value
    DS1302_in()                                 // read data value 
    io_write(PORTB, 14, LOW)                     // CE    
endf

function DStime()
    while comkey?(2) = 0
    d$="Time = "
    DS1302_read(0x85)   // Hour
    h = str2int(hex$(v))
    d$=d$+":" 
    DS1302_read(0x83)   // Minute
    m = str2int(hex$(v))
    d$=d$+":"
    DS1302_read(0x81)   // Second
    s = str2int(hex$(v))
    print d$
    print " - "
    print h*3600+m*60+s // number os seconds that day
           

    d$="  Date = "
    DS1302_read(0x87)
    dd = str2int(hex$(v))
    d$=d$+"/"
    DS1302_read(0x89)
    mm = str2int(hex$(v))
    d$=d$+"/20"
    DS1302_read(0x8D)
    yy = str2int(hex$(v))
    print d$
    print " - "
    print mm*31+dd  // day of year (but each month=31 days)
        
    d$="  Dow = "    
    DS1302_read(0x8b)
    wd = str2int(hex$(v))
    print d$
    print "  "
    s$=mid$(dow$,wd*3,3)
    print s$

    print "\n"
    wait(950)
    wend
endf    

Re: DS1302 Real Time Clock, Coding
August 17, 2014 10:51AM
Thanks,
contribution much appreciated.
Sorry, you do not have permission to post/reply in this forum.