Shop OBEX P1 Docs P2 Docs Learn Events
Discussion of Stamp I/O Registers — Parallax Forums

Discussion of Stamp I/O Registers

microcontrollerusermicrocontrolleruser Posts: 1,194
edited 2017-01-30 20:01 in BASIC Stamp
Stampsters

This is regarding the two I/O bytes in a Stamp 1.

While it looks like HIGH and LOW 'set' the I/O pins.

So do INPUT and OUTPUT.

How do you 'read' pins to see what state there in or get a byte value back?

I'm getting that 'Old Feeling' it might just be 'POLLIN' and 'POLLOUT' only available with 2P and above chips.

READ and WRITE info in Basic Stamp Help only mentions working with EEPROM.

I don't think they'd flat leave out you can read and write to PORT Pins and Dirs.

Thanks!
«13

Comments


  • Stampsters

    This is from Basic Stamp Help.

    'Reading PINS effectively reads the I/O pins directly, returning an 8-bit set of 1's and 0's corresponding to the high and low state of the respective I/O pin at that moment. Writing to PINS will store a high or low value on the respective I/O pins (though only on pins that are set to outputs).'

    Sounds great!

    Don't see anything in there about how to do this so far.

    How do you read and write to PINS and DIRS please?

    Thanks!
  • In the Basic Stamp Tool, go to FILE>Open From>DEfault BS1 Directory.

    There are demo programs to illustrate what you are asking.
  • ercoerco Posts: 20,244
    Pins=bytevalue

    If pins=bytevalue then...
  • JonnyMacJonnyMac Posts: 8,912
    edited 2017-01-06 23:19
    You can read all IO as a group using PINS:
      someVar = PINS
    
    Keep in mind that any pins set to output mode will be reflected in what you read back.

    You can also write to the PINS register -- but this will only affect pins with a DIR bit set to 1:
      PINS = someVar
    
    You can also read and write to individual pins with bit variables -- same rules apply. The INPUT and OUTPUT keywords are rarely used. By default, all pins start as inputs; you need to make them outputs. Many keywords handle this for you. HIGH and LOW, for example, set the pin to output mode. PULSIN makes the pin an input.

    You have to be careful with the PINx bit variables. I frequently see newcomers try to use these where a constant should be used. Here's the thing: when a PINx variable appears on the right side of an expression, the IO pin will be read and the result (0 or 1 for a bit) used in the expression. Here's another trap:
      SEROUT PIN7, T2400, ("Hello, World!)
    
    The intent here is to send a serial message on P7 but it will actually go out P0 or P1 -- depending on the state of P7. The correct syntax is:
      SEROUT 7, T2400, ("Hello, World!)
    
    ...though I don't like using literals for pin numbers. Use a SYMBOL to give it a name; that way you can change configuration easily.
  • Perhaps starting with a template will help. The file attached is what I start my Prop-1 Controller projects with, and this illustrates points from above:

    -- SEROUT requires a constant so SIO is defined as 7 (for P7)
    -- The trigger is an input so it is defined using PIN6
    -- PINS and DIRS are used to preset IO as a group.

  • Stampies

    All good stuff!

    Thank you.

    Will respond alertly in a couple hours.

    Have a good one!
  • microcontrollerusermicrocontrolleruser Posts: 1,194
    edited 2017-01-07 16:10
    Developers

    Okay. Figured out what this means.

    'When the BS1 is powered up, or reset, all memory locations are cleared to 0, so all pins are inputs (DIRS = %00000000). Also, if the PBASIC program sets all the I/O pins to outputs (DIRS = %11111111), then they will initially output low, since the output latch (PINS) is cleared to all zeros upon power-up or reset, as well.'

    Okay. After reset: When you power up Stamp board or start.

    PINS = 0 is low.

    DIRS = 0 inputs.

    Now we can go to next step.

    PINS = 1 is high

    DIRS = 1 is output.

    And then there are 8 individual bits that control pins 0-7 respectively.

    Thanks!

  • I think you'll find that having a project goal will help you create practical working code that will be a great path to experience. When the BS1 was released there was no Internet, but Parallax had a BBS where we could exchange questions. Every night after work I would download questions, solve them, then upload answers in the morning. I learned by writing programs for other people. The BS1 doesn't get as much use as other processors, but it is still very useful. Pick a project and work your way through it.

  • Johnny

    Appreciate it.

    Going to do the App Notes terminal.

    Thanks!
  • JonnyMacJonnyMac Posts: 8,912
    edited 2017-01-08 00:59
    I'm biased (because I wrote them [when I worked for Parallax and helped with the Help file]), but I would suggest you read and review the "Elements of PBASIC Style." The source code in the BS1 app notes are formatted using an old -- dare I say, antiquated -- style; a style that can be difficult on the eyes which makes bug tracking difficult.

    Remember, source code is for HUMANS. By spending a little time formatting with a clean style, you'll save hours of time troubleshooting later, and other programmers will appreciate your listings.

    Always strive for clarity. The template I share above gives you a good start.

  • Mac

    You wrote this?

    https://www.parallax.com/sites/default/files/downloads/BS1-IC-BASIC_Stamp_1_AppNotes.pdf

    That will be handy in case we have any questions!

    Thanks!
  • Ahh...since it's on the parallax website, I think Parallax wrote that, but it probably had plenty of input from JonnyMac.

  • JonnyMacJonnyMac Posts: 8,912
    edited 2017-01-08 20:01
    Mac

    You wrote this?

    https://www.parallax.com/sites/default/files/downloads/BS1-IC-BASIC_Stamp_1_AppNotes.pdf

    That will be handy in case we have any questions!

    Thanks!

    No, I did not -- my friend Scott Edwards did. Like you, I learned a lot from those app notes and have extended the techniques used in it. I now teach EFX-TEK customers how to use the BS1 via our Prop-1 controller.

    Scott Edwards started the Stamp Applications column in Nuts & Volts magazine which I took over. I took a break and a guy named Lon took it for a while. I came back and it morphed to the SX/B compiler which I worked on with Bean. The column changes to "The Spin Zone" where I discuss Propeller programming.

    What I was referring to is a section of the help file called "Elements of PBASIC Style." Programming style is a must for pros, and I think that hobbyists are best served adopting similar standards.

    Here's the link to a bunch of Nuts & Volts articles by Scott and me that will be helpful. The BS1 projects are in the beginning. Sometimes it will show up in later issues, but the BS2 was so popular it became dominant very quickly.

    -- https://www.parallax.com/downloads/nuts-and-volts-basic-stamps-volume-1

    You may notice in my early articles I used Scott's formatting style. I ultimately developed my own (which is described in "Elements of PBASIC Style") because I find it easier to format, read, and debug.

    Final note: my name used to be Jon Williams. I work in the entertainment industry (SAG/AFTRA) hence needed a new name.
  • Ahh yes.. Our good friend Scott Edwards. He wrote many Nuts and Volts. That's how I got started.





  • microcontrollerusermicrocontrolleruser Posts: 1,194
    edited 2017-01-08 21:44
    Johnny and Publison

    Yours truly just upgraded from a Basic Stamp 1 SIPP on a Super Carrier board

    (Stamp 1 Starter Kit) to a Basic Stamp 1 Project Board.

    Project Board doesnt't require Stamp 1 adapter. I'm still wondering about that one!

    That's made things easier to just put it out and use it.

    Will run programs that output to Terminal to get warmed up.

    Like in the Propellor C Learn section. 'Start Simple'. Works for me!

    Thanks!


  • Developers

    You can read the pins with DEBUG.

    Here's program to read pins.

    ' {$STAMP BS1}
    ' {$PBASIC 1.0}

    DEBUG #PIN4, CR

    DEBUG #PIN7, CR

    END

    Thanks!
  • Keep in mind that the way you've structured the program it will run just once and the stop -- this means you must have your IO set before the program runs. With a very simple change (add a label and a command), your program will run forever so you can watch changes on your inputs.
    ' {$STAMP BS1}
    ' {$PBASIC 1.0}
    
    Main:
      DEBUG #PIN4, CR
      DEBUG #PIN7, CR
      GOTO Main
    
  • microcontrollerusermicrocontrolleruser Posts: 1,194
    edited 2017-01-09 13:58
    Mr McPhalen

    Thank you!

    I am going to 'cut and burn'. Make one pass through commands and

    then come back again.

    Next on list are INPUT and OUTPUT.

    Plus HIGH and LOW.

    I think there are some 4 box logic diagrams coming up that I'll have to make.

    Thanks!
  • INPUT and OUTPUT are not very useful. It's a rare case when a pin is set to OUTPUT mode that you make it an input. Commands that require pin to be in INPUT state (e.g., PULSIN) take care of that for us.

    HIGH and LOW take care of setting the target pin to output mode.
  • microcontrollerusermicrocontrolleruser Posts: 1,194
    edited 2017-01-09 21:21
    Johnny

    Thanks!

    HIGH and LOW more useful than INPUT and OUTPUT.

    Right now the Debug reading the Pins and Dirs is returning a O or a 1.

    Returns a 1 after setting one pin to HIGH.

    I'll check if 'Debug' PIN2 for example is accurate.

    How do you get a Debug value on all eight bits at once for PINS and DIRS?

    Still working on logic diagram.

    Probably do HIGH and LOW first.

    Thanks!




  • Developers

    This is first draft of logic chart.

    [img][/img]

    This chart shows what a 0 or 1 byte stored in PINS and DIRS means.

    You can change PINS by individual bytes by HIGH and LOW.

    Next figure out how to change (write to) DIRS.

    Thanks!

  • Johnny

    I guess this sums up what you said.

    'To simultaneously make a pin an output and set its state use the HIGH and LOW commands'

    Now there are two things going on with a pin. There is the state and the direction.

    So the chart might be a little wrong. Oops!

    Thanks!
  • Returns a 1 after setting one pin to HIGH.

    That is expected because HIGH writes 1 to the pin output and dir registers. If you read the PINx variable it will see this and report 1.

    I promise you'll have fun with practical projects -- the BS1 is simple; enjoy the simplicity.
  • microcontrollerusermicrocontrolleruser Posts: 1,194
    edited 2017-01-10 15:51
    Johnny

    Found what has been bugging me about this.

    This is from PIC Source Book.

    'Some of PBASIC's I/O instructions automatically change a pin's data direction when they execute and then restore it when they are done. The PBASIC environment permits this by keeping a copy of the PIC's tristate (TRIS) register in normal read/write memory.

    I will keep looking at these Pbasic I/O commands and see if I can make 'rhyme and reason' of it.

    I just feel like Pbasic is keeping me from doing some things like READ the PINS and DIRS registers.

    Also WRITE to the registers.

    Having to work with a grab bag of commands.

    Thanks!
  • JonnyMacJonnyMac Posts: 8,912
    edited 2017-01-10 22:56
    I just feel like Pbasic is keeping me from doing some things like READ the PINS and DIRS registers.

    No, it's not. I wonder... maybe you're not quite understanding the structure of IO pins. The attached diagram will help. The top is the output buffer; when the DIRx bit is 1 this will allow the level on the output PINx bit to go to the pin. If DIRx is 0, the output buffer is disconnected from the IO pin.

    io_pin_structure.png

    Note that there is a separate input buffer that we read when we have PINx on the right side of an expression. Now you can see why when you make a pin HIGH (output mode, with level of 1) that you will read 1 back from the pin if you treat it as an input. It's important to ensure that the DIRx bit of any pin monitoring external signals is set to 0.

    You can read the DIR register -- it will be what you last wrote to it.

    612 x 395 - 22K
  • GenetixGenetix Posts: 1,740
    edited 2017-01-10 23:08
    microcontrolleruser,

    You're mixing things up.

    PINS and DIRS are "variables" so they can be read or changed like any other variable.
    DIRS = 64 ' 64 = 0100-0000, so P6 will be an Output
    PINS = 128 ' 128 = 1000-0000, so P7 will be Low if it was an Output
    
    DEBUG DIRS ' Displays "DIRS = 64" on the screen in decimal. (Page 160 of the BASIC Stamp Manual)
    DEBUG PINS ' Displays "PINS = 128" on the screen in decimal. (Assuming no inputs are connected to the other pins)
    

    You only use READ and WRITE to access the EEPROM where your PBASIC program is stored.
    You can store program data in the EEPROM with the EEPROM command.
    Just remember that your PBASIC program also uses the EEPROM so that you don't accidently overwrite your program.

    PBASIC doesn't have any commands for any of the PIC hardware since PBASIC takes care of the PIC hardware for you.

  • Mr McPhalen

    I'm just dealing with it at the 'Run command' and 'See results' level.

    There is a link though between what cannot be accessed and

    the results that are a little hard to decipher.

    Genetix

    'PBASIC doesn't have any commands for any of the PIC hardware since PBASIC takes care of the PIC hardware for you. '

    It is frustrating seeing the Parallax PASM for PIC and then coming back to Pbasic.

    I've adjusted to Pbasic's limitations and appreciate it's usefulness for rapid prototyping.

    Thanks!

  • Johnny and Genetix

    Don't want to muddy the water here but I should tell you we have a BS2P24.

    My theory is the added commands (61, P24) may give us more control

    of the hardware.

    Thanks!
  • microcontrolleruser,

    The BS2p24 is the 2nd most powerful BASIC Stamp module.
    I don't believe the hardware is accessible but I am not familiar with SX microcontroller that it's based on.

    You asked about the BS1 so what is it that you are trying to do?
  • Developers

    Found this in Input/Output Example code Stamp Help.

    'PIN7 = 0 ' Write 0 to output latch
    DEBUG "After 0 written to OUT7: " '

    More digging and experimenting to do.

    Thanks!
Sign In or Register to comment.