Shop OBEX P1 Docs P2 Docs Learn Events
Serin and the Stack — Parallax Forums

Serin and the Stack

AmitAmit Posts: 27
edited 2006-10-05 17:28 in General Discussion
I am trying to run a serin command with a timeout from within a subroutine. However if the timeout occurs I still want to return to the command after the subroutine call. An example of what I want to do is below. The problem is that the variable gled1 never gets cleared in normal run mode if the timeout occurs. However if I uncomment the break and run it in debug mode it works fine. I am checking the pin status using a good scope.

From my understanding the timeout triggers a jmp to 'missed' so the original return address should still be at the top of the stack. The return in 'missed' or 'rs' should be accessing the same address, I think. If I replace the serin with 'goto missed' it also only works in debug mode. Anyone have any suggestions?

Also is there any way to empty the call stack?

Thanks a lot!
Amit


DEVICE          SX48, OSCHS2
FREQ              50_000_000
PROGRAM          start

GLED1          var    RC.2        'LED
RLED2          var    RC.3        'LED

missed            sub                      'signal missed comm
rs                   sub                      'receive serial comm

start:
    temp = 0
    output gled1
    output rled2
    goto mainloop

rs:
    \clrb rled2
    serin ra.0, T57600, temp, 1, missed
    'goto missed
return


missed: 
    \setb rled2
return


MainLoop:
    \setb gled1
    rs
    \clrb gled1
    temp=6
'    watch temp
'    break
goto MainLoop

Comments

  • BeanBean Posts: 8,129
    edited 2006-10-04 17:10
    It's really bad form to JUMP from one subroutine to another.
    I would just set a bit variable.
    DEVICE         SX48, OSCHS2
    FREQ           50_000_000
    PROGRAM        start
    
    GLED1          PIN RC.2 OUTPUT        'LED
    RLED2          PIN RC.3 OUTPUT        'LED
    
     
    temp           VAR    byte ' I assume ???
    flag           var    bit
    
    rs             sub                      'receive serial comm
    
    start:
    
        temp = 0 ' Not really needed since SX/B clears all variables at startup
        goto mainloop
    
    rs:
        rled2 = 0
        flag = 1 ' Assume we will miss it
        serin ra.0, T57600, temp, 1, missed
        flag = 0 ' Cool, we didn't miss it
     
    missed:
        rled2 = flag
        return
    
    
    MainLoop:
        gled1 = 1
        rs
        gled1 = 0
        temp = 6 
    goto MainLoop
    
    

    Bean.


    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Cheap used 4-digit LED display with driver IC·www.hc4led.com

    Low power SD Data Logger www.sddatalogger.com
    SX-Video Display Modules www.sxvm.com

    Don't mistake experience for intelligence. And vis-vera.
    ·
  • AmitAmit Posts: 27
    edited 2006-10-04 17:23
    Thanks a lot Bean! I can modify my code in the spirit of that example to use flags instead and I'm guessing that should work no problem. But I am really curious as to why what I was doing wasn't working. Other than being poor form, what is actually going wrong. I am still learning about the SX so knowing this would clear up some confusion about subroutines and the stack. Also I'm afraid that there may be a bigger problem indicated by it working in debug mode but not otherwise. Any info would be appreciated!

    Thanks,
    Amit
  • Sparks-R-FunSparks-R-Fun Posts: 388
    edited 2006-10-05 17:28
    Amit,

    I am wondering if there is a chance that the program is working as you expect but just too fast for you to observe the result with your current scope setting. Your gled1 is going to clear and then set very quickly so be sure to use a very fast sweep rate on your scope.

    Including the break command and running the debugger allows the program to suspend while gled1 is clear. I suspect that including some form of a pause in place of the break statement would reveal whether or not gled1 is actually setting and clearing.

    Just trying to help.
Sign In or Register to comment.