Bascom and AVR, Arrays and DATA


Arrays
Arrays are nothing more than a group of identical variables. They are grouped such that access to a specific variable goes through an index. For example, consider a number of houses in a street:



We could make an array of integers where the number of the house is stored. Such an array is dimensioned as:
Dim Housenr(10) as Integer
Now what if we also made an array where the name of the houseowners are stored:



Such an array is dimensioned as:
Dim Owner(10) as String*4
Using the same index, we could access the housenumber as well as the name of the owner:
Dim Index as Integer
For Index = 1 to 10
  Print "Housenumber: " ; Housenr(Index)
  Print "Owner:       " ; Owner(Index)
Next Index
Now, the dimension of the Owner array might seem a bit 'stringy', but consider that most controllers have limited random access memory. The AT90S2313 has only 128 bytes! If you dimension too much, Bascom will report: "...out of SRAM space..."

Also note that in Bascom, array indexes >always< start at 1! This is different from almost all other programming languages.

The type of the index variable can be anything but bit, single or double.


DATA
The Data statement is used to store series of constants in program memory. They do not take up precious room in RAM, where variables are stored. Those constants can be anything. Once they are needed, they are read in a variable using the Restore or Read statement. Those variables should of course fir the type of constant used. A Data statement must be precede with a lable naming the Data block.
Let us suppose we have four byte constants for controlling an IC:
IC1codes:
DATA &H09, &H77, &H14, &H23
These constants can be read from the Data block:
Dim IC1code(4) as Byte
Dim Codeindex as Byte
Restore IC1codes
For Codeindex = 1 to 4
  Read IC1code(Codeindex)
Next Codeindex
We could have different sets of IC1codes in different Data blocks with different labels and read them in an array as soon as they are needed, thus sparing precious RAM space.
Note:
- In a Data block all integer constants have to end with %
- All single constant with !
- All Long constants with &
(Apparently byte constants, as used in the example above do not need a specific last character)
- Data lines come after the last End line in the program

Data blocks of constants can be treated (sort of) as arrays using the Lookup statement. Lookup searches the list of Data constants for a specific constant at a given position in the list:
Dim ICode as Byte
Dim Codeindex as Byte
...
Codeindex = 3
ICode = Lookup(Codeindex, IC1codes)
Here, in the IC1codes Data block the third code (&H14) is searched and the constant found is assigned to the ICode variable.
Lookupstr does the same for strings:
Dim Owner as String*4
Dim Ownerindex as Integer
...
Ownerindex = 2
Owner = Lookupstr(Ownerindex, Owners)
...
...
Owners:
Data "Witn", "Jero", "Wiel", ...
will produce the second string ("Jero") from the list.

The opposite from Lookup/Lookupstr is Lookdown. You can specify a specific constant and find the position of that constant in the Data block:
Dim Calval as Byte
Dim Calindex as Byte
Calval = 102
Calindex = Lookdown(Calval, ADCcalibration, 10)
...
...
ADCcalibration:
Data 17, 24, 47, 75, 102, 201, 435...
will produce Calindex 5. Lookdown must know the maximum (10 in this example) of constants in the Data block to search.
(I could get Lookdown to work only for byte type constants!)

TOC