Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống
1
/ 35 trang
THÔNG TIN TÀI LIỆU
Thông tin cơ bản
Định dạng
Số trang
35
Dung lượng
487,18 KB
Nội dung
Y=INP(x) In place of x you put the decimal address of the port you want to read. In the case of the main printer port at starting address 888, the address of the status register is 889. The Y is a variable used to store the return value for future use in the program. For testing, you can PRINT the value of Y, which shows the decimal equivalent of the binary bit pattern on the screen. Listing 30.3 is a sample program that displays the current values of the four inputs con- nected to the Robot Experimenter’s Interface. The values are shown as 0 (“false”) and -1 (“true”). Bear in mind that the Busy and Online lines are active-low; therefore their logic is the reverse of the others. The code in the test program “compensates” for the active-low condition by reversing the logic in the If expressions. Also note the less-than-straightforward method for determining if pins 15 and 12 are triggered exclusively. These extra If tests are needed because the parallel port (most, any- way) will automatically bring pin 12 HIGH if pin 15 is brought HIGH. Weirdness is also 476 COMPUTER CONTROL VIA PC PRINTER PORT GND 7 8 9 10 11 12 13 14 15 16 6 5 4 3 2 1 OE OE VCC 74367 I O I O I O I O I O I O Inputs Outputs OE IO L L H L L H H X HI-Z Truth table 74367 FIGURE 30.9 The internal configura- tion of the 74367 chip. Note the two indepen- dent ENABLE lines, on pins 1 and 15. Ch30_McComb 8/29/00 8:34 AM Page 476 encountered if pin 15 is brought HIGH while trying to read the values of pins 10 and 11. The port reads pins 10 and 11 as LOW, even though they may be HIGH on the interface. Again, this is the action of pin 15 (printer error), and for this reason, it’s usually a good idea to limit its use or to ensure that the values of other inputs are ignored whenever pin 15 is HIGH. LISTING 30.3. DIM BaseAddress AS INTEGER, StatusPort AS INTEGER DIM DataPort AS INTEGER, ControlPort AS INTEGER DIM x AS INTEGER, Count AS INTEGER BaseAddress = 888 DataPort = BaseAddress StatusPort = BaseAddress + 1 ControlPort = BaseAddress + 2 WHILE (1) x = INP(StatusPort) + 1 IF (x AND 64) = 64 THEN PRINT "Pin 10: 1" ELSE PRINT "Pin 10: 0" END IF IF (x AND 128) <> 128 THEN PRINT "Pin 11: 1" ELSE INPUTTING DATA 477 Data lines Data outputs Data outputs Data outputs Data lines Data lines ENABLE ENABLE Pin Pin 17 Pin 14 Parallel port connector 74367 74367 74367 ENABLE Pins 1-9 D0-D7 FIGURE 30.10 Block diagram for a selectable parallel port, using three 74367 ICs to independently control three separate devices. Ch30_McComb 8/29/00 8:34 AM Page 477 PRINT "Pin 11: 0" END IF IF ((x AND 16) = 0) AND ((x AND 8) = 0) THEN PRINT "Pin 12: 1" ELSE PRINT "Pin 12: 0" END IF IF ((x AND 32) = 32) AND (x AND 8) = 8 AND (x AND 16) = 16 THEN PRINT "Pin 15: 1" ELSE IF ((x AND 32) = 0) AND (x AND 8) = 0 THEN PRINT "Pin 15: 1" ELSE PRINT "Pin 15: 0" END IF END IF PRINT "": PRINT "" FOR Count = 1 TO 10000: NEXT Count CLS WEND Before moving on, notice the use of the DIM keyword in the program shown in Listing 30.3. The DIM (for “dimension”) keyword tells Basic what kind of variables are used in the program. While using DIM is not absolutely mandatory (in QBasic and later), you’ll find that adopting it in your programs will not only help reduce errors and bugs. Most of all, it will make your programs run much faster. Without the DIM keyword, the Basic inter- preter creates an all-purpose “variant” variable type that can hold numbers of different sizes, as well as strings. Every use of the variable requires Basic to rethink the best way to store the variable contents, and this takes time. A Practical Application of the Parallel Port Input Lines You can use the status bits for the robot’s various sensors, like whiskers, line-tracing detec- tors, heat and flame detectors, and so forth. The simple on/off nature of these sensors makes them ideal for use with the parallel port. Listing 30.4 shows a simple demonstrator program that turns two drive motors forward until either switch located on the front of the robot is activated. Upon activation of either switch, the robot will back up for one second, spin on its axis for two seconds, then go forward again. The demonstrator program is an amalgam of techniques discussed previously in this chapter. The program assumes you have a two-wheel robot of the type described earlier in the chapter, with the motors controlled according to the definitions in Table 30.8. Whisker or bumper switches are attached to pins 10 and 11. LISTING 30.4. DECLARE SUB GetAway () DIM BaseAddress AS INTEGER, StatusPort AS INTEGER DIM SHARED DataPort AS INTEGER 478 COMPUTER CONTROL VIA PC PRINTER PORT Ch30_McComb 8/29/00 8:34 AM Page 478 DIM ControlPort AS INTEGER DIM x AS INTEGER, Count AS INTEGER BaseAddress = 888 DataPort = BaseAddress StatusPort = BaseAddress + 1 ControlPort = BaseAddress + 2 CLS PRINT "Press Ctrl+Break to end program " WHILE (1) OUT DataPort, 3 ' drive forward x = INP(StatusPort) + 1 ' read sensors IF (x AND 64) = 64 THEN ' if sensor 1 active GetAway END IF IF (x AND 128) <> 128 THEN ' if sensor 2 active GetAway END IF FOR Count = 1 TO 500: NEXT Count WEND A PRACTICAL APPLICATION OF THE PARALLEL PORT INPUT 479 OUT 10 1 2 3 4 5 6 7 8 10 11 12 9 13 14 15 0 7 6 5 4 3 2 1 23 22 21 20 19 18 17 16 8 9 ENABLE 15 +5vdc Output 14 13 A B C GND 74150 Inputs 24 11 D FIGURE 30.11 Basic wiring diagram for the 74150 multiplexer chip. Ch30_McComb 8/29/00 8:34 AM Page 479 SUB GetAway OUT DataPort, 15 ' back up SLEEP 1 ' wait one second OUT DataPort, 7 ' hard left turn SLEEP 2 ' wait two seconds END SUB Expanding the Number of Inputs Normally, you can have up to five sensors attached to the parallel port (though many ports only support three or four inputs, depending on their specific design). However, by using the ENABLE pins of the buffers in the 74367 chips, it is possible to select the input from a wide number of sensors. For example, using just four control lines with a 74150 data selector means you can route up to 16 sensors to the parallel port. See Fig. 30.11, above, for a pinout diagram of the 74150. From Here To learn more about… Read Computers and microcontrollers for robots Chapter 28, “An Overview of Robot ‘Brains’” Connecting computers and microcontrollers Chapter 29, “Interfacing with Computers to “real-world” devices such as motors and sensors and Microcontrollers” Using remote control to activate your robot Chapter 34, “Remote Control Systems” Using sensors to aid in robot navigation Part 6, “Sensors and Navigation” 480 COMPUTER CONTROL VIA PC PRINTER PORT Ch30_McComb 8/29/00 8:34 AM Page 480 Since its inception, the Basic Stamp, from Parallax, Inc., has provided the “on-board brains” for countless robotics projects. This thumbprint-sized microcontroller uses Basic- language commands for instructions and is popular among robot enthusiasts, electronics and computer science instructors, and even design engineers looking for an inexpensive alternative to microprocessor-based systems. The original Basic Stamp has been greatly enhanced, and new models sport faster speeds, more memory capacity, easier software programming, and additional data lines for interfacing with motors, switches, and other robot parts. In this chapter, you’ll learn the fundamentals of the Basic Stamp and how to use it in your robotics projects. You will also want to read Chapters 32 and 33, which provide full coverage of the BasicX and the OOPic, two other microcontrollers that use an embedded high-level language for programming. Inside the Basic Stamp The Basic Stamp is really an off-the-shelf PIC from Microchip Technologies (“PIC” stands for “programmable integrated circuit,” though other definitions are also commonly cited, including “peripheral interface controller” and “programmable interface controller”). Embedded in this PIC is a proprietary Basic-like language interpreter called PBasic. The 31 USING THE BASIC STAMP 481 Ch31_McComb 8/29/00 8:34 AM Page 481 Copyright 2001 The McGraw-Hill Companies, Inc. Click Here for Terms of Use. chip stores commands downloaded from a PC or other development environment. When you run the program, the language interpreter built inside the Stamp converts the instruc- tions into code the chip can use. Common instructions involve such things as assigning a given data line as an input or output or toggling an output line from high to low in typical computer-control fashion. The net result is that the Basic Stamp acts like a programmable electronic circuit, with the added benefit of intelligent control—but without the complexity and circuitry over- head of a dedicated microprocessor. Instead of building a logic circuit out of numerous inverters, AND gates, flip-flops, and other hardware, you can use just the Basic Stamp module to provide the same functionality and doing everything in software. (To be truth- ful, the Stamp often requires that at least some external components interface with real- world devices.) Nor do you need to construct a microprocessor-based board for your robot followed by the contortions of programming the thing in some arcane machine language. Because the Stamp accepts input from the outside world, you can write programs that interact with that input. For instance, it’s a slam dunk to activate an output line—say, one connected to a motor—when some other input (like a switch) changes logic states. You could use this scheme, for instance, to program your robot to reverse its motors if a bumper switch is activated. Since this is done under program control and not as hardwired circuitry, it’s easier to change and enhance your robot as you experiment with it. As of this writing there are several versions of the Basic Stamp, including the original Basic Stamp Rev D, the Basic Stamp I (“BSI”), the Basic Stamp II (“BSII”), and the Basic Stamp II-SX. Though in their day they were useful, the Rev D and BSI products are of lim- ited use in most robotics applications, which leaves the BSII and BSII-SX as the serious contenders. The BSII and BSII-SX share many of the same features, though the latter is faster. In this chapter, I’ll concentrate on the BSII, but in most cases the specifications and command sets apply to the BSII-SX as well. You should expect continued development of the Basic Stamp, with new and updated versions. Be sure to check the Parallax Web site at www.parallaxinc.com for news. The microcontroller of the Basic Stamp uses two kinds of memory: PROM (program- mable read-only memory) and RAM. The PROM memory is used to store the PBasic inter- preter; the RAM is used to store data while a PBasic program is running. Memory for the programs that you download from your computer is housed in a separate chip (but is still part of the Basic Stamp itself; see the description of the BSII module in the next section). This memory is EEPROM, for “electrically erasable programmable read-only memory” (the “read-only” part is a misnomer, since it can be written to as well). In operation, your PBasic program is written on a PC, then downloaded— via a serial connection—to the Basic Stamp, where it is stored in EEPROM, as shown in Fig. 31.1. The program in the EEPROM is in the form of “tokens”; special instructions that are read, one at a time, by the PBasic interpreter stored in the Basic Stamp’s PROM memory. During program execution, temporary data is kept in RAM. Note that the EEPROM memory of the Basic Stamp is nonvolatile—remove the power and its contents remain. The same is not true of the RAM. Remove the power from the Basic Stamp and any data stored in the RAM is gone. Also note that the PBasic interpreter, which is stored in the PROM memory of the microcontroller, is not replaceable. As a modern microcontroller, the Basic Stamp II is a little tight when it comes to available memory space. The chip sports only 2K of EEPROM and just 32 bytes of 482 USING THE BASIC STAMP Ch31_McComb 8/29/00 8:34 AM Page 482 RAM. Of those 32 bytes, 6 are reserved for storing the settings information of the input/output pins of the Basic Stamp, leaving only 26 bytes for data. For many robotics applications, the 2K EEPROM (program storage) and 26-byte RAM (for data storage) are sufficient. However, for complex designs you may need to use a second Basic Stamp or select a microcontroller—such as the Basic Stamp II-SX—that provides more memory. Stamp Alone or Developer’s Kit The Basic Stamp is available directly from its manufacturer or from a variety of dealers the world over. The prices from most sources are about the same. In addition to the BSI, BSII, and BSII-SX variations mentioned earlier, you’ll find that the Basic Stamp is available in several different premade kits as well as a stand-alone product. ■ BSII Module. The Basic Stamp module (see Fig. 31.2) contains the actual microcon- troller chip as well as other support circuitry. All are mounted on a small printed circuit board that is the same general shape as a 24-pin IC. In fact, the BSII is designed to plug into a 24-pin IC socket. The BSII module contains the microcontroller that holds the PBasic interpreter, a 5-volt regulator, a resonator (required for the microcontroller), and a serial EEPROM chip. ■ BSII Starter Kit. The starter kit is ideal for those just, well, starting out. It includes a BSII module, a carrier board, a programming cable, a power adapter, and software on CD-ROM. The carrier board, shown in Fig. 31.3, has a 24-pin socket for the BSII mod- ule, a connector for the programming cable, a power adapter jack, and a prototype area for designing your own interface circuitry. ■ Basic Stamp Activity Board. The Activity Board, which is typically sold without a BSII module, offers you a convenient way to experiment with the Basic Stamp. It contains four LEDs, four switches, a modular jack for experimenting with X-10 remote control modules, a speaker, and two sockets so you easily interface such things as serial ana- log-to-digital converters (ADCs). STAMP ALONE OR DEVELOPER’S KIT 483 Computer Program Basic Stamp Conversion to “tokens” or byte codes Download of “tokens” FIGURE 31.1 Programs are downloaded from your PC to the Basic Stamp, where they are stored in “tokenized” format in EEPROM. The PBasic interpreter executes these tokens one by one. Ch31_McComb 8/29/00 8:34 AM Page 483 484 USING THE BASIC STAMP FIGURE 31.2 The Basic Stamp II module, containing microcon- troller, voltage regulator, resonator, and EEPROM. FIGURE 31.3 The Basic Stamp carrier board, ideal for experiment- ing with the BSII. Sockets are provided for both the BSI and BSII. Ch31_McComb 8/29/00 8:34 AM Page 484 ■ Growbot and BOE Bot. The Growbot and BOE Bot products are small mobile robot kits that are designed to use the Basic Stamp microcontroller. The robots are similar (the BOE Bot is a little larger and heavier) and are able to accommodate more experiments. A BSII module is generally not included with either robot kit. ■ Basic Stamp Bug II. Another robot kit, the Basic Stamp Bug II, is a six-legged walking robot. The Bug is meant to be controlled with a BSI microcontroller, though you could refit it to use the BSII. The Basic Stamp module is extra. Physical Layout of the BSII The Basic Stamp II is a 24-pin device; 16 of the pins are input/output (I/O) lines that you can use to connect with your robot. For example, you can use I/O pins to operate a radio-controlled (R/C) servo. Or you can use a stepper motor or a regular DC motor, when you use them with the appropriate power interface circuitry. As outputs, each pin can source (that is, output 5 volts) 20mA of current or sink (output 0 volts) about 25 mA. However, the entire BSII should not source or sink more than about 80-100mA for all pins. You can readily operate a series of LEDs, without needing external buffer circuitry to increase the power-handling capability. Or you can connect the BSII to a Polaroid sonar range-finding module (see Chapter 38, “Navigating through Space”), various bumper switches, and other sensors. The “direction” of each I/O pin can be individually set, so some pins can be used for outputs and others for inputs. You can dynamically configure the direction of I/O pins during program execution. This allows you to use one pin as both an input and an output, should this be called for. Fig. 31.4 shows the pin layout of the BSII. The Basic Stamp II supports three ports, referred to as A, B, and C. Port A is used for internal connections, namely, the serial lines to the outboard EEPROM chip, as well as the RS-232 serial connections to and from the PC that is used for programming. This leaves two full 8-bit ports, B and C, for use as I/O lines. Through PBasic commands, you can control all eight bits of the each port together or each pin individually. PHYSICAL LAYOUT OF THE BSII 485 1 12 13 24 Power In Gnd Reset +5V P15 P14 P13 P12 P11 P10 P9 P8 P7 P6 P5 P4 P3 P2 P1 P0 Gnd Atn RX TX FIGURE 31.4 The layout of the pins on the Basic Stamp II module. Ch31_McComb 8/29/00 8:34 AM Page 485 [...]... from the TX 1 24 Power In RX Gnd Atn Reset Gnd +5V P0 P 15 P1 P14 P2 P13 P3 P 12 P4 P11 P5 P10 P6 P9 P7 12 13 P8 FIGURE 32. 2 Pinout diagram of the BasicX -2 4 chip Note that several of the pins serve double duty (as explained in the text) Ch 32_ McComb 8 /29 /00 8:33 AM Page 50 5 PROGRAMMING THE BX -2 4 50 5 FIGURE 32. 3 The easiest way to experiment with the BX -2 4 is to use the carrier board that is included as part. .. to the other components of the robot You should not rely on the BX -2 4 ’s on-board regulator for this task Pinout Diagram for the BX -2 4 Fig 32. 2 shows the pinout diagram of the BX -2 4 as well as the functions of its 24 pins Of main interest are the following: I Pin 24 This is the unregulated power input Apply an unregulated DC voltage of 5. 5 to 15 volts here The onboard regulator will provide a stable 5. .. LEDs Pin 27 serves as the output capture I/O line As with pins 25 and pin 26 , this pin is available if you solder directly to the BX -2 4 chip Programming the BX -2 4 To program the BX -2 4 you need to purchase the BasicX -2 4 developer’s kit, which contains one BX -2 4 , a programming cable, a power supply, a “carrier board” (see Fig 32. 3), and programming software on CD-ROM You plug the BX -2 4 into the carrier... FOR THE BX -2 4 50 3 FIGURE 32. 1 The BasicX -2 4 consists of surface-mount integrated circuits on a small circuit board The BX -2 4 circuit board has the same dimensions as a standard 24 -pin IC The BX -2 4 board comes with its own five-volt voltage regulator, which provides enough operating current for all the components on the board, plus several LEDs or logic ICs If you plan on using the BX -2 4 to operate a robot, ... not work on the other Additionally, the BX -2 4 has several additional features not found in the Basic Stamp II, such as built-in analog-to-digital conversion and 32K of EEPROM memory Fig 32. 1 shows the BX -2 4 “chip,” which (like the Basic Stamp) is actually several integrated circuits on a small circuit board The layout of the pins on the BX -2 4 is identical to that of any standard-sized 24 -pin IC, so... registers of the BX -2 4 chip The BX -2 4 is based on the Atmel AT90S 853 5 microcontroller (download the data sheet for the ‘ 853 5 to learn more about the internals of this powerful chip) By controlling the hardware registers of the BX -2 4 you can program features that the BasicX language itself does not directly support For example, by setting a few registers for Timer1 (one of three timers in the Atmel ‘ 853 5), you... for the BX -2 4 circuitry I Pin 23 , 4 This is the ground You can use either or both of these pins when connecting to other circuitry I Pin 21 This is for 5 vdc input Instead of using pin 24 for power, you may directly apply regulated 5 vdc to this pin Or, if power is applied through pin 24 , pin 21 serves as a convenient source of regulated 5 vdc power The voltage regulator on the BX -2 4 Ch 32_ McComb 8 /29 /00... selling point of the BasicX -2 4 (which we’ll refer to as the BX -2 4 from here on) is that it is pin-for-pin compatible with Parallax’s Basic Stamp II That is, the functions of all 24 pins of the BX -2 4 replicate the functions of the Basic Stamp II, including power and ground connections It’s important to note, however, that the BX -2 4 is not a Stamp “clone.” The two microcontrollers don’t share the same programming... regular 24 -pin socket Additional plated-through holes are provided on either end of the BX -2 4 board, making it just slightly longer than the Basic Stamp II These holes provide connections to additional input/output lines provided on the BX -2 4 I’ll get to those in a bit The BX -2 4 directly supports 16 input/output (I/O) lines, the same number as the Basic Stamp II For each I/O line, or pin, you can change the. .. Ch 32_ McComb 8 /29 /00 8:33 AM Page 5 02 5 02 USING THE BASICX MICROCONTROLLER ment platform, you write software for the microcontroller using a custom program editor The software is then compiled to a series of tokens or bytecodes and then downloaded to the microcontroller Joining the ranks of powerful embedded-language programmable microcontrollers is the BasicX -2 4 , by NetMedia, a company founded by the . 479 OUT 10 1 2 3 4 5 6 7 8 10 11 12 9 13 14 15 0 7 6 5 4 3 2 1 23 22 21 20 19 18 17 16 8 9 ENABLE 15 +5vdc Output 14 13 A B C GND 74 150 Inputs 24 11 D FIGURE 30.11 Basic wiring diagram for the 74 150 multiplexer. their logic is the reverse of the others. The code in the test program “compensates” for the active-low condition by reversing the logic in the If expressions. Also note the less-than-straightforward. all eight bits of the each port together or each pin individually. PHYSICAL LAYOUT OF THE BSII 4 85 1 12 13 24 Power In Gnd Reset +5V P 15 P14 P13 P 12 P11 P10 P9 P8 P7 P6 P5 P4 P3 P2 P1 P0 Gnd Atn RX TX FIGURE