Basic Micro - Home
CART IS EMPTY
  » BasicATOM
  » BasicATOM Nano
  » BasicATOM Pro
  » PICmicro
  » Motor Drivers
  » Robotics
  » Power
  » GPS
  » R/C Lighting
  » Wiring Kits
  » Books
  » Accessories
  » Component Store
Visit Our Resellers

» LynxMotion Robot Kits

» RobotShop US Robots

» Advance Micro

Home > Tutorials > Basic Stamp 2 Workshop > Software

Software

Basic Stamp 2 Workshop - SOFTWARE

Reprinted from original article by Vincent Leclerc

available here

 

MAIN Programming Concepts & Tips PBASIC Language

 

Programming Concepts & Tips

1001101110100110

Microprocessors such as the one in the BS2 function with series of bits. Bits can have 2 values: 0 or 1, true or false, open or closed, etc. Fortunately we do not need to program with bits anymore. Over the years people have invented several languages that can be translated to binary strings. These languages share common characteristics. Below are the most important programming concepts.

Variables

Variables are containers for information. You can define a variable by giving it a name and a maximum size. Once a variable is defined, you can initialize it. That is to put information in it. In general the information is a number.
For example, you could define a variable called AccountNumber and say it can hold values less than 1 000. Then you could say that AccountNumber contains the value 345. This way everytime you refer to AccountNumber, you refer in fact to the number 345.

Assignment

At any time you can put a new value in a variable. This is called an assignment operation.
For example, using the previous variabe AccountNumber, at any time in your program you could assign it a new value. You could say "now put 755 inside AccountNumber". This would erase the prevoius value of 345 and AccountNumber would now correspond to the number 755.

Arithmetics

You can perform simple arithmetic operation on numbers and variables containing numbers. You can perform additions, substractions, multiplications, divisions, modulo, absolute value, etc.
For example, you could create the variable MyMoney that can hold a maximum of 10 000. Then say that MyMoney currently holds 5 000. You could then add numbers to MyMoney by using the addition and the assignment operators. You would say something like "take the value of MyMoney add it 600 and store the result back in MyMoney". MyMoney would then correspond to the number 5 600.

Comparisons

You can compare variables with each other.
For example, you could ask if MyMoney is greater than AccountNumber.

Loops

You can ask the program to perform a set of operations for a specific ammount of times. It is a very useful feature of most programming languages. This way you do not have to type the same commands over and over.
For example you could tell the program to flash a light on and off 50 times.

Control statements

This a powerful tool in programming. Control statements are of the form of " if this do that".
For example, you in a banking system, you could want to add a money bonus to your 10 first clients (account number from 001 to 010). You would do something like: if AccountNumber is smaller than 10 then add 25 to the money in that bank account.

Subroutines

A subroutine is a set of operations that has a name. You can refer to this name at anytime in your program. When you refer to a subroutine, the program jumps to this set of operations, performs them and goes back to the previous statement when it has finished.
For example, you could define a subroutine called FlashLights. You could insert statements in FlashLights such as: turn the lights on for 1 second, then turn the lights off. This way anywhere in your program, you could simply say "FlashLights". That would cause the program to perform the operations in the subroutine FlashLights and then go back to what it was doing before.

Comments

A comment is a string of information that will not be read by the microprocessor. In general, it is used to explain what a specific line of code means. You should use them as much as possible. This way when you read a program you wrote 2 months ago, you remember what each line of code was for.

 

PBASIC Language

 

References

Parallax Inc are the developpers of PBASIC. They wrote a very detailled document about almost all you can do with a BS2 in terms of software. The Basic Stamp Programming Manual is free and online on their website.

BASIC Stamp Editor

This is the most popular editor for BS2s. It is very simple and easy to use. You simply type in your code, plug your BS2 in your serial port with the RS232 cable and click on RUN in the editor and your BS2 is ready to go.
You must put a special command on the first line of your program. This command will tell the Editor how to translate your program from PBASIC to binary strings before uploading it in the microcontroller:

'{$STAMP BS2}

Comments

In PBASIC everything that follows the symbol ' is a comment and will not be executed by the µprocessor.

Memory

The BS2 has 32 bytes of variable RAM space. Of these, the first six bytes are reserved for input, output, and direction control of the I/O pins. The remaining 26 bytes are available for general-purpose use as variables. That means that your program can hold a maximum of 26 variables having a maximum value of 1 byte (256).

I/O

The I/O pins of the BS2 are numbered from 0 to 15.
If you want to use them as input, you can access the variables IN0 to IN15. For example you can check if a switch connectec to pin 6 is open or closed. If the switch is open IN6 will be equal to 0. If the switch is closed IN6 will be equal to 1.
If you want to use the pins as output, you can use the variables OUT0 to OUT15.
For example, if you want to turn on a LED connected to pin 4, one possible way would be to assign the value 1 to the variable OUT4.

Variables

You can define a variable the following way:

Name VAR Size

