Shop OBEX P1 Docs P2 Docs Learn Events
PropBASIC code issue and general questions — Parallax Forums

PropBASIC code issue and general questions

bill-phxbill-phx Posts: 11
edited 2011-02-12 09:55 in Propeller 1
I have just started programming in PropBASIC, and am having a few issues.

I have a simple piece of code (I think) that is an on/off timer. The main code is the display portion (4-bit LCD connection), it then launches a cog for actually turning a relay on/off, and a cog that looks for button inputs to change the timer values.

Currently, my "off time" keeps decrementing by 1, in what looks like less than a second. If I comment out line 384 (WRLONG to put decremented value back in hub memory), the decrementing stops. All the other up/dn buttons work fine.

I have tried changing the input PIN, removed the input PIN entirely, grounded the input PIN. I don't think it is a wiring issue. btw- buttons are wired like on page 133 of the propeller education kit labs book.

3.3v
|
button
|
+
100ohm
PINx
|
10k ohm
|
GND

I'm at a bit of a loss here. I am guessing that it is something simple that I either overlooked with tired eyes, or a fundamental issue with how I am trying to do it.

Also-- can someone explain:

1) Cog variables -- what happens when you have a SUB call a SUB? I assume that they both cannot use the same temporary variable names, or strange things happen? (Variables are local to a cog, not a SUB, right?)

2) What exactly are the __tempx variables? Are they unique in a SUB? Is it a long? Can it be used as a temporary string?

3) Any other __xxxxx variables I should be aware of?

If anyone wants to look at the code, that would be great. I am also open to suggestions on how to code more efficiently, so suggest at will! (Guessing that this being one of my first programs, I am probably doing at least one thing "the hard way" if not "the wrong way." :) )

*** NOTE: Code moved down a few entries, cleaned it up a little, still having issues. ***

Thanks!
bill

