BASIC Stamp Programming Language

modified on 20 May 2010 at 01:59 - 1,092 views

From RoboWiki

(Redirected from BASIC)
Jump to: navigation, search

If you are looking for how to interface with sensors, see BASIC Sensors Overview.

For those who are developing with the Parallax BASIC Stamp, here is a tutorial on the principles of their programming language. Remember to use your help file as well as other tutorials on the BASIC Stamp on the Robo Wiki. It is recommended, but not necessary, to have experience with programming before. Though BASIC has an older syntax which is not similar to C but is still a linear programming language. BASIC is special in the sense it can be both a high level and low level language: Memory is managed for you, but is relatively limited, yet the developer still has access to registers, pins, and even the direct memory on the chip!

All in all, the BASIC stamp is a great chip to get started with if you know nothing about programming or much about hardware. Look into other Microcontrollers if you have any experience, as they may fit your needs better.

Contents

[edit] Setting up your BASIC Stamp

For those using the original serial models or newUSB models, there are no real differences in terms of creating code. Connect the device to the target PC and launch the IDE (Integrated Development Environment) and create a new text file. Within

[edit] Coding Basics

The BASIC programming language lets you create commented lines using the ' character, which are never compiled. A sample would be:

   ' No matter what is on this line, it will not compile due to this is a comment.

Please note, however, there must always be two special comment lines at the top of all source files to declare the type of stamp and language you are using. For our hardware, always write these two lines:

   ' {$STAMP BS2}
   ' {$PBASIC 2.5}

[edit] Variables

Variables and variable manipulations are similar to concepts found in many other languages, but again the syntax is very different. To create a variable, follow the following structure:

   [name of variable] VAR [variable type]

Note that the name of variable can not contain special characters (except underscore '_') and cannot start with a number. Variable types are always unsigned integers (meaning no negative values exist and that fractions may not exist, though there are hacks to 'fake' this functionality) The variable types are as follows:

  • Bit - A variable type of a single bit, ranging from 0 to 1
  • Nib - A variable type of a nibble (half a byte, 4 bits), ranging from 0 to 15
  • Byte - A variable type of a single byte (8 bits), ranging from 0 to 255
  • Word - A variable type of two bytes (16 bits), ranging from 0 to 65535

If you do not understand the basic concepts of bits and bytes, do not worry, as this is not necessary to learn. Look into the Wikipedia Article on binary if you are interested.

Some examples of variables are:

   sampleBoolean VAR Bit 'bool-like
   someNumber VAR Nib 'very small range
   robotVelocity VAR Byte 'medium range
   robotState VAR Word 'large range

You can make reference variables, variables that mirror any changes to and from a target variable, by using slightly modified variable syntax:

   [name of variable] VAR [name of referred variable]

A sample of this is:

   CatAge VAR Byte 'Create a single byte variable
   CatAge = 2 'Set the age to two
   Age VAR CatAge 'Create a reference variable that mimics CatAge, called Age
   Age = 4 'By setting Age to four, CatAge is also now four
   CatAge = 6 'By setting CatAge to six, Age is now six

Constants, variables that are set once and never change at run-time, are similar to standard variable declaration:

   [name of variable] CON [variable type]

An example of a constant is:

   RobotSize CON 20 'Represent the robot's size as 20, as it will never change

Setting variables is as simple as using the '=' set operator:

   myBool = 1
   someSwitch = 12
   myVelocity = 123
   someLargeCounter = 12345

If you would like to set a value based on binary or hexadecimal, use either '$' and '%' before the number, for example:

   SomeHex Var Byte
   SomeHex = $f 'This sets the SomeHex variable to the number 16, since 'f' in hexadecimal is 16.
   SomeBinary Var Byte
   SomeBinary = %1010 'This sets the SomeBinary variable to the number 10, since '1010' in binary is 10.

[edit] Arrays

BASIC arrays are limited and unmanaged, similar to languages such as C/C++. You can not resize an array, or have different data types. Also, BASIC does not support any notion of custom structures or classes, thus us very far from Object Oriented languages.

The basic syntax for an array deceleration is:

   [Array name] VAR [Data type]([Elements needed])

Two samples of array creation are:

   RobotServos VAR Word(10) ' Create an array of type word (two bytes) of ten elements (20 bytes in total)

Accessing each element in the array is based on an index starting from 0, meaning that an array of five elements starts at index 0, 1, 2, 3, and 4. An example of accessing and setting array elements is:

   RobotServos VAR Word(10) ' Same as above sample code
   RobotServos(0) = 10 ' Set the first element, or slot, of the array to 10
   MyServo VAR Word ' Create a new variable
   MyServo = RobotServos(2) ' Get the value of the third element and set it to MyServo

[edit] Goto/Label and Routines

BASIC does not allow you to have formal functions, similar to languages such as C/C++. The concept of function parameters and return variables do not exist, and thus special ways of passing/returning data must be done. There are two ways to mimic these concepts. The first is to use a goto/label in which the execution flow jumps to a label you wrote in the code, then jumps back to another label. The issue here is the goto/label method does not allow you to return to where it was called.

The syntax for a label is:

   [Label name]:

The syntax for a goto is:

   GOTO [Label name]

The below sample code demonstrates an infinite loop:

   MyFirstLabel: ' Declare a new label
   ' Do something here...
   GOTO MyFirstLabel ' Jump to the label

