Shop OBEX P1 Docs P2 Docs Learn Events
Hex problem with my DS1302 — Parallax Forums

Hex problem with my DS1302

HermieHermie Posts: 36
edited 2009-09-07 18:59 in BASIC Stamp
I'm trying to get my BS2 to do things based on the time and need to convert the Hex values to real numbers that the Stamp can compute. IE:

If mins = 23 THEN......

But the mins are in Hex format and I'm pulling my hair out trying to change the mins to a real number.
Can anyone help?

Hermie

Comments

  • VaatiVaati Posts: 712
    edited 2009-09-06 20:18
    For example:

    test VAR word

    test = $0A

    DEBUG DEC test

    Run that. Notice that the variable test is defined as a hex number, and is converted to a decimal number to display. You can replace your code with "IF DEC mins = 23 THEN" and I think it will work.

    I hope this helps,
    Vaati

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Quit buying all those fixed voltage regulators, and·get an Adjustable Power Supply·for your projects!· Includes an LED testing terminal!

    *-NEW-* SD Card Adapter·Now available!· Add extra memory to your next Propeller project with ease!
  • Beau SchwabeBeau Schwabe Posts: 6,559
    edited 2009-09-06 21:02
    Hermie,

    This has tripped me up a couple of times too... It's not so much that the numbers are in HEX format, rather than they are in BCD (Binary Coded Decimal) which is slightly different than just HEX format. Also with the DS1302, there are a couple of masked bits used in the seconds and hours that add different meaning to the overall value.

    I will try to touch up on the BCD later if someone doesn't beat me to it... right now I have to run out the door.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Beau Schwabe

    IC Layout Engineer
    Parallax, Inc.
  • HermieHermie Posts: 36
    edited 2009-09-06 21:06
    I tried the IF DEC mins = 23 THEN....but get a syntax error with the DEC in front of mins, using Pbasic 2.5

    Hermie
  • Mike GreenMike Green Posts: 23,101
    edited 2009-09-06 21:17
    Instead of "DEC mins", try "(mins >> 4)*10+(mins & $F)"
  • xanatosxanatos Posts: 1,120
    edited 2009-09-06 22:29
    Hi Hermie,

    I got all sorts of confuzzled on this back when I first started using teh DS1302. What threw me was the "time stored in hex format" thing. So when I wanted to see a "10" in the minutes slot, for example, I thought I had to enter it as "0A". But that's not the case. 10 is stored as 10 - but the binary is not 1010, it is 0001 0000. Since the digits in any given slot never exceed 9, it can store as hex, but still operate as dec. I'm not sure this makes sense to you, but it works in all my programs, and I have a LOT of them running in the field now.

    It is also a LOT easier if you can use 24 hour time rather than the am/pm thing. If you use 24 hour format, you can forget completely about the masked bits thing. It makes your programming much simpler this way (IMO).

    So think of each number in a given time section as a separate nibble, and a time value in total (hours, or mins, or secs) as a byte. So 10:03:51 looks like this:

    0001 0000 : 0000 0011 : 0101 0001

    I had tried everything under the sun to convert from hex to dec, to enter digits as hex... and I was getting time values like 0A:34:8B...

    Here is the link to the original post I made.· Pay particular attention to my last post which begins "I R DUMB!" :-)· That was the post where it finally dawned on me what I needed to do...

    http://forums.parallax.com/showthread.php?p=815813

    Hope this helps!

    Dave X

    Post Edited (xanatos) : 9/6/2009 10:35:56 PM GMT
  • Beau SchwabeBeau Schwabe Posts: 6,559
    edited 2009-09-07 00:51
    xanatos,

    What you are describing is BCD and not really HEX. In order for Hermie to do what I think he wants to do, he needs some sort of BCD<-->Decimal conversion.


    Decimal VAR B0
    BCD     VAR B1
    
    
    'BCD to Decimal
    Decimal = BCD & $F
    Decimal = Decimal + (((BCD >>4) & $F) * 10)
    
    'Decimal to BCD
    BCD = Decimal / 10
    BCD = BCD>>4 + (Decimal - BCD * 10) 
    
    

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Beau Schwabe

    IC Layout Engineer
    Parallax, Inc.
  • xanatosxanatos Posts: 1,120
    edited 2009-09-07 01:02
    True - I just saw the "need to convert the Hex values to real numbers" part and that triggered my traumatic memories of my first encounter with the DS1302! smile.gif Those BCD<-->DEC conversion routines should be ideal from what Hermie stated... Hermie, can you provide more specifics on what you want to do in that IF/THEN statement?

    Dave
  • HermieHermie Posts: 36
    edited 2009-09-07 01:37
    Dave,

    To answer your question, I want to trigger different routines based on the time. In other words: If hrs = xx and mins = xx THEN goto/gosub whatever.

    I'm not that fimilar with hex and only today heard of BCD, so I guess I have alot of homework to do

    Hey Mike Green, thanks for the tip....but I didn't understand any of that routine....I will have my nose in the manual for awhile figuring that one out. But thank you....you've always been a source of knowledge!

    Thanks to everyone else as well, this is deeper than I thought, but I will learn this as it is the heart of the project.

    Just think....Another couple thousand hours of Pbasic study and I'll be the one answering questions! [noparse]:)[/noparse]· I love this stuff!

    Hermie
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2009-09-07 02:39
    In my time setting routines on the DS1302 the current value being set (minutes, seconds, etc.) is first converted from BCD to DEC and then after getting the value I want it is shifted back. The following routines should help you out. Note that each is a single line of code and that no work variables are required. I hope this helps. Take care.
    bcdNum = (decNum / 10 << 4) + (decNum // 10)        ' Decimal To BCD
    decNum = (bcdNum.NIB1 * 10) + bcdNum.NIB0           ' BCD To Decimal
    

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Chris Savage

    Parallax Engineering
    50 72 6F 6A 65 63 74 20 53 69 74 65
    ·
  • MoskogMoskog Posts: 554
    edited 2009-09-07 09:00
    Hermie

    I let my BS do things based on the time too, hours, not mins but should that be different?

    hrs var byte

    gosub CheckTime················· 'Ask 1302 what time..

    if (hrs = $21) then....··········· ' Things shall happend at 21.00 (1302 set to 24 hrs mode)
  • HermieHermie Posts: 36
    edited 2009-09-07 18:59
    Well I found something that seems to work just fine, at least for a beginner programmer. Once I learn the advance sugestions everyone has offered I'm sure I'll rewrite the routine the correct way. I made a list of the actual value for each second and minute, and it applies to hours up to 23. The first number in the following is the actual min or sec. The = is the value for each:

    '00=00,01=01,02=02,03=03,04=04,05=05,06=06,07=07,08=08,09=09
    '10=16,11=17,12=18,13=19,14=20,15=21,16=22,17=23,18=24,19=25
    '20=32,21=33,22=34,23=35,24=36,25=37,26=38,27=39,28=40,29=41
    '30=48,31=49,32=50,33=51,34=52,35=53,36=54,37=55,38=56,39=57
    '40=64,41=65,42=66,43=67,44=68,45=69,46=70,47=71,48=72,49=73
    '50=80,51=81,52=82,53=83,54=84,55=85,56=86,57=87,58=88,59=89

    So if I want something to happen at lets say 14:23:59 using 24 hour time it would look like this.

    IF hrs=20 and mins=35 and secs=89 then gosub get_temp

    This may not be the politically correct way to write the routine, but it works and I understand it. So thanks to all for the help

    Hermie
Sign In or Register to comment.