Shop OBEX P1 Docs P2 Docs Learn Events
SX/B Toggle behavior — Parallax Forums

SX/B Toggle behavior

DunnseptDunnsept Posts: 115
edited 2006-04-11 14:05 in General Discussion
I have a really simple program with a timer in the ISR that increments secs, mins, and hrs. This code is right from the examples for clocks.
Then what I tried to do is make an LED blink every second.
To accomplish this I am using
result = secs // 2
if result = 0 then
·· toggle rb.0
endif

The issue is that the LED behaves oddly. I did read in the SX/B help that toggle can have results other than what you expect. I tried that solution too but to no avail.
What the LED does is·it goes from off to dim to bright to dim to off. then repeats:
off - dim - bright - dim - off - dim - bright - dim.
if I replace the above code with something like:
if result = 0 then
·low rb.0
else
·high rb.0
endif

it works as expected.· I tried the toggle.sxb example and it works fine. Is something in the getting in the way? or is there some stupid little issue I missed.
thanks!

Comments

  • Jon WilliamsJon Williams Posts: 6,491
    edited 2006-04-11 13:31
    I'm not sure what's happening with your LED, but you can simplify the program like this:

    Main:
    · RB.0 = secs.0
    · GOTO Main

    TOGGLE and HIGH and LOW all set the pin to an output state -- you've already done that with LOW RB.0 in the Setup section so there's no need to repeat that when you can write the odd bit of seconds (secs.0) directly to the LED pin.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax
  • DunnseptDunnsept Posts: 115
    edited 2006-04-11 13:40
    Thanks Jon.
    obviously it works. worked too with the if then else but this is obviously more compact.
    I took the original code, removed the ISR and just put in INC SECS and it works just fine with TOGGLE..
    seems to be something goofy in my ISR. but heck if I can figure it out. I think I might start from scratch again, just to see if I can figure out why it doesn't work.
    gonna drive me nuts all day if I can't find it
  • BeanBean Posts: 8,129
    edited 2006-04-11 13:49
    Dunnsept,
    The reason it is "dim" is because when result = 0 the LED will be toggled EVERY time it loops. When result changes to 1 if the LED was off, it stays off, if the LED was on it stays on.

    BTW: "RB.0 = ~RB.0" is much faster than "TOGGLE RB.0" if the pin is already setup as an output.
    BTW: Your timer will be more accurate if you use "RETURNINT 250" and "IF tix = 125 THEN"

    Bean.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    "SX-Video·Module"·available from Parallax for only $28.95 http://www.parallax.com/detail.asp?product_id=30012

    "SX-Video OSD module"·available from Parallax for only·$49.95 http://www.parallax.com/detail.asp?product_id=30015
    Product web site: www.sxvm.com

    Available now! Cheap 4-digit LED display with driver IC·www.hc4led.com

    "I reject your reality, and substitute my own." Mythbusters


    Post Edited (Bean (Hitt Consulting)) : 4/11/2006 2:00:19 PM GMT
  • Jon WilliamsJon Williams Posts: 6,491
    edited 2006-04-11 13:50
    What fun would programming be if it didn't drive us nuts once in a while? [noparse];)[/noparse]

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax
  • DunnseptDunnsept Posts: 115
    edited 2006-04-11 14:05
    exactly... and a big DUH from me... now I get it. I made the stupid assumption that my main loop is following along at the same pace..
    turning out that the biggest obstacle so far is trying to not let PC/X86 programming cloud my thinking (at least that's my excuse and I'm sticking with it)
    Bean:· thanks for the suggestion. I had just copied that code from the 'digital clock' example and my main goal was to get the timing things working. I was going to worry about that part later on, but wasn't even too concerned as what I want to control doesn't need to be really accurate anyway. If my timings were off by a few seconds it wouldn't matter. I need to go back and re-read the section about how to figure out those return values anyway so I know how to calc this stuff myself.


    thanks guys.. I think I'll go let C++ and win32 drive me nuts for a while now instead wink.gif

    Post Edited (Dunnsept) : 4/11/2006 2:13:49 PM GMT
Sign In or Register to comment.