Industrologic, Inc.
3201 Highgate Lane
St. Charles, MO
63301
USA
Phone: (636) 723-4000
www.industrologic.com
info@industrologic.com
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.
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.
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.
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.)
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.
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.
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