The above mentioned issue of not being able to returned to the position called can be resolved using the 'END' keyword, which states the end of a block of code. The below sample shows that by having the 'END' keyword, you jump back to the calling position.

   MyFirstLabel: ' Declare a new label
   GOTO MyTest ' Jump to the label MyTest
   GOTO MyFirstLabel ' Jump to the label
   
   MyTest:
   ' Do something here
   END ' By having the END here, we jump back to the line in which we called jumped to this block of code

Again, notice how with these methods we can not pass parameters or return variables. The solution is to place all needed parameters or data into global space, or global access. Simply declare your variables at the top of your programing, and use them at any position needed. The concept of variable scope and lifetime are non-existent in BASIC.

[edit] Control Structures

It might be needed to have loops that manipulate variables on each iteration or that strictly loop based on a certain condition. The FOR and WHILE loops are powerful tools that enable you to have controlled looping structures in your program. WHILE loops continue to loop until a case tells it to quit. FOR loops are the same as while loops, but allow for built-in iteration of a variable. An example of why a WHILE loop might be used is when your robot needs to repeat a central piece of code. A FOR loop may be used to iterate through an array or query the same sensor a set amount of times.

The syntax for a WHILE loops is as follows:

   DO [WHILE([Boolean statement)]
       [Code]
   LOOP

The syntax may seem complex, but is quite easy. The reality is that the WHILE loop may not need the 'WHILE' keyword. A simple infinite loop will only need the 'DO' keyword, then any amount of code, and finished with a 'LOOP' keyword. The keyword 'WHILE' is used so that you can tell the program when to stop looping, which is when the Boolean statement is false.

An example of a simple WHILE loop is as follows:

   Counter VAR Word ' Create a double-byte variable
   Counter = 0 ' Set it to zero
   DO ' Start the loop
       Counter = Counter + 1 ' Add one to the counter
   LOOP ' End of loop

This code loops infinitely and keeps on adding one to the Counter variable. How can we write this such that we stop looping when the Counter variable reaches a certain value? We use the 'WHILE' keyword:

   Counter VAR Word ' Create a double-byte variable
   Counter = 0 ' Set it to zero
   DO WHILE(Counter < 10) ' Start the loop, and only loop as long as Counter is smaller than 10
       Counter = Counter + 1 ' Add one to the counter
   LOOP ' End of loop

If there are several points in which the program should break from within a loop, you can use a simple IF statement, such as IF(Counter > 9) THEN EXIT. Another type of loop, as mentioned, is the FOR loop. It can be similar to a WHILE loops, but allows iteration of variables within the loop code itself.

   FOR [Variable name] = [Starting value] TO [End value]
       [Code]
   NEXT

An example of this would be:

   Counter VAR Byte ' Create a single byte counter
   FOR counter = 0 TO 123 ' Loops 124 times from 0, 1, 2 ... 122, 123
       ' Come code here...
   NEXT

For loops are great tools for going through an array. A sample array/for usage is:

   Index VAR Byte ' Create a single byte index variable
   MyValues VAR Byte(10) ' Create an array of ten single-byte elements
   FOR Index = 0 TO 9 ' Start the loop from 0 to 9
       MyValues(Index) = Index * 2 ' At the n position, place the value (n x 2)
   NEXT ' End the loop

[edit] Memory Manipulation

A nice feature of the BASIC programming language is that it gives you direct access to memory. This may be useful for storing large pieces of memory, doing complex bit-wise manipulations, and creating custom data types in memory.

To access specific bits or bytes, the follow syntax is used:

   [Variable name].[Access type]

The above mentioned access types are the type of bit or byte you would like to access. The following table comes from the help file on the BASIC programming language

  • LOWBYTE - LOW Byte of a Word
  • HIGHBYTE - HIGH Byte of a Word
  • BYTE0 - LOW Byte of a Word
  • BYTE1 - HIGH Byte of a Word
  • LOWNIB - LOW nibble of a Word OR Byte
  • HIGHNIB - HIGH nibble of a Word OR Byte
  • NIB0 - Nibble 0 of a Word OR Byte
  • NIB1 - Nibble 1 of a Word OR Byte
  • NIB2 - Nibble 2 of a Word
  • NIB3 - Nibble 3 of a Word
  • LOWBIT - LOW Bit (LSB) of a Word, Byte, OR nibble
  • HIGHBIT - HIGH Bit (MSB) of a Word, Byte, OR nibble
  • BIT0 - Bit 0 (LSB) of a Word, Byte, OR nibble
  • BIT1 - Bit 1 of a Word, Byte, OR nibble
  • BIT2 - Bit 2 of a Word, Byte, OR nibble
  • BIT3 - Bit 3 of a Word, Byte, OR nibble
  • BIT4 ... BIT7 - Bits 4 through 7 of a Word OR Byte
  • BIT8 ... BIT15 - Bits 8 through 15 of a Word

A simple example of bit-wise access is as follows:

   MyNumber VAR Word ' Create a two-byte variable
   MyNumber = 123 ' Set my number to 123
   
   FirstByte VAR Byte ' Create a single-byte variable
   FirstByte = MyNumber.LOWBYTE ' We are taking the first/left byte of MyNumber

A multiple depth bit-wise access sample is as follows:

   MyNumber VAR Word ' Create a two-byte variable
   MyNumber = %10011001001100 ' Set my number to a value (In this case, it is 9804 in binary)
   
   FirstBit VAR Bit ' Create a single-bit variable
   FirstBit = MyNumber.LOWBYTE.LOWNIB.NIB0 ' We are taking the first byte's (low/left byte) first nibble's (low/left half of byte) first bit, which is 1