where Name is whatever name you want, VAR tells PBASIC that it is a new variable and Size is the maximum value you will be allowed to put in this variable.
There are certain rules regarding variable names. Variables must start with a letter, can contain a mixture of letters, numbers, and underscore characters, and must not be the same as PBASIC keywords or labels used in your program. Additionally, variables can be up to 32 characters long.
The Size argument has four choices: BIT (1 bit), NIB (nibble; 4 bits), BYTE (8 bits), and WORD (16 bits). For example:

  • Mouse VAR BIT
    'here the value can be 0 or 1
  • Cat VAR NIB
    'here the value can be 0 to 15
  • Dog VAR BYTE
    'here the value can be 0 to 255
  • Rhino VAR WORD
    'here the value can be 0 to 65535

Assignments

Once a variable is declared you can start assigning it values. You do it with the equal sign. For example:

  • Mouse = 1
  • Cat = 9
  • Dog = Cat
    'here we say copy the value stored
    'in Cat into the variable Dog

Arithmetics

You can perform arithmetic operations on numbers and variables. For example:

  • 5 * 5
    'multiply 2 numbers
  • Mouse + 10
    'add 10 to the value contained in Mouse
  • Rhino / Cat
    'divide the value of Rhino
    'by the value of Cat

Now it is important to realize that those operations will not affect anything in the program because we do not store the result anywhere. If we want to store the result somewhere we need to incorporate the assignment operator to the statement. For example:

  • Dog = 3 - 2
    'substract 2 to 3 and put the result
    'in the variable Dog
  • Dog = Cat * 2
    'multiply the value stored in Cat 2 times
    'and put the result in the variable Dog
  • Dog = Dog / Dog
    'divide the current value of Dog
    'by the current value of Dog and
    'put the new result in Dog.
    'In this case Dog would equal 1.

I/O part 2

LOW 'Make pin output low.
HIGH 'Make pin output high.

HIGH and LOW are easier ways to turn on and off circuits connected to output pins. HIGH sends 5V through the pin and LOW grounds the pin. For example:

HIGH 3 'this will turn on pin 3 of the BS2

Waiting

PAUSE 'Pause execution for 0–65535 milliseconds.

PAUSE is very useful in programs. The microprocessor makes very fast calculations, sometimes too fast. You might want to have it wait for some time before executing the next command. in this case you use the PAUSE command. For example:

HIGH 1 'turn on whatever is connected to pin 1

PAUSE 1000 'leave it on for 1 second

LOW 1 'and turn it off

Loops

Label: 'define a label
GOTO 'Branch to the label

This command is for lazy people. To use it, you first need to put a label at the beginning of important code you would wish to go to many times in the life of your program. Then anywhere in the program, you can simply say "GOTO WhateverLabel" and the program will jump to this piece of code. For example:

Flash: 'define the label

...... 'lines of code

GOTO Flash 'jump to the label Flash

This example would create an infinite loop.

Control Statements

IF...THEN 'Compare and conditionally branch.

This is used to make a comparsion and, depending on the result, GOTO a label or a subroutine (see below). For example:

IF IN2 = 1 THEN Flash

That means: IF the input pin 2 is on (if a button is pressed for example) THEN GOTO label Flash and execute the commands right under this label. If it is not on, simply ignore this statement.

Loops part 2

FOR...TO...NEXT 'Establish a FOR-NEXT loop.

This a more controllable form of looping. You can tell the program how many times to loop. For example:

counter VAR BYTE 'define a variable called counter

FOR counter = 0 TO 9 'tell the program to loop 10 times

.... ' execute commands in the loop

NEXT 'tell the program this is the end of the looping commands

In this code, all the commands between "FOR counter = 0 TO 9" and "NEXT" would be executed 10 times.

Subroutines

GOSUB 'Branch to subroutine at address.
RETURN 'Return from subroutine.
END 'Terminate the whole program.

The same way you create labels to use in GOTO statements, you can create subroutines. You define a label just like in GOTO but you encapsulate the code in the label by adding a RETURN or an END statement at the end. For example:

Strobe: 'define a label for the subroutine Strobe

..... 'code to be executed when calling the subroutine Strobe

RETURN 'End of the subroutine

..... 'arbitrary commands

GOSUB Strobe 'jump to subroutine Strobe

.... 'rest of the program

In this example, we define a subroutine Strobe (that could strobe LEDs for example). Then somewhere else in the program we want to strobe LEDs. So we simply call the subroutine Strobe that will strobe the LEDs and, once finished strobing, will go back to the rest of the program. If we would have put END at the end of the subroutine, that would have terminated the whole program.

Measuring potentiometers

RCTIME 'Measure an RC charge/discharge time. Can be used to measure potentiometers.

Please refer to page 256 of the Basic Stamp Programming Manual.

Generating sound

FREQOUT 'Generate one or two sine waves of specified frequencies.

Please refer to page 123 of the Basic Stamp Programming Manual.

Debugging

DEBUG 'Send information to the PC for viewing.

DEBUG is used to get dynamic information on the behaviors of you program at runtime. It opens a window in the BASIC Stamp Editor and displays whatever you tell it to display. For example:

DEBUG IN6 'display the state of input pin 6

Other Commands

There are many more commands you can use with a BS2. I suggest you have an extensive look at the Basic Stamp Programming Manual for more information and examples about them.

 
Phone (800) 535-9161
International (714) 442-8689
Support: support@basicmicro.com
Basic Micro.com, Inc.
Basic Micro Inc.
23811 Washington Ave.
Suite C110-283
Murrieta, CA. 92562