Beginner's Guide to Programming in Tiny Machine Basic

Industrologic, Inc.
3201 Highgate Lane
St. Charles, MO
63301
USA

Phone: (636) 723-4000
www.industrologic.com
info@industrologic.com



Trademark and Copyright Information

Tiny Machine Basic and TMB are Trademarks of Industrologic, Inc. 2000. This document is Copyright (c) 2002 by Industrologic, Inc. All rights reserved.

No part of this manual may be reproduced or transmitted in any form or by any means, electronic or mechanical, including photocopying or scanning, for any purpose other than the personal use by the purchaser of this product. Industrologic, Inc. reserves the right to revise this document at any time without obligation to notify any person of such revision. Industrologic, Inc. assumes no responsibility for any errors that may appear in this document.



Warning/Disclaimer

Whereas effort has been made to make the information in this document as useful and accurate as possible, Industrologic, Inc. assumes no responsibility for the application, usefulness, or completeness, of the information contained herein. Under no circumstance will Industrologic, Inc. be responsible or liable for any damages or losses including indirect or consequential damages or losses arising from either the use of any information contained within this manual or the use of any products or services referenced within this manual.



Introduction

If you have purchased one of Industrologic's products that includes a microcontroller with Tiny Machine Basic but have never done any computer or microcontroller programming, this guide will help get you started. Industrologic has created a customized version of Tiny Machine Basic for each of its microcontroller boards in order to take advantage of the specific hardware on each board, therefore each version will be slightly different. However, to keep things simple this guide will refer only to information common to all versions. Once you have written a few simple programs you will need to refer to the documentation supplied with your product to learn about the features of Tiny Machine Basic specific to that board.



Simple programming concepts you should know

Computer programming is simply telling a computer what to do in a step by step manner. The actual instructions that you use to represent what you want them to do may vary depending on the programming language you use, but there are some basic concepts in common to all programs.

Variables - A variable is a place in a computer's memory where you can store a number. It "varies" while the program is running depending on what you do to it. In more advanced languages variables can be represented by entire words, but in simpler languages like Tiny Machine Basic the letters of the alphabet, that is, "A" through "Z" are used (although not all of these letters are available in every version of Tiny Machine Basic.) Programs can do calculations on a variable and then make decisions based on what the value becomes. Calculations are written much like they are in mathematical formulas, for example, A=A+1. (This would make the variable A one greater than before.)

Computers perform most tasks one instruction at a time starting with the first task, but instructions exist to modify this sequence. These instructions are called "program flow" instructions, and are the basis of all useful computer programs. Many parts of a program simply perform a number of instructions one after the other, but often a program will need to run certain parts of the program at certain times and under certain conditions. It is at these times that a program will need program flow instructions.

Refer to Figure 1, Program Loops, Branching, and Subroutines.

Program loops - Sometimes for test purposes you can write a program that does only one or two tasks and then just ends, but in most cases you will need a program to remain in a "loop". This is how a program can continue running while it responds to inputs and operates outputs. Most programs have a "main loop", which is a simple loop that runs through a number of instructions and then goes to the first instruction and begins again. Of course there may be many instructions in this list, including some of the other type of program flow instructions.

Conditional loops - Within programs there may often be one or more "conditional loops", which are loops that are set up to do something a specified number of times, and then continue. Some languages have several different ways of setting up conditional loops. With conditional loops, the number of the loop being performed can be used to make other program decisions and perform different functions each time through the loop.

Conditional branching - Instructions that tell the computer where to go next in the program based on certain conditions that are present are called "conditional" instructions or "conditional branching" instructions. These instructions exist for many different types of checks: Equal to, not equal to, greater than, less than, greater than or equal to, less than or equal to, etc.

Subroutines - There are times when a main program loop needs to do a number of instructions several times, and the sequence of these instructions is identical each time they are used. Groups of instructions like these can be placed in "subroutines" which are "called" from the main program when needed and "return" to the main program. When subroutines are called, they remember which instruction called them, and when they return, they continue at the next instruction in the program.

simple programming concepts
Figure 1 - Program Loops, Branching, and Subroutines



Information specific to Tiny Machine Basic

The "prompt" - If Tiny Machine Basic is not already running your program, it displays a prompt, which is the "greater than" (>) character. This means that it is "prompting" you to enter an instruction or begin entering a program. If you like you can enter instructions one at a time to see what they do. You simply need to type the instruction name and hit the ENTER key.

Programs in Tiny Machine Basic use "line numbers" to identify instructions. When Tiny Machine Basic runs your program it begins with the lowest line number it finds. To write one line of your program you would type a line number, a space, the instruction, and the ENTER key. (Programs can also be loaded automatically from software running on your PC.)

Special instructions - There are a few instructions that you will need every time you use Tiny Machine Basic, but these instructions should never actually be part of a program. They should only be used at the prompt or without a line number.

NEW - This erases all of your program lines in preparation for entering a new program
RUN - This tells Tiny Machine Basic to run your program once entered (or loaded from the software running on the PC)
LIST - This displays your program to the screen for you to read
BOOTON - This tells Tiny Machine Basic to run your program automatically when power is applied to the board.
BOOTOFF - This reverse the action of BOOTON and causes Tiny Machine Basic to return the prompt when power is applied.

