PBASIC Programming/Input and Output
A computer program that does not communicate with the outside world is very boring indeed. Programs, including programs on the BasicStamp need to take input and generate output.
INPUT and OUTPUT[edit | edit source]
The BasicStamp has 16 basic I/O pins, all of which can be used as either an input or an output. To specify that a pin is an input we use the INPUT command:
This makes pin 12 an input pin. To specify that a pin is being used as an output, the OUTPUT command can be used:
This makes pin 15 an output.
DIRS Register[edit | edit source]
The INPUT and OUTPUT commands affect a special memory value called DIRS. DIRS, which is short for "Directions" is a special memory location known as a "Tri-State Buffer". DIRS contains 16 bits, each corresponding to one of the I/O ports. If the pin in DIRS is a 0, then the corresponding pin is an input. If the pin in DIRS is a 1, then the corresponding pin is an output.
For instance, if we wanted to set the top byte to be input, and the bottom byte to be output, we could write:
DIRS = %1111111100000000
This is easier then having to write 16 INPUT and OUTPUT commands!
I/O Ports[edit | edit source]
1-Bit Ports[edit | edit source]
I/O ports have a special name, using the prefix "In" or "Out", followed by the pin number. For instance, if we have a variable called MyVariable, and if all the ports were properly designated as INPUT or OUTPUT, we could write all of the following:
MyVariable = IN3
MyVariable = IN5 + 10
OUT12 = MyVariable
OUT15 = MyVariable - 10
Multi-Bit Ports[edit | edit source]
We can combine multiple I/O pins into multi-bit I/O ports. Each port has a specific name:
|Input||INS||INL, INH||INA, INB, INC, IND||IN0 - IN15|
|Output||OUTS||OUTL, OUTH||OUTA, OUTB, OUTC, OUTD||OUT0 - OUT15|
The "S" suffix is used to denote the whole 16-bit word. "L" is for the low byte, "H" is for the high byte. "A", "B", "C", and "D" are used for the 4 possible nibbles, with "A" being the lowest (pins 0 - 3) and "D" being the highest (pins 12 - 15).
Sending and Receiving[edit | edit source]
When we want to send data from the BasicStamp to another device, we write that data to an output port. The external device that we are trying to communicate with will then read that value from an input port, and use that value internally. Data values to read and write from the BasicStamp are 5 volt values. Too much voltage will cause damage to the circuit, and too little voltage cannot be read. In general, we can associate our values for 1 and 0 with voltages:
"Voltage?"[edit | edit source]
We are going to digress from the main narrative of the book here for a minute and discuss some issues of electricity. Because of the embedded nature of the BasicStamp, most projects that involve them are going to also involve some manner of hardware integration. Improperly connecting your BasicStamp to external hardware can cause damage to the chip, and could even cause harm to you.
Ohm's Law[edit | edit source]
A basic law of electricity, and the only such law that we need to consider for the purposes of this book, is Ohm's Law. Ohm's law can be written as:
v is the voltage, in volts. i is the current, in amps. R is the resistance, in Ohms.
We know that the output port of the BasicStamp supplies 5V. If there is too much current, the stamp could get hot or even become damaged. We should try to keep our current to 5 milliamps or less (preferably less). This means, using Ohms's law that:
This means that for every circuit we use with our BasicStamp, we should have at least 1000Ω of resistance. Any less resistance and we risk damage to our BasicStamp.