Transcript Mic1b

Basic Stamp
OEM module
By Wilmer Arellano
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
Flashing LED + Switch Input
' {$STAMP BS2sx}
' {$PBASIC 2.5}
G
r
e
e
n
:
'
L
a
b
e
l
O
r
p
o
i
n
t
w
e
r
e
y
o
u
c
a
n
R
E
T
U
R
N
LOW 4
'Set pin 4 an output pin and low voltage "0"
PAUSE 1000
'Pause 1000 ms
HIGH 4
'Set pin 4 an output pin and high voltage "1"
PAUSE 1000
'Pause 1000 ms
DEBUG ? IN1 'Input the value of pin 1
PAUSE 1000
'Pause 1000 ms
GOTO Green
' {$STAMP BS2}
' {$PBASIC 2.5}
OUTPUT 4
Green:
PAUSE 1000
OUT4=1
PAUSE 1000
DEBUG ? IN1
PAUSE 1000
GOTO Green
' {$STAMP BS2}
' {$PBASIC 2.5}
OUTPUT 4
INPUT 1
Green:
OUT4=IN1
PAUSE 1000
GOTO Green
Variables
Bit
 Nibble (Nib)
 Byte
 Word
32767

0 or 1
0-15
0-255
0-65535 or -32768 to +
' {$STAMP BS2}
' {$PBASIC 2.5}
NS VAR Bit
EW VAR Bit
TOTAL VAR Byte
NOC CON 20
TOTAL = 0
OUTPUT 4
INPUT 1
TOTAL = TOTAL +100
DEBUG ? TOTAL
TOTAL= TOTAL/3
DEBUG ? TOTAL
DEBUG ? NOC
Green:
OUT4=IN1
PAUSE 1000
GOTO Green
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.

Start
Measure
Temperature
Temp.
< 100
Yes
Energize
Heater
No
Temp.
> 102
Yes
Energize
Fan
No
Star
t
13
Sequential Flow Example
Pseudo-Code:
Flowchart:
Start
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

Turn OFF LED1
Turn OFF LED2
2Second Pause
Turn ON LED1
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
2Second Pause
End of program
Turn ON LED2
End
14
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
Flowchart:
Pseudo-Code:
Start
Turn OFF LED1
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

Turn OFF LED2
2Second Pause
Turn ON LED1
2Second Pause
Go back to start
Code:
' <<<< 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
16
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
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
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.
Flowchart
Program Code
Main
Pseudo-Code
False
S
tart:
•
Is button 1 pressed?
•
Yes, Go sound Alarm
•
No, Go back to start
Alarm
•
S
ound speaker
•
Go back to start of program
Button 1
Pressed
' <<<< INSERTSECTION 5 COMMON
' CIRCUIT DECLARATIONS>>>>
'Prog 6C: Conditional Branching Alarm
True
S
peaker
2000Hz for
1 second
Main
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
19
' {$STAMP BS2}
' {$PBASIC 2.5}
NS VAR Bit
EW VAR Bit
OUTPUT 4
OUTPUT 10
INPUT 1
Green:
OUT4=1
OUT10=0
NS=IN1
IF NS=1 THEN green
OUT4=0
OUT10=1
PAUSE 10000
GOTO Green
Car1


















' {$STAMP BS2sx}
' {$PBASIC 2.5}
PAUSE 5000
HIGH 10
LOW 11
start:
HIGH 4
HIGH 8
LOW 9
PAUSE 3000
LOW 4
LOW 8
PAUSE 3000
HIGH 9
PAUSE 3000
LOW 9
PAUSE 3000
GOTO start