Shop OBEX P1 Docs P2 Docs Learn Events
Help with DS1307 and BS2p — Parallax Forums

Help with DS1307 and BS2p

fiveslofiveslo Posts: 40
edited 2005-05-01 02:37 in BASIC Stamp
I've been successful in "hacking" various downloads from the internet to making my BS2p talk with the DS1307... However now that I'm in the final stages of designing my ON/OFF timer with the '1307, I've found that my "clock" is acting really weird.· By this I mean that for instance if I set a date of say 04/27/2005, the clock/program come up correctly.. however when the time reaches 23:59:59 and then cycles to the so called next day.... the clock continues normally... however the date now becomes 04/20/2005.... this weird circumstance occurs no matter what month I set!!!! This also happens on later days of the month... such as 25 becomes 32, 29 becomes 24, and other wild and crazy numbers!!!· Also when I put in 02/28/2005 and the clock cycles past midnight, guess what I get 02/29/2005... The data sheet for the DS1307 states that its leap year compliant... what gives???· I believe I'm not properly "clearing" the registers of the DS1307 when I write to them after I input my new clock settings ie: new time, new date, etc... Is there a simple way of clearing all the registers prior to writing to them??· My snippet for reading the DS1307 is below... Can anyone please help

Main:
··· GOSUB Get_Time····························· 'get current time
··· GOSUB Read_MemKey_Offtime_Presets·········· 'read MemKey Offtime Presets
··· IF secs=80 THEN Clock_Not_Set
··· SEROUT LCD, N9600, [noparse][[/noparse]PosCmd, 64, DEC2 month, "/", DEC2 day, "/20", DEC2 year]
··· SEROUT LCD, N9600, [noparse][[/noparse]PosCmd, 76,DEC2 hrs,":", DEC2 mins, ":", DEC2 secs]


the Get_Time subroutine is duplicated in multiple banks....


....... Another Bank to Set Date......

Get_Time:
· I2CIN SDA, Rd1307, 0, [noparse][[/noparse]secs, mins, hrs,date,day,month,year]····· ' read time registers
· secs = secs.NIB1 * 10 + secs.NIB0········································ ' BCD to decimal
· mins = mins.NIB1 * 10 + mins.NIB0
· hrs = hrs.NIB1 * 10 + hrs.NIB0
· RETURN

