Shop OBEX P1 Docs P2 Docs Learn Events
Yet another ds1302 question — Parallax Forums

Yet another ds1302 question

press9761press9761 Posts: 6
edited 2005-12-15 21:58 in BASIC Stamp
I need a timer so, I'm using a ds1302. I downloaded the code posted in the forums ( the big one made by a parallax employee) and disassembled it to the smallest form possible. The original code used something like 96% of the memory on the bs2. The code I have uses 4%. However I would like to get it a bit smaller and gain some understanding of how the ds1302 /bs2 interact. So, first off, here is the code I am using. It works quite well as a timer.



************************************************
' {$STAMP BS2}
' {$PBASIC 2.5}
'
[noparse][[/noparse] I/O Definitions ]

DataIO PIN 1 ' DS1302.6
Clock PIN 0 ' DS1302.7
rst PIN 2 ' DS1302.5
'
[noparse][[/noparse] Constants ]
WrBurst CON %10111110 ' Write Burst Of Data
RdBurst CON %10111111 ' Read Burst Of Data
'
[noparse][[/noparse] Variables ]

secs VAR Byte ' Seconds
mins VAR Byte ' Minutes
hrs VAR Byte ' Hours

'
[noparse][[/noparse] Program Code ]
DO
HIGH rst
SHIFTOUT DataIO, Clock, LSBFIRST, [noparse][[/noparse]wrburst]
SHIFTOUT DataIO, Clock, LSBFIRST , [noparse][[/noparse]secs, mins, hrs,0,0,0,0,0]
LOW rst

DO
HIGH rst
SHIFTOUT DataIO, Clock, LSBFIRST, [noparse][[/noparse]RdBurst]
SHIFTIN DataIO, Clock, LSBPRE, [noparse][[/noparse]secs, mins,hrs]
LOW rst


DEBUG HOME
DEBUG HEX2 hrs, ":", HEX2 mins, ":", HEX2 secs
LOOP
LOOP
**********************************************
I changed the write/read burst from hex to binary so I could see what was going on. I removed all the variables I could. My problem is with the date, month, day, year component. If I remove these peaces the clock stops working properly. I found that if I removed those variables and put 0's in the second shift out line ( to keep the space) the clock works fine.

My question is what is going on with that? why cant I just have the sec/min/hour.
I am having trouble making changes to the burst mode data. I have looked at the data sheet and anytime I change the binary I have a clock failure. any suggestions.

I'm just trying to make things as simple as possible

JP

