Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

Whats A Microcontroller v3

.0.pdf
Скачиваний:
22
Добавлен:
14.04.2015
Размер:
5.21 Mб
Скачать

Lights On – Lights Off · Page 31

When you start building your circuit, make sure to check it against the schematic symbol and part drawing. For the part drawing, note that the LED’s leads are different lengths. The longer lead is connected to the LED’s anode, and the shorter lead is connected to its cathode. Also, if you look closely at the LED’s plastic case, it’s mostly round, but there is a small flat spot right near the shorter lead that that tells you it’s the cathode. This really comes in handy if the leads have been clipped to the same length.

LED Test Circuit Parts

(1) LED – Green

(1) Resistor – 470 Ω (yellow-violet-brown)

Identifying the parts: In addition to the part drawings in Figure 2-2 and Figure 2-4, you can use the photo on the last page of the book to help identify the parts in the kit needed for this and all other activities.

Building the LED Test Circuit

You will build a circuit by plugging the LED and resistor leads into small holes called sockets on the prototyping area shown in Figure 2-5. This prototyping area has black sockets along the top and along the left. The black sockets along the top have labels above them: Vdd (+5 V), Vin (the unregulated voltage straight from your battery or power supply), and Vss (0 V, also called ground). These are called the power terminals, and they will be used to supply your circuits with electricity. The black sockets on the left have labels like P0, P1, up through P15. These are sockets that you can use to connect your circuit to the BASIC Stamp module’s input/output pins.

Vdd

Vin

Vss

X3

 

 

P15

 

 

P14

 

 

P13

 

 

P12

 

 

P11

 

 

P10

 

 

P9

 

 

P8

 

 

P7

 

 

P6

 

 

P5

 

 

P4

 

 

P3

 

 

P2

 

 

P1

 

 

P0

 

 

X2

 

 

Figure 2-5

Prototyping Area

Power terminals (black sockets along top), I/O pin access (black sockets along the side), and solderless breadboard (white sockets)

Page 32 · What’s a Microcontroller?

Input/output pins are usually called I/O pins, and after connecting your circuit to one or more of these I/O pins, you can program your BASIC Stamp to monitor the circuit (input) or send on or off signals to the circuit (output). You will try this in the next activity.

The white board with lots of holes in it is called a solderless breadboard. You will use this breadboard to connect components to each other and build circuits. This breadboard has 17 rows of sockets. In each row, there are two five-socket groups separated by a trench in the middle. All the sockets in a 5-socket group are connected together. So, if you plug two wires into the same 5-socket group, they will make electrical contact. Two wires in the same row but on opposite sides of the center trench will not be connected. Many devices are designed to be plugged in over this trench, such as the pushbutton we will use in Chapter 3.

More about breadboarding: To learn about the history of breaboards, how modern breadboards are constructed, and how to use them, see the video resources at www.parallax.com/go/WAM.

Figure 2-6 shows a circuit schematic, and a picture of how that circuit will look when it is built on the prototyping area. Each 5-socket group can connect up to five leads, or wires, to each other. For this circuit, the resistor and the LED are connected because each one has a lead plugged into the same 5-socket group. Note that one lead of the resistor is plugged into Vdd (+5 V) so the circuit can draw power. The other resistor lead connects to the LED’s anode lead. The LED’s cathode lead is connected to Vss (0 V, ground) completing the circuit.

You are now ready to build the circuit shown in Figure 2-6 (below) by plugging the LED and resistor leads into sockets on the prototyping area. Follow these steps:

9Disconnect power from your Board of Education or HomeWork Board.

9Use Figure 2-4 to decide which lead is connected to the LED’s cathode. Look for the shorter lead and the flat spot on the plastic part of the LED.

9Plug the LED’s cathode into one of the black sockets labeled Vss on the prototyping area.

9Plug the LED’s anode (the other, longer lead) into the socket shown on the breadboard portion of the prototyping area.

9Plug one of the resistor’s leads into the same 5-socket group as the LED’s anode. This will connect those two leads together.

9Plug the resistor’s other lead into one of the sockets labeled Vdd.

Lights On – Lights Off · Page 33

Direction does matter for the LED, but not for the resistor. If you plug the LED in backward, the LED will not emit light when you connect power. The resistor just resists the flow of current. There is no backwards or forwards for a resistor.

9Reconnect power to your Board of Education or HomeWork Board.

