Upload
anis-norton
View
212
Download
0
Embed Size (px)
Citation preview
Basic StampOEM module
By Wilmer Arellano
OEM BASIC Stamp 2sx Wiring diagram
Note:
- is connection to negative pole of the battery
22
0 O
hm
22
0 O
hm
22
0 O
hm
22
0 O
hm
Pushbutton
Buzzer
LED
LED LED
OUT
PIRMotionsensor
9 V battery
CO
M p
ort
Mic
rocon
troller
ch
ip
15
KO
hm
' {$STAMP BS2}' {$PBASIC 2.5}OUTPUT 4 'PIN 4 Declared as outputINPUT 0 'PIN 0 Declared as inputGreen:OUT4=IN0 'PIN 4 will take the value of PIN 0 (switch)PAUSE 1000GOTO Green
Turn ON LED with Switch Input Output Declarations
Variables
Bit 0 or 1 Nibble (Nib) 0-15 Byte 0-255 Word 0-65535 or -32768 to +
32767
' {$STAMP BS2}' {$PBASIC 2.5}NS VAR Bit 'NS is a Bit VariableEW VAR Bit 'EW is a Bit VariableTOTAL VAR Byte 'TOTAL is a Byte VariableNOC CON 20 'NOC is a constantTOTAL = 0OUTPUT 4INPUT 1TOTAL = TOTAL +100DEBUG ? TOTALTOTAL= TOTAL/3DEBUG ? TOTALDEBUG ? NOC
Arithmetic Example
Pseudo Code
Start of program Measure temperature
Temperature < 100 F? Yes, Turn on heat
Temperature > 102 F? Yes, Turn on cooling fan Go back to start.
7
Start
MeasureTemperature
Temp.< 100
EnergizeHeater
Temp.> 102
EnergizeFan
Start
Yes
No
Yes
No
Flowchart
8
Sequential Flow Example
Pseudo-Code:
Start of program Turn off LED 1 Turn off LED 2 Pause for 2 seconds Light LED 1 Pause for 2 seconds Light LED 2
End of program
Flowchart:Flowchart:
' {$STAMP BS2}' {$PBASIC 2.5}LED_OFF CON 0 'LED_OFF is a constant of value 0LED_ON CON 1 'LED_ON is a constant of value 1start:OUTPUT 4OUTPUT 15OUT4 =LED_OFFOUT15 =LED_OFFPAUSE 1000OUT4 =LED_ONOUT15 =LED_ONPAUSE 1000GOTO start
Code:Code:
Start
Turn OFF LED1
Turn OFF LED2
2 Second Pause
Turn ON LED1
Turn ON LED2
2 Second Pause
End
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
' {$STAMP BS2}' {$PBASIC 2.5}OUTPUT 4 'PIN 4 Declared as outputINPUT 0 'PIN 0 Declared as inputGreen:OUT4=IN0 'PIN 4 will take the value of PIN 0 (switch)PAUSE 1000GOTO Green
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 conditions
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 = value2value1 > value2value1 < value2IN8 = 1
12
IF-THEN Example: Alarm This program will sound the alarm as long as pushbutton 1 is pressed.
Start: Start: • Is button 1 pressed? Is button 1 pressed? • Yes, Go sound Alarm Yes, Go sound Alarm • No, Go back to start No, Go back to start Alarm Alarm • Sound speaker Sound speaker • Go back to start of programGo back to start of program
' {$STAMP BS2}' {$PBASIC 2.5}SWITCH VAR Bitstart:SWITCH=IN0IF SWITCH=1 THEN buzz 'IF switch is pressed then turn on buzzerGOTO startbuzz:FREQOUT 1, 1000, 5000GOTO start
Pseudo-Code Flowchart
Program Code
Button 1Pressed
Main
Speaker2000Hz for1 second
Main
TrueFalse
' {$STAMP BS2}' {$PBASIC 2.5}NS VAR BitEW VAR BitOUTPUT 4OUTPUT 10INPUT 1Green:OUT4=1OUT10=0NS=IN1IF NS=1 THEN greenOUT4=0OUT10=1PAUSE 10000GOTO Green
What does this program do?