Shop OBEX P1 Docs P2 Docs Learn Events
setting pins with constants — Parallax Forums

setting pins with constants

mikeamikea Posts: 283
edited 2013-06-30 19:57 in Propeller 1
How can I assign a set of pins using binary with a letter...for ex: x=%1111 . To set pins 0-3 high. Thanks

Comments

  • Duane DegnDuane Degn Posts: 10,588
    edited 2013-06-28 21:17
    This is one way to assign the pin numbers as constants.
    CON
    
      FIRST_PIN = 0
      PINS_TO_SET = 3
      LAST_PIN = FIRST_PIN + PINS_TO_SET - 1
    
    PUB SetPins
    
      dira[LAST_PIN..FIRST_PIN] := $FF ' all bits high  
    
    

    The above would set pins 0, 1 and 2 as outputs.

    If you also want the bit mask to be a constant you could do something like this:
    CON
    
      FIRST_PIN = 0
      PINS_TO_SET = 3
      LAST_PIN = FIRST_PIN + PINS_TO_SET - 1
      PIN_STATE = %110
      
    PUB SetPins
    
      dira[LAST_PIN..FIRST_PIN] := PIN_STATE 
    
    

    The above would set pins 1 and 2 as outputs.

    Edit: As with many programming tasks there are many ways to do this. A pin mask could be bitwise "or"ed to the dira register to set desired pins high.
  • mikeamikea Posts: 283
    edited 2013-06-29 03:32
    CON
            _clkmode = xtal1 + pll16x                                               'Standard clock mode * crystal frequency = 80 MHz
            _xinfreq = 5_000_000
     a=111
    VAR
      long  symbol
       
    pub sky
    dira[0..4]~~
    repeat
     outa[a]
    
    'the outa[a] line doesn't want to compile, I was going to use many combinations of [0..4] so assigning each state in the con to a single letter would save time. In the con section it should read : a=111(for some reason when I edit the reply it goes back to a=111)
  • TtailspinTtailspin Posts: 1,326
    edited 2013-06-29 08:21
    I think you need to use a colon in front of the equal sign.
    A := % 1111
    
    oops, never mind me, you are using a CON section, and don't need a colon...
    now my head hurts...

    -Tommy
  • Mike GreenMike Green Posts: 23,101
    edited 2013-06-29 08:29
    No, you don't use a colon in front of the equal sign. "a" is the name of a constant and you just write "a = %111". Note that you forgot the "%", so the value of "a" was one hundred and eleven which is an invalid pin number, thus the error message when you wrote "outa[ a ]".
  • dgatelydgately Posts: 1,630
    edited 2013-06-29 10:33
    mikea wrote: »
    CON
            _clkmode = xtal1 + pll16x                                               'Standard clock mode * crystal frequency = 80 MHz
            _xinfreq = 5_000_000
     a=111
    VAR
      long  symbol
       
    pub sky
    dira[0..4]~~
    repeat
     outa[a]
    
    'the outa[a] line doesn't want to compile, I was going to use many combinations of [0..4] so assigning each state in the con to a single letter would save time. In the con section it should read : a=111(for some reason when I edit the reply it goes back to a=111)

    As Mike stated your CON section needs to state " a = percent-sign111" <== Note to post readers: the web editor for the forum turns the percent sign and 2 following '1's to a null character...


    But, the real error "may" be that you are not actually setting the outa register with your code: "outa[a]"...

    Don't you need to do something like "outa[a]~" to set the pins low and "outa[a]~~" to set them high?


    I get the following error in SimpleIDE when I try to compile your code:
    Project Directory: /Users/.../Downloads/
    
    spin -I /Users/...SpinObjects/test.spin
    Propeller Spin/PASM Compiler (c)2012 Parallax Inc. DBA Parallax Semiconductor.
    Compiled on Apr  9 2013
    Compiling test.spin...
    test.spin(12:8) : error : Variable needs an operator
    Line:
     outa[a]
    Offending Item: ]
    

    So changed the code to:
    CON
            _clkmode = xtal1 + pll16x                                               'Standard clock mode * crystal frequency = 80 MHz
            _xinfreq   = 5_000_000
            a              = percent-sign111                                          ' <== replace "percent-sgn" with % for the forum editor
    VAR
      long  symbol
       
    pub sky
    dira[0..4]~~
    repeat
        outa[a]~~       ' needs an operator! (could use: outa[a] := 1, as well)
    

    which sets bits 0-2 to high...

    I think we were all confused by the web editor's policy of reformatting the percent-sign characters when preceding the 1111's


    dgately
  • Duane DegnDuane Degn Posts: 10,588
    edited 2013-06-29 11:20
    What's not clear to me is whether mikea wants to set the bit mask as a constant or the pin numbers as a constant.

    My second example in post #2 sets both the pins used and the bit mask as constants. My verbose constant names could be changed to single letter names to better match mikea's original request. I didn't use single letter constants since I personally very much dislike single letter variable and constant names.

    In general I try to make variable and constant names long enough a ctrl-f search will find only the name I'm looking for and not sections of other names.

    BTW, The percent sign issue can be fixed by using switching to the "Basic Editor". From your profile select "Settings" (top right of screen), then select "General Settings" (under "My Settings"\"My Account" on the left side of the screen). Under "Miscellaneous Options" select "Basic Editor - A simple text box" ("Miscellaneous Options" is the last section of the "General Settings" page).

    The Basic Editor can be a pain to use when you want to add pictures, links or video, so you may want to switch back to "Enhanced Interface - Full WYSIWYG Editing" after you've posted your code. The Basic Editor doesn't have the code "#" button in the advanced editor so you'll need to type out the code tags yourself.
  • dgatelydgately Posts: 1,630
    edited 2013-06-29 16:07
    Duane Degn wrote: »
    What's not clear to me is whether mikea wants to set the bit mask as a constant or the pin numbers as a constant.

    My second example in post #2 sets both the pins used and the bit mask as constants. My verbose constant names could be changed to single letter names to better match mikea's original request. I didn't use single letter constants since I personally very much dislike single letter variable and constant names.

    In general I try to make variable and constant names long enough a ctrl-f search will find only the name I'm looking for and not sections of other names.

    BTW, The percent sign issue can be fixed by using switching to the "Basic Editor". From your profile select "Settings" (top right of screen), then select "General Settings" (under "My Settings"\"My Account" on the left side of the screen). Under "Miscellaneous Options" select "Basic Editor - A simple text box" ("Miscellaneous Options" is the last section of the "General Settings" page).

    The Basic Editor can be a pain to use when you want to add pictures, links or video, so you may want to switch back to "Enhanced Interface - Full WYSIWYG Editing" after you've posted your code. The Basic Editor doesn't have the code "#" button in the advanced editor so you'll need to type out the code tags yourself.

    Ah yes, it is a bit unclear what is needed versus what was asked. :innocent:

    Yes, variable names should have meaning and allow easy search...

    Thanks for the info on the percent sign and using the basic editor, as I've had several posts that required special characters.

    I generally type in the code tags anyway!

    Thanks Duane!

    dgately
  • mikeamikea Posts: 283
    edited 2013-06-30 14:56
    Thanks guys, I am trying to set a bit mask as a constant to turn on/off a row of leds to save repeated typing and maybe a mistake when putting this info in the code. In post 2 there is an example. Is there a simpler way to set them? I've tried a:=outa[0..3]:=[percent 0101] and put "a" as a variable, and other ways but they won't compile. The compiler says I need an operator.
  • Duane DegnDuane Degn Posts: 10,588
    edited 2013-06-30 16:31
    mikea wrote: »
    Thanks guys, I am trying to set a bit mask as a constant to turn on/off a row of leds to save repeated typing and maybe a mistake when putting this info in the code. In post 2 there is an example. Is there a simpler way to set them? I've tried a:=outa[0..3]:=[percent 0101] and put "a" as a variable, and other ways but they won't compile. The compiler says I need an operator.

    How about making your outa statement a method?
    PUB Main
    
      repeat
        'do stuff
        A
        'do other stuff
    
    PUB A
    
      outa[0..3]:= %0101
    

    Then whenever you type "A" you get the pins set the way you want.
  • mikeamikea Posts: 283
    edited 2013-06-30 19:57
    That's what I was looking for. Wish I would've thought of that. Thanks Duane.
Sign In or Register to comment.