DS1307 returns incorrect data ???
+ Reply to Thread
Results 1 to 18 of 18
  1. #1
    Join Date
    Jul 2008
    Posts
    38

    Default DS1307 returns incorrect data ???

    Hi All,

    I know how much we dread the DS1307 RTC discussions, but here goes.....


    I recently added a DS1307 to my pic16f877 for a data logging project.
    I read a serial string into the pic, and send out some serial data with an added time/date stamped onto it for loggin purposes......fairly simple stuff

    I can write to the RTCC, and READ from the RTCC with good results...

    However, for some reason it displays 90 (yes, NINETY!) seconds before it will increment the minutes.

    Also, when the minutes reaches certain numbers, it will jump by 6 mins on the next increment.
    For example 12:37:00 will change to 12:43:00 when the next 'minute increment' occurs.

    I'm NOT using a 12pf 32,768khz Xtal at the moment, however i thought having the wrong Xtal would only make my clock run slow/fast.

    I can't understand where i get 90secs from?

    I am writing DECIMAL numbers into the RTCC, not hex etc, as i've seen people writing/reading in 2 different number formats which confused them.

    Anyone seen this before?

    Thanx,
    Marty.

  2. #2
    Join Date
    Nov 2005
    Location
    Bombay, India
    Posts
    866

    Default

    I use the DS1307 regularly and do not find this behaviour. Perhaps some interrupt routine is disturbing your RTC read? I am not underestimating your skill, but, maybe something is wrong with the code. It will be better if you can post it here for review.

  3. #3
    Join Date
    Jul 2008
    Posts
    38

    Default

    HI there,

    Here's the basic code i use, which seems to work, except for the odd figures i mentioned initially.

    Code:
    I2CWrite PORTC.2,PORTC.3,$D0,$00,[0, 15, 15, 5, 1, 10, 9, 10]	
    'WRITE time and date e.g. this dsets clock to 15:15:00 (3:15pm) on 
    'Thur (5), 1st (1), October (10), 2009 (9), with control set to "10" for 
    'a 1Hz pulseout on pin7.
    
    'and later i send this to serial port....
    SerOut TX,T9600,["Logged at",#RTCHour,":",#RTCMin,":",#RTCSec,",13,10]

    Obviously later, i remmed out the WRITE line, so the pic doesnt keep restarting at the same time/date.


    Happy to be pointed out any wrong byte(s) i'm using or missing
    Marty.
    Last edited by gtvmarty; - 1st October 2009 at 09:44.

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

    Default

    Bit 7 of the seconds register in a 1307 is the "CH" bit, or "Clock Halt".

    If that bit is set, you could easily get values of 90+ seconds.
    Except that if that bit is set, the clock isn't running.

    On initial power-up of a 1307 the internal registers are "undefined", so you actually have to set that bit to 0 to make sure the clock is running.

    But if it's counting, and you get 90+ .... OMG.
    <br>
    DT

  5. #5
    Join Date
    Feb 2006
    Location
    france
    Posts
    47

    Default corect

    Hello

    Your routine is correct, try this program
    Attached Files Attached Files

  6. #6
    sinoteq's Avatar
    sinoteq Guest

    Default formats

    Hi
    I wounder if you have not done a mistake in how you use the RTC, the DS has a "funny" way to handle numbers. They are BCD coded and can not be written as Decimal numbers straight. You say you write I2CWrite PORTC.2,PORTC.3,$D0,$00,[0, 15, 15, 5, 1, 10, 9, 10]. Please look at the datasheet Timekeeper registers. If you want to load 15 minutes in the DS you have to write it as 21DEC. Why? Becasue the low 4 bits are minutes and the higher 3 bits are 10 minutes so.... Low bits should be 5 (%0101) and then we need 1 ten-minutes thus getting %00010101

    Minutes and Hours are kind of straight forward BCD but the Hours register you have to setup for 12pm or 24 hours function and on top of this use BDC codes.

    You can not use almost any register from the DS without convering it from BCD code and you get 90 and the strange jumps becasue of this.

  7. #7
    Join Date
    Jul 2008
    Posts
    38

    Default

    Thanx everyone for your responses.

    This is my FIRST 1307 project (it just took me several years to get around to trying it out hehehe).

    I knew i was close, just a little more fiddling with certain register values and correct bcd data-handling as you all suggested, and i'll be on track.

    Thanx again for the friendly support
    Marty.

  8. #8
    Join Date
    Feb 2006
    Location
    france
    Posts
    47

    Default ds1307

    Hello

    It works well try this program.
    Attached Images Attached Images  
    Attached Files Attached Files

  9. #9
    Join Date
    Jul 2008
    Posts
    38

    Angry

    Sorry guys, getting confused....
    Can somebody PLEASE simply adjust "MY" few lines of code (up near top of thread) to show me EXACTLY what i should be writing/reading.

    If i write decimal or hex i can't get the right figures.
    Someone tells me i can't write in decimal, someone else says i can, and so on.....

    If i remove the "#" in my SerOut line, i get NOTHING to come out.

    What the ???????

  10. #10
    Join Date
    Jul 2008
    Posts
    38

    Default

    Isn't writing $15(hex) the same as 21(dec)???
    I see some examples are writing a bunch of hex characters...

    I2CWrite PORTC.2,PORTC.3,$D0,$00,[$00, $15, $15, $05, $01, $10, $09, $10]

    Marty.


    Quote Originally Posted by sinoteq View Post
    Hi
    I wounder if you have not done a mistake in how you use the RTC, the DS has a "funny" way to handle numbers. They are BCD coded and can not be written as Decimal numbers straight. You say you write I2CWrite PORTC.2,PORTC.3,$D0,$00,[0, 15, 15, 5, 1, 10, 9, 10]. Please look at the datasheet Timekeeper registers. If you want to load 15 minutes in the DS you have to write it as 21DEC. Why? Becasue the low 4 bits are minutes and the higher 3 bits are 10 minutes so.... Low bits should be 5 (%0101) and then we need 1 ten-minutes thus getting %00010101

    Minutes and Hours are kind of straight forward BCD but the Hours register you have to setup for 12pm or 24 hours function and on top of this use BDC codes.

    You can not use almost any register from the DS without convering it from BCD code and you get 90 and the strange jumps becasue of this.
    Last edited by gtvmarty; - 6th October 2009 at 06:39.

  11. #11
    sinoteq's Avatar
    sinoteq Guest

    Default

    Sure is.
    But in your first example you did not write hex and then you only get 15 and not 21.

    I would start by just writing 0 to all locations and from there start to read the DS once every second. I would do this to make sure I have a routine that can read and display the data in a correct way. When I am sure this is working I would set the DS to a specific time and date and test again.

    You need the # in the serout function to convert the data in the variable to ASCII numbers. Without this you do not get nothing, you get a ASCII character that is difficult to detect but you do get something. Yes unless you send NUL

    Code:
    DS_CHECK:				I2C_Adr_B=0
    						I2CREAD SDA,SCL,RTC,I2C_Adr_B,[Seconds,Minutes,Hours]
    						t=Seconds &%00001111
    						Seconds.7=0
    						n=Seconds>>4
    						Seconds=(n*10)+t
    						I2C_Adr_B=0
    						t=Minutes &%00001111
    						n=Minutes>>4
    						Minutes=(n*10)+t
    						Hours=Hours &%00001111
    						System_Time=(Hours*3600)+(Minutes*60)+Seconds
    						DS_COUNT=0
    						RETURN
    This is one way to use the DS, maybe not the best and most code efficient but it works. Please note that it is only working to 9hours 59min 59 sec because I have not implemented the "10-hour" bit from the Hours register. The equipment I am using will never be on for more than a few hours and I am using the DS as a timer and not as a real RTC. I reset the RTC to 0 at startup.
    Last edited by sinoteq; - 6th October 2009 at 08:12.

  12. #12
    Join Date
    Jul 2008
    Posts
    38

    Default

    Although i'm NOW writing as hex data to keep things clear for me, i think my main problem is Reading/converting the 'read data' into correct decimal form.

    getting closer ;-)
    Marty.

  13. #13
    Join Date
    Nov 2005
    Location
    Bombay, India
    Posts
    866

    Default

    Look at the DS1307 registers and format your data accordingly. The values you write should be packed BCD values wherever needed.

    eg: To write 21 minutes, you need to write HEX 21 to the minutes register. Same for the other registers like hours and seconds. Only make sure to write to the bits needed by the registers.

    I haven't used PBP to write a DS1307 yet, but have some C code that works with the IC. Let me know if you wish to look at it for inspiration

  14. #14
    Join Date
    Nov 2003
    Location
    Wellton, U.S.A.
    Posts
    5,926

    Default

    This is for a DS1337C but it might give you an idea...

    Routine to set the RTC:
    Code:
    <font color="#0000FF"><b>SET_RTC</b></font>:
    <font color="#0000FF"><b>yr </b></font>= <font color="#800000"><b>$09
    </b></font><font color="#0000FF"><b>mon </b></font>= <font color="#800000"><b>$03
    </b></font><font color="#0000FF"><b>date </b></font>= <font color="#800000"><b>$07
    </b></font><font color="#0000FF"><b>sec </b></font>= <font color="#800000"><b>$00
    </b></font><font color="#0000FF"><b>mins </b></font>= <font color="#800000"><b>$28
    </b></font><font color="#0000FF"><b>hr </b></font>= <font color="#800000"><b>$05
    </b></font><font color="#FF0000"><b>I2CWRITE </b></font><font color="#0000FF"><b>DS_SDA</b></font>, <font color="#0000FF"><b>DS_SCL</b></font>, <font color="#0000FF"><b>RTC</b></font>, <font color="#0000FF"><b>SecReg</b></font>, [<font color="#0000FF"><b>sec</b></font>,<font color="#0000FF"><b>mins</b></font>,<font color="#0000FF"><b>hr</b></font>,<font color="#0000FF"><b>day</b></font>,<font color="#0000FF"><b>date</b></font>,<font color="#0000FF"><b>mon</b></font>,<font color="#0000FF"><b>yr</b></font>]
    <font color="#FF0000"><b>RETURN 
    </b></font>
    To READ and send to terminal:
    Code:
    <font color="#FF0000"><b>I2CREAD </b></font><font color="#0000FF"><b>DS_SDA</b></font>, <font color="#0000FF"><b>DS_SCL</b></font>, <font color="#0000FF"><b>RTC</b></font>, <font color="#0000FF"><b>SecReg</b></font>, [<font color="#0000FF"><b>sec</b></font>,<font color="#0000FF"><b>MINs</b></font>,<font color="#0000FF"><b>hr</b></font>,<font color="#0000FF"><b>day</b></font>,<font color="#0000FF"><b>date</b></font>,<font color="#0000FF"><b>mon</b></font>,<font color="#0000FF"><b>yr</b></font>] 
    <font color="#FF0000"><b>PAUSE </b></font><font color="#800000"><b>100
    </b></font><font color="#FF0000"><b>SEROUT2 </b></font><font color="#0000FF"><b>PORTC</b></font>.<font color="#800000"><b>6</b></font>, <font color="#800000"><b>16572</b></font>, [ <b>&quot;TIME &quot;</b>, <font color="#FF0000"><b>HEX2 </b></font><font color="#0000FF"><b>hr</b></font>, <b>&quot;:&quot;</b>, <font color="#FF0000"><b>HEX2 </b></font><font color="#0000FF"><b>MINs</b></font>, <b>&quot;:&quot;</b>,<font color="#FF0000"><b>HEX2 </b></font><font color="#0000FF"><b>sec</b></font>,<font color="#800000"><b>$d</b></font>, <font color="#800000"><b>$a</b></font>]
    <font color="#FF0000"><b>SEROUT2 </b></font><font color="#0000FF"><b>PORTC</b></font>.<font color="#800000"><b>6</b></font>, <font color="#800000"><b>16572</b></font>, [ <b>&quot;DATE &quot;</b>, <font color="#FF0000"><b>HEX2 </b></font><font color="#0000FF"><b>mon</b></font>,<b>&quot;-&quot;</b>,<font color="#FF0000"><b>HEX2 </b></font><font color="#0000FF"><b>date</b></font>,<b>&quot;-&quot;</b>,<font color="#FF0000"><b>HEX2 </b></font><font color="#0000FF"><b>yr</b></font>,<font color="#800000"><b>$d</b></font>, <font color="#800000"><b>$a</b></font>]
    Dave
    Always wear safety glasses while programming.

  15. #15
    Join Date
    Jul 2008
    Posts
    38

    Talking

    Thanx Dave, i'll try that when time allows....

    I tried some things from other examples recently, but anything with a DEC2 or HEX2 command in it wouldn't compile for me.

    I was using PBP 2.40 with Code Designer lite 1.7, although today i've just purchased PBP 2.60 upgrade (& manual) from local supplier.

    Does it sound possible that v2.4 didn't support DEC/HEX commands?

    Anyway, back to it
    Marty.

  16. #16
    Join Date
    Nov 2003
    Location
    Wellton, U.S.A.
    Posts
    5,926

    Default

    Quote Originally Posted by gtvmarty View Post
    Does it sound possible that v2.4 didn't support DEC/HEX commands?
    Not sure, could be.
    Make sure your local supplier does not sell you an old version. Get 2.6.
    Last edited by mackrackit; - 7th October 2009 at 12:01.
    Dave
    Always wear safety glasses while programming.

  17. #17
    Join Date
    Jul 2008
    Posts
    38

    Default

    Yep, it IS v2.6 from Dontronics (Aus).

  18. #18
    Join Date
    Feb 2006
    Location
    france
    Posts
    47

    Default

    hello

    a good example of converting binary bcd
    http://www.picaxeforum.co.uk/showthr...307+Correction

Similar Threads

  1. Using Nokia LCD
    By BobP in forum mel PIC BASIC Pro
    Replies: 300
    Last Post: - 3rd May 2018, 04:47
  2. Read/Write Problem
    By Tobias in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 10th February 2010, 01:51
  3. Big characters on HD44780 4x20
    By erpalma in forum mel PIC BASIC Pro
    Replies: 23
    Last Post: - 7th January 2007, 02:21
  4. LCD + bar graph
    By DynamoBen in forum mel PIC BASIC Pro
    Replies: 13
    Last Post: - 5th October 2005, 14:50
  5. Sinus calculating !
    By Don Mario in forum mel PIC BASIC Pro
    Replies: 29
    Last Post: - 28th November 2004, 23:56

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