ВУЗ: Не указан

Категория: Не указан

Дисциплина: Не указана

Добавлен: 12.06.2025

Просмотров: 1658

Скачиваний: 0

ВНИМАНИЕ! Если данный файл нарушает Ваши авторские права, то обязательно сообщите нам.

Page 20 · Basic Analog and Digital

(misc.) Jumper wires

The Pushbutton

There's just one new part and circuit symbol to introduce for this experiment, the pushbutton in Figure 2-1. Note how each terminal on the circuit symbol corresponds to two pins on the part. If you want to connect to a particular terminal shown on the symbol, you can connect to either (or both) of the two corresponding pins on the part.

The open space in the circuit symbol indicates that the switch is normally open. When the two terminals of a switch are not connected, the switch is referred to as an open circuit. Under normal circumstances (when the pushbutton is not pressed), the circuit is open, thus the name normally open.

Figure 2-1

Pushbutton Circuit Symbol Compared to the Component

Building the Circuit

Figure 2-2 shows the schematic for this experiment. Remember to think of a schematic as a list of components and connections. For example, the anode of the right LED is connected to the P5 terminal on the Board of Education. The cathode is connected to one terminal of a 470 resistor. The other pin on that same resistor is connected to the Vss terminal on the Board of Education, and so on. Follow schematics faithfully when constructing circuits.


P1

P0

Chapter 2: Introduction to Bit Crunching · Page 21

Vdd Vdd

220 Ω

220 Ω

10 kΩ

10 kΩ

Figure 2-2

Schematic featuring two

pushbutton circuits and

Vss

Vss

two LED circuits.

P5

P4

470 Ω

470 Ω

LED

LED

Vss

Vss

Before making a PBASIC program telling the BASIC Stamp how to interface with this circuit, it's essential to understand how the circuit works. The LEDs are pretty straightforward. Set P4 high and the LED lights up; set P4 low and the LED goes dark again. The LED circuit connected to P5 works the same way.

Now, what about the pushbuttons? Let's look at what pin P0 sees when the pushbutton is pressed, then not pressed. When the pushbutton is pressed, P0 gets connected directly to Vdd, which is 5 volts. P0 sees a high signal. When the pushbutton is not pressed, P0 is connected to Vss (0 volts) through the 10 k resistor. Then P0 sees a low signal. This concept applies to both pushbuttons shown in Figure 2-2.

Figure 2-3 shows a breadboard example of the circuit schematic. Of the two BASIC Stamp I/O pins used for the pushbuttons, the lower pin (P0) is connected to the right pushbutton. Likewise, the right pin (P1) is connected to the lower pushbutton. The reason


Page 22 · Basic Analog and Digital

the wires for the pushbuttons cross relates to the way binary numbers are written, which will be explained later in this experiment.

P0 P1 P2 P3 P4 P5 P6 P7 P8 P9 P1 P1 P1 P1 P1 P1 X 0 1 2 3 4 5 2

X3

Vdd

Vni

Vss

Figure 2-3

Breadboard Example

Entering binary numbers on the pushbuttons will be easiest if you orient the Board of Education as shown.

Note that the left button is connected to pin P1, and the right button is connected to P0.

Program Listing 2.1 makes the left LED in Figure 2-3 light up when the left pushbutton is pressed. Likewise, the right LED lights up when the right pushbutton is pressed. The program also displays the activity of the pushbuttons in the Debug Terminal.

Programming the Project

Here is a more precise description of the program specifications for the pushbuttons and LEDs.

When P0 receives a low signal, P5 should send a low signal.

When P0 receives a high signal, P5 should send a high signal.

When P1 receives a low signal, P4 should send a low signal.

When P1 receives a high signal, P4 should send a high signal.

The Debug Terminal can be used to display what the BASIC Stamp receives at pins P0 and P1. DEBUG commands are used to display the binary values the BASIC Stamp receives as well as their decimal equivalents in the Debug Terminal.

Let's see how this can be done using PBASIC. Enter the Program Listing 2.1 into the BASIC Stamp Editor, and save it as PL2_1R0.bs2. This stands for Program Listing 2.1 Revision 0. Make sure the Board of Education has power and the programming cable is properly connected, then run the program.


Chapter 2: Introduction to Bit Crunching · Page 23

'Basic Analog and Digital - PL2_1R0.bs2

'Program Listing 2.1 Revision 0.

'{$STAMP BS2}

'{$PBASIC 2.5}

a

VAR

Bit

b

VAR

Bit

d

VAR

Nib

INPUT 0

INPUT 1

OUTPUT 4

OUTPUT 5

DEBUG CLS

DO

a = IN0 b = IN1 OUT4 = b OUT5 = a

d = (2*b) + (1*a)

DEBUG HOME, "State of pin P0 is ", BIN a, CR DEBUG "State of pin P1 is ", BIN B, CR, CR DEBUG "2-bit binary number: ", CR

