Books
in black and white
Main menu
Share a book About us Home
Books
Biology Business Chemistry Computers Culture Economics Fiction Games Guide History Management Mathematical Medicine Mental Fitnes Physics Psychology Scince Sport Technics
Ads

Electronics for dummies - McComb G.

McComb G., Boes E. Electronics for dummies - Wiley publishing, 2005. - 433 p.
ISBN: 0-7645-7660-7
Download (direct link): electronicsfordummies2005.pdf
Previous << 1 .. 106 107 108 109 110 111 < 112 > 113 114 115 116 117 118 .. 149 >> Next

Figure 13-9:
The LED flasher program in the BASIC Stamp program editor.
The BASIC Stamp software editor treats any text after an apostrophe as a comment. You make comments just for yourself; the BASIC Stamp ignores them and doesnt process them as instructions. Line 1 of the LED program falls into the comment category. You may want to get into the habit of adding at least a few comments to your programs to remind you of why you wrote what you wrote. Then, down the road, if you revisit a program, the comments give you a handy reminder of your intentions.
Step 3: Let 'er rip!
Okay, budding programmers: youre now ready to upload and try out the program:
1. Connect a serial or USB cable to the Board of Education and your PC.
The type of cable depends on the version of the BOE that you have.
2. Apply power to the BOE by plugging the wall transformer into the BOE power jack.
Alternatively, you can attach a 9-volt battery to the battery terminals in the upper-left corner of the BOE.
^BASIC Stamp - C:\Proqram Files.Parallax Inc.Stamp Editor v2.1\Samples\LEDFIasher.bs2
Eile Edit Directive Bun Help
DoSfc'H m SI s s s s s
a ffi) > A A A A A &
hi CHOPiasher bs2 J
Cj Netscape D NewsRove ffi Q NewsShark B Q Norton Antft/rus (_J Outlook Express Cj Ozum 0 Q Parallax Inc 1 Q Stamp Editor v21 Q BS1 Q BS2 BS2p Q BS2sx Samples 3 J 4 1 {$STAMP BS2 > loop: HIGH 0 ' pin 0 high (the LED turns on) PAUSE 250 1 wait 250 milliseconds LOU 0 1 pin 0 low (the LED turns off) PAUSE 250 1 wait 250 milliseconds GOTO loop 1 loop forever
<i I >
LEDFlasher.bs2 S witch. bs2
J BASIC Stamp files f\b*1: ba$.*.bs2.b$e;* b$x; Ml I I
fl: 1 INS |
TERM LinG - LIVE, Informative, Non-cost and 1 !
296 Part V: A Plethora of Projects
3. In the BASIC Stamp editor, press Ctrl+R (the Ctrl key and the R key at the same time).
This keystroke combo runs your program and downloads it to the BASIC Stamp.
If you have everything set up right, the LED should begin to flash twice each second. If the BASIC Stamp editor displays an error on your computer screen, locate and fix the problem (maybe a typo in your code?) and try again.
Making changes made easy
Heres where the power of microcontrollers really shines! Make the following changes in the program that we describe in the section Step 2: Programming the darned thing earlier in this chapter:
Ii^ Change Line 4 to PAUSE 100 Also change Line 6 to PAUSE 100
Now, run the program (press Ctrl+R). What happens when you run the program this time? Your computer downloads the changes into the BASIC Stamp, and the LED now flashes much more quickly. Instead of pausing 250 milliseconds each time that the LED turns on and off, the BASIC Stamp pauses for only 100 milliseconds.
Change the program again with these adjustments:
1^ Change Line 4 to PAUSE 1000 Also change Line 6 to PAUSE 1000
You can probably guess what happens. The LED flashes relatively slowly . . . once every other second. The BASIC Stamp now pauses 1000 milliseconds, or one full second, each time the LED turns on or off.
As you can see, by simply changing a line or two of programming code, you alter the behavior of your circuit.
Adding a switch to the mix
In this section, you can experience the versatility of the BASIC Stamp. You use the LED for the next demonstration, so make sure that you leave it hooked up, as we describe in the section Getting to Know the BASIC Stamp 2 earlier in this chapter. Connect a switch to the Board of Education, following the schematic in Figure 13-10. You can use any ordinary switch, but a momentary
TERM LinG - Live, informative, Non-cost and Genuine !
Chapter 13: The Exciting World of Microcontrollers 297
Figure 13-10:
Connect a single-pole, single-throw switch to I/O pin 1 of the BASIC Stamp.
pushbutton does the job the best. Be sure to add the 10K ohm resistor as Figure 13-10 shows you. If you use the BOE, you see that I/O pin 1 is labeled as P1 in the column of labels next to the solderless breadboard.
+V

TO BASIC STAMP
I/O PIN 1
What purpose does the 10K ohm resistor serve? It functions as a pulldown, which means that when the switch is not closed, the signal input feeding into the BASIC Stamp is 0 volts, or low. The resistor keeps the input to the BASIC Stamp from varying (called floating), which can cause the BASIC Stamp to give you erratic results.
Now, enter the test program:
'{$STAMP BS2}
OUTPUT 0 set pin 0 as output (for LED)
btn VAR Byte define "btn" as a variable
loop:
BUTTON 1,0,255,250,btn,0,noSwitch check
switch
0UT0 = btn turn LED on if switch was triggered
PAUSE 150 wait 150 milliseconds
0UT0 = 0 turn LED off noSwitch: GOTO loop repeat loop
Heres how this program works:
^ Line 1: Tells the editor what kind of BASIC Stamp youre using in this case, the BASIC Stamp 2. (The computer doesnt take any action regarding this comment line because of the apostrophe that begins it.)
Previous << 1 .. 106 107 108 109 110 111 < 112 > 113 114 115 116 117 118 .. 149 >> Next