Comments

  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2005-12-15 15:13
    Hello,

    ·· I think the code you are referring to was one I posted.· The reason it took up so much space is that it was designed to demonstrate use of most of the functions of the DS1302 except the trickle-charger, and it required a user interface to do this.· That takes up space.
    ·· Bottom line is that I have used the DS1302 in well over a dozen projects and it takes up very little space in the program, few variables and only one extra I/O line to implement.· Anyway, on to your problem...
    ·· The problem is you're using BURST mode, which expects all the variables in one transmission.· If you only want to read the hours, mins and secs, you will need to do it one register at a time, and pad the remaining input with a NUL variable.· For example, you could change your SHIFTING to read:
    SHIFTIN DataIO, Clock, LSBPRE, [noparse][[/noparse]secs, mins,hrs, tmp, tmp, tmp, tmp]
    

    Where tmp is a byte variable that you will just throw away.· Otherwise you will need to read the individual registers and not use burst mode.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Chris Savage
    Parallax Tech Support
    csavage@parallax.com
  • NewzedNewzed Posts: 2,503
    edited 2005-12-15 16:27
    JP, I've been playing around with your code and I came up with this.· Run it and see it it helps you any.



    '
    [noparse][[/noparse] Constants ]

    '
    [noparse][[/noparse] Variables ]

    secs······ VAR···· Byte··········· ' Seconds
    mins······ VAR···· Byte··········· ' Minutes
    hrs······· VAR···· Byte··········· ' Hours


    '
    [noparse][[/noparse] Program Code ]
    init:
    secs = $00
    mins = -1
    hrs = 0

    DO
    HIGH rst
    SHIFTOUT DataIO, Clock, LSBFIRST, [noparse][[/noparse]128]
    SHIFTOUT DataIO, Clock, LSBFIRST , [noparse][[/noparse]secs]
    LOW rst

    DO

    HIGH rst
    SHIFTOUT DataIO, Clock, LSBFIRST, [noparse][[/noparse]129]
    SHIFTIN DataIO, Clock, LSBPRE, [noparse][[/noparse]secs]
    LOW rst

    IF secs=$00 THEN mins = mins + 1
    hrs = mins/60
    DEBUG home,·· DEC hrs, ":",DEC mins, ":", HEX2 secs
    PAUSE 999
    LOOP
    LOOP

    By way of explanation the 128 in the first loop = %10000000.· The MSB 1 enables the clock.· The LSB 0 puts it in a write mode.· In the second loop 129 = %10000001 the LSB 1 puts it in a read mode.· Let me know how it works out for you.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Sid Weaver
    Do you have a Stamp Tester yet?
    http://hometown.aol.com/newzed/index.html

    ·
  • NewzedNewzed Posts: 2,503
    edited 2005-12-15 17:00
    JP, I forgot one little thing.

    Delete the line that says "hrs = mins/60" and substitute the following:

    IF mins=60 THEN
    hrs = hrs + 1
    mins = -1
    endif

    This makes sure that when hrs rolls over to 1, mins is reset to 0.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Sid Weaver
    Do you have a Stamp Tester yet?
    http://hometown.aol.com/newzed/index.html

    ·
  • Robert KubichekRobert Kubichek Posts: 343
    edited 2005-12-15 17:12
    Newzed said...
    JP, I forgot one little thing.

    Delete the line that says "hrs = mins/60" and substitute the following:

    IF mins=60 THEN
    hrs = hrs + 1
    mins = -1
    endif

    This makes sure that when hrs rolls over to 1, mins is reset to 0.

    Wouldn't that be -

    IF mins=60 THEN
    hrs = hrs + 1
    mins = 0
    endif


    Bob N9LVU scool.gif
  • NewzedNewzed Posts: 2,503
    edited 2005-12-15 17:18
    No, because if Mins = 0, the next time secs = $00, mins would roll over to 1 instead of 0.· Run the program with mins = 0 and you will see what I mean.

    Thanks for checking, Bob.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Sid Weaver
    Do you have a Stamp Tester yet?
    http://hometown.aol.com/newzed/index.html

    ·
  • Robert KubichekRobert Kubichek Posts: 343
    edited 2005-12-15 17:48
    Newzed said...
    No, because if Mins = 0, the next time secs = $00, mins would roll over to 1 instead of 0. Run the program with mins = 0 and you will see what I mean.

    Thanks for checking, Bob.

    Gotcha! yeah.gif

    Bob N9LVU scool.gif
  • NewzedNewzed Posts: 2,503
    edited 2005-12-15 17:53
    Bob, my apologies - you are correct.

    I set mins = 58 in init, and had to change mins from -1 to 0 to get it to display correctly.· I also had to add "clreol" after "debug hex2 secs" to suppress a mysterious 9 that kept appearing on the debug screen.· Now I think everything is OK.· Thanks again.

    JP, please take note and adjust your program.· If you have any problems let me know and I will rewrite the whole program for you.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Sid Weaver
    Do you have a Stamp Tester yet?
    http://hometown.aol.com/newzed/index.html

    ·
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2005-12-15 18:06
    Sid,

    ·· I don't see why you are manipulating the hrs, mins and secs...The DS1302 can keep track of all of that information by itself.· It knows when one item has rolled over and when to update the next.· No math should be involved in that process, you're just adding to the code by doing so.· The OP was trying to cut back on it as I recall.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Chris Savage
    Parallax Tech Support
    csavage@parallax.com
  • NewzedNewzed Posts: 2,503
    edited 2005-12-15 18:57
    ·Chris, you are, of course, correct.· His original program took 93 bytes, my program took 114 bytes.· My understanding was that he wanted to simplify the shiftout/shiftin routines, and that was the only way I could do it.· In my opinion, his original program is what I would use in a similar situation, but I was just trying to make him happy.

    Sid
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2005-12-15 19:58
    Sid & JP,

    ·· The Burst Mode is the quickest and easiest way to get the variables.· I would use a variable that you're not using at that time (or create a dummy one if you have the variable space) and do what I showed.· That will keep the code functional.·

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Chris Savage
    Parallax Tech Support
    csavage@parallax.com
  • press9761press9761 Posts: 6
    edited 2005-12-15 21:13
    Hay guys

    Thank you all for you responses. It was very helpful. What I gathered from all the posts is that I was not far off the mark. Thanks bob, Sid and Chris. Oh and Chris, thanks a lot for the original code.


    Jordan
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2005-12-15 21:58
    Jordan, if you want to see a smaller version of the code, see the Digital Thermostat Project or the Binary Digital Clock.· The code for the clock sections of both of those is much smaller.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Chris Savage
    Parallax Tech Support
    csavage@parallax.com
Sign In or Register to comment.