PIC16F887 and DS1307


Closed Thread
Results 1 to 22 of 22
  1. #1
    Join Date
    Jun 2008
    Posts
    84

    Default PIC16F887 and DS1307

    I've been reading all posts here regarding the DS1307 RTC, but I really can't get it to work.
    I just get the value '0' when trying to read it.
    Is soething in my code is wrong ?

    Code:
    	RTCSec var byte		' Seconds
    	RTCMin var byte		' Minutes
    	RTCHour var byte	' Hours
    	RTCWDay var byte	' Weekday
    	RTCDay var byte		' Day
    	RTCMonth var byte	' Months
    	RTCYear var byte	' Year
    	RTCCtrl var byte	' Control 
    	SetTime var byte	' 12/24 Hour Clock
    	SetSec var byte		' Seconds
    	SetMin var byte		' Minutes
    	SetHour var byte	' Hours
    	SetDay var byte		' Day
    	SetMonth var byte	' Months
    	SetYear var byte	' Year
    	TimeOut var word	' Variable for SetUp Menu Time-Out
    RTCSec=1
    RTCMin=4
    
    I2CWRITE PORTC.4,PORTC.3,$D0,$00,[RTCSec,RTCMin,RTCHour,RTCWDay,RTCDay,RTCMonth,RTCYear,RTCCtrl]
    pause 100
    main:
    I2CRead PORTC.4,PORTC.3,$D0,$00,[RTCSec,RTCMin,RTCHour,RTCWDay,RTCDay,RTCMonth,RTCYear,RTCCtrl]
    SEROUT PORTC.6,6,[254,1]
    toggle PORTD.7 : pause 1000
    SEROUT PORTC.6,6,[254,128,#RTCSec]
    pause 1000
    goto main

  2. #2
    skimask's Avatar
    skimask Guest


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by menta View Post
    I've been reading all posts here regarding the DS1307 RTC, but I really can't get it to work.
    I just get the value '0' when trying to read it.
    Is soething in my code is wrong ?
    A quick search on DS1307 will get you a long way. But since your SEARCH function seems to be broken, I'll show you use mine.
    http://rentron.com/PicBasic/SERLED.htm
    No, this code isn't 100% perfect for what you need, but I'll bet ANYTHING that you can easily modify for your purposes.

    And besides, why are you trying to set the same time that you're trying to read?
    If the set time is zero and the read time is zero, then it seems to me that your program, if that is the whole program, is working correctly.

    Got any pullups on the I2C lines?
    Last edited by skimask; - 26th June 2008 at 20:02.

  3. #3
    Join Date
    Jun 2008
    Posts
    84


    Did you find this post helpful? Yes | No

    Default

    Well, i write the time, then I keep reading it, isn't it suppose to be chance every second when I keep reading it ?
    pullups, you mean resistors ? I tried, then I only got 16 or 32 instead of 0

  4. #4
    skimask's Avatar
    skimask Guest


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by menta View Post
    Well, i write the time, then I keep reading it, isn't it suppose to be chance every second when I keep reading it ?
    I don't know if it's supposed to chance every second, but if the clock is running, it may change every second.

    pullups, you mean resistors ? I tried, then I only got 16 or 32 instead of 0
    Complete this sentence:
    The value of my resistors connected to +5v and to the I2C clock and data line is __________."

  5. #5
    Join Date
    Jun 2008
    Posts
    84


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by skimask View Post
    The value of my resistors connected to +5v and to the I2C clock and data line is ____10k____."
    ..........

  6. #6


    Did you find this post helpful? Yes | No

    Default rtc

    control register may need to be set

    alsoooooo....
    the I2c read and write address/control string have to be different, I just used binary 00011110 (just an example) to make sure the read write is correctly coded.

    don
    Last edited by amgen; - 28th June 2008 at 00:07.

  7. #7
    Join Date
    Jun 2008
    Posts
    84


    Did you find this post helpful? Yes | No

    Default

    tried that already. It doesn't help.
    What do you mean by "the I2c read and write address/control string have to be different" ?

  8. #8


    Did you find this post helpful? Yes | No

    Default rtc

    RTCADW CON %11010000 'DS1307 RTC ADDRESS SEND...RECEIVE
    RTCADR CON %11010001

    I2CWRITE SDA,SCL,RTCADW,7 ,%00010011 'TURN ON SQU WAVE OUT 32k HZ

    I2CREAD SDA,SCL,RTCADR,0,[RTC[1],RTC[2],RTC[3],RTC[4],RTC[5],RTC[6],RTC[7]]


    from my rtc stuff
    don

  9. #9
    Join Date
    Jun 2008
    Posts
    84


    Did you find this post helpful? Yes | No

    Default

    Ok, So I tried to use your example.
    Still getting '0' when reading. If i use 10K resistors on the SDA and SCL I get '16'

    Code:
    INCLUDE "modedefs.bas"
        RTC     VAR BYTE[8] ' Data byte array
    
    RTCADW CON %11010000 'DS1307 RTC ADDRESS SEND...RECEIVE
    RTCADR CON %11010001 
    SDA var PORTC.4
    SCL var PORTC.3
    pause 1000
    I2CWRITE SDA,SCL,RTCADW,7,%00010011 'TURN ON SQU WAVE OUT 32k HZ
    
    main:
    ;I2CREAD PORTC.4,PORTC.3,%11010000,$00,[RTCSec]
    I2CREAD SDA,SCL,RTCADR,0,[RTC[1],RTC[2],RTC[3],RTC[4],RTC[5],RTC[6],RTC[7]]
    SEROUT PORTC.6,6,[254,1]
    toggle PORTD.7 : pause 1000
    SEROUT PORTC.6,6,[254,128,#RTC[1]]
    pause 1000
    goto main

  10. #10


    Did you find this post helpful? Yes | No

    Default

    well,
    data on 1307 is hex so.......

    SEROUT2 PORTA.0,6,1,[ "_T" ,HEX RTC[6],"/",HEX2 RTC[5],"/20",_
    HEX2 RTC[7]," , ",HEX RTC[3],":",HEX2 RTC[2],":",HEX2 RTC[1]," ",RTC[9],"M _" ]
    ( use your own port etc.)

    the voltage on SCL & SDA (to gnd) has to be +5 at rest, no signal . Thats what the 10k resisters do.

    also, you can check square wave out at 1 HZ with led/resister on the output pin of 1307 as visual check of rtc.
    I2CWRITE SDA,SCL,RTCADW,7,%00010000 'TURN ON SQU WAVE OUT to...1... HZ
    Last edited by amgen; - 28th June 2008 at 03:10.

  11. #11
    skimask's Avatar
    skimask Guest


    Did you find this post helpful? Yes | No

    Default

    PBP handles the R/W bit for you just like it says in the manual...so...
    Code:
    INCLUDE "modedefs.bas"
    RTC VAR BYTE[8] ' Data byte array
    RTC CON %11010000 'DS1307 RTC ADDRESS SEND...RECEIVE
    SDA var PORTC.4
    SCL var PORTC.3
    pause 1000
    I2CWRITE SDA,SCL,RTC,7,%00010011 'TURN ON SQU WAVE OUT 32k HZ
    main:
    I2CREAD SDA,SCL,RTC,0,[RTC[1],RTC[2],RTC[3],RTC[4],RTC[5],RTC[6],RTC[7]]
    SEROUT PORTC.6,6,[254,1]
    toggle PORTD.7 : pause 1000
    SEROUT PORTC.6,6,[254,128,#RTC[1]]
    pause 1000
    goto main

  12. #12
    Join Date
    Jun 2008
    Posts
    84


    Did you find this post helpful? Yes | No

    Default

    well,
    I can't get any signal on the SWQE output, not by LED or by the logic analyzer of the PICkit2. So I might guess the 1307 is malfunctioning.
    This is what I get when analyzing the SCL vs SDA

    This is what I get when analyzing the SCL vs SWQ

    This is what I get when analyzing the SCl vs SDA
    Last edited by menta; - 28th June 2008 at 11:15.

  13. #13
    Join Date
    Jul 2003
    Posts
    2,405


    Did you find this post helpful? Yes | No

    Default

    Here are a few things that 'may' help;

    1. If you're using an oscillator >8MHz, then you'll want to include DEFINE I2C_SLOW 1
    since the 1307 max clock speed is 100kHz.

    2. The SQW output is an open-drain type. I.E. it cannot output a logic 1. Use a 10K
    pull-up on the SQW output. Then you should see a square wave output on your scope.

    3. Use the external pull-ups on SDA and SCL.

    4. Connect a 3V battery to the 1307 Vbat input.
    Regards,

    -Bruce
    tech at rentron.com
    http://www.rentron.com

  14. #14
    Join Date
    Jun 2008
    Posts
    84


    Did you find this post helpful? Yes | No

    Default

    non of the above really helps.
    Probably the 1307 is dead.

  15. #15
    Join Date
    Jun 2008
    Posts
    84


    Did you find this post helpful? Yes | No

    Default

    I replaced the DS1307 with VS1307 which seems to work.
    Is there any other way to convert the data to dec other than:

    I2CRead SDA, SCL, $D0, $00, [A]
    Sec1=(A.0*1)+(A.1*2)+(A.2*4)+(A.3*8)
    Sec10=(A.4*1)+(A.5*2)+(A.6*4)
    Last edited by menta; - 5th July 2008 at 09:21.

  16. #16
    Join Date
    Jun 2008
    Posts
    84


    Did you find this post helpful? Yes | No

    Default

    Now I need to understand how to convert from decimal (which comes from the user setup) to write to the RTC chip.
    Last edited by menta; - 5th July 2008 at 22:11.

  17. #17
    Join Date
    Jun 2008
    Posts
    84


    Did you find this post helpful? Yes | No

    Default

    Found it
    ConvertBCD:
    CounterB=CounterA DIG 1
    CounterB=CounterB<<4
    CounterB=CounterB+CounterA DIG 0
    Return

  18. #18
    skimask's Avatar
    skimask Guest


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by menta View Post
    Found it
    What happens if CounterA = $EC?

  19. #19
    Join Date
    Jul 2003
    Posts
    2,358


    Did you find this post helpful? Yes | No

    Default

    We're talking about a DS1307 RTC here (and ignoring the MSB of certain Registers) where the only valid individual digits are in the range 0-9. The subroutine converts two digit DECIMAL in the range 0-99 to a single BCD Byte. $EC is not a valid conversion requirement for this application.

  20. #20
    skimask's Avatar
    skimask Guest


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by Melanie View Post
    $EC is not a valid conversion requirement for this application.
    My point was that there isn't any sort of error checking, flipped bits, logic errors, and so on...

  21. #21
    Join Date
    Sep 2008
    Posts
    7


    Did you find this post helpful? Yes | No

    Default

    Hi!!!
    I just wan't to know if its possible how i can set manualy the initial time of the clock using a bic basic code!i tried to do it but i found some problems due to bcd and hex code!
    so there someone that do it?!?!

    thank you so much

    bye bye Cris

  22. #22
    skimask's Avatar
    skimask Guest


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by crisxstyle View Post
    Hi!!!
    I just wan't to know if its possible how i can set manualy the initial time of the clock using a bic basic code!i tried to do it but i found some problems due to bcd and hex code!
    so there someone that do it?!?!
    96 hits on these forums alone for ds1307...
    Surely there's something in one of those threads to help you out...

Similar Threads

  1. Pic16f887 vs ds1307
    By larzazral in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 30th January 2010, 04:12

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