Input and Output Developed by: Electronic Systems Technologies College of Applied Sciences and Arts Southern Illinois University Carbondale http://www.siu.edu/~imsasa/est Martin Hebel [email protected] With support from: Will Devenport, Mike Palic and Mike Sinno Sponsored by: Parallax, Inc. http://www.parallax.com/
Inputs, Processing, and Outputs Input Processing Output Any system or program accepts input, process information, and controls outputs. The BASIC Stamp, and other microcontrollers, specialize in using input devices such as switches, and controlling output devices such as LEDs (Light Emitting Diodes).
Stamp I/O There are 16 I/O (Input/Output) pins on the BS2 labeled P0 to P15. These are the pins through which input and output devices may be connected. Depending on the code that is written, each pin may act as an input to read a device, or as an output to control a device.
Output - Connecting an LED Vdd, NOT Vin. Connected on P8. Angle of shot makes it appear to be on P9. In this configuration a LOW, or 0V, at P8 will allow current to flow through the LED to Vdd (+5V) lighting it. When P8 is HIGH (+5V), no current will flow and the LED will not light. The LED is Active Low.
Another configuration that could be used is to have the LED Active-High. In this configuration the LED will light when the output is HIGH, or +5V. Current flows from ground or Vss (0V) to the 5V output on P8. The 220 resistor will limit current flow to approximately 20mA . The output current from a BS2 pin should be limited to 20mA maximum. The maximum current for an LED is generally 30mA.
Blinking the LED with HIGH, LOW The following program should cause the LED to blink at a rate of 1 second ON and 5 seconds OFF ‘Prog 4A: Blink LED program Main: HIGH 8 PAUSE 1000 LOW 8 PAUSE 5000 GOTO Main 'Turn off LED 'Wait 1 second 'Turn on LED 'Wait 5 seconds 'Jump back to beginning
Code Discussion HIGH defines the pin to be an output and sets it to a HIGH state, digital 1 or 5V. HIGH pin 0-15 HIGH 8 LOW defines the pin to be an output and sets it to a LOW state, digital 0 or 0V. LOW pin 0-15 LOW 8 PAUSE instructs the BS2 to wait for the defined number of milliseconds (1/1000 seconds). PAUSE time in milliseconds 0-65535 PAUSE 1000 GOTO instructs the BS2 to jump to the defined label. More about this will be covered in Programming Structures. GOTO Label
Blinking the LED with OUTPUT and OUT The HIGH and LOW instructions perform 2 actions: Sets direction of the I/O pin to an output. Sets the state of the output to be 0 or 1 (0V or 5V) Another means to perform the same process is to use code to set the direction, then the state. ' Prog 4B: Blink LED program using OUTPUT and OUT OUTPUT 8 Main: OUT8 = 1 PAUSE 1000 OUT8 = 0 PAUSE 5000 GOTO Main 'Set P8 to be an output 'Turn off LED1 'Wait 1 second 'Turn on LED1 'Wait 5 seconds 'Jump back to beginning
Code Discussion OUTPUT sets the pin to act as an output. OUTPUT pin OUTPUT 8 The BS2 on startup sets all I/O pins to inputs. OUT sets the state of the output. OUTpin = 1 or 0 OUT8 = 1 1 sets the output HIGH (5V – Digital High or 1). 0 sets the output LOW (0V – Digital Low or 0). Depending on program need, sometimes it is better to use the HIGH and LOW instructions, and other times to use OUTPUT and OUT.
Connecting an Active-Low Switch Connect a push-button switch to P10 The push-button is a momentary normallyopen (N.O.) switch. When the button IS NOT pressed (open), P10 will sense Vdd (5V, HIGH, 1) because it is pulled-up to Vdd. When PB1 IS pressed (closed), P10 will sense Vss (0V, LOW, 0) making it Active-Low.
Active-High Push-Button Switch Another configuration that could have been used is shown here. Notice that the position of the switch and resistor have been reversed. When the button IS NOT pressed (open), P10 will sense Vss (0V, LOW, 0) because it is pulled-down to Vss. When PB1 IS pressed (closed), P10 will sense Vdd (5V, HIGH, 1) making it Active-High. The BASIC Stamp has uncommitted inputs. That is, when an I/O pin is not connected and acting as an input, it cannot be assured to be either HIGH or LOW. Pull-up and pull-down resistors are needed to commit the input to the non-active (open) state for switches. The 1K resistor is used to prevent a short-circuit between Vdd and Vss when the switch is closed.
Reading the Switch The digital value of an input can be read using the INpin instruction. A 1 or 0 will be returned indicating a HIGH or LOW state on the input. This program uses DEBUG to display the digital value. 'Prog 4E: Display the status of PB1 on P10 INPUT 10 'Set P10 to be an input Main: DEBUG ? IN10 PAUSE 500 GOTO Main 'Display status of P10 'Short pause 'Jump back to beginning
Controlling Outputs with Inputs Now that we can control outputs and read inputs, it's time to perform a little processing and put the pieces together. The state of an input may be read with INpin. The state of an output may be controlled with OUTpin. Here is a program that will use the input pushbutton PB1 on P10 to control output LED1 on P8. 'Prog 4F: Controlling LED1 with input PB1 INPUT 10 OUTPUT 8 'Set P10 to be an input 'Set P8 to be an output Main: OUT8 = IN10 GOTO Main 'Set LED1 = PB1 'Jump back to beginning
DIRS, INS, OUTS Up to this point I/O have been set as inputs or outputs, and states set or read individually. Looking at the Memory Map, there are 3 16-bit registers which set the direction for the I/O, and which are read or written to. IN10 reads the value in the 10th bit (P10) of INS. OUT9 =1 sets the output state in the OUTS register for bit 9 (P9). OUTPUT 8 sets bit 8 (P8) for output in the DIRS register. This may also be written as DIR8=1 (1=output, 0 = input).
The I/O can also be addressed as nibbles, bytes or the entire word. IN15 OUT15 DIR15 IN0 OUT0 DIR0 TO IND OUTD DIRD INC OUTC DIRC INB OUTB DIRB (High Byte) INH OUTH DIRH INA OUTA DIRA (Low Byte) INL OUTL DIRL INS OUTS DIRS As BITS As NIBBLES As BYTES As 16-Bit WORDS
In our circuit, there are output devices on P8 and P9, and input devices on P10 and P11. P8 – P11 make up nibble C. The direction of the I/O can be set as a nibble with: DIRC = %0011 in binary. It may also be written as DIRC = 3 in decimal, but the binary form is much easier to read for determining individual bit states. This will set the DIRS nibble C for input (P11), input (P10), output (P9), output (P8). Note that the bit positions are most-significant bit (MSB) to least-significant bit (LSB).
Some various examples to illustrate the flexibility, code savings, and increased speed possibilities: To read the entire lower byte (P0-P7) as inputs: DIRL=%00000000 X = INL To count up in binary on 8 LEDs connected on P8 to P15: DIRH = %11111111 FOR X = 0 to 255 OUTH = X NEXT To set 4 outputs on P4-P7 equal to 4 inputs on P12-P15: DIRS = %0000000011110000 OUTB = INDD