Built-in Hardware Edit
The BasicATOM Pro has hardware functions that are independent of the main microcontroller. Examples include:
- Analog to digital converters
- Pulse width modulators
- Capture/Compare modules
Built in hardware runs independently of the microcontroller, and can be set up via your program then allowed to run independently.
RAM is Random Access Memory, which is "volatile" (i.e. the contents are lost if power is removed). The Atom Pro has 2 kbytes of RAM, the Atom Pro Plus has 4 kbytes. RAM is used to store:
- Values used by the system (registers) - 0 to 256 bytes
- Program stack, counters, etc. - approx. 80 to 120 bytes.
Remaining RAM is available for user data.
The microcontroller on which the Atom Pro is based, uses a number of registers to control its internal operation, set status, etc. These registers occupy memory addresses in RAM. Registers are accessible by using their numeric addresses (see the H8/3664F or H8/3687F hardware manuals for these addresses) or by using the register name.
EEPROM is Electrically Erasable Programmable Read Only Memory. It is similar to RAM in that numbers can be stored in EEPROM and retrieved from EEPROM, but it is slower than RAM (particularly for writing values) and has a limited number of write-cycles (about 10 million) before it fails.
- The Atom Pro has no built-in EEPROM. An external I2C EEPROM may be connected, see Notes on page 172.
- The Atom Pro 28 and Atom Pro Plus 40 have 4 kbytes of EEPROM.
EEPROM is normally used for storing constants that don't change frequently. It is accessed by means of the DATA, READ, READDM, WRITE and WRITEDM commands (see page 170).
Program Memory (Flash) Edit
Programs are stored in "Flash EEPROM" which can be re-written many times. Flash is non-volatile, so your programs will be saved during power off periods. More complex programs require more memory. Program memory can also be used to store constants (see Tables on page 45). The ATOM Pro has 32 kbytes of program memory, the ATOM Pro Plus has 56 kbytes. Flash provides fewer rewrite cycles before failure than EEPROM.
Number Types Edit
Atom BASIC lets you store numbers with varying precision to minimize memory space requirements. The following number types are used:
|Bit||1||1 or 0|
|Nib||4||0 to 15|
|Byte||8||0 to 255|
|Sbyte||8||-127 to +128|
|Word||16||0 to 65535|
|Sword||16||-32767 to +32768|
|Long||32||0 to 4294967295|
|Slong||32||-2147483647 to +2147483648|
|Float||32||± 2 -126 to ± 2 127|
Use the smallest number type that gives the range and precision you need. This will minimize the use of RAM.
Variables are used to store temporary values while your program runs. Like most compiled BASICs, with Atom BASIC you must define your variables before using them. Variables are always stored in user RAM.
Variables are defined with the VAR keyword. They may be defined as any of the number types described in Table 1. Remember to define variables as SBYTE, SWORD, SLONG or FLOAT if they may have negative values.
- variable name VAR number type
- red var byte
- tick var nib
- switch var bit
- totals var long
- fraction var float
Note that the compiler will automatically "pack" nib and bit variables to use as few bytes as possible.
Variable names must start with a letter, but they can contain letters, numbers, and special characters. They are case-independent so RED and red are the same variable. Variable names may be up to 1024 characters long. We recommend that they be made long enough to be meaningful, but short enough for easy reference. The length of a variable name does not affect the length of your compiled program. You may not define the same variable twice within a program.
The following may not be used for variable names:
- Atom BASIC reserved words (see page 191)
- label names used within your program
Array variables (strings) Edit
Arrays are special variables used in BASIC to hold a number of related values. Atom BASIC provides for linear arrays (also known as one-dimensional arrays or "subscripted" variables).
Syntax to Define an Array
- variable name VAR number type(number of cells)
Suppose you want to store values for 5 temperature sensors in an array. Each sensor requires a WORD value. Define the array as:
- temp var word(5)
This sets aside 5 "words" in RAM for your array, numbered 0 to 4.
You can access each value as if it was a separate variable, for example you can access the third value as:
The number "2" in this case is called the "index". The third value has an index of 2 because the array is numbered 0, 1, 2, 3, 4.
When accessing array values, the index can be a variable:
- cntr var byte
- temp var word(5)
- for cntr=0 to 4
- temp(cntr)= cntr+15
This will assign the values of 15, 16, 17, 18 and 19 to the array cells, so that temp(3) would have a value of 18, etc.
Using Array Variables to Hold Strings Edit
A common use of array variables is to hold "strings" of ASCII characters. When used for this purpose, arrays should be defined as byte variables.
The following program excerpt will illustrate this use of array variables.
- var byte mystring(20)
- mystring = "This is a test"
This will assign "T" to mystring(0), "h" to mystring(1), etc.
An alias gives you a means of assigning more than one name to the same variable. You may need temporary variables at different points in your program, but not want to use too much RAM to store all of them.
- new variable VAR existing variable
- sensor VAR byte
- eye VAR sensor
This will create a variable "eye" that points to the same RAM location as the variable "sensor". In your program you might have a loop:
- for sensor = 1 to 10
- (some code or other)
Then later in your program you could have a loop:
- for eye = 2 to 8
- (some code or other)
Sensor and eye would use the same RAM location, thus saving 1 byte of RAM.
Variable Modifiers Edit
Variable modifiers are used to access parts of a variable. For example, you could access the high or low nibble of a "byte" variable. Modifiers can be used both when variables are defined, and "on the fly" during program execution.
A complete list of modifiers appears on page 41.
Example 1 shows an alias that accesses only part of a variable. The modifier is used to define a variable.
- sensor VAR byte
- eye VAR sensor.highnib
Sensor is a byte (8 bit) variable. Eye is defined as the high nibble (most significant 4 bits) of Sensor. Changing the value of Eye will change only the top 4 bits of Sensor.
Example 2 Example 2 shows the use of modifiers during execution of a program. First, the variables are defined:
- maxval VAR word
- topval VAR byte
Then, at a later point in the program, the statement:
- topval = maxval.highbyte
Assigns the value contained in the high 8 bits of maxval to topval. (We could also have used maxval.byte1 to get the same value in this example.)
Variable modifiers can be used in conditional statements:
- if maxval.bit0 = 0 then even
Where "even" is a label. Note that if bit0 = 0 the number must be an even number, so this test can be used to determine if a number is even or odd.
List of Modifiers
The following modifiers are allowed. Note that in some cases different modifiers will give the same result (for example, in a "word" variable, "highbyte" and "byte1" are the same).
|lowbit||returns the low bit (least significant bit) of a variable|
|highbit||returns the high bit (most significant bit) of a variable|
|bitn|| returns the "nth" bit of a variable. n can have a value of:
|lownib||returns the low nibble (4 bits) of a variable|
|highnib||returns the high nibble (4 bits) of a variable|
|nibn|| returns the "nth" nibble of a variable. n can have a value of:
|lowbyte||returns the low byte of a variable|
|highbyte||returns the high byte of a variable|
|byten|| returns the "nth" byte of a variable. n can have a value of:
|lowword||returns the low word (16 bits) of a variable|
|highword||returns the high word of a variable|
|wordn||returns the "nth" word of a long variable. n can have a value of 0 or 1 (which are equivalent to lowword and highword respectively)|
Pin Variables (Ports) Edit
Pin variables are special bit-mapped variables used to set the direction and set or read the state of any I/O pin. Pin Variables are also known as Ports.
At program start all I/O pins are set as inputs. Directions can be set using the Input and Output commands, or using a Pin variable from the following list:
|DIRE||32||P0 - P31|
|DIRS||16||P0 - P15|
|DIRES||16||P16 - P31|
|DIRL||8||P0 - P7|
|DIRH||8||P8 - P15|
|DIREL||8||P16 - P23|
|DIREH||8||P24 - P31|
|DIRA||4||P0 - P3|
|DIRB||4||P4 - P7|
|DIRC||4||P8 - P11|
|DIRD||4||P12 - P15|
|DIREA||4||P16 - P19|
|DIREB||4||P20 - P23|
|DIREC||4||P24 - P27|
|DIRED||4||P28 - P31|
|(Where # is a number from 0 - 31.)|
Each direction bit must be set to 0 for input and 1 for output.
The state of an I/O pin can be read or set using the IN and OUT pin variables.
The table below shows all IN and OUT Pin variables:
|INE or OUTE||32||P0 - P31|
|INS or OUTS||16||P0 - P15|
|INES or OUTES||16||P16|
|INL or OUTL||8||P0 - P7|
|INH or OUTH||8||P8 - P15|
|INEL or OUTEL||8||P16 - P23|
|INEH or OUTEH||8||P24 - P31|
|INA or OUTA||4||P0 - P3|
|INB or OUTB||4||P4 - P7|
|INC or OUTC||4||P8 - P11|
|IND or OUTD||4||P12 - P15|
|INEA or OUTEA||4||P16 - P19|
|INEB or OUTEB||4||P20 - P23|
|INEC or OUTEC||4||P24 - P27|
|INED or OUTED||4||P28 - P31|
|IN# or OUT#||1||P#|
|(Where # is a number from 0 - 31.)|
The state bit is 0 for low and 1 for high.
As an example, let's say we want to set P0 to P3 as inputs and P4 to P7 as outputs. Here are two different ways to accomplish the same thing.
Using 4 bit variables:
- dira = 15 ; 15 decimal = 1111 binary
- dirb = 0 ; 0 decimal = 0000 binary
Using an 8 bit variable:
- dirl = 15 ; 15 decimal = 00001111 binary
Now we want to set and read the I/O pins we've set up. Let's say we expect the following, where 0 = low and 1 = high:
Using two 4 bit variables:
- outa = 11 ; 11 decimal = 1011 binary
- status = inb ; status will equal 6 decimal
- or 0110 binary
Constants are similar to variables except that the values are set at compile time and can't be changed by the program while it's running. Think of a constant as a convenient way to give a name to a numeric value so you don't have to remember what it is each time it's used.
Defining Constants Edit
Constants are defined with the CON keyword. The numeric type is automatically set by the compiler based on the value you enter.
- constant name CON value of constant
- temperature_adjust CON 24
- kilo CON 1000
- true CON 1
- false CON 0
- endpoint CON -3442567
Constant Names Edit
Names of constants follow the same rules as names of variables, see page 45.
Tables are to constants as Arrays are to variables. A table can be used to store a number of related constants which are then referred to using the index number.
The table contents are written to program memory while the Atom Pro is being programmed, therefore defined constants and variables can NOT be used to populate tables. However, the table is accessed from within a running program, so variables can be used to "index" into a table.
- tablename TableType data, data, data...
Table names follow the same rules as variable and constant names.
TableType can be any of the following:
- ByteTable (8 bit data)
- WordTable (16 bit data)
- LongTable (32 bit data)
- FloatTable (floating point data)
Byte Tables can also be used to store "string" values.
data is a constant, or a series of constants or constant expressions. Variables and named constants can't be used as data because the table is populated before your program starts running. However, expressions such as 3 * 10 are legal.
- adjust WordTable 100,350,5678,73,9,8133*3,0
This creates a constant array with values as shown. Cell numbering starts with 0, so the value of "adjust(3)" would be 73. As with arrays, when referring to the table the index can be a variable.
- letter ByteTable "This is a test",0
This creates a constant array containing a string value, terminated with a zero. In this example the value of "letter(0)" would be "T", the value of "letter(3)" would be "s", etc.
Terminating the table with 0 allows you to test for the end of the table, without knowing how many characters it contains.
Pin Constants Edit
Pin constants are pre-defined for easy reference to I/O pins.
- P0 = 0
- P1 = 1
- P31 = 31
I/O pin 8 could be set to Output state and to High (5V) using either:
- high 8
- high P8
Another example of the use of Pin constants can be found in the Traffic Light program on page 23.
Each Atom Pro module also has two special serial I/O pins used for programming and serial data communications. They can be referred to in your program as:
S_IN and S_OUT
See the appropriate data sheet to identify these pins.