Shop OBEX P1 Docs P2 Docs Learn Events
Lookup command and hex numbers? — Parallax Forums

Lookup command and hex numbers?

mmccartymmccarty Posts: 3
edited 2009-10-01 01:02 in BASIC Stamp
I'm pulling time and date from a RTC and trying to use the lookup command to store times for daily timed events, but I am running into problem with the dates stored as hex numbers. When I use the "day" variable as the index for the lookup command it counts over by the decimal equivalent. (29th day of the month counts over 41 places instead of 29). Am I missing something simple here or do I need to convert the hex day into a decimal index variable beforehand? How?

Comments

  • Carl HayesCarl Hayes Posts: 841
    edited 2009-09-29 14:40
    They aren't stored as hex numbers. They're stored as BCD numbers. Isolate the high-order four bits, which represent the tens digit of a decimal representation; multiply it by ten; and then add the low-order four bits, which represent the units digit. That gives you the binary (or hex, if you prefer) equivalent of the BCD number you get out of the RTC.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    · -- Carl, nn5i@arrl.net
  • mmccartymmccarty Posts: 3
    edited 2009-09-29 21:01
    Thanks for the help, I have it working:

    indx = (date.NIB1 * 10) + date.NIB0

    The next question:

    I have times stored in a table in a four digit format (1545, 1600, 1830). What would the best way to convert into separate hours and minutes variables to compare to the RTC for alarm events?

    -Mac, KB9HV
  • ZootZoot Posts: 2,227
    edited 2009-09-30 00:22
    Not sure that code will give accurate results. Try this:

    time var Word
    hrs VAR Byte
    mins VAR Byte
    hrs = time/100
    mins = time//100
    
    



    Personally, I would do your table in BCD as HEX rather decimal, then you can just deal with the nibs:

    TimeTable DATA Word $1600, Word $0500, Word $1330, Word $1200
    
    FOR idx = 0 TO 3
      READ TimeTable+(idx<<1), Word time
      hrs = time.NIB3 * 10 + time.NIB2
      mins = time.NIB1 * 10 + time.NIB0
      DEBUG "The time is ", DEC2 hrs, ":", DEC2 mins, CR
    NEXT
    
    



    But you could leave it BCD especially if you are using an RTC that is also BCD -- you can just compare the "read" word and an assembled BCD word from the RTC -- greater than, less than, equal to, etc. and it will work just fine as Hex words. If you need to compare just hours or just mins, you could use the high and low bytes.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    When the going gets weird, the weird turn pro. -- HST

    1uffakind.com/robots/povBitMapBuilder.php
    1uffakind.com/robots/resistorLadder.php
  • mmccartymmccarty Posts: 3
    edited 2009-10-01 01:02
    Thanks all for the help. The code that was posted did work, although I'm not sure why. (That message seems to have been deleted). Zoot's code had me smacking myself in the head, fairly obvious, y'all make this seem easy... Although as Zoot stated I didn't really need to do a conversion, just do the table in BCD and compare apples to apples:

    offtime is the times in the lookup table and hrs and mins is the time from the RTC:

    IF offtime.BYTE1=hrs AND offtime.BYTE0=mins THEN
    DEBUG HOME, "TIMES UP!!!"
Sign In or Register to comment.