DEBUG "P1

P0", CR

DEBUG

" ", BIN

b, "

", BIN

a, CR, CR

DEBUG

"Decimal

equivalent: ",

DEC1 d, CR

LOOP

The Output

Here's how the program should work. When no pushbuttons are pressed, the Debug Terminal output should match Figure 2-4, and both LEDs should be off. Try pressing the right button (in Figure 2-3). Did the right LED light up? Did the state of P0 in the Debug Terminal change to 1? Is the decimal equivalent 1? If so, it looks like your circuit and program are working well so far.

Page 24 · Basic Analog and Digital

Figure 2-4

Debug Terminal

Output for Program

Listing 2.1

So how do you count from decimal-0 to decimal-3 using the binary pushbuttons? The two-bit binary equivalent of decimal-0 is 00. When you don’t press either of the pushbuttons, the decimal output is 0 in the Debug Terminal. When you press the right button, you get 01, which has a decimal equivalent of 1. When you press the left button, you get 10, which has a decimal equivalent of 2. When you press both pushbuttons, you get 11, which has a decimal equivalent of 3.

About the Code

As with the program of the previous chapter, the first lines start with an apostrophe, so they are comments and compiler directives, and the BASIC Stamp ignores them.

'Basic Analog and Digital - PL2_1R0.bs2

'Program Listing 2.1 Revision 0.

'{$STAMP BS2}

'{$PBASIC 2.5}


Chapter 2: Introduction to Bit Crunching · Page 25

Next, three variables are defined. Variables can be used to store values while the program is running. The letters a and b are defined as variables that store 1-bit each. So, the variable a can store a single binary digit, likewise with the variable b. The letter d is defined to be a variable that stores a "nibble" of binary information.

a

VAR

Bit

b

VAR

Bit

d

VAR

Nib

Memory and Food: A bit of memory can store one binary digit, either a 0 or a 1.

A nibble of memory stores 4-bits.

A byte (pronounced bite) stores 8-bits.

A word stores 16 bits.

Since a bit, a nibble, and a byte, all sound like references to eating food, perhaps a better name for 16-bits might have been "dinner".

This segment of code uses commands introduced in the last chapter. First, two I/O pins are declared inputs and two more pins are declared outputs. Then the Debug Terminal is opened and cleared.

INPUT 0

INPUT 1

OUTPUT 4

OUTPUT 5

DEBUG CLS

We want the BASIC Stamp keep checking the inputs over and over again. We also want the BASIC Stamp to automatically update the LEDs and the Debug Terminal with the latest information on the pushbuttons. The way to accomplish this is to keep repeating the program inside a DO…LOOP loop. To define the start point of the loop we use DO and to send the program back to this point we’ll use the LOOP command.

DO

Next, we need to check the state of the pushbuttons by checking the input at pins P0 and P1. The first of these two commands sets the bit variable a equal to the state measured at pin P0. The second command sets the bit variable b equal to the state measured at pin P1.

a = IN0 b = IN1

Page 26 · Basic Analog and Digital

Next, we need to set the output at pin P4 equal to the input taken at pin P1. The left LED which is connected to P4 will light up when the left button, which is connected to P1, is pressed. Likewise, we need to set the output at pin P5 equal to the input measured at pin P0.

Since the input values were set to the variables, a and b, we can use a and b to dictate the output values at pins P4 and P5.

OUT4 = b

OUT5 = a

We could just as easily have used the commands OUT4=IN1 and OUT5=IN0; however, using variables to store the values in memory has advantages as the programs get more complicated. In the next experiment, it will be necessary to use variables to store values.

The reason we used variables in this program is because they can be manipulated arithmetically, and the next task is to convert from binary to decimal. To do this, multiply the variable b by 2 and the variable a by 1 and add them together. The nibble variable d is used to store this new value. This is the method for converting a 2-bit binary number to a decimal number. The next section shows how to do this for a binary number of any size.

d = (2*b) + (1*a)

BASIC Stamp Memory:

RAM: The BASIC Stamp has 26 bytes of RAM (random access memory) that can be used for storing variable values. Another 6 bytes of RAM is used to interface the BASIC Stamp with its I/O pins.

EEPROM: Short for electrically erasable programmable read only memory, EEPROM is used mainly to store the PBASIC programs. EEPROM can also be used to store data values that do not change frequently.

In the calculation we just did using PBASIC, the parentheses are necessary to maintain the normal algebraic order of operation. This is because the BASIC Stamp performs its math beginning at the left. Then, it performs each operation it encounters while checking the line from left to right.

Without the parentheses, d would be set equal to the value ((2 x b + 1) x a) because that's the order in which the operators (+, -,*, /, etc) are encountered. When parentheses are used, the BASIC Stamp completes operations within parentheses first, and then it does its sweep of operations from left to right.