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

Whats A Microcontroller v3

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

Measuring Light · Page 211

How ReadLightMeasurementsFromEeprom.bs2 Works

As with the WRITE command, the READ command relies on byte addresses. Since we want to read word values from EEPROM, the eepromAddress variable has to have 2 added to it each time through the FOR...NEXT loop.

FOR eepromAddress = 0 to 58 STEP 2

The READ command gets the word size value at eepromAddress, and the value gets copied to the time variable.

READ eepromAddress, Word time

The values of the time and eepromAddress variables are displayed in adjacent columns as a table in the Debug Terminal.

DEBUG DEC2 eepromAddress, "

", DEC time, CR

NEXT

Your Turn – More Measurements

9Modify StoreLightMeasurementsInEeprom.bs2 so that it takes and records twice as many measurements in the same amount of time.

9Modify ReadLightMeasurementsFromEeprom.bs2 so that it displays all of the measurements from the just-modified StoreLightMeasurementsInEeprom.bs2.

ACTIVITY #3: GRAPHING LIGHT MEASUREMENTS (OPTIONAL)

Lists of measurements like the ones in Activity #2 can be tedious to analyze. Imagine reading through hundreds of those numbers looking for when the sun set. Or maybe you’re looking for a particular event, like when the light sensor was briefly covered. You might even be looking for a pattern in how frequently the light sensor was covered. This information could be useful if the light sensor is placed in an area where a person or animal walks over it, or an object passing over it on a conveyer belt needs to be recorded and analyzed. Regardless of the application, if all you have to work with is a long list of numbers, finding those events and patterns can be a difficult and time-consuming task.

If you instead make a graph of the list of measurements, it makes finding events and patterns a lot easier. The person, animal or object passing over the light sensor will show up as a high point or spike in the measurements. Figure 7-7 shows an example of a graph that might indicate that rate at which objects on a conveyer belt are passing over the sensor. The spikes in the graph occur when the measurements get large. In the case of a

Page 212 · What’s a Microcontroller?

conveyer belt, it would indicate that an object passed over the sensor casting a shadow. This graph makes it easy to see at a glance that an object passes over the sensor roughly every 7 seconds, but that the object we were expecting at 28 seconds was wasn’t there.

Figure 7-7 Graph of Phototransistor Light Measurements

Decay Time Vs. Time for Phototransistor RC Circuit

 

9000

 

 

 

 

 

 

 

8000

 

 

 

 

 

 

 

7000

 

 

 

 

 

 

(2 us)

6000

 

 

 

 

 

 

5000

 

 

 

 

 

 

Time

 

 

 

 

 

 

4000

 

 

 

 

 

 

Decay

 

 

 

 

 

 

3000

 

 

 

 

 

 

 

 

 

 

 

 

 

 

2000

 

 

 

 

 

 

 

1000

 

 

 

 

 

 

 

0

 

 

 

 

 

 

 

0

10

20

30

40

50

60

"Decay Time"

Time (s)

The graph in Figure 7-7 was generated by copying and pasting values in the Debug Terminal to a text file which was then imported into a Microsoft Excel spreadsheet. Some graphing utilities can take the place of the Debug Terminal and plot the values directly instead of displaying them as lists of numbers. Figure 7-8 shows an example of one of these utilities, called StampPlot LITE.

Measuring Light · Page 213

Figure 7-8 StampPlot LITE

In this optional activity, you can go to www.parallax.com/go/WAM and then follow the Data Plotting link to try a number of activities that demonstrate how to plot values using various spreadsheets and graphing utility software packages.

Page 214 · What’s a Microcontroller?

ACTIVITY #4: SIMPLE LIGHT METER

Light sensor information can be communicated in a variety of ways. The light meter you will work with in this activity changes the rate that the display flickers depending on the light intensity it detects.

Light Meter Parts

(1) Phototransistor

(1)Resistor – 220 Ω (red-red-brown)

(2)Capacitors – 0.01 μF (labeled 103)

(1)Capacitor – 0.1 μF (labeled 104)

(1)7-segment LED display

(8)Resistors – 1 kΩ (brown-black-red)

(6)Jumper wires

Building the Light Meter Circuit

Figure 7-9 shows the 7-segment LED display and phototransistor circuit schematics that will be used to make the light meter, and Figure 7-10 shows a wiring diagram of the circuits. The phototransistor circuit is the same one you have been using in the last two activities, and the 7-segment LED display circuit is the same one from Figure 6-11 on page 176.

9Build the circuit shown in Figure 7-9 and Figure 7-10.

9Test the 7-segment LED display to make sure it is connected properly, using the program SegmentTestWithHighLow.bs2 from Chapter 6, Activity #2, which starts on page 175.

Measuring Light · Page 215

Figure 7-9

Light Meter Circuit

Schematic

Figure 7-10

Wiring Diagram for

Figure 7-9

Page 216 · What’s a Microcontroller?

Using Subroutines

Most of the programs you have written so far operate inside a DO...LOOP. Since the entire program’s main activity happens inside the DO...LOOP, it is usually called the main routine. As you add more circuits and more useful functions to your program, it can get kind of difficult to keep track of all the code in the main routine. Your programs will be much easier to work with if you instead organize them into smaller segments of code that do certain jobs. PBASIC has some commands that you can use to make the program jump out of the main routine, do a job, and then return right back to the same spot in the main routine. This will allow you to keep each segment of code that does a particular job somewhere other than your main routine. Each time you need the program to do one of those jobs, you can write a command inside the main routine that tells the program to jump to that job, do it, and come back when the job is done. The jobs are called subroutines and this process is calling a subroutine.