Set_DS1307:
· secs = (secs / 10 << 4) + (secs // 10)···································· 'decimal to BCD
· mins = (mins / 10 << 4) + (mins // 10)
· hrs· = (hrs / 10 << 4) + (hrs // 10)
··I2COUT SDA, Wr1307, 0, [noparse][[/noparse]secs,mins,hrs,date,day,month,year]····· 'update time registers
· RETURN

Please help anyone..... Rob

Comments

  • Tracy AllenTracy Allen Posts: 6,658
    edited 2005-04-29 07:32
    Hi Rob,

    It looks like the program also needs to convert the date from BCD to decimal, just like it does with the time.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tracy Allen
    www.emesystems.com
  • NewzedNewzed Posts: 2,503
    edited 2005-04-29 11:53
    You know you can use HEX2 numbers with the 1307.· Much simpler - you don't have to worry about DEC to BCD conversion and vice versa.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Sid Weaver
    NEW! 4 MB EEPROM

    http://hometown.aol.com/newzed/page4.html
    ·
  • Jon WilliamsJon Williams Posts: 6,491
    edited 2005-04-29 12:37
    HEX2 does not convert values to BCD, it converts values to TEXT in hexadecimal format (using 2 characters).· The HEX2 modifier is only useful when sending values to text devices like LCDs and the Debug Terminal.

    I've attached a simple DS1307 demo that shows the conversions to and from BCD.· See the Set_Time (decimal to BCD) and Get_Time (BCD to decimal) routines.


    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax
    Dallas, TX· USA
  • NewzedNewzed Posts: 2,503
    edited 2005-04-29 13:19
    HEX2 format works quite well with the DS1307, just as it does with the DS1302.· I have attached a sample program for my 1307, running on a BS2PE.· It works equally well with a BS2P24.· Please note that the program uses Pins 8 and 9 for SCA/SCL, but these can be changed to Pins 0 and 1.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Sid Weaver
    NEW! 4 MB EEPROM

    http://hometown.aol.com/newzed/page4.html
  • Jon WilliamsJon Williams Posts: 6,491
    edited 2005-04-29 13:28
    The program you posted is using HEX2 as a text to numeric conversion -- proving my point that HEX2 is only useful with text (converting from text, or two text). But in operational programs, going number to number, it does not work. The code that I posted, and as Tracy also alluded to, uses a bit of simple math to convert a decimal number to a BCD number, and vice-versa.

    Decimal to BCD:

    · bcdNum = (decNum / 10 << 4) + (decNum // 10)


    BCD to Decimal:

    · decNum = (bcdNum.NIB1 * 10) + bcdNum.NIB0

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax
    Dallas, TX· USA


    Post Edited (Jon Williams) : 4/29/2005 1:31:53 PM GMT
  • NewzedNewzed Posts: 2,503
    edited 2005-04-29 13:43
    Jon, could you explain what you mean by "operational programs, going
    number to number".· My program works with the DS1307 and that was the object.· Am I missing something?

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Sid Weaver
    NEW! 4 MB EEPROM

    http://hometown.aol.com/newzed/page4.html
    ·
  • Larry~Larry~ Posts: 242
    edited 2005-04-29 14:02
    The ds1302 uses what the data sheet calls Modified BCD while it works with this chip using the hex2 modifier may\will not work with other BCD programs or chips. Jon writes programs for learning and if he shows this for the 1302 then someone will surly use it in another program for which it will not work.

    I also use the hex2 modifier in the input stage
  • Jon WilliamsJon Williams Posts: 6,491
    edited 2005-04-29 14:59
    Let me try to clarify again: HEX2 is useful for converting BCD text input (as from DEBUGIN) to a value, and for sending a value to a text device (Debug Terminal, serial LCD, etc.). Many programs, however, will need to do direct exchange and HEX2 is NOT useful in the case. The coversion routines shown above will work.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax
    Dallas, TX· USA
  • Jon WilliamsJon Williams Posts: 6,491
    edited 2005-04-29 15:02
    Yes, Sid, what you're missing is that your program is going from text (DEBUGIN) to a number.· If, for example, one was using only pushbuttons and a multiplexed LED display, there would be no place for text, hence HEX2 is useless in this application.· In that case -- as is most of the time -- you need to use BCD-to-Decimal and Decimal-to-BCD routines as shown in my examples.
    Newzed said...
    Jon, could you explain what you mean by "operational programs, going
    number to number".· My program works with the DS1307 and that was the object.· Am I missing something?

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax
    Dallas, TX· USA
  • fiveslofiveslo Posts: 40
    edited 2005-04-30 01:21
    Thanks for the replies to Tracy and Jon!· I'll try adding the lines that both of you suggested, it sounds like it should work...

    · bcdNum = (decNum / 10 << 4) + (decNum // 10)

    · decNum = (bcdNum.NIB1 * 10) + bcdNum.NIB0

    In any case, I've added the conversions to the day, date, month and year registers for both the read and write subroutines for my BS2P program, later tonight or should I say earlier tomorrow morning I'll download and try the newly updated program... I'll try to post my results to this almost finished project.... Thanks again.. Rob
  • fiveslofiveslo Posts: 40
    edited 2005-04-30 06:38
    IT WORKS!!!!hop.gif

    I express my gratitude to all who have replied to my posting·and giving the solution to my problem.·Especially·Tracy and Jon...··After adding the conversions (ie: bcdNum or decNum) to the date, day, month, year variables in all the areas that either read or write to the DS1307.· This project, in case anyone is interested, is a program that I have written which will look at the current day, and then lookup the current day in a table to find its local area sunset time, which will be the turn on time for timer one.· The program will then add ten minutes to the current days sunset time, this will be the turn on time for timer two.· All "data" will be input to the BS2p via a 3x4 keypad that is interfaced thru a MemKey module, that I've purchased so long ago, and completely lost track of why I originally purchased it, figured this would be the time to use it.·· All information so far is being viewd on a 4x20 backlighted serial LCD, which will most likely end up in the final project...· Also turn off times are fully programmable and I suppose if I wanted I could also add multiple turn on/off times.... The only reason the whole project came about was the·fact that I became very sick of constantly adjusting and readjusting the mechanical timers for outside lighting around my house, and figured this was the way to·go after "playing" with the BS2 for so many years...· Once again thank you for all your input and your answers to my problem... Rob
  • Tracy AllenTracy Allen Posts: 6,658
    edited 2005-05-01 02:01
    Congratulations, that is good to hear that it works!

    Some of my customers are studying bats, and the monitoring equipment has to run from a little before sunset to a little after sunrise. The Stamp controls the power, so it has to have those local times entered for each location. We got tired of coding in the data tables. I found a superb Excel spreadsheet online (users.vei.net/pelican/sunrise.html by Greg Pelletier) that has set of VB macros for all that, so I added a worksheet that allows us to enter a location and a base date, and the spreadsheet generates all the Stamp DATA statements, as well as a BS2pe slot program to return the sunrise and sunset time for any given date 9 years into the future for that location. Maybe I'll put that on my web site, if others could make use of it.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tracy Allen
    www.emesystems.com
  • fiveslofiveslo Posts: 40
    edited 2005-05-01 02:37
    Tracy:
    Could you please post the worksheet that you've added to the Excel sheet you recommeded in your last email.· I'd very much be interested in this, since I was actually considering doing exactly as you described... rewriting all the sunset data for each year, that·is a "flaw" that I see in this whole project, that will consume alot of time to recreate each and every year, for each and every location.... If this is as you describe... it would be of great help ... Thank you... Rob
    ·
Sign In or Register to comment.