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

What is a Microcontroller (Paralax, v2.2, student guide, 2004)

.pdf
Скачиваний:
111
Добавлен:
12.08.2013
Размер:
5.64 Mб
Скачать

Page 284 · What’s a Microcontroller?

'What's a Microcontroller - ReadPushbuttonState.bs2

'Check and send pushbutton state to Debug Terminal every 1/4 second.

'{$STAMP BS2}

'{$PBASIC 2.5}

DO

DEBUG ? IN3

PAUSE 250

LOOP

Your Turn – Building and Testing the Second Pushbutton Circuit

Once the first pushbutton circuit has been built and tested, the process can be repeated for the second pushbutton.

Add the pushbutton circuit shown in Figure 10-3.

Test the second pushbutton circuit by modifying ReadPushbuttonState.bs2 so that it reads the circuit connected to P4. Run the program and verify that the second pushbutton works.

Correct any problems you catch before moving on to the next activity.

Figure 10-3

Adding a Second Pushbutton Circuit to the Project

 

 

Vdd

Vin

Vss

 

Vdd

X3

 

 

 

P15

 

 

 

 

 

 

 

 

P14

 

 

P4

 

P13

 

 

 

P12

 

 

220

 

P11

 

 

 

 

P10

 

 

 

 

P9

 

 

 

 

P8

 

 

 

 

P7

 

 

10 k

 

P6

 

 

 

P5

 

 

 

 

P4

 

 

 

 

P3

 

 

Vss

 

P2

 

 

 

P1

 

 

 

 

P0

 

 

 

 

X2

 

 

Chapter #10: Running the Whole Show · Page 285

ACTIVITY #2: BUILDING AND TESTING EACH RC-TIME CIRCUIT

Now that the two pushbutton circuits are built and tested, you can move on to the RCtime circuits.

Extra Parts for Potentiometer and Photoresistor Circuits

(2) Resistors – 220 (red-red-brown)

(2) Capacitors – 0.1 µF

(4) Jumper wires

(1) Photoresistor

(1) Potentiometer – 10 k

Building the Potentiometer Circuit

Add the potentiometer circuit shown in Figure 10-4 to the project.

Figure 10-4

Adding a Potentiometer Circuit

P7 220

 

 

Vdd

Vin

Vss

 

 

X3

 

 

 

 

P15

 

 

nc

 

P14

 

 

 

P13

 

 

X

 

 

 

 

P12

 

 

Pot

 

 

 

0.1 µF

P11

 

 

10 k

P10

 

 

 

 

 

 

 

P9

 

 

 

 

P8

 

 

 

 

P7

 

 

 

 

P6

 

 

 

 

P5

 

 

 

 

P4

 

 

Vss

 

P3

 

 

 

P2

 

 

 

 

P1

 

 

 

 

P0

 

 

 

 

X2

 

 

Testing the Potentiometer Circuit

You can test this circuit using ReadPotWithRcTime.bs2. This program was first used in Chapter #5 to read the potentiometer circuit.

Example Program: ReadPotWithRcTime.bs2

Enter and run the ReadPotWithRcTime.bs2.

Page 286 · What’s a Microcontroller?

Verify that the BASIC Stamp gets reliable measurements from the potentiometer.

Correct any problems you catch before moving on to the next circuit.

'What's a Microcontroller - ReadPotWithRcTime.bs2

'Read potentiometer in RC-time circuit using RCTIME command.

'{$STAMP BS2}

'{$PBASIC 2.5}

time

VAR

Word

DO

 

 

HIGH 7

 

 

PAUSE 100

 

 

RCTIME 7, 1, time

 

DEBUG HOME, " time =

", DEC5 time

LOOP

Your Turn – Building and Testing the Photoresistor Circuit

Once the first RC-time circuit has been built and tested, the process can be repeated for the second RC-time circuit.

Add the photoresistor circuit shown in Figure 10-5 to your project on the breadboard.

Modify ReadPotWithRcTime.bs2 so the photoresistor is connected to P9.

Correct any problems you catch before moving on to the next activity.

 

Chapter #10: Running the Whole Show · Page 287

Figure 10-5

 

 

 

Adding the Photoresistor Circuit

 

 

 

 

Vdd

Vin

Vss

 

X3

 

 

P9

P15

 

 

P14

 

 

220

P13

 

 

 

P12

 

 

0.1 µF

P11

 

 

P10

 

 

 

P9

 

 

 

P8

 

 

 

P7

 

 

 

P6

 

 

 

