Presentation is loading. Please wait.

Presentation is loading. Please wait.

Programming the BASIC Stamp

Similar presentations


Presentation on theme: "Programming the BASIC Stamp"— Presentation transcript:

1 Programming the BASIC Stamp
Computers do exactly what you tell them, no more, no less BASIC Stamp uses the PBASIC programming language You can go a long ways with just a few instructions See the BASIC Stamp Guide (2011 web site) - Programs are the list of instructions (recipe) for telling the computer what to do. - Programming language defines a particular vocabulary set and syntax. You must follow without deviations. Mistakes often hard to find. - PBASIC is (a little) like BASIC, but greatly simplified, and tailored to running I/O ports on the Stamp chip. - Pentium III runing Visual Basic: $600 - Microchip running Stamp Basic: $1

2 On the Stamp Programming cable Power pins EEPROM Brain I/O pins
Breadboard area 9V battery Reset

3 Schematic icons 1K TIP120

4 Dealing with the outside world
SENSOR COMPUTER ACTUATOR Switch Light beam Potentiometer Encoder Temperature Lamp Relay Motor Solenoid

5 Driving actuators High 4 Program sets ports high/low (1/0) Low 4 +5V
Stamp board pins set to +5V/0V TIP120 1K PIN 4 +12 V Interface electronics use signal voltages and power supply to switch motors on/off

6 Reading sensors Program reads value of ports (1/0) IF in4 = 1 THEN ….
Stamp board pins set to +5V/0V 10K PIN 4 +5 V Interface electronics change sensor signals into +5V/0V

7 Format Labels, comments loop: toggle 0 ‘ toggle motor
pause 250 ‘ pause for 250 goto loop Labels - colon to define, no colon to use - letters, numbers, _ start with letter Symbol Definition - text substitution - can make it simpler to read - don’t used reserved words - define at the top - use capitals (convention) - symbols are not variables Comments - ‘ or REM Misc: - case insensitive. Symbols LED con 1 MOTOR con 0 start: toggle LED toggle MOTOR pause 250 goto start

8 Data sizes A bit is one binary digit: 0/1 A byte is 8 bits b0 b7
(binary) = 3 (decimal) = 255 Generic, for any computer Most modern programming languages deal in 32-bit words (4 billion or 4 Gigs) - Stamp is positive integers only - byte: 0-255 - word: 0-65,535 Type #bits Number range bit byte word ,535

9 Variables Pins 15 i/o pins 0-15 for outputs in0-in15 for inputs
Data variables 26 bytes of variable space byte (0-255)or word(0-65,535) Allocate like this: length var byte width var byte area var word Then in program: length = 20 width = 20 area = length * width Positive numbers only!! For HIGH, LOW, TOGGLE, use 0-7 rather than Pin0-Pin7

10 Math +,- add, subtract * multiply / division (truncates) x var byte
x = x+2 result? Positive integers only Will overflow at 255 (demo with debug dec x) watch multiply overflow If x defined as a byte x = 50*50 = error (will return 196) division truncates y = 5/2 will return 2 Can use variables and constants x var byte x = 254 x = x+2 result? y var word y = 50*50 = 2500 x var byte x = 5/2

11 A FIRST PROGRAM ‘Turns on LED for a brief time LED con 0 high LED
pause 200 low LED end Logical conditionals: = <> > < >= Most common use is to check state of a switch

12 LOOP LED con 0 doit: toggle LED pause 200 goto doit
Logical conditionals: = <> > < >= Most common use is to check state of a switch

13 FOR…NEXT LOOP LED con 0 i var byte for i = 1 to 20 toggle LED
pause 200 next end Logical conditionals: = <> > < >= Most common use is to check state of a switch

14 IF....THEN x var byte y var byte x = 5 y = 7 start:
if x < y then motor high 1 ‘ led on goto continue motor: high 0 ‘ motor on continue: Conditionals: = <> > < >= <= Logical conditionals: = <> > < >= Most common use is to check state of a switch

15 IF....THEN, CHECKING AN INPUT
motor con 1 start: if in4 = 1 THEN start gomotor: high motor ‘ motor on pause 2000 low motor end Logical conditionals: = <> > < >= Most common use is to check state of a switch

16


Download ppt "Programming the BASIC Stamp"

Similar presentations


Ads by Google