Comments

  • BeanBean Posts: 8,129
    edited 2011-02-11 13:52
    1) Variables are global to the COG, not to a SUB. The variable will be trashed.

    2) The __tempx variables are used by PropBasic commands. They are also global to the cog.

    3) __paramx variables are used to pass parameters to subroutines. You can use them as a temporary variable if you don't call a subroutine when using them.

    Bean
  • bill-phxbill-phx Posts: 11
    edited 2011-02-11 16:57
    Bean wrote: »
    2) The __tempx variables are used by PropBasic commands. They are also global to the cog.

    As in they are reserved, or I may use them in a SUB (as long as I do not use the same one in another SUB I call from a SUB)?
    Bean wrote: »
    3) __paramx variables are used to pass parameters to subroutines. You can use them as a temporary variable if you don't call a subroutine when using them.

    So basically, the __tempx and __paramx variables are really the same thing (undefined variables that you can use), except the __paramx has the added feature of auto-filling when you give arguments to a SUB call?

    Any chance you want to give my code a quick once-over? I am also looking to make sure that I am doing things cleanly, as well as fix the variable decrementing issue...

    thank you
    bill
  • bill-phxbill-phx Posts: 11
    edited 2011-02-11 20:18
    Here is a cleaned up version of the same code. Same issue. Problematic part hilighted below.

    Some memory or variable allocation issue that I missed?

    DEVICE          P8X32A, XTAL1, PLL16X     ' 1st overnight test at PLL4X good and 5 sec pause between display iterations
    XIN             5_000_000
    
    ' ----------------------------------------------------------------------
    ' Constants
    ' ----------------------------------------------------------------------
    
    ' ----------------------------------------------------------------------
    ' I/O Pins
    ' ----------------------------------------------------------------------
    
    LCDRS  PIN 0 LOW
    LCDRW  PIN 1 LOW
    LCDE   PIN 2 LOW
    
    LCDDB7 PIN 7 LOW
    LCDDB6 PIN 6 LOW
    LCDDB5 PIN 5 LOW
    LCDDB4 PIN 4 LOW
    LCDDB PIN 7..4 LOW
    
    LED PIN 17 OUTPUT
    
    btn1ontimedn  PIN 12 INPUT
    btn1ontimeup  PIN 13 INPUT
    btn1offtimedn PIN 14 INPUT
    btn1offtimeup PIN 15 INPUT
    OUTP1         PIN 16 LOW
    
    btn2ontimedn  PIN 20 INPUT
    btn2ontimeup  PIN 21 INPUT
    btn2offtimedn PIN 22 INPUT
    btn2offtimeup PIN 23 INPUT
    OUTP2         PIN 24 LOW
    
    ' ----------------------------------------------------------------------
    ' Shared (hub) Variables (Byte, Word, Long)
    ' ----------------------------------------------------------------------
    
    tempstr HUB STRING(4)           ' Temporary value when using STR to display numbers
    
    timeon HUB LONG(10)=0           ' Timer on time (timer#)
    timeoff HUB LONG(10)=0          ' Timer off time (timer#)
    timecycles HUB LONG(10)=0       ' Total cycles completed (timer#)
    
    
    ' ----------------------------------------------------------------------
    ' Shared (hub) Data (DATA, WDATA, LDATA, FILE)
    ' ----------------------------------------------------------------------
    
    
    ' ----------------------------------------------------------------------
    ' TASK Definitions
    ' ----------------------------------------------------------------------
    
    btns TASK
    outputs1 TASK
    outputs2 TASK
    heartbeat TASK
    
    
    ' ----------------------------------------------------------------------
    ' Cog Variables (Long only)
    ' ----------------------------------------------------------------------
    
    x VAR LONG                      ' Main display loop, lcd x position
    y VAR LONG                      ' Main display loop, lcd y position
    maintemp VAR LONG               ' Main display loop, temp variable for rdlong and wrlong
    
    pushbytex VAR LONG              ' SUB pushbyte temp variable
    pushbytey VAR LONG              ' SUB pushbyte temp variable
    pushbytez VAR LONG              ' SUB pushbyte temp variable
    
    xpos VAR LONG                   ' SUB LCDpos temp variable
    ypos VAR LONG                   ' SUB LCDpos temp variable
    cmd  VAR LONG                   ' SUB LCDpos temp variable
    cols VAR LONG                   ' SUB LCDpos temp variable
    
    lcdprintx VAR LONG              ' SUB LCDprint temp variable
    
    
    ' ----------------------------------------------------------------------
    ' SUB/FUNC Definitions
    ' ----------------------------------------------------------------------
    
    LEDblink SUB 1
    LCDenable SUB 0
    LCDpushnibble SUB 1
    LCDpushbyte SUB 1
    LCDdigitalwrite SUB 1
    LCDcommandwrite SUB 1
    LCDcommandwritenibble SUB 1
    LCDprint SUB 1
    LCDprintstr SUB 1
    LCDcls SUB 0
    LCDinit SUB 0
    LCDpos SUB 2
    
    ' ======================================================================
      PROGRAM Start
    ' ======================================================================
    
    Start:
      LCDinit
    
      WRLONG timeon(1),5
      WRLONG timeoff(1),15
    
      WRLONG timeon(2),2
      WRLONG timeoff(2),1
    
      COGINIT outputs1, 1
      COGINIT outputs2, 2
      COGINIT btns, 5
      COGINIT heartbeat, 6
    
      LCDpos 1,1
      LCDprintstr "Dev:"
      LCDpos 2,1
      LCDprintstr "On:"
      LCDpos 3,1
      LCDprintstr "Off:"
      LCDpos 4,1
      LCDprintstr "Cyc:"
    
    Main:
    DO
      y=6
      FOR x = 1 to 2
        tempstr = STR x,6
        LCDpos 1,y
        LCDprintstr tempstr
    
        RDLONG timeon(x),maintemp
        tempstr = STR maintemp,6
        LCDpos 2,y
        LCDprintstr tempstr
    
        RDLONG timeoff(x),maintemp
        tempstr = STR maintemp,6
        LCDpos 3,y
        LCDprintstr tempstr
    
        RDLONG timecycles(x),maintemp
        tempstr = STR maintemp,6
        LCDpos 4,y
        LCDprintstr tempstr
    
        y = y + 7
      NEXT
      PAUSE 500
    LOOP
    END
    
    
    ' ----------------------------------------------------------------------
    ' SUB/FUNC Code
    ' ----------------------------------------------------------------------
    
    {
    SUB LEDblink
      b=__param1
      LOW LED
      for i = 1 to b
        HIGH LED
        PAUSE 100
        LOW LED
        PAUSE 250
      NEXT
      PAUSE 1000
    ENDSUB
    }
    
    SUB LCDpos
      xpos = __param1
      ypos = __param2 - 1
      cols = 20
    
      IF xpos = 1 THEN
        cmd = 128 + ypos
      ENDIF
    
      IF xpos = 3 THEN
        cmd = 128 + ypos
        cmd = cmd + cols
      ENDIF
    
      IF xpos = 2 THEN
        cmd = 128 + ypos
        cmd = cmd + cols
        cmd = cmd + cols
        cmd = cmd + cols
        cmd = cmd + 4
      ENDIF
    
      IF xpos = 4 THEN
        cmd = 128 + ypos
        cmd = cmd + cols
        cmd = cmd + cols
        cmd = cmd + cols
        cmd = cmd + cols
        cmd = cmd + 4
      ENDIF
    
      LCDcommandwrite cmd
      PAUSE 1
    ENDSUB
    
    SUB LCDinit
      PAUSE 1000
    
      LCDcommandwritenibble %0011     ' sequence to put in 8-bit mode
      PAUSE 10
      LCDcommandwritenibble %0011     ' sequence to put in 8-bit mode
    
    '  PAUSE 1
    '  LCDcommandwritenibble %0011     ' sequence to put in 8-bit mode
    '  PAUSE 1
    
    
      LCDcommandwritenibble %0010     ' sequence to put in 4-bit mode, repeat twice
      PAUSE 1
    
    '  LCDcommandwritenibble %0010     ' sequence to put in 4-bit mode, repeat twice
    '  PAUSE 1
    
      LCDcommandwrite %00101000     ' 4-bit, display lines, font, null, null
      PAUSE 5
    
    
    '  LCDcommandwrite %00001110     ' cmd, display on, cursor on, blink off
      LCDcommandwrite %00001111     ' cmd, display on, cursor on, blink on
      PAUSE 5
    
          LCDcommandwrite %00000110     ' entry mode, increment right, do not shift display
      PAUSE 5
    
       LCDcls
    
    '   LCDpos 1,3
    '   LCDprintstr welcome1
    '   PAUSE 1000
    '   LCDpos 3,1
    '   LCDprintstr welcome2
    
    '   PAUSE 3000
    '   LCDcls
    ENDSUB
    
    
    SUB LCDcls
      LCDcommandwrite %00000010
      LCDcommandwrite %00000001
      pause 20
    ENDSUB
    
    SUB LCDprintstr
      __param2 = __param1   ' LCDprint uses __param1
      DO
        RDBYTE __param2, __param3
        IF __param3 = 0 THEN EXIT
    
        LCDprint __param3
        INC __param2
    '    PAUSEus 500
      LOOP
    ENDSUB
    
    SUB LCDprint
      lcdprintx = __param1
      HIGH LCDRS
      LOW LCDRW
      LCDpushbyte lcdprintx
    ENDSUB
    
    SUB LCDcommandwrite
      LOW LCDRS
      LOW LCDRW
      LCDpushbyte __param1
    ENDSUB
    
    SUB LCDcommandwritenibble
      LOW LCDRS
      LOW LCDRW
      LCDpushnibble __param1
    ENDSUB
    
    SUB LCDpushbyte
      pushbytex = __param1
      pushbytez = pushbytex & $0F
      pushbytey = pushbytex SHR 4
      LCDpushnibble pushbytey
      LCDpushnibble pushbytez
    ENDSUB
    
    SUB LCDpushnibble
      LCDDB = __param1
      PAUSEus 100
      LCDenable
    ENDSUB
    
    SUB LCDenable
      PAUSE 1
      PULSOUT LCDE, 1             ' 5 was working for a long time
      PAUSE 1
    ENDSUB
    
    ' ----------------------------------------------------------------------
    
    
    ' ======================================================================
    ' TASK Code
    ' ======================================================================
    
    TASK btns
      a VAR LONG
      DO
        IF btn1ontimedn = 1 THEN
          RDLONG timeon(1),a
          a = a - 1
            if a < 1 then
              a = 0
            endif
          WRLONG timeon(1),a
        ELSEIF btn1ontimeup = 1 THEN
          RDLONG timeon(1),a
          a = a + 1
            if a < 1 then
              a = 0
            endif
          WRLONG timeon(1),a
        ELSEIF [highlight]btn1offtimedn = 0 THEN
          RDLONG timeoff(1),a
          a = a - 1
            if a < 1 then
              a = 0
            endif
          WRLONG timeoff(1),a              ' COMMENTING THIS OUT STOPS THE ISSUE (but then button does not work)
    [/highlight]
        ELSEIF btn1offtimeup = 1 THEN
          RDLONG timeoff(1),a
          a = a + 1
            if a < 1 then
              a = 0
            endif
          WRLONG timeoff(1),a
        ENDIF
    
        PAUSE 500
      LOOP
    ENDTASK
    
    TASK outputs1
      b VAR LONG
      DO
        RDLONG timeon(1), b
        __temp1 = b * 1000
        HIGH OUTP1
        PAUSE __temp1
    
        RDLONG timeoff(1), b
        __temp1 = b * 1000
        LOW OUTP1
        PAUSE __temp1
    
        RDLONG timecycles(1), b
        b = b + 1
        WRLONG timecycles(1), b
      LOOP
    ENDTASK
    
    TASK outputs2
      value VAR LONG
      DO
        RDLONG timeon(2), value
        __temp1 = value * 1000
        HIGH OUTP2
        PAUSE __temp1
    
        RDLONG timeoff(2), value
        __temp1 = value * 1000
        LOW OUTP2
        PAUSE __temp1
    
        RDLONG timecycles(2), value
        value = value + 1
        WRLONG timecycles(2), value
      LOOP
    ENDTASK
    
    TASK heartbeat
      DO
        PAUSE 250
        TOGGLE LED
      LOOP
    ENDTASK
    
    
  • kuronekokuroneko Posts: 3,623
    edited 2011-02-12 00:02
    What's wrong with this picture? I'm not an expert but that looks inconsistent.
    TASK btns
      DO
        IF     btn1ontimedn  = 1 THEN
          ...
        ELSEIF btn1ontimeup  = 1 THEN
          ...
        ELSEIF btn1offtimedn = 0 THEN
          ...
        ELSEIF btn1offtimeup = 1 THEN
          ...
        ENDIF
    
        PAUSE 500
      LOOP
    ENDTASK
    
  • bill-phxbill-phx Posts: 11
    edited 2011-02-12 01:21
    Well....

    a) I knew it would be something simple.

    b) That was left over from debigging why it was happening.

    thank you!!

    bill
  • BeanBean Posts: 8,129
    edited 2011-02-12 09:55
    bill-phx wrote: »
    So basically, the __tempx and __paramx variables are really the same thing (undefined variables that you can use), except the __paramx has the added feature of auto-filling when you give arguments to a SUB call?

    The __tempx variables will be used by PropBASIC commands, __paramx variables are ONLY used by subroutine calls.

    Bean
Sign In or Register to comment.