P5

 

 

 

P4

 

 

Vss

P3

 

 

P2

 

 

 

P1

 

 

 

P0

 

 

 

X2

 

 

ACTIVITY #3: SUBSYSTEM INTEGRATION EXAMPLE

Now that all the elements in the sensor array are built and tested, you can now write a program that makes use of all four sensor circuits. This example will demonstrate how to program the BASIC Stamp to display a terminal with a menu that a technician might use to monitor the sensors.

Programming a Menu System and Using the Input/Output PIN Directive

The ON…GOSUB command can be very useful for menus.

ON offset , GOSUB Target1, {Target2, Target3,…}

In the next example program, the ON...GOSUB command uses the value of a variable named request to direct it to one of four subroutines. If the value of request is 0, the program executes a GOSUB Read_Pushbutton_1. If the Value of request is 1, the program executes a GOSUB Read_Pushbutton_2, and so on.

ON request GOSUB Read_Pushbutton_1, Read_Pushbutton_2, Read_Pot, Read_Photoresistor

You can use the PIN directive to give a name to each I/O pin you are going to use in a PBASIC program. The BASIC Stamp Editor will then figure out whether you are using the I/O pin as an input or an output (or both). The PIN directive’s syntax is:

PinName PIN PinNumber

Page 288 · What’s a Microcontroller?

The next example program demonstrates how you can declare and then use a PinName. For example, the BASIC Stamp Editor assumes that you want to display the input value of an I/O pin (IN3) if you are using the Pb1Pin PinName in a DEBUG command. This DEBUG command will display a 1 or 0 depending on whether the pushbutton connected to P3 is pressed or not pressed. Why? Because the BASIC Stamp Editor knows to substitute IN3 for Pb1Pin.

Pb1Pin

PIN

3

DEBUG CLS, ? Pb1Pin

Here is another example where the BASIC Stamp Editor knows to substitute the value of 9 because the PhotoPin PinName is used in a HIGH command and an RCTIME command.

PhotoPin PIN 9

HIGH PhotoPin

RCTIME PhotoPin, 1, time

Example Program: TerminalOperatedSensorArray.bs2

Figure 10-6 shows the Debug Terminal displayed by TerminalOperatedSensorArray.bs2. The main menu is shown on the left, and an example of the display when ‘4’ is selected is shown on the right. Remember to use the upper Windowpane to send your menu selections to the BASIC Stamp.

Enter and run TerminalOperatedSensorArray.bs2.

Click the Debug Terminal’s upper Windowpane, and type digits to make your menu selections.

The measurement displayed is the one taken at the instant the menu selection is made, and it is displayed for 1.5 seconds. Keep this in mind when you press and hold the pushbuttons, adjust the potentiometer and cast shade onto the photoresistor.

Chapter #10: Running the Whole Show · Page 289

Figure 10-6

Using the Debug Terminal to Select from a Menu

'-----[ Title ]-----------------------------------------------------------

'What's a Microcontroller - TerminalOperatedSensorArray.bs2

'Use Debug Terminal to choose to read one of four sensors.

'{$STAMP BS2}

'{$PBASIC 2.5}

'-----[ I/O Definitions ]-------------------------------------------------

Pb1Pin

 

PIN

3

Pb2Pin

 

PIN

4

PotPin

 

PIN

7

PhotoPin

PIN

9

' -----

[ Constants

]-------------------------------------------------------

DelayRc

 

CON

100

DelayReader

CON

1500

' -----

[ Variables

]-------------------------------------------------------

request

 

VAR

Nib

time

 

VAR

Word

' -----

[ Main Routine ]----------------------------------------------------

DO

 

 

 

GOSUB Display_Menu

GOSUB Get_Request

 

ON request GOSUB

Read_Pushbutton_1, Read_Pushbutton_2,

 

 

 

Read_Pot, Read_Photoresistor

Page 290 · What’s a Microcontroller?

LOOP

' -----[ Subroutine - Display_Menu ]---------------------------------------

Display_Menu:

DEBUG CLS, "MENU: ", CR, CR,

"1) Pushbutton 1", CR,

"2) Pushbutton 2", CR,

"3) Potentiometer", CR,

"4) Photoresistor", CR, CR

RETURN

' -----[ Subroutine - Get_Request ]----------------------------------------

Get_Request:

DEBUGIN DEC1 request request = request - 1

RETURN

' -----[ Subroutine - Read_Pushbutton_1 ]----------------------------------

Read_Pushbutton_1:

DEBUG CLS, ? Pb1Pin

