ds1307


Closed Thread
Results 1 to 8 of 8

Thread: ds1307

Hybrid View

  1. #1
    Join Date
    Jun 2006
    Posts
    60

    Default ds1307

    I'm trying to read a ds1307 using code that Bruce wrote but it will only show 00:00:00 for time & date. I'm using real pic simulator. Do I blame it on simulator & go on?

    Code:
    DEFINE OSC 4
    
    ' Note: use 10K pull-up resistors on both SDA and SCL lines.
    ' DS1307 control pins
    SDA Var PORTB.0 ' DS1307 SDA pin #5
    SCL Var PORTB.1 ' DS1307 SCL pin #6
    
    ' Variables
    DB       Var BYTE[8] ' Data byte array
    RTCSec   Var DB[0]   ' alias individual bytes in array
    RTCMin   Var DB[1]
    RTCHour  Var DB[2]
    RTCDay   Var DB[3]
    RTCDate  Var DB[4]
    RTCMonth Var DB[5]
    RTCYear  Var DB[6]
    RTCCtrl  Var DB[7]
    AMPM     Var BIT
    AM       Con 0
    PM       Con 1
    
    ' Port configuration
    TRISB   =   0
    TRISA   =   0
    CMCON   =   %00000111 ' Comparators = off
    
        ' 12-hour mode clock routine for the DS1307 RTC
        GOSUB Write_1307 ' Write time & date on power-up
        
    Main:
        GOSUB Read_1307  ' Read the time & date
        
        ' Now display it
        HSEROUT ["Time: "]
        HSEROUT [DEC((RTCHour>>4)& $01), DEC(RTCHour & $0f),":"]
        HSEROUT [DEC((RTCMin>>4)& $0f), DEC(RTCMin & $0f),":"]
        HSEROUT [DEC((RTCSec>>4)& $0f), DEC(RTCSec & $0f)]
        
        AMPM = RTCHour.0[5] ' Bit 5 of RCTHour indicates AM or PM
        
        IF AMPM = AM THEN        ' If RTCHour.bit5 = 0 it's AM
           HSEROUT [" AM",13,10] ' Print AM
        ELSE                     ' Else it's PM
           HSEROUT [" PM",13,10] ' Print PM
        ENDIF
        
        HSEROUT ["Day: ", DEC(RTCDay & $07),13,10]
        HSEROUT ["Date: ", DEC((RTCMonth>>4)& $01), DEC(RTCMonth & $0f),":"]
        HSEROUT [DEC((RTCDate>>4)& $03), DEC(RTCDate & $0f),":"]
        HSEROUT [DEC(((RTCYear>>4)& $0f)*100),DEC(RTCYear & $0f),13,13,10]
        PAUSE 1000
        GOTO Main
        
    Read_1307: 
        ' Read order is in Secs,Mins,Hours,Day,Date,Month,Year,Control
        I2CRead SDA,SCL,$D0,$00,[STR DB\8]  ' Read string of 8 bytes from DS1307
        RETURN
        
    Write_1307:
        ' $71 in the Hours register would make it 11 PM
        ' $51 in the Hours register would make it 11 AM
        '
        ' Example:
        ' $51 = %0101 0001 <-- 1's position of hour
        '        ||||_________ 10's position of hour. 10+1 = 11 O-clock
        '        |||__________ 1 = PM, 0 = AM (in 12-hour mode) 
        '        ||___________ 1 = 12-hour mode, 0 = 24-hour mode
        '        |____________ ignore this bit
        '
        ' Set time & date to  11:59:00, Day 2, Date:Month:Year 30:08:2007
        I2CWRITE SDA,SCL,$D0,$00,[$00,$59,$51,$02,$30,$08,$27,$90] ' Write to DS1307
        RETURN                  ' Sec Min  Hr Day  D   M   Y  Control
                
        End

  2. #2
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default Re: ds1307

    I don't know anything about that simulator, but that code seems to work fine in the Proteus Simulator with a 16F88.
    What chip are you using?

    DT

  3. #3
    Join Date
    Jun 2006
    Posts
    60


    Did you find this post helpful? Yes | No

    Default Re: ds1307

    I'm using a 16F690. I have never used any rtc's at all so don't know if my simulator will do this. It does have a rtc ds1307 in it though. My simulator is keeping time off the computers time.This is my chip set up:
    Code:
    TRISC   =   %00000000
    TRISB   =   %00000000
    TRISA   =   %000000
    CM1CON0.7 =  0 ' Comparators = off
    CM2CON0.7 =  0 ' Comparators = off
    ANSEL   =   %00000000
    ANSELH  =   %00000000
    Last edited by mel4853; - 6th January 2013 at 22:53.

  4. #4
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default Re: ds1307

    The code specifies PORTB.0 and PORTB.1 as SDA and SCL.
    But the 16F690 doesn't have either RB0 or RB1, it only has RB4-RB7.

    What pins are the DS1307 connected to in your simulation?
    DT

  5. #5
    Join Date
    Jun 2006
    Posts
    60


    Did you find this post helpful? Yes | No

    Default Re: ds1307

    SDA to PORTA.0 & SCL to PORTA.1 sorry forgot to post that also.

  6. #6
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default Re: ds1307

    Using a 16F690 with the changes you posted ... It still works.

    I can't comment on the simulator you are using.
    But I think you can assume the problem is not in Bruce's code.

    DT

Similar Threads

  1. DS1307 RTC example
    By malc-c in forum Code Examples
    Replies: 13
    Last Post: - 12th November 2012, 19:56
  2. ds1307
    By jonas2 in forum Code Examples
    Replies: 0
    Last Post: - 2nd November 2009, 09:50
  3. ds1307 i2c
    By tamertokgoz in forum mel PIC BASIC Pro
    Replies: 6
    Last Post: - 13th June 2008, 15:26
  4. DS1307 help
    By Orlando in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 17th March 2005, 15:17
  5. Ds1307
    By miniman in forum mel PIC BASIC Pro
    Replies: 7
    Last Post: - 2nd February 2005, 08:29

Members who have read this thread : 0

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