Shop OBEX P1 Docs P2 Docs Learn Events
strange variable behavior — Parallax Forums

strange variable behavior

Mikael SMikael S Posts: 60
edited 2007-11-25 20:57 in BASIC Stamp
Hello!

im programming a realtime clock. and im using the DS1302 chip.

Everything works fine. But i want to change the time during runtime. I have made a clockset procedure with 3 buttons.

This happends:


The user presses on the button for hours

hr = hr + 1

and then presses the set buton. The program then sends the hr variable to the time set. And it works perfectly until you want to set 16 hrs, then it sets the clock to "10". But it works perfectly up to 15. Same goes for the minutes.

Ive done so it sends the hr variable to debug, and it shows the right numbers, but something screws it up.

What am i doing wrong?

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2007-11-25 17:23
    Have you read the datasheet for the DS1302? It's not the clearest description, but all of the time and date information is stored as BCD (binary coded decimal) with one digit in the least significant 4 bits and another digit in the most significant 4 bits (sometimes with other information mixed in). These 4 bit values should range from 0 to 9. For values from 10 to 15, the behavior is unspecified. In any event, the value of 16 consists of a one bit in the tens place and a zero in the units place (in other words "10").
  • Mikael SMikael S Posts: 60
    edited 2007-11-25 17:35
    What should i do to overcome this then?
  • Mike GreenMike Green Posts: 23,101
    edited 2007-11-25 17:53
    For all values less than 10, you can directly write them to the DS1302. For all values 10 or greater, add 6 before writing to the DS1302. That way, 0 to 9 gets written as $00 to $09 and 10 to 19 becomes $10 (16) to $19 (25) which is just what you need. Do look at the picture of the registers in the DS1302 datasheet.

    When reading values back into the Stamp, you reverse the process. You do have to keep in mind that some of the upper bits of the DS1302 registers have special functions (like set 12/24h mode). Again, read the datasheet for details.
  • Mikael SMikael S Posts: 60
    edited 2007-11-25 19:38
    Thank you for you response! That helped me alot.


    I get the same problem with minutes greater then 30, should i add a specific number for that to?
  • Mike GreenMike Green Posts: 23,101
    edited 2007-11-25 20:57
    On the Stamp, you need to separate the tens and units digits, then position them in the proper part of the DS1302 register.

    Like this: ds1302 = ((minutes / 10) * 16) + (minutes // 10)

    The same thing works for any decimal to BCD conversion.
Sign In or Register to comment.