PAUSE DelayReader

RETURN

' -----[ Subroutine - Read_Pushbutton_2 ]----------------------------------

Read_Pushbutton_2:

DEBUG CLS, ? Pb2Pin

PAUSE DelayReader

RETURN

' -----[ Subroutine - Read_Pot ]-------------------------------------------

Read_Pot:

HIGH PotPin

PAUSE DelayRc

RCTIME PotPin, 1, time

DEBUG CLS, ? time, " "

PAUSE DelayReader

Chapter #10: Running the Whole Show · Page 291

RETURN

' -----[ Subroutine - Read_Photoresistor ]---------------------------------

Read_Photoresistor:

HIGH PhotoPin

PAUSE DelayRc

RCTIME PhotoPin, 1, time

DEBUG CLS, ? time, " "

PAUSE DelayReader

RETURN

Your Turn – Modifying Subroutines

Terminals are not the only application for ON…GOSUB. For example, if you want to call the four different subroutines one after another, here is a way to do it:

DO

'GOSUB Display_Menu

'GOSUB Get_Request FOR request = 0 TO 3

ON request GOSUB Read_Pushbutton_1, Read_Pushbutton_2,

Read_Pot, Read_Photoresistor

NEXT

LOOP

Notice that the two subroutines that do the terminal work were commented and the ON…GOSUB command was placed in a FOR…NEXT loop.

Save your example program under the new name: TerminalOperatedSensorArrayYourTurn.bs2.

Modify the main routine by making the changes just discussed.

Run the program and verify that it cycles through the measurements.

ACTIVITY #4: DEVELOPING AND ADDING A SOFTWARE SUBSYSTEM

Let’s say that your sensor array is getting placed in a device that only certain employees are allowed to use, and they have to supply a password. This means you will have to expand your program so that it saves and verifies a password. It’s better to write a password program that functions on its own first, and then add it to your larger program.

Page 292 · What’s a Microcontroller?

Programming a Password Checker

You can save a password in a PBASIC program by using a DATA directive. For example:

Password

DATA

"pass!"

The same as: This DATA directive is the same as Password DATA "p", "a", "s", "s", "!".

You will need a few variables for storing other values in the program:

index

VAR

Nib

temp

VAR

Byte

If you are using a five character password, there is a special kind of variable declaration (called an array declaration) that you use to declare five variables with the same name.

userEntry VAR Byte(5)

Now you have five byte variables named userEntry: userEntry(0), userEntry(1), userEntry(2), userEntry(3), and userEntry(4).

The DEBUGIN command has a formatter called STR that automatically loads characters into an array. For example, you can use:

DEBUGIN STR userEntry \5

If you type five characters in the Debug Terminal’s Transmit Windowpane, the first will get placed in userEntry(0), the second will get placed in userEntry(1), etc.

There is a PBASIC keyword called EXIT that you can use to break out of a loop. To check a password, you can use an IF…THEN statement with an EXIT command to cause the loop to end early if not all the characters are identical. When the loop ends early, it means that index has not counted all the way up to five, which in turn means the password was not correct:

FOR index = 0 TO 4

READ Password + index, temp

IF temp <> userEntry(index) THEN EXIT

NEXT

IF index <> 5 THEN

DEBUG CR,"Password not correct.", CR

ENDIF

Chapter #10: Running the Whole Show · Page 293

This next example program places the DEBUGIN command, the FOR…NEXT loop, and the IF…THEN statement inside a DO…LOOP UNTIL statement that keeps executing until the value of index gets to 5 (indicating the correct password was entered).

Example Program: PasswordChecker.bs2

Figure 10-7 shows the Debug Terminal displayed by PasswordChecker.bs2. When run, this program will wait for you to type the letters “pass!” in response to the prompt.

Enter and run PasswordChecker.bs2.

Try entering some letter combinations that are not the password, then enter the letter combination that is the password: “pass!”.

Figure 10-7

Entering Password into the Transmit Windowpane

Click the Transmit (upper) Windowpane and enter the password.

'What's a Microcontroller - PasswordChecker.bs2

'Check password entered in Debug Terminal's Transmit Windowpane.

'{$STAMP BS2}

'{$PBASIC 2.5}

Password

DATA

"pass!"

' Store "secret" password here.

index

VAR

Nib

' Index variable.

temp

VAR

Byte

' Stores single char.

userEntry

VAR

Byte(5)

' Store user entered password.

DO

 

 

 

DEBUG "Enter

password: "

' User instructions.