Shop OBEX P1 Docs P2 Docs Learn Events
Question on SXB integers (going below 0 is what?) — Parallax Forums

Question on SXB integers (going below 0 is what?)

T&E EngineerT&E Engineer Posts: 1,396
edited 2009-11-02 19:01 in General Discussion
I am trying to modify the SXB Example code for the Clock / Timer. This counts up and I want it to count down to tell me how many days and hours or so till Christmas. I have adapted it for the Sure Electronics 0832 display. In the attached code, you will see in the interupt for 'secs' that checks this byte variable for any number below or lower than zero (0). The attached code and Youtube video is currently set up now to skip 0 and just go back to 59.

Update_Timer:
  INC ms      ' update ms counter
  IF ms = 1000 THEN     ' check for 1 second
    ms = 0
    DEC secs
    IF secs = 0 THEN     ' check for new minute
   
    [b]'IF secs < 0 THEN  ' This is what it should be but get strange display chars[/b]

      secs = 59
      DEC mins
      IF mins = 0 THEN    ' check for new hour
        mins = 59
        DEC hrs
        IF hrs = 0 THEN   ' check for new day
          hrs = 23
          DEC days
          IF days = 0 THEN
            days = 99
          ENDIF
        ENDIF
      ENDIF
    ENDIF
  ENDIF


If I change it to check for a number lower than 0, then the ascii that is displayed is strange like: I5, then counts down to H5, etc.. Where is this comming from. Are numbers less then 0 negative or does it equal 128 or something?



http://www.youtube.com/watch?v=qfYYjz1V1lg

Comments

  • BeanBean Posts: 8,129
    edited 2009-11-02 16:23
    Byte variables will go to 255, word variables will go to 65535.

    Bean.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    Does that byte of memory hold "A", 65, $41 or %01000001 ?
    Yes it does...


    ·
  • T&amp;E EngineerT&amp;E Engineer Posts: 1,396
    edited 2009-11-02 16:53
    Thanks Bean. That solved my question to why I was seeing an "I5" displayed when it went below 0 (into 255).


    secs = 15
    times(3) = secs / 10··' times(3) = 15 / 10 = 1 (rounded)
    times(3) = times(3) + $30·' times(3) = $31 or "1"

    times(4) = __REMAINDER··' times(4) = 5
    times(4) = times(4) + $30·' times(4) = $35 or "5"


    secs = 255
    times(3) = secs / 10··' times(3) = 255 / 10 = 25 (rounded)
    times(3) = times(3) + $30·' times(3) = $49 or "I"

    times(4) = __REMAINDER··' times(4) = 5
    times(4) = times(4) + $30·' times(4) = $35 or "5"
  • JonnyMacJonnyMac Posts: 9,212
    edited 2009-11-02 17:49
    You can also "fix" the problem with IF-THEN-ELSE, for example:

    IF secs > 0 THEN
      DEC secs
    ELSE
      secs = 59
    ENDIF
    



    This solves the problem of that code running before time values have been initialized. This method does the same thing, though may be considered less elegant:

    DEC secs
    IF secs = 255 THEN
      secs = 59
    ENDIF
    



    This fixes the wrap-under from what a byte does to what you want it to do as a time register.

    Post Edited (JonnyMac) : 11/2/2009 5:55:31 PM GMT
  • T&amp;E EngineerT&amp;E Engineer Posts: 1,396
    edited 2009-11-02 19:01
    Thanks JonnyMac. This looks a bit simpler to follow.

    I'll try it out tonight.
Sign In or Register to comment.