Shop OBEX P1 Docs P2 Docs Learn Events
Sticking Loop Mystery — Parallax Forums

Sticking Loop Mystery

coryco2coryco2 Posts: 107
edited 2013-01-17 15:44 in Propeller 1
I am greatly perplexed after writing what I thought was a simple bit of code to cycle through a series of items on an LCD display when a button is pushed. For some reason, the code will function for a while (depending on the delay length in the loop), then freeze randomly while running one of the display methods. The crash seems to occur after a set time, not necessarily at any point in the code. Also, sometimes (if the delay is commented out, for example), the LCD backlight will go out as well or it will display garbage characters, and/or the Prop will spontaneously restart. The Prop seems to work just fine if I load other code into it, though, so there's got to be bug somewhere in here:
CON

  _clkmode = xtal1 + pll16x                      ' System clock settings
  _xinfreq = 5_000_000

  LCDBasePin    = 17  

OBJ

  LCD: "BenkyLCDdriver"                        'Declare LCD display driver

VAR

  BYTE MenuLevel                
  LONG maxT                         ' Variable used by LCD driver


PUB Start
 
    LCD_DisplayInit 

PUB LCD_DisplayInit

    LCD.start( LCDBasePin,0 )                      ' Start LCD Display
    DIRA[16]~~                                     ' Set pin 16 "high" to turn on backlight
    OUTA[16]~~
    maxT:=LCD.getMax
    waitcnt(clkfreq + cnt)                          ' Wait 1 s before starting

    MenuLevel:=1       'Resets to top of Main Menu
    MenuLoop 

PUB MenuLoop

  repeat
     waitcnt(((clkfreq/1000)*10) + cnt) 'Pause for specified number of millliseconds between loops 

     if MenuLevel>2 or MenuLevel<1     'Stay in range of menu
       MenuLevel:=1

    case MenuLevel  
      1: DisplayMenuItem1         
      2: DisplayMenuItem2
 
PUB DisplayMenuItem1 

      LCD.exec( LCD#CMD_INSTR, 000000 + $00)              'sets cursor position - see chart in BenkyLCDdriver
      LCD.exec( LCD#CMD_PRINT, string("   Menu Item:   ") )
      LCD.exec( LCD#CMD_INSTR, 000000 + $40)              'sets cursor position - see chart in BenkyLCDdriver
      LCD.exec( LCD#CMD_PRINT, string("    Number 1    ") )
      repeat
        if ina[15]==1    ' Check button
          ++MenuLevel    ' Add one to MenuLevel
        MenuLoop

PUB DisplayMenuItem2  

      LCD.exec( LCD#CMD_INSTR, 000000 + $00)              'sets cursor position - see chart in BenkyLCDdriver
      LCD.exec( LCD#CMD_PRINT, string("   Menu Item:   ") )
      LCD.exec( LCD#CMD_INSTR, 000000 + $40)              'sets cursor position - see chart in BenkyLCDdriver
      LCD.exec( LCD#CMD_PRINT, string("    Number 2    ") )
      repeat
        if ina[15]==1    ' Check button 
          ++MenuLevel    ' Add one to MenuLevel
        MenuLoop


I would very much appreciate if anyone could shed some light on this for me.

Comments

  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2013-01-17 14:38
    You've got recursive method calls going on, i.e. MenuLoop calls the DisplayMenuItemx routines, and they in turn call MenuLoop. This will quickly result in stack overflow, which is what is causing your program to seize up.

    -Phil
  • coryco2coryco2 Posts: 107
    edited 2013-01-17 15:37
    Sorry, I don't follow what I'm doing wrong (?). Wouldn't MenuLoop just go to one of the DisplayMenuItemx subroutines, run it, and then return to MenuLoop? Isn't that kind of just typical branching? How else would I do it?
  • JonnyMacJonnyMac Posts: 9,108
    edited 2013-01-17 15:39
    You're calling your menu levels from MenuLoop and instead of returning to the upper level you're calling it. Use return to escape out of your lower level back up to the higher.
  • coryco2coryco2 Posts: 107
    edited 2013-01-17 15:44
    Yep, that just dawned on me as well. Duh... :smile:

    Thanks for the help!
Sign In or Register to comment.