9Check to make sure your green LED is emitting light. It should glow green.

Vdd

Vdd

Vin

Vss

X3

 

+

 

P15

 

 

 

 

 

P14

 

 

470 Ω

P13

 

 

P12

 

 

 

P11

 

 

 

P10

 

 

 

P9

 

 

LED

P8

 

 

P7

 

 

 

P6

 

 

 

P5

 

 

 

P4

 

 

Vss

P3

 

 

P2

 

 

 

P1

 

 

 

P0

 

 

 

X2

 

 

Figure 2-6

LED On, wired directly to power

Schematic (left) and Wiring Diagram (right).

Note that one resistor lead and the green LED’s anode lead are plugged into the same 5-socket group. This electrically connects the two components.

If your green LED does not emit light when you connect power to the board:

9Some LEDs are brightest when viewed from above. Try looking straight down onto the dome part of the LED’s plastic case from above.

9If the room is bright, try turning off some of the lights, or use your hands to cast a shadow on the LED.

If you still do not see any green glow, try these steps:

9Double check to make sure the LED’s cathode and anode are connected properly. If not, simply remove the LED, give it a half-turn, and plug it back in. It will not hurt the LED if you plug it in backwards, it just doesn’t emit light. When you have it plugged in the right direction, it should emit light.

9Double check to make sure you built your circuit exactly as shown in Figure 2-6.

Page 34 · What’s a Microcontroller?

9If you are using a What’s a Microcontroller kit that somebody used before you, the LED may be damaged, so try a different one.

9If you are in a lab class, check with your instructor.

Still stuck? Try these free online resources:

Visit the Stamps In Class moderated forums: If you don’t have an instructor or friend who can help, you can always check with the Stamps in Class forum at http://forums.parallax.com. If you don’t get your questions answered there, you can contact Parallax Technical Support department by following the Support link at www.parallax.com.

How the LED Test Circuit Works

The Vdd and Vss terminals supply electrical pressure in the same way that a battery would. The Vdd sockets are like the battery’s positive terminal, and the Vss sockets are like the battery’s negative terminal. Figure 2-7 shows how applying electrical pressure to a circuit using a battery causes electrons to flow through it. This flow of electrons is called electric current, or often just current. Electric current is limited by the resistor. This current is what causes the diode to emit light.

 

-

-

 

 

-

 

 

 

 

 

 

 

 

+

 

 

 

-

 

 

-

 

 

-

 

 

 

 

 

 

 

 

N N N

 

-

 

 

 

+ + +

+

+

-

-

 

+ + +

=

N

 

 

+ + +

 

 

-

 

 

 

- -

-

 

 

-

-

 

- -

-

 

 

 

_

- -

N

 

 

 

 

 

N -

N

 

 

 

 

-

 

-

 

 

 

 

 

 

-

 

 

-

 

-

 

 

-

-

 

Figure 2-7

LED On Circuit Electron Flow

The minus signs with the circles around them are used to show electrons flowing from the battery’s negative terminal to its positive terminal.

Lights On – Lights Off · Page 35

Chemical reactions inside the battery supply the circuit with current. The battery’s negative terminal contains a compound that has molecules with extra electrons (shown Figure 2-7 by minus-signs). The battery’s positive terminal has a chemical compound with molecules that are missing electrons (shown by plus-signs). When an electron leaves a molecule in the negative terminal and travels through the wire, it is called a free electron (also shown by minus-signs). The molecule that lost that extra electron no longer has an extra negative charge; it is now called neutral (shown by an N). When an electron gets to the positive terminal, it joins a molecule that was missing an electron, and now that molecule is neutral too.

Figure 2-8 shows how the flow of electricity through the LED circuit is described using schematic notation. The electrical pressure across the circuit is called voltage. The + and

– signs are used to show the voltage applied to a circuit. The arrow shows the current flowing through the circuit. This arrow is almost always shown pointing the opposite direction of the actual flow of electrons. Benjamin Franklin is credited with not having been aware of electrons when he decided to represent current flow as charge passing from the positive to negative terminal of a circuit. By the time physicists discovered the true nature of electric current, the convention was already well established.

Voltage +

Current

Voltage -

Vdd

Resistance

LED

Vss

Figure 2-8

LED-On Circuit Schematic Showing

Conventional Voltage and Current

Flow

The + and – signs show voltage applied to the circuit, and the arrow shows current flow through the circuit.

