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

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

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

Добавлен: 12.06.2025

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

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

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

Chapter 1: Analog Voltage and Binary States · Page 9

X3

P15

P14

P13

P12

P11

P10

P9

P8

P7

P6

P5

P4

LM358

P3

P2

P1

P0

X2

Figure 1-8

Breadboard Example

Compare this breadboard example to the schematic from Figure 1-7. Is the LM358 connected right? Does Vdd go to pin 8 and does Vss go to pin 4?

It turns out that the answer to both questions is yes. Vss is connected by a wire to the leftmost terminal on the pot. Then another wire connects from the leftmost pot terminal to pin 4 on the LM358.

Since you can follow a wire all the way from pin 4 on the LM358 to the Vss terminal, that means it is in fact connected directly to Vss.

IMPORTANT: Pay careful attention to placing the LM358 so that the index mark is to the top, as shown on this breadboard example. If you place it in reverse, the op-amp will be ruined after the battery or power supply is connected to the Board of Education.

Programming the Project

Program State_of_P7.bs2 shows how PBASIC can be used to instruct the BASIC Stamp to do several tasks. First, the BASIC Stamp monitors the state of the pin P7, which is set to function as an input. Remember, P7 is connected to the wiper terminal of the pot.

Depending on the analog voltage level at pin P7, the BASIC Stamp interprets the input as low or high (binary 0 or 1). As soon as the input to P7 receives the high signal, the BASIC Stamp sends a high signal to the LED circuit via pin P12. When the input is low, a low signal is sent to P12. The Debug Terminal is also used to monitor the state of pin P7.

Enter Program State_of_P7.bs2 into the BASIC Stamp Editor, and save it under a convenient name, such as State_of_P7_1_1R0.bs2. This stands for Program Listing 1.1 Revision 0. Make sure the programming cable is properly connected to your Board of Education and to the serial port on your computer. Also make sure that a battery or power supply is properly connected, then run the program by pressing the Run Ź button, or Ctrl-R.


Page 10 · Basic Analog and Digital

'Basic Analog and Digital - State_of_P7.bs2

'Check the state of P7 and show it on the Debug Terminal.

'{$STAMP BS2}

'{$PBASIC 2.5}

DEBUG CLS

INPUT 7

OUTPUT 12

DO

OUT12 = IN7

DEBUG HOME, "The state of P7 is ", BIN IN7

LOOP

About the Code

The first lines start with an apostrophe. This means that they’re comments and not PBASIC commands. The first line reminds you of the book and file name, for future reference.

' Basic Analog and Digital - State_of_P7.bs2

The second comment is a description of the program. What does the program do?

' Check the state of P7 and show it on the Debug Terminal.

The next two lines are special comments. We call them compiler directives and they are intended to identify the BASIC Stamp and the PBASIC version we are using. For example if you’re following this manual with a BASIC Stamp 2 SX, you might replace the compiler directive “'{$STAMP BS2}” with “'{$STAMP BS2SX}”.

'{$STAMP BS2}

'{$PBASIC 2.5}

It's good to start the Debug Terminal and clear it before using it to display data. This way you will avoid inadvertently displaying outputs from previous programs that were in the BASIC Stamp's memory. The Debug Terminal starts automatically the first time it encounters the DEBUG command in a PBASIC program. This DEBUG command clears the Debug Terminal after it is opened:

DEBUG CLS

The BASIC Stamp needs to be told how to treat the I/O pins connected to the circuit. They can either be set to function as inputs or outputs. This PBASIC command sets BASIC Stamp I/O pin P7 to function as an input pin:


Chapter 1: Analog Voltage and Binary States · Page 11

INPUT 7

Likewise, I/O pin P12 functions as output with this command:

OUTPUT 12

The rest of the program should be done over and over again, so this is a good place to put a DO…LOOP loop. So, at the point we want to start to repeat the code we put:

DO

Later in the program, the command LOOP is entered. Each time the program gets to the LOOP command, it returns to DO and starts executing instructions all over again.

The next task is to make the LED connected to pin P12 light up when the voltage at P7 is high enough to qualify as a binary high signal. In other words, if the input value measured at P7 is a binary-1, then the output at P12 should be set to binary-1. Although there are several ways to accomplish this, the easiest way is to set the binary output value of pin P12 equal to the binary input value of pin P7.