Figure 7-11 shows an example of a subroutine and how it’s used. The command GOSUB Subroutine_Name causes the program to jump to the Subroutine_Name: label. When the program gets to that label, it keeps running and executing commands until it gets to a RETURN command. Then, the program goes back to command that comes after the GOSUB command. In the case of the example in Figure 7-11, the next command is:

DEBUG "Next command".

DO

GOSUB Subroutine_Name

DEBUG "Next command"

LOOP

Figure 7-11

Subroutine_Name: How Subroutines Work

DEBUG "This is a subroutine..."

PAUSE 3000

RETURN

Measuring Light · Page 217

What’s a label? A label is a name that can be used as a placeholder in your program. GOSUB is one of the commands you can use to jump to a label. Some others are GOTO, ON GOTO, and ON GOSUB. A label must end with a colon, and for the sake of style, separate words with the underscore character so they are easy to recognize. When picking a name for a label, make sure not to use a reserved word or a name that is already used in a variable or constant. The rest of the rules for a label name are the same as the ones for naming variables, which are listed in the information box on page 43.

Example Program: SimpleSubroutines.bs2

This example program shows how subroutines work by sending messages to the Debug Terminal.

9Examine SimpleSubroutines.bs2 and try to guess the order in which the DEBUG commands will be executed.

9Enter and run the program.

9Compare the program’s actual behavior with your predictions.

'What's a Microcontroller - SimpleSubroutines.bs2

'Demonstrate how subroutines work.

'{$STAMP BS2}

'{$PBASIC 2.5}

PAUSE 1000

DO

DEBUG CLS, "Start main routine.", CR

PAUSE 2000

GOSUB First_Subroutine

DEBUG "Back in main.", CR

PAUSE 2000

GOSUB Second_Subroutine

DEBUG "Repeat main...", CR

PAUSE 2000

LOOP

First_Subroutine:

DEBUG " Executing first "

DEBUG "subroutine.", CR

PAUSE 3000

RETURN

Page 218 · What’s a Microcontroller?

Second_Subroutine:

DEBUG " Executing second "

DEBUG "subroutine.", CR

PAUSE 3000

RETURN

How SimpleSubroutines.bs2 Works

Figure 7-12 shows how the First_Subroutine call in the main routine (the DO...LOOP) works. The command GOSUB First_Subroutine sends the program to the First_Subroutine: label. Then, the three commands inside that subroutine are executed. When the program gets to the RETURN command, it jumps back to the command that comes right after GOSUB First_Subroutine, which is DEBUG "Back in

Main.", CR.

What’s a subroutine call? When you use the GOSUB command to make the program jump to a subroutine, it is called a subroutine call.

PAUSE 2000

GOSUB First_Subroutine DEBUG "Back in main.", CR

First_Subroutine:

DEBUG " Executing first "

DEBUG "subroutine.", CR

PAUSE 3000

RETURN

Figure 7-12

First Subroutine Call

Figure 7-13 shows a second example of the same process with the second subroutine call (GOSUB Second_Subroutine).

Measuring Light · Page 219

PAUSE 2000

GOSUB Second_Subroutine

DEBUG "Repeat main...", CR

Second_Subroutine:

DEBUG " Executing second "

DEBUG "subroutine", CR

PAUSE 3000

RETURN

Your Turn – Adding and Nesting Subroutines

Figure 7-13

Second Subroutine Call

You can add subroutines after the two that are in the program and call them from within the main routine.

9Add the subroutine shown in Figure 7-11 on page 216 to SimpleSubroutines.bs2.

9Make any necessary adjustments to the DEBUG commands so that the display looks right with all three subroutines.

You can also call one subroutine from within another. This is called nesting subroutines.

9Try moving the GOSUB command that calls Subroutine_Name into one of the other subroutines, and see how it works.

When nesting subroutines the rule is no more than four deep. See the BASIC Stamp Manual or the BASIC Stamp Editor’s Help for more information. Look up GOSUB and

RETURN.

Light Meter Using Subroutines

The next program, LightMeter.bs2 uses subroutines to control the display of the 7- Segment LED depending on the level of light detected by the phototransistor. The display’s segments cycle on and off in a circular pattern that gets faster when the light on the phototransistor gets brighter. When the light gets dimmer, the circular pattern cycling goes slower. The LightMeter.bs2 example program uses a subroutine named Update_Display to control the order in which the light meter segments advance.

Page 220 · What’s a Microcontroller?

The program that runs the light meter will deal with three different operations:

1.Read the phototransistor.

2.Calculate how long to wait before updating the 7-segment LED display.

3.Update the 7-segment LED display.

Each operation is contained within its own subroutine, and the main DO...LOOP routine will call each one in sequence, over and over again.

Example Program: LightMeter.bs2

Controlled lighting conditions make a big difference! For best results, conduct this test in a room lit by fluorescent lights with little or no direct sunlight (close the blinds). For information on how to calibrate this meter to other lighting conditions, see the Your Turn section.

9Enter and run LightMeter.bs2.

9Verify that the cycling speed of the circular pattern displayed by the 7-segment LED is controlled by the lighting conditions the phototransistor is sensing. Do this by casting a shadow over it with your hand or a piece of paper and verify that the rate of the circular display pattern slows down.

'What's a Microcontroller - LightMeter.bs2

'Indicate light level using 7-segment display.

'{$STAMP BS2}

'{$PBASIC 2.5}

PAUSE 1000

DEBUG "Program Running!"

index

VAR

Nib

' Variable declarations.

time

VAR

Word

 

OUTH = %00000000

 

' Initialize 7-segment display.

DIRH = %11111111

 

 

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