A schematic drawing (like Figure 2-8) is a picture that explains how one or more circuits are connected. Schematics are used by students, electronics hobbyists, electricians, engineers, and just about everybody else who works with circuits.

Appendix B: More about Electricity contains some glossary terms and an activity you can try to get more familiar with measurements of voltage, current and resistance.

Page 36 · What’s a Microcontroller?

Your Turn – Modifying the LED Test Circuit

In the next activity, you will program the BASIC Stamp to turn the LED on, then off, then on again. The BASIC Stamp will do this by switching the LED circuit between two different connections, Vdd and Vss. You just finished working with the circuit where the resistor is connected to Vdd, and the LED emits light. Make the changes shown in Figure 2-9 to verify that the LED will turn off (not emit light) when the resistor’s lead is disconnected from Vdd and connected to Vss.

9Disconnect power from your Board of Education or HomeWork Board.

9Unplug the resistor lead that’s plugged into the Vdd socket, and plug it into a socket labeled Vss as shown in Figure 2-9.

9Reconnect power to your Board of Education or HomeWork Board.

9Check to make sure your green LED is not emitting light. It should not glow green.

Why does the LED not glow? Since both ends of the circuit are connected to the same voltage (Vss), there isn’t any electrical pressure across the circuit. So, no current flows through the circuit, and the LED stays off.

 

 

Vdd

Vin

Vss

 

 

X3

 

+

 

 

P15

 

 

 

 

 

 

 

P14

 

Figure 2-9

 

 

P13

 

 

 

P12

 

LED Off Circuit

 

 

P11

 

 

 

 

P10

 

Schematic (left) and

 

 

P9

 

 

470 Ω

P8

 

wiring diagram (right).

 

P7

 

 

LED

P6

 

 

 

 

P5

 

 

 

 

P4

 

 

Vss

Vss

P3

 

 

P2

 

 

 

 

P1

 

 

 

 

P0

 

 

 

 

X2

 

 

Lights On – Lights Off · Page 37

ACTIVITY #2: ON/OFF CONTROL WITH THE BASIC STAMP

In Activity #1, two different circuits were built and tested. One circuit made the LED emit light while the other did not. Figure 2-10 shows how the BASIC Stamp can do the same thing if you connect an LED circuit to one if its I/O pins. In this activity, you will connect the LED circuit to the BASIC Stamp and program it to turn the LED on and off. You will also experiment with programs that make the BASIC Stamp do this at different speeds.

SOUT 1

 

24

VIN

SIN

2

BS2

23

VSS

ATN 3

 

22

RES

VSS 4

 

21

VDD (+5V)

P0

5

Vdd

20

P15

P1

6

Vss

19

P14

P2

7

18

P13

P3

8

 

17

P12

P4

9

 

16

P11

P5

10

 

15

P10

P6

11

 

14

P9

P7

12

 

13

P8

 

 

BS2-IC

 

 

SOUT 1

 

24

VIN

SIN

2

BS2

23

VSS

ATN 3

 

22

RES

VSS 4

 

21

VDD (+5V)

P0

5

Vdd

20

P15

P1

6

Vss

19

P14

P2

7

18

P13

P3

8

 

17

P12

P4

9

 

16

P11

P5

10

 

15

P10

P6

11

 

14

P9

P7

12

 

13

P8

 

 

BS2-IC

 

 

Figure 2-10

BASIC Stamp

Switching

The BASIC Stamp can be programmed to internally connect the LED circuit’s input to Vdd or Vss.

There are two big differences between changing the connection manually and having the BASIC Stamp do it. First, the BASIC Stamp doesn’t have to cut the power to the development board when it changes the LED circuit’s supply from Vdd to Vss. Second, while a human can make that change several times a minute, the BASIC Stamp can do it thousands of times per second!

LED Test Circuit Parts

Same as Activity #1.

Connecting the LED Circuit to the BASIC Stamp

The LED circuit shown in Figure 2-11 is wired almost the same as the circuit in the previous exercise. The difference is that the resistor’s lead that was manually switched between Vdd and Vss is now connected to a BASIC Stamp I/O pin.

9Disconnect power from your Board of Education or HomeWork Board.

9Modify the circuit you were working with in Activity #1 so that it matches Figure 2-11.

Page 38 · What’s a Microcontroller?

P14 470 Ω

Vss

 

Vdd

Vin

Vss

 

X3

 

+

 

