compiler will insert relevant text in its place.
Statements and Commands There are three main types of statements:
• Comparison and conditional statements
• Repetition and looping statements
• Library Commands
Comparison and conditional statements allow us to compare two or more variables, ports, port pins or special function registers and then make a decision to execute one set of instructions or other set.
Considering the importance of these statements, BASIC language provides many different constructs of this. We shall explore these below.
Repetition and looping is one of the greatest advantage of microprocessors. We can instruct the microcontroller to continuously repeat certain instructions, either endlessly, or till a certain condition exists.
For example to keep an LED on, till the temperature is high from a set point. Again since they are important structures a number of methods exists to control loops.
Library commands, are not truly speaking commands of BASIC language, but are provided by the manufacturer of compiler to do the common tasks. For example a library command to display some data on LCD or to read analog data from an input pin. The more extensive is the library, more powers you have and shorter is the programming time.
With this review, lets talk about individual topics one by one, in context of Proton Basic.
Labels
In order to mark statements that the program may wish to reference with the goto, call or gosub commands, PROTON+ uses line labels. Unlike many older BASICs, PROTON+ doesn't allow or require line numbers and doesn't require that each line be labelled. Instead, any line may start with a line label, which is simply an identifier followed by a colon (:).
LAB:
PRINT "Hello World"
GOTO LAB
Label names can be up to 32 characters in length and may contain any alphanumeric character, but they must not begin with a numeric value. For example:
LABEL1:
is perfectly valid, however:- 1LABEL:
will produce a syntax error because the labels starts with the value 1. A label that contains more than 32 characters will produce a syntax error pointing out the offending label. Underscores are also permitted as part of the label's characters. This helps create more meaningful label names. For example:-
THISISALABEL:
does not have the same clarity of meaning as:- THIS_IS_A_LABEL:
Variables
Variables are used to temporarily store data or to hold numbers to be used in calculations. The number of variables which can be used in a program depends upon the RAM of your microcontroller. In Harvard architecture, the RAM part of memory is separated from program memory. Therefore if you have 16K of program memory and 256 bytes of RAM, you can not use the free program memory to store data. Variables are therefore nothing but memory bytes. To facilitate this job, compiler allows you to give these memory locations names, called variable names. It will then automatically compute the correct address of RAM, when a memory variable is used in your program.
Although variables exist in RAM, as a sequence of bytes, yet they can be grouped together to make larger organizations to hold different kinds of data.
Data types are defined as various types of data which can be manipulated by our compiler. Compilers from different manufacturers differ in this facility, however certain standard data types are supported by all.
The variables are declared using a Dim statement, followed by variable name and its data type. Dim statements can appear anywhere however it’s a good programming practice to place all Dim statements after the declares at top of the program.
Dim Dog As Byte ' Create an 8-bit unsigned variable (0 to 255) Dim Cat As Bit ' Create a single bit variable (0 or 1)
Dim Rat As Word ' Create a 16-bit unsigned variable (0 to 65535) Dim Large_Rat As DWord ' Create a 32-bit signed variable (-2147483647 to +2147483647)
Dim Pointy_Rat As Float ' Create a 32-bit floating point variable
Dim ST As STRING * 20 ' Create a STRING variable capable of holding 20 characters
The data types as Bit, Byte, Word, DWord, Float and String define the number of bytes reserved for the variable. This also defines the number range, which can be stored, as well as the nature of stored number.
The stored numbers can be signed or un-signed as well as they may contain a decimal point. A string on the other hand, is a collection of Byte sized variables, to hold character data.
The compiler itself will use some memory to store its internal variables. The amount of RAM used by compiler depends upon the complexity of the program. As there are more and more control structures, and loops, so the compiler will use more and more RAM. Keep this fact in mind that compiler is sharing your RAM.
Variable names follow the same general guide lines as identifiers. However there are certain reserved words which can not be used as variable names. See documentation of your compiler for details of reserved words.
Accessing Part of a Variable
Many a times part of a variable needs to be accessed, either for reading or writing. Most of the times in a Byte sized variable a particular bit needs to be accessed. A Byte consists of 8 bits numbered from 0 to 7. Bit 0 being the least significant and bit 7, the most significant. An individual bit of a variable is accessed by a period followed by bit number in a variable name. Thus if we have x as a byte sized variable, its least significant bit can be accessed by x.0 and most significant bit by x.7.
Dim x As Byte value of x.0 and transfers it into y.0
In case of word sized or DWord sized variables, the same can be done, in addition the high order byte and low order byte, or Byte0, Byte1,Byte2 etc can be separately accessed.
DIM DWD as DWORD ' Declare a 32-bit variable named DWD DIM PART1 as DWD.WORD0 ' Alias PART1 to the low word of DWD DIM PART2 as DWD.WORD1 ' Alias PART2 to the high word of DWD
Symbols
Symbols are in fact a way to simplify things. It assign an alias to a register, variable, or constant value. That alias will then be used in your program, the compiler will replace the alias with actual data before compilation.
Symbol LED = PORTB.0 High LED
In this example a symbol LED has been defined and equated to PORTB.0. thus whenever we use the word LED in our program it would mean PORTB.0 this makes program easier to understand, and make it more logical.
Arrays
Array is a common structure used in programming. The concept is to use multiple variables, with same name, but having an index number to refer them. Since an index number itself can be a variable, it is easier to walk through a huge array of variables, just by changing the index.
To declare a variable as an array, we have to mention its length.
Dim Temp[20] As Byte Dim x As Byte
For x=0 To 19 Temp[x]=0 Next x
In this example a variable named Temp has been declared as an array of 20 variables, each being a byte sized. The index number of these 20 variables will be from 0 to 19. Thus to access first element of array, we will use Temp[0] instead of just Temp. The index number itself can be a variable.
Strings
Strings are a series of alphanumeric data, which not to be used in any mathematical calculation. For example your name, Country, Address are all strings. Strings are nothing but arrays of bytes. However when such arrays are to be used as strings, the last byte of your data should contain a 0.
Dim String1[5] As Byte ' Create a 5 element array
Dim String2[5] As Byte ' Create another 5 element array
Str String1 = "ABCD" , 0 ' Fill the array with ASCII, and NULL terminate it Str String2 = "EFGH" , 0 ' Fill the other array with ASCII, and NULL termi-nate it
Str String1 = Str String2 ' Copy String2 into String1 Print Str String1 ' Display the string
The use of a prefix Str before array name tells the compiler to deal the array as a string.