Discussion of Stamp I/O Registers
microcontrolleruser
Posts: 1,194
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!
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!
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!
There are demo programs to illustrate what you are asking.
If pins=bytevalue then...
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:
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: 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: ...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.
-- 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!
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!
Johnny
Appreciate it.
Going to do the App Notes terminal.
Thanks!
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!
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.
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!
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!
HIGH and LOW take care of setting the target pin to output mode.
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!
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.
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!
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.
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.
You're mixing things up.
PINS and DIRS are "variables" so they can be read or changed like any other variable.
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!
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?
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!