Once the BOOTON instruction is used you will need to press the pushbutton switch on the board (or type a Control C for some boards) while applying power in order to return to the prompt. Refer to the Tiny Machine Basic manual for the board.



Getting your microcontroller product ready to program

  1. Connect the board to an RS-232 serial port on your IBM PC compatible computer with the included cable.
  2. Connect the wallblock power supply to the board and apply power.
  3. Install the disk included with your product and view the README.TXT file. which explains the contents of the disk. (You can either use the installation program on the disk or manually copy the files you need to a folder on your computer.)
  4. Run the communication program called TERMBAS.EXE.
  5. Select the Com port according to which RS-232 port you used on the computer.
  6. Hit the ENTER key, and if everything has been set up correctly you will see the Tiny Machine Basic prompt, the ">".
You are now ready to do any of the following:

Entering and running a simple program

At the prompt do the following:

You have just written a program that consists of a simple loop. You should now see the numbers 0 through 255 being continuously displayed.

To stop your program you can turn off power to the board and then turn it back on so that it displays the prompt again. (With some versions of Tiny Machine Basic you can stop the program by entering a "Control C" character, that is, holding down the "Ctrl" key while pressing the "C" key.)



Other example programs

In the first program, we were very specific about what you were supposed to type because you had never before entered a program, but for the rest of the programs in this guide we will simply list the program lines much like the LIST instruction would do once your program was entered. Enter these programs just like you did the first one, ending each line with the ENTER key. (The left column is the only part to enter. The right column explains the instruction.) Before entering a program remember to use the NEW instruction to erase the old one, and after entering the new program use the RUN instruction to start it.



Example of how to check for a key pressed:

1 A=KEY        if a key is pressed put value in variable A (zero if no key pressed)
2 IF A=0 1     if A is zero go back to line 1 (this is a conditional branch)
3 PRINT A      display value of the key which is in variable A (try the Esc key)
6 GOTO 1       go to line 1 (this creates a loop from line 1 to line 6)

Add these lines to the above program:

4 A=A-27       subtract the value for the Escape key
5 IF A=0 10    if variable A is zero (used to be 27) go to line 10 (program will end)

This method will allow the Escape key to exit the loop. No power cycling necessary to return to the prompt.



Example of a time delay in a program:

1 PRINT "X"    display the X character (could be any character)
2 DELAY 20     delay 20 tenths of a second (2 seconds)
5 GOTO 1       go to line 1

Add these lines to the above program:

3 PRINT 13     send the "carriage return" character
4 PRINT 10     send the "line feed" character

These are two special control characters that must be sent to go to the next line of the display.



This is an example of how to make the program wait for you to enter a number from 0 to 255 at the keyboard and then use the value in the program. (Remember, a number cannot include any of the alphabet or punctuation characters.) It also shows how LOOP and ENDLOOP work to do something a specified number of times:

1 INPUT A      wait for a value to be entered followed by ENTER key
2 LOOP A       initiate a loop for the number of times in variable A
3 PRINT "X"    display the X character (could be any character)
4 ENDLOOP      this marks the end of the loop
5 GOTO 1       go to line 1



This is an example of how to have a main program call a subroutine:

1 A=10         put the value 10 into variable A
2 GOSUB 20     call the subroutine at line 20
3 A=15         put the value 15 into variable A
4 GOSUB 20     call the subroutine at line 20
5 DELAY 10     delay 10 tenths of a second (1 second)
6 GOTO 1       go to line 1
20 LOOP A      initiate a loop for the number of times in variable A
21 PRINT "X"   display the X character (could be any character)
22 ENDLOOP     this marks the end of the loop
23 PRINT 13    send the "carriage return" character
24 PRINT 10    send the "line feed" character
25 RETURN      must be at the end of every subroutine



By now you are probably getting a feel for what programming is and what you can do with just a few common BASIC language instructions. Remember, these example programs used only those instructions common to all version of Tiny Machine Basic, but there are a lot more. What you should now do is to learn about the instructions related to the specific hardware of your particular microcontroller board by referring to the Tiny Machine Basic documentation you received with your board. Using what you have learned from these simple example programs will allow you to better understand how to use these additional instructions.



Writing programs in a text editor

The previous program examples have all been written while at the Tiny Machine Basic prompt, and have been entered directly into the Tiny Machine Basic program storage area. Although this works well for short programs, there is a much better way to write programs that allows you to save the programs on your PC. Since Tiny Machine Basic programs are text only you can use a "text editor" program to create, edit, and save your programs. There are a number of text editors available including some that are included with the PC operating system, like Windows Notepad. (If you use a "word processing" program to write programs, they must be saved as "plain text" and not in the word processing document format.)

The communication software included with Industrologic microcontroller products includes either a text editor (or a way to access a text editor) just for this purpose, along with an easy way to load your program into Tiny Machine Basic just as though you were entering it by typing at the prompt.



Using the NEW and BOOTON instructions in a text editor

Remember that the special instructions should be used only at the command prompt or without line numbers. Here is an example of how to use the NEW and BOOTON instructions in a program written in a text editor:

NEW               erases all program lines from any old program
1 PRINT "X"
2 DELAY 20
5 GOTO 1
BOOTON            runs your program when power is applied to board



end of manual
log file