• No results found

To test the downloaded program and wired circuit to verify that it simu- simu-lates a logic gate

PROGRAMMING LOGIC FUNCTIONS

3. To test the downloaded program and wired circuit to verify that it simu- simu-lates a logic gate

MATERIALS Qty.

1 BASIC Stamp 2 development board (such as the Board of Education or HomeWork board available from Parallax)

1 BS2 module (this may be mounted on the development board) 1 PC with MS Windows

1 PBASIC editor from Parallax

1 Serial cable (for downloading to BS2 module on development board) 1 Red LED (light-emitting diode)

2 Push-button switches, N.O. contacts 1 150- resistor

2 10-k resistors

Note 1: BASIC Stamp 2 module, development board, PBASIC software, and both experiment and reference manuals are available from Parallax, Inc., 599 Menlo Drive, Rocklin, CA 95765. Many of the manuals and software can be downloaded free from Parallax’s websites. General website: www.parallax .com. Telephone: 916-624-8333.

Note 2: Check with your instructor.

Older version of BS2 module. It uses a serial cable for downloading from the computer’s serial port to the BS2 module.

Newer version of BS2 module. It uses a USB (universal serial bus) cable for downloading from the computer’s USB port to the BS2 module.

Note 3: This lab is written for the older version of BS2 module using a serial cable.

SYSTEM DIAGRAMS

The first circuit to be wired to the BASIC Stamp 2 module (BS2 IC) is represented in Fig. 3-25. The BS2 module is functioning as a two-input logic gate. Input switches B and A are wired as active HIGH push-buttons con-nected to ports 11 and 12 (P11 and P12) of the BS2 module. An active HIGH input switch means that P11 is normally LOW but goes HIGH when push-button switch SW2is pressed or closed, connecting it directly to Vddor () of the power supply. The PBASIC program downloaded into the BS2 IC must configure P11 and P12 as inputs. One red LED with limiting resistor is con-nected between port 1 (P1) of the BS2 module and Vss () of the power sup-ply. Port 1 (P1) of the BS2 module must be defined in the PBASIC program as an output. The red LED will only light when a HIGH appears at port 1 of the BS2 IC.

Programming is accomplished on a MS Windows–based personal computer using the PBASIC editor for the BS2 BASIC Stamp. The program is downloaded using a serial cable between the PC’s serial port and the BASIC

Copyright © by McGraw-Hill.

Stamp 2 development board. The BS2 module stores the program in EEPROM memory. The PBASIC program is interpreted and executed by the micro-controller on the BS2 module.

The following PBASIC program will cause the BS2 module to operate as a two-input AND gate. The first line of the program (‘2-input AND function) is the title of the program. The apostrophe symbol (‘) at the beginning of this line identifies it as a remark statement. Remark statements are not executed by the BS2 IC but are displayed in the PBASIC listing to aid human understanding of the program. Lines 2 through 7 are statements used to declare the variables used and configure input and outputs. As an example, line 2 (A VAR Bit

‘Declare A as a variable, 1 bit) tells the microcontroller that a variable called A will be used in the program and it will only be 1 bit long. The remark section of line 2 helps clarify the PBASIC statement in more human terms.

The ‘2-input AND function program contains a continuous program loop starting with a label (Ckswitch:). Labels are identified by names ending with a colon (:). Labels are places in a program that other lines of code can easily locate and jump to. The label (Ckswitch:) shows the beginning of the main routine, while the second label (redLED:) identifies the start of the subroutine, that lights the red LED.

In the Ckswitch: main program routine line 9 (OUT1 ⴝ 0) initializes output port 1 to 0. Lines 10 and 11 (Aⴝ IN12 and B ⴝ IN11) assign the value (0 or 1) at the two input ports to variables A and B. Line 12 (Yⴝ A & B) logically ANDs the input A and B placing the output in variable Y. IF-THEN statements are decision points in a program. Line 13 (IF Y ⴝ 1 THEN redLED) checks to see if Y equals 1; if the first part of the statement is true, then the BS2 executes the second part of the IF-THEN and jumps to the

66

Fig. 3-25 BASIC Stamp 2 module programmed to generate two-input logic functions.

tok20807_ch03_027-070.qxd 12/10/12 3:43 PM Page 66

subroutine redLED:, which lights the red LED. However, if the first part of the statement (Yⴝ 1) is false, then the BS2 microcontroller disregards the rest of the program line and drops to the next line of code which is GOTO Ckswitch. The GOTO statement causes a jump back to the beginning of the main routine labeled Ckswitch:. The main routine continues to repeat hundreds of times per second.

For reference, a diagram of Parallax’s Board of Education (BOE) devel-opment board is shown in Fig. 3-26. A few of its features are identified on the drawing, including:

a. Serial input (for downloading PBASIC programs from PC with serial cable)

b. BASIC Stamp 2 module (24-pin DIP)

c. Power connections—either 9-V battery or dc supply with barrel plug d. ON/OFF switch and power indicator

e. Reset button—for restarting PBASIC programs f. Voltage regulator

g. Breadboard, solderless

h. Power supply header [Vdd(⫹) and Vss(⫺)]

i. BS2 input/output (I/O) pins header

'2–input AND function 'Title of program (Lab 3–10) A VAR Bit 'Declare A as a variable, 1 bit B VAR Bit 'Declare B as a variable, 1 bit Y VAR Bit 'Declare Y as a variable, 1 bit INPUT 11 'Declare port 11 as an input INPUT 12 'Declare port 12 as an input

INPUT 1 'Declare port 1 as an output (red LED) Ckswitch: 'Label for check switch routine

OUT1 = 0 'Initialize: port 1 at 0, red LED off A = IN12 'Assign value: port 12 input to variable A B = IN11 'Assign value: port 11 input to variable B Y = A & B 'Assign value: A ANDed with B to variable Y

IF Y = 1 THEN redLED 'If Y = 1 then go to red subroutine, otherwise next line GOTO Ckswitch 'Go to Ckswitch: begin check switch routine again

redLED: 'Label for lighting red LED, means HIGH OUT1 = 1 'Output P1 goes HIGH, lights red LED PAUSE 100 'Pause 100 ms

GOTO Ckswitch 'Go to Ckswitch: begin check switch routine again

Copyright © by McGraw-Hill.

68

Fig. 3-26 Board of Education development board. (Used by permission of Parallax, Inc.) Reset

Header (BS2 I/O pins)

Breadboard

9-V battery or dc supply with barrel

1. Refer to Fig. 3-25. Wire the circuit to a BS2 module using a development board such as Parallax’s BOE (see Fig. 3-26). BS2 pins P11 and P12 are both used as inputs. BS2 pin P1 is used as an output in this circuit.

2. Using an MS Windows–based PC, start the BASIC Stamp editor and write