P15

 

LED

 

 

P14

 

 

 

 

 

 

P13

 

 

 

P12

 

 

 

P11

 

 

 

P10

 

 

 

P9

 

 

 

P8

 

 

 

P7

 

 

 

P6

 

 

 

P5

 

 

 

P4

 

 

 

P3

 

 

 

P2

 

 

 

P1

 

 

 

P0

 

 

 

X2

 

 

Figure 2-11

BASIC Stamp

Controlled LED Circuit

The LED circuit’s input is now connected to a BASIC Stamp I/O pin instead of Vdd or Vss.

Resistors are essential. Always remember to use a resistor. Without it, too much current will flow through the circuit, and it could damage any number of parts in your circuit, BASIC Stamp, or Board of Education or HomeWork Board.

Turning the LED On/Off with a Program

The example program makes the LED blink on and off one time per second. It introduces several new programming techniques at once. After running it, you will experiment with different parts of the program to better understand how it works.

Example Program: LedOnOff.bs2

9Enter the LedOnOff.bs2 code into the BASIC Stamp Editor.

9Reconnect power to your Board of Education or HomeWork Board.

9Run the program.

9Verify that the LED flashes on and off once per second.

9Disconnect power when you are done with the program.

Lights On – Lights Off · Page 39

'What's a Microcontroller - LedOnOff.bs2

'Turn an LED on and off. Repeat 1 time per second indefinitely.

'{$STAMP BS2} '{$PBASIC 2.5}

DEBUG "The LED connected to P14 is blinking!"

DO

HIGH 14

PAUSE 500

LOW 14

PAUSE 500

LOOP

How LedOnOff.bs2 Works

The command DEBUG "The LED connected to P14 is blinking!" makes this statement appear in the Debug Terminal. The command HIGH 14 causes the BASIC Stamp to internally connect I/O pin P14 to Vdd. This turns the LED on.

The command PAUSE 500 causes the BASIC Stamp to do nothing for ½ a second while the LED stays on. The number 500 tells the PAUSE command to wait for 500/1000 of a second. The number that follows PAUSE is called an argument. Arguments give PBASIC commands the information that they need to execute. If you look up PAUSE in the BASIC Stamp Manual, you will discover that it calls this number the Duration argument. The name Duration was chosen for this argument to show that the PAUSE command pauses for a certain “duration” of time, in milliseconds.

What’s a Millisecond? A millisecond is 1/1000 of a second. It is abbreviated as ms. It takes 1000 ms to equal one second.

The command LOW 14 causes the BASIC Stamp to internally connect I/O pin P14 to Vss. This turns the LED off. Since LOW 14 is followed by another PAUSE 500, the LED stays off for half a second.

The reason the code repeats itself over and over again is because it is nested between the PBASIC keywords DO and LOOP. Figure 2-12 shows how a DO…LOOP works. By placing the code segment that turns the LED on and off with pauses between DO and LOOP, it tells the BASIC Stamp to execute those four commands over and over again. The result is

Page 40 · What’s a Microcontroller?

that the LED flashes on and off, over and over again. It will keep flashing until you disconnect power, press and hold the Reset button, or until the battery runs out. Code that repeats a set of commands indefinitely is called an infinite loop.

DO

HIGH 14

PAUSE 250

LOW 14

PAUSE 250

LOOP

Figure 2-12

DO…LOOP

The code between the keywords DO and LOOP gets executed over and over endlessly.

A Diagnostic Test for your Computer

Although it’s not common, there are some computer systems, such as certain laptops and docking stations, that will halt the PBASIC program after the first time through a DO...LOOP. These computers have a non-standard serial port design. By placing a DEBUG command in the program LedOnOff.bs2, the open Debug Terminal prevents this from possibly happening. You will next re-run this program without the DEBUG command to see if your computer has this non-standard serial port problem. It is not likely, but it would be important for you to know.

9Open LedOnOff.bs2.

9Delete the entire DEBUG command.

9Run the modified program while you observe your LED.

If the LED blinks on and off continuously, just as it did when you ran the original program with the DEBUG command, your computer will not have this problem.

If the LED blinked on and off only once and then stopped, you have a computer with a non-standard serial port design. If you disconnect the serial cable from your board and press the Reset button, the BASIC Stamp will run the program properly without freezing. In programs you write yourself, you will always need to add a single DEBUG command, such as:

DEBUG "Program Running!"

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]