OUT12 = IN7

The DEBUG command can be used to display the signal levels received by an I/O pin functioning as an input in the Debug Terminal. The DEBUG command below prints three different items. When printing more than one item with a DEBUG command, always separate each of the items with commas.

DEBUG HOME, "The state of P7 is ", BIN IN7

The first item displayed using the DEBUG command is HOME, and it sends the cursor to the top-left "home" position in the Debug Terminal. Note how HOME is followed by a comma to separate it from the next item. The next item is a message in quotes: "The state of pin P7 is ". Whenever you want to display a text message in the Debug Terminal, use quotes. The third item is BIN IN7, which tells the Debug Terminal to display the binary input value measured at pin P7.

We want the BASIC Stamp keep checking the voltage at P7 over and over again. We also want the BASIC Stamp to automatically update the LED and the Debug Terminal with the latest information from P7. This is accomplished by repeatedly sending the program back to the loop we created earlier.

Page 12 · Basic Analog and Digital

To send the program back to the DO statement to start the process all over again, use the command:

LOOP

Troubleshooting

Here are a few tips on things to check if your program doesn't work as expected.

It often takes a few tries to catch all the wiring mistakes and programming errors. The most common mistake is program entry errors. In some cases the BASIC Stamp Editor will tell you there is a mistake. For example if you misspelled a command, the BASIC Stamp Editor will not understand it, and it will tell you which term it didn't understand by highlighting it and displaying a brief message.

In other cases the program will run even though a line of code was typed incorrectly. For example you might have typed 13 when you meant to type 12. With a mistake like this, when you try to run the program, the LED connected to pin P12 won't light up when you expect it to because the high signal gets sent to pin P13 instead.

Another common mistake is to plug a wire into the wrong socket on the breadboard. If an LED is not turning on and off the way it should, and there are no programming errors, check the wiring. Also check to make sure the cathode and anode of the LED are connected to the proper places. When an LED is hooked up backwards it won't emit light.

If the information in the Debug Terminal appears garbled or nonsensical, try closing the Debug Terminal and running the program again.


Chapter 1: Analog Voltage and Binary States · Page 13

The Output

As you adjust the potentiometer, note how the LED at the voltage follower's analog output can vary in brightness. Meanwhile the LED circuit driven by P12 either turns on or off. This characterizes the difference between analog voltage and digital (binary) voltage.

The output displayed in the Debug Terminal should resemble the output shown in Figure 1-9. The state of P7 will either be 0 or 1. Correspondingly, the LED circuit driven by P12 will either be off or on.

Figure 1-9

Debug Terminal

Output for Program

Adjust the potentiometer until you have found the threshold voltage. You will know when you've found it because the Debug Terminal will indicate that the state of P7 skips back and forth between 0 and 1 with just the slightest adjustment of the pot. Note the position of the potentiometer. After we build a DC voltmeter in Chapter #3, we can find out how close we are to 1.4 volts, which is the actual threshold voltage of a BASIC Stamp I/O pin when it's functioning as an input.

Page 14 · Basic Analog and Digital

About the Comparator

Using PBASIC, we programmed the BASIC Stamp to function as a comparator. A comparator is so named because it's a circuit that compares its input voltage to a particular voltage, also called the threshold voltage. If the input voltage is higher than the threshold voltage, the comparator sends a high signal at its output. If the input to the comparator is below the threshold voltage, it sends a low signal.

In the case of our circuit and program, when the analog voltage at pin P7 is below 1.4 volts, the BASIC Stamp sends a low signal (0 volts) at pin P12. When the analog voltage at pin P7 is above 1.4 volts a high signal (5 volts) is sent from pin P12. As you can see in the Debug Terminal, the BASIC Stamp interprets analog input below 1.4 volts as low (0) and input above 1.4 volts as high (1).

Operating a comparator near the threshold voltage is interesting because you can make a small change in voltage at a BASIC Stamp input pin, say from 1.3 to 1.5 volts, and it results in a fairly large change, from 0 to 5 volts at the output.

Chapter 1: Analog Voltage and Binary States · Page 15

What have I learned?

On the lines below, insert the appropriate words from the list on the left.

A __________ shows circuit symbols connected to each other by lines.

carbon

Each circuit symbol corresponds to a component and the lines connecting

