Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Software and documentation www.parallax.com Downloads BASIC Stamp software Software for Windows: BASIC Stamp Windows Editor version 2.2.6 (~6.0 MB). Windows NT4/2K/XP. With BS1 Support! Download and install: “Setup-Stamp-Editor-Lrg-v2.2.6.exe” www.parallax.com Downloads Documentation BASIC Stamp Syntax and Reference Manual Version 2.2 (5.3 MB) OEM module The BASIC Stamp 2 OEM is a discreet component version of the BS2 which may be purchased in kit form. The male header provides the means to ‘plug-it’ into your own board, or connect to other boards. P0-P15 I/O Reset Input (RES) Power the board with EITHER: A) 5.5-15VDC on Vin. This will also provide 5 VDC regulated output on Vdd. B) Regulated 5V Input on Vdd. 5.5 – 15V input (Vin) Ground- 0V (Vss) Regulated 5V output (Vdd) Using the breadboard (Socket board) The bread board has many strips of metal (copper usually) which run underneath the board. To use the bread board, the legs of components are placed in the holes (the sockets). The holes are made so that they will hold the component in place. The circuit is built by placing components and connecting them together with jumper wires. The metal strips are laid out as shown in orange. The long top and bottom row of holes are usually used for power supply connections. Connection 220 Ohm resistor should be connected to pin P4 of the OEM Basic Stamp 2sx. “+” (long) lead of LED should be connected to opposite side of the resistor. The other (short) lead of the LED goes to “-” 220 Ohm 9V battery LED 220 Ohm OEM BASIC Stamp 2sx 220 Ohm 220 Ohm 9 V battery OEM BASIC Stamp 2sx 220 Ohm COM port Microcontroller chip Wiring diagram 220 Ohm LED 15 KOhm LED Note: - is connection to negative pole of the battery LED PIR Motion sensor OUT Push button Buzzer BASIC Stamp software “Run” Stamp Mode: BS2sx “Identify” Finds COM port with BASIC Stamp connected Your saved program files Example 0 Your first program: File: “Example-00.bsx” ' My first program ' {$STAMP BS2sx} ' {$PBASIC 2.5} DEBUG "Hello, is anybody home?" END Run this program when your BS2sx microcontroller is powered and connected to the computer via COM port. What do you see on the screen? Example 01a File: “Example-01a.bsx” Red LED ' Example 01a ' {$STAMP BS2sx} ' {$PBASIC 2.5} Red: 'Lable LOW 4 PAUSE 1000 HIGH 4 PAUSE 50 'Makes 'Pause 'Makes 'Pause GOTO Red 'Makes program to go back to “Red” lable END pin 4 to have low voltage 1 second pin 4 to have high voltage 0.05 sec Example 01b File: “Example-01b.bsx” Red LED ' Example 01b ' {$STAMP BS2sx} ' {$PBASIC 2.5} DO LOW 4 PAUSE 1000 HIGH 4 PAUSE 50 LOOP END 'Makes 'Pause 'Makes 'Pause pin 4 to have low voltage 1 second pin 4 to have high voltage 0.05 sec Example 01c File: “Example-01c.bsx” Red LED ' Example 01c ' {$STAMP BS2sx} ' {$PBASIC 2.5} RedLED PIN 4 IsOn CON 1 IsOff CON 0 Setup: OUTPUT RedLED Main: DO RedLED = IsOn PAUSE 1000 RedLED = IsOff PAUSE 1000 LOOP END ' red LED is connected to pin 4 ' IsOn = 1(constant) LED is active high ' IsOff = 0(constant) LED is active low ' sets OUTPUT to be pin 4 ' ' ' ' same as pause 1 same as pause 1 HIGH 4 s = 1000 ms LOW 4 s = 1000 ms Example 02a Red LED and pushbutton File: “Example-02a.bsx” ' Example 02a ' {$STAMP BS2sx} ' {$PBASIC 2.5} Green: DEBUG ? IN0, CR PAUSE 1000 GOTO Green ' ' ' ' ' label “green” sends information to the PC about the status of input pin 0 pause 1 s = 1000 ms return to label “green” END Run program. Press the pushbutton. What do you see on the screen? Release the pushbutton. What do you see on the screen? Example 02b Red LED and pushbutton File: “Example-02b.bsx” ' Example 02b ' {$STAMP BS2sx} ' {$PBASIC 2.5} DO DEBUG ? IN0 IF (IN0 = 1) THEN HIGH 4 PAUSE 50 LOW 4 PAUSE 50 ELSE PAUSE 100 ENDIF LOOP ' sends information to the PC ' about the status of input pin 0 Example 03 Input from the PC File: “Example-03.bsx” ' Example 03 ' {$STAMP BS2sx} ' {$PBASIC 2.5} myNumber VAR Nib DO DEBUG CR, "Enter a number (from 1 to 5)? --> " DEBUGIN DEC1 myNumber IF ((myNumber >= 1) AND (myNumber <= 5)) THEN DEBUG CR, " -- You entered: ", DEC1 myNumber ELSE DEBUG CR, " -- Sorry, your number is out of range" ENDIF LOOP END Example 04 3 LEDs ' Example 04 ' {$STAMP BS2sx} ' {$PBASIC 2.5} RedLED PIN 4 YellowLED PIN 13 GreenLED PIN 15 IsOn CON 1 IsOff CON 0 Setup: OUTPUT RedLED OUTPUT YellowLED OUTPUT GreenLED Main: DO RedLED = IsOn PAUSE 100 RedLED = IsOff PAUSE 10 YellowLED = IsOn PAUSE 100 YellowLED = IsOff PAUSE 10 GreenLED = IsOn PAUSE 100 GreenLED = IsOff PAUSE 500 LOOP END ' ' ' ' ' File: “Example-04.bsx” red LED is connected to pin 4 Yellow LED is connected to pin 13 Green LED is connected to pin 15 IsOn = 1(constant) LED is active high IsOff = 0(constant) LED is active low ' sets OUTPUT to be pin 4 Example 05a Buzzer ' Example 05a ' {$STAMP BS2sx} ' {$PBASIC 2.5} FREQOUT 1,3000,1900 PAUSE 1000 FREQOUT 1,3000,1900,2533 PAUSE 1000 FREQOUT 1,3000,1900,1903 END Run the program. What do you hear? File: “Example-05a.bsx” Buzzer: SOS Example 05b File: “Example-05b.bsx” ' Example 05b ' {$STAMP BS2sx} ' {$PBASIC 2.5} Dit CON 90 Dah CON 3*Dit index VAR Nib sos VAR Nib ' Short span of time in milliseconds. ' Longer time, 3 times the above. ' Index. FOR sos=1 TO 3 FOR index=1 TO 3 FREQOUT 1, Dit, 1900 PAUSE Dit NEXT ' Send 5 sounds. ' Send a dit. ' Short silence. PAUSE Dah FOR index=1 TO 3 FREQOUT 1, Dah, 1900 PAUSE Dah NEXT ' ' ' ' FOR index=1 TO 3 FREQOUT 1, Dit, 1900 PAUSE Dit NEXT ' Send 5 sounds. ' Send a dit. ' Short silence. PAUSE Dah*3 NEXT END Longer silence between digits. Send 5 sounds. Send a Dah. Short silence. 3 LEDs and buzzer ' Example 04 ' {$STAMP BS2sx} ' {$PBASIC 2.5} RedLED PIN 4 YellowLED PIN 13 GreenLED PIN 15 IsOn CON 1 IsOff CON 0 Setup: OUTPUT RedLED OUTPUT YellowLED OUTPUT GreenLED Main: DO RedLED = IsOn PAUSE 100 RedLED = IsOff PAUSE 10 YellowLED = IsOn PAUSE 100 YellowLED = IsOff PAUSE 10 GreenLED = IsOn PAUSE 100 GreenLED = IsOff PAUSE 500 LOOP END Example 05c ' ' ' ' ' File: “Example-05c.bsx” red LED is connected to pin 4 Yellow LED is connected to pin 13 Green LED is connected to pin 15 IsOn = 1(constant) LED is active high IsOff = 0(constant) LED is active low ' sets OUTPUT to be pin 4 Format of variables 1. 2. 3. 4. Bit Nibble (Nib) Byte Word 0 or 1 0-15 0-255 0-65535 or -32768 to + 32767 Example of variables: Mouse Cat Dog Rhino VAR VAR VAR VAR BIT NIB BYTE WORD ' ' ' ' Value Value Value Value can can can can be be be be 0 0 0 0 or to to to 1. 15. 255. 65535. Example 06 Performing calculations ' {$STAMP BS2sx} ' {$PBASIC 2.5} TOTAL VAR Byte X CON 20 TOTAL = 0 TOTAL = TOTAL +100 DEBUG ? TOTAL TOTAL= TOTAL/3 DEBUG ? TOTAL DEBUG ? X END Run the program. What do you see? File: “Example-06.bsx” Pseudo Code • Start of program • Measure temperature - Is temperature < 100 F ? • Yes, Turn on heat - Is temperature > 102 F ? • Yes, Turn on cooling fan • Go back to start. Flow Chart Start Measure Temperature Temp. < 100 Yes Energize Heater No Temp. > 102 Yes Energize Fan No 22 Sequential Flow Example Pseudo-Code: Flowchart: Start of program Start Turn off LED 1 Turn OFF LED1 Turn off LED 2 Turn OFF LED2 Pause for 2 seconds 2 Second Pause Light LED 1 Pause for 2 seconds Light LED 2 End of program Turn ON LED1 2 Second Pause Code: ' <<<< INSERT COMMON ' CIRCUIT DECLARATIONS >>>> 'Prog 6A: Example of sequential flow ' ****** Main program ******** LED1 = LED_Off 'Turn off LED 1 LED2 = LED_Off 'Turn off LED 2 PAUSE 2000 'Pause for 2 sec. LED1 = LED_On 'Light LED 1 PAUSE 2000 'Pause for 2 sec. LED2 = LED_On 'Light LED 2 END Turn ON LED2 End 23 Branching Overview “GOTO” • Branching is the act of breaking out of a sequence to perform code in another location of the program. • The simplest form of branching is to use the GOTO instruction: GOTO label Looping Flow Example Pseudo-Code: Start of program Flowchart: Code: Start Turn off LED 1 Turn OFF LED1 Turn off LED 2 Turn OFF LED2 Pause for 2 seconds 2 Second Pause Light LED 1 Pause for 2 seconds Light LED 2 Go back to start Turn ON LED1 2 Second Pause ' <<<< INSERT COMMON ' CIRCUIT DECLARATIONS >>>> 'Prog 6B: Example of sequential ' flow with looping ' ****** Main program ********** Main: LED1 = LED_Off 'Turn off LED 1 LED2 = LED_Off 'Turn off LED 2 PAUSE 2000 'Pause for 2 sec. LED1 = LED_On 'Light LED 1 PAUSE 2000 'Pause for 2 sec. LED2 = LED_On 'Light LED 2 GOTO Main 'Repeat sequence Turn ON LED2 25 Conditionals Overview • The previous example is an unconditional branch; the program will branch back to Main regardless of any code parameters. • In a conditional branch a decision is made based on a current condition to branch or not to branch. • As humans, we constantly make decisions based on input as to what to perform. Shower too cold? Turn up the hot. Shower too hot? Turn down the hot water. • Microcontrollers can be programmed to act based on condition. “IF…THEN” • The “IF-THEN” is the primary means of conditional branching. IF condition THEN addressLabel • If the condition is evaluated to be true, execution will branch to the named address label. • If the condition is not true, execution will continue to the next step in the program sequence. • A condition is typically an equality: value1 = value2 value1 > value2 value1 < value2 IN8 = 1 Note: Compared to many versions of BASIC and other languages, the PBASIC 2.0 implementation of the IF-THEN is fairly limited. See the PBASIC 2.5 appendix for new implementations of IF-THEN. “IF-THEN” Example: Alarm This program will sound the alarm as long as pushbutton 1 is pressed. Start: Pseudo-Code Is button 1 pressed? • Yes, Go sound Alarm • No, Go back to start Alarm: • Sound speaker • Go back to start of program Flowchart ' <<<< INSERT SECTION 5 COMMON ' CIRCUIT DECLARATIONS >>>> 'Prog 6C: Conditional Branching Alarm Main False Button 1 Pressed Program Code True Speaker 2000Hz for 1 second Main: ' If pushbutton 1 is pressed, ' then go sound alarm IF PB1 = PB_On THEN Alarm GOTO Main Alarm: 'Sound the alarm FREQOUT Speaker, 1000, 2000 GOTO Main Main 28 Example 07 IF…THEN…ELSE File: “Example-07.bsx” ' Example 07 ' {$STAMP BS2sx} ' {$PBASIC 2.5} RedLED PIN YellowLED PIN PushButton PIN IsOn CON IsOff CON X VAR Bit 4 13 0 1 0 Setup: OUTPUT RedLED OUTPUT YellowLED ' ' ' ' ' red LED is connected to pin 4 yellow LED is connected to pin 13 push button is connected to pin 0 IsOn = 1(constant) LED is active high IsOff = 0(constant) LED is active low ' sets OUTPUT to be pin 4 ' sets OUTPUT to be pin 4 MainA: ' label “MainA” RedLED = IsOff YellowLED = IsOn X = IN0 IF X = 0 THEN MainA MainB: ' label “MainB” YellowLED = IsOff RedLED = IsOn X = IN0 IF X = 0 THEN GOTO MainA ELSE GOTO MainB END PIR - sensor ' ' ' Example PIR File: “Example-PIR.bsx” File: Example-PIR.bsx {$STAMP BS2sx} {$PBASIC 2.5} ' -----[ I/O Definitions ]------------------------------------------------PIR PIN 7 ' I/O Pin For PIR Sensor RED PIN 4 ' I/O Pin For RED LED GREEN PIN 15 ' I/O Pin For GREEN LED YELLOW PIN 13 ' I/O Pin For YELLOW LED ' -----[ Variables ]------------------------------------------------------counter VAR Byte ' Trip Counter ' -----[ Initialization ]-------------------------------------------------DEBUG CLS ' Clear DEBUG Screen LOW RED LOW GREEN HIGH YELLOW FOR counter = 20 TO 0 ' Wait 40 Seconds For PIR Warm-Up DEBUG HOME, "WARMING UP:", DEC2 counter PAUSE 1000 ' Display Counter Every Second NEXT LOW YELLOW counter = 0 ' Clear Counter Variable DEBUG HOME, "WAITING... " ' Display Waiting Message ' -----[ Program Code ]---------------------------------------------------Main: HIGH GREEN DO IF PIR = 1 THEN ' Motion Detected? counter = counter + 1 ' Update Trip Counter LOW GREEN HIGH RED ' Light LED DEBUG HOME, "TRIPPED...", DEC3 counter DO : LOOP UNTIL PIR = 0 ' Wait For PIR To Clear DEBUG HOME, "CLEARED...", DEC3 counter LOW RED ' Turn Off LED HIGH GREEN ENDIF LOOP