Tom's Main Menu

Physical Computing Home

Intro to Physical Computing Syllabus

Networked Objects

Sustainable Practices

blog

Resources

code, circuits, & construction

my del.icio.us links

 

Programming the BX-24

 
The BX-24 is programmed using a variation of the BASIC programming language, based on Visual BASIC. As the name implies, it's a very simple language. To program it, you need a Windows-based PC, a copy of the BX-24 BASIC compiler (available from NetMedia), and a cable to connect the PC to the BX-24.

What follows is an abbreviated introduction to the BX-24's version of BASIC. For a more comprehensive treatment, see NetMedia's documentation files, especially the BX-24 Language Reference and BX-24 System Library.

The BASIC compiler is a program for the PC that allows you to write and edit programs for the BX-24, then compiles your program into the BX-24's machine language and downloads it to the BX-24. It connects to the BX-24 using a serial cable, attached to the PC's serial port.

BASIC programs for the BX-24 are divided into files called modules. Each module is just a text tile containing BASIC code. Every program must include at least one module, and most simple programs contain only one. Modules allow you to break your program up into separate files, for easy reuse. For example, functions related to serial communication are often divided into a separate module. Since serial functions are the same from program to program, the same file (module) can be used in many programs without having to rewrite it.

Modules are divided into subprograms called subroutines (alternately procedures or functions). Subroutines are blocks of code that perform specific actions, and can be called by other subprograms. They are similar to handlers in Lingo. (More on functions below). There are two ways to call a subprogram, but they both do the same thing. The only difference is in the syntax. You can use this syntax:

call mySub(parameter1, parameter2, parameter3)

'or

call mySub() 
' if there are no parameters

or this syntax:

mySub parameter1, parameter2, parameter3

If you use they keyword "call" without using parentheses around your parameters, you get an error. Conversely, if you use parentheses without the keyword "call", you get an error. Choose either one, and stick with it. Consistency will make your life easier.

Every program must contain at least one subprogram, called Main. That main subroutine may call other subroutines as well. The other subroutines get placed after the main subroutine.

    Sub main()
	    call delay(0.5)  ' start  program with a half-second delay 

       ' example of calling another subprogram
        call mySub()                 
    end Sub


    Sub mySub()
        ' do different stuff
    end Sub

There are many built-in functions of the BASIC language that can be called in a program. All of these are detailed in the System Library, which you can download along with the other BX-24 documents from NetMedia as a .zip file. Some of those built-in functions are used below.

Your BASIC program will begin execution at the top of your code, and stop running when you reach the end of the main subroutine. If the main subroutine calls any other subroutines, execution will jump to the called routine, then return to where it was in the main subroutine at the end of the called subroutine. Once the main subroutine reaches the end, the program stops. The simplest program might look something like this:

    Sub main()
	call delay(0.5)  ' start  program with a half-second delay 

        debug.print "Hello world!"
    end Sub

This program prints the message "Hello World!", then stops.

Usually we don't want the program to stop, but we want it to keep going for as long as the microcontroller is turned on. For example, we may have a switch wired to the BX-24 on one pin, and an LED on the other, and as long as the chip is powered, it should check the switch, and turn the LED on if the switch is on. To keep the program running indefinitely, we put an infinite loop in the main subroutine. Here's a program that does that:

    Sub main()
	call delay(0.5)  ' start  program with a half-second delay 

        do
            if getPin(5) = 1 then
                call putPin(10, 1)
            else
                call putPin(10,0)
            end if
        loop
    end Sub

In this program, the BX-24 looks for input on pin 5. If it getsa high (+5V) input on pin 5, it ouputs a high (+5V) signal on pin 10. If it gets a low (0V) input on pin 5, it outputs a low (0V) signal on pin 10. The "loop" keyword tells it to go back to the "do" keyword, so everything inside the "do...loop" block of code is repeated over and over again.

Getpin(), putPin(), and debug.print are examples of the built-in functions of the language mentioned earlier. You can find them listed in the system library. Each function has certain parameters it expects you to give it:

GetPin() expects a number in the parentheses that represents one of the input/output pins. the function will return a value, 1 or 0, depending on whether there is a high signal on the pin or not.

PutPin() expects two numbers separated by a comma. The first number is an input/output pin on which to put a signal, and the second pin is the signal to put out (1 or 0).

debug.print expects a string of characters. It will send that string out the serial port.

Other built-in functions will have other parameters, as detailed in the system library.

For more information on BASIC for the BX-24, see NetMedia's documentation, my examples for the BX-24, andNetMedia's application notes for the BX-24. The BX-24 Language Reference covers the syntax of the language in detail, and the System Library covers the functions of the language.
If-then, Variables, Loops

Like most programming languages, the main tools of BASIC are if statements, repeat loops, and variables.

If statements are used to direct the program's actions based on changing conditions.

if x = 5 then 
  ' sets pin 12 to high (+5V)
      call putPin(12, 1)    

     ' reads the analog voltage on pin 20
  ' (see analog in notes)
     pot = getADC(20)        
                            
end if

Variables are storage locations in the BX-24's memory for changing information. In BASIC, variables have to be declared at the start of the program, or at the start of any subroutine that uses them, so the BX-24 knows how much memory to set aside for them. Every variable has a data type. The data type of a variable determines how much memory the BX-24 needs for the variable, and how it will use the data stored in the variable. You declare both the name and the Data type of the variable. There are several data types, and each takes a different amount of memory. For details, see the notes on Variables.

The BX-24 has 400 bytes (3200 bits) of RAM total, so use it wisely.

Variables are declared using the Dim statement:

    Dim byteVar as Byte
    Dim singleVar as Single

' (a 5-character string)
    Dim stringVar as String * 5  

Repeat loops make the program repeat a command or series of commands until some condition is met. There are 2 main variations on the repeat loop, For-Next loops, and Do loops.

For-next loops cause a condition to repeat until a particular variable reaches a given end condition

 For x = 1 to 10
     ' convert X to a string and print it:
     debug.Print " X = "; CStr(x)
     y = x * 10
     ' convert Y to a string and print it:
     debug.Print " Y = "; CStr(Y)
 next
 

Do loops repeat until a certain condition is met. They are more flexible than for-next loops in that the condition does not have to depend on a counter variable. There are a number of variations on the do loop:

do while x = False
    if getPin(13) = 0 then
        x = True
    end if
   ' do other stuff
loop

(note: unless you want a loop to repeat infinitely, there has to be a way for the loop's ending condition to be changed within the loop, as there is above)

do
    ' gets the state of pin 13:
    Y = getPin(13) 

    if Y = 0 then
        x = False
    end if
loop while x = true



do until x = true
    Y = getPin(13) ' gets the state of pin 13:
    if Y = 0 then
        x = True
    end if
loop

Do loop conditions can come at the beginning or the end of the loop. If they are at the end, the loop will be run at least once before the loop condition is checked.

Comments (statements that are not executed) in BASIC all start with a single quote ('). When the BASIC compiler sees a single quote, it ignores everything else to the end of the line. You should comment your code well, so you (and anyone else reading it) knows what the program does. Comment your code well, so you (and others) know what your program does.