Shop OBEX P1 Docs P2 Docs Learn Events
New user, overwhelmed, where to start!? — Parallax Forums

New user, overwhelmed, where to start!?

There are tons of examples, so many different ways to do things, but, as far as I can tell, no clear starting point for a new user. I have the P2 Eval board along with the Add-on boards. I am trying to learn Sping2/PASM.

Simple question: How to read a pushbutton? What to expect the state of the pushbutton to be?
The best question of all though, how would I find this out on my own? I am reading through the Parallax Propeller 2 Spin2 Language Documentation and I am having difficulty finding pertinent information. Browsing through the forums hasn't yielded any clear starting point. There is a lot of jargon, acronyms, and verbiage that seem to answer questions but are foreign to a beginner.

Thank you for your assistance. I am really looking forward to getting all of the boards attached and working on my Eval board at some point!

For my pushbutton scenario: Assume using "Control" board and the P2 Eval board. Assume "Control" board is placed on the P0 through P7 pins on the Eval board with the GND pins lined up properly. I have the four leds blinking just fine using this simple code:

PUB main ()
repeat
    PINHIGH(0 ADDPINS 3)
    waitms(1000)
    PINLOW(0 ADDPINS 3)
    waitms(500)

Comments

  • JonnyMacJonnyMac Posts: 8,918
    edited 2022-01-19 21:50

    I wrote an object for that control board. The short answer is pinread() to read the state of a pin, or a group of pins. The attached demo should get you started.

  • @JonnyMac Thank you for the example code. This includes a lot of information that will help with serial and buttons! I noticed many of the demos included with the Spin2 download of the Prop tool have top objects included but no way to see the child object. Your example code has the top object and all the child objects as well. For me, this is incredibly helpful!

    @whicker Thank you for sharing the quick-bytes link. That is very helpful if the quick bytes has something very specific, otherwise, it is very overwhelming for a new user. In my case, I used the quick bytes to get started with the LED Matrix and was successful.

  • evanhevanh Posts: 15,184

    Sounds like you're using the four button add-on board from the Accessory Set - https://www.parallax.com/product/p2-es-eval-board-accessory-set/

    The old Prop1 method of reading the inbuilt INA variable still works. But the alternative new way is via PINREAD() method.

    con
        pbcbase = 0
    
    
    pub  four_button_test() | buttons
    
        wrpin( pbcbase+4 addpins 3, P_SCHMITT_A | P_HIGH_FLOAT | P_LOW_15K )
        pinlow( pbcbase+4 addpins 3 )
    
        repeat
            buttons := pinread( pbcbase+4 addpins 3 )
            pinwrite( pbcbase+0 addpins 3, buttons )
    

    So this code echoes the four button switches to their respective LEDs. Of note is the add-on board is lacking pull-down resistors for the buttons. Therefore to prevent the inputs from staying high all the time I've added config for active pull-down using weak (15k) outputs on the input pins.

    Makes a good example of showing how I/O pins, even on the Prop1, are always inputs. Even while they're ordinary outputs, they're still readable as inputs. You just see the level of the output then.

  • evanhevanh Posts: 15,184

    Another quirk with that add-on board is the pin order for the lower two buttons and LEDs is swapped.

  • travelerdawgtravelerdawg Posts: 18
    edited 2022-01-20 16:51

    @evanh and @JonnyMac , apparently pinw is not the same as wrpin. Thank you for the help! I have thoroughly pressed my palm against my forehead today for not catching this before now. The code turned out quite simple:

    CON
        _clkfreq = 160_000_000
        cpbase = 0
    PUB main() | buttons
        wrpin(cpbase+4 addpins 3, P_LOW_15k)
        pinl(cpbase+4 addpins 3)
        repeat
          buttons := pinr(cpbase+4 addpins 3)
          pinw(cpbase addpins 3, buttons)
    

    However, I still don't fully understand the meaning of all the Pin methods in the Parallax Spin2 Documentation. The details are a little sparse and the short examples included in the document are few.

  • JonnyMacJonnyMac Posts: 8,918
    edited 2022-01-21 15:52

    ...apparently pinw is not the same as wrpin.

    No, it's not. pinw is a short for pinwrite which allows you to write the digital level of one or more pins (it also sets the DIR bit[s] to 1 for output mode). wrpin is used to set special pin configuration bits (e.g., for enabling pull-ups or pull-downs or setting a smart pin mode). What can be confusing at first is how to deal with pull-ups and pull-downs. Keep in mind that even when a pin is in regular output mode (not a smart pin), the input register can see what's going on. To add a pull-down to a pin, we do this:

    wrpin(BTN, P_LOW_15K)
    pinlow(BTN)  
    

    The wrpin() line is telling the P2 to insert a 15K resistor between the pin an the FET that switches that pin to ground when it's an output and low. The second line engages that FET which causes the pin to go low through the 15K resistor. Yes, we can still read the pin as an input because the input register is available when the pin is in a tri-state mode (input or output-high or output-low).

    If you come from the Arduino world, the two lines above are similar to the pinmode() function -- except that the P2 has pull-ups and pull-downs, and different levels for each (i.e., it's more flexible).

    Other pin commands for tri-state operation

    • pinhigh() sets the pin/pingroup to output and high (3.3v) -- this may be direct or through a pull-up
    • pinlow() sets the pin/pingroup to output and low (ground) -- this may be direct or through a pull-down
    • pinwrite() allows you to write a value to the pin/pingroup
    • pintoggle() will invert the output state(s) of the pin/pingroup
    • pinfloat() will set the pin/pingroup to input mode (do not use this after setting pull-ups or pull-downs)
    • pinread() will return the digital state(s) of the pin/pingroup

    The idea of a pingroup can be confusing as well. What this allows you to do is specify a contiguous group of pins (that shouldn't cross the pin 31/pin 32 boundary). If you wanted to treat P0..P3 as a group, you could define that group like this:

      btns := 0 addpins 3
    

    You could do the same thing manually like this:

      btns := 0 | (3 << 6)
    

    ...where 0 is the base pin and 3 is the number of additional pins in the group.

    Smart pins are another topic -- you might want to get very comfortable with controlling pins via the tri-state modes before tackling the smart pins (ADCs, DACs, PWM, UARTs, SPI, etc.)

    In the early days of the P2 I wrote an article for Nuts & Volts magazine. It may help you.
    -- https://www.nutsvolts.com/magazine/article/an-introduction-to-the-parallax-propeller-2

  • @JonnyMac this was an excellent explanation, thank you! I understand much better now.

  • TL;DR Suggestion - a forum post dedicated to explaining to newcomers how they can navigate the long form videos available on the Parallax YouTube channel.

    @JonnyMac I have been watching some of the videos, especially this one "Spin 2 Beginners Series Part 1: RBG LEDs with JonnyMac". My initial thoughts when searching through the videos on the Parallax YouTube channel was, "Three hours!", "Two hours!", etc...until I noticed there are time tags in the comments. I also found the link in the description of the video to the forum post that includes your documents mentioned in the video. It might be helpful for newcomers to have a forum post explaining this info. If this already exists, I must have over looked it. Thanks for all your help, the videos go to great lengths to assist in understanding the P2 ins and outs along with explaining the coding portion and has been very informative.

Sign In or Register to comment.