the symbols work like a list of connections that can be used to guide the

op-amp

construction of a working circuit on a __________.

An __________ is an analog building block that was used in this

analog

experiment as a voltage follower.

threshold

In this experiment, the potentiometer was used as a source of __________

output

voltage.

The voltage at the wiper terminal of the potentiometer can be made to vary

schematic

depending on where the wiper is in contact with the __________ element.

A comparator is a device that sends a binary output that depends on

breadboard

whether its analog input is above or below a certain __________ voltage.

A comparator can react to a small change in input voltage with a

comparatively large change in __________ voltage.


Page 16 · Basic Analog and Digital

Questions

1.Circle the word that makes the sentence true: The input for a voltage follower is at the ( inverting / non-inverting ) terminal of the op-amp in this experiment.

2.How do you tell the difference between the cathode and the anode on an LED from your parts kit?

3.If the threshold for a comparator is 2.5 volts, and the input is at 1.5 volts, what's the output going to be?

4.Explain what the command DEBUG HOME does. What has to be done to display more than one item using a single DEBUG command?

5.What command would you use to set pin P8 to function as an input?

Challenge!

1.Add another LED to the circuit to the Board of Education and use pin P11 to drive it in an inverted state. In other words, when one LED is on, the other off. Hint: Add 1 to the output value at P11 to invert it.

2.Modify the code from Program State_of_P7.bs2 so that the LED output flashes on and off when the potentiometer output is above the threshold voltage of the BASIC Stamp input pin. Hint: You can use the command pause 500 to make the program pause for half a second.

3.Modify Program State_of_P7.bs2 to cause one LED to light up when the input voltage at pin P7 is above the threshold voltage and the other LED to light up when the voltage at pin P7 is below threshold voltage.

Chapter 1: Analog Voltage and Binary States · Page 17

Why did I learn it?

In this experiment, we compared a binary LED output to an analog LED output. Aside from knowing that the voltage at the wiper terminal had just crossed the threshold voltage, there was no way of indicating the potentiometer's position or the LEDs brightness. On the other hand, in the neighborhood of the threshold voltage, fine variations in analog voltage could be detected.

Even with the limited amount of analog information provided by a binary input, we were able to develop a device called a comparator, which has many applications in electronics design. As we'll discover in a later experiment, the 555 timer in you’re A & D parts kit can do some pretty amazing things. This is due in part to two microscopic comparators in the chip.

How can I apply this?

In later experiments, we'll use the threshold voltage to measure the frequency of sound for record and playback purposes. We can also build another type of analog to digital converter using a very simple circuit, the BASIC Stamp and the concept of threshold voltage. We'll use this technique to measure light intensity as well as values for capacitors from the Analog and Digital parts kit.

Chapter 2: Introduction to Bit Crunching · Page 19

Chapter #2: Introduction to Bit Crunching

An important step in learning how to make the BASIC Stamp process analog data is learning how to make it send and receive binary numbers. It's also important to understand how binary numbers work, and how to convert from a binary number to a decimal number.

BASIC COMMUNICATION

This experiment introduces some techniques for transmitting and receiving binary numbers using the BASIC Stamp. In this experiment, we'll make a binary keypad for transmitting binary numbers to the BASIC Stamp. The BASIC Stamp will also be programmed to process and display the binary numbers it receives. The binary numbers will be displayed by LEDs as well as by the Debug Terminal. The Debug Terminal will also come in handy for monitoring and displaying the binary numbers in decimal form.

In What’s a Microcontroller?, we learned that binary is the number system used by microcontrollers, and that it works with two digits, 0 and 1. The BASIC Stamp is part of an entire class of digital electronic devices that can interpret 0 volts as binary-0 and 5 volts as binary-1.

Binary is useful for describing both states and numbers. In terms of states, the two digits in the binary number system (0 and 1) can be used to describe off/on, closed/open, no/yes, etc. Combinations of binary digits can be used to describe numbers. For example, the binary numbers 101, 110, and 111 describe the decimal numbers 5, 6, and 7. These numbers can in turn be used to describe analog information, such the position of a door as it swings open and closed.

Parts Required

Gather these parts from your parts kit and let's get started.

(2) 470 resistors

(2) 220 resistors

(2) 10 k resistors

(2) Pushbutton switches

(2) Red LEDs