Basic Course for 8051 Microcontrollers
..............................................................................................................

Standard Basic language elements
Default extension for basic source files is BAS. The compiler output is assembler source file (with ASM extension) that can be translated to binary code using integrated assembler. Smart editor marks all reserved keywords in different color, that simplifies debugging process. BASIC compiler's assembler output has all necessary comment lines, that makes it very useful for educational purposes, also.

Data Types
Four data types are supported:
- Bit (1-bit, 0 or 1)
- Byte (1-byte integers in the range 0 to 255)
- Word (2-byte integers in the range 0 to 65,535)
- Long (4-byte integers in the range 0 to 4,294,967,295) - optional module

Declarations
Declarations may be placed anywhere in the program. All variables are considered global. The total number of variables is limited by the available microcontroller SRAM memory. Variables are declared using DIM statement:
DIM A AS BIT
DIM B AS BYTE
DIM C AS WORD
DIM D AS LONG

It is also possible to use one-dimensional arrays. For example:
DIM A(10) AS BYTE
declares an array of 10 Byte variables with array index in the range [0-9].



Constants
Constants can be used in decimal number system with no special marks, in hexadecimal number system with leading 0x or leading $ notation (or with H at the end) and in binary system with leading % mark (or with B at the end). Keywords True and False are also available for Bit type constants. For example:
DIM A AS BIT
DIM B AS BYTE
A = TRUE
B = 0x55

You can specify a hexadecimal or binary number with the prefix &H or &B.
a = &HA  ,  a = &B1010   and   a = 10  are all the same.

Constants can also be assigned to symbolic names using CONST directive:
DIM A AS WORD
CONST PI = 314
A = PI

Operators
This chapter describes how expressions are formed and concludes by describing the following kind of operators:

Five arithmetic operations (+, -, *, /, MOD) are available for Byte, Word and Long data types. The compiler is able to compile all possible complex arithmetic expressions. For example:
DIM A AS WORD
DIM B AS WORD
DIM C AS WORD
A = 123
B = A * 234
C = 2
C = (B * C - 12345) / (A + C)




For Bit data type variables seven logical operations are available. It is possible to make only one logical operation in one single statement. Logical operations are also available for Byte and Word variables. For example:
DIM A AS BIT
DIM B AS BIT
DIM C AS BIT
C = NOT A
C = A AND B
C = A OR B
C = A XOR B
C = A NAND B
C = A NOR B
C = A NXOR B


DIM A AS WORD
DIM B AS WORD
A = A OR B
P0 = P0 AND &H2A

The clock frequency of the target device can be specified by setting the CRYSTAL parameter with the DEFINE directive (the value is expressed in Hz). This parameter should be setup at the beginning of the basic program. For example:
$crystal = 12000000

The GOTO statement uses line label name as argument. Line labels must be followed by colon mark ":". Here is one example:
DIM A AS WORD
A = 0
loop: A = A + 1
GOTO loop

WAITMS and WAIT statements can be used to force program to wait for the specified number of milliseconds or seconds. It is also possible to use variable argument of Byte or Word data type. These routines use Clock Frequency parameter that can be changed from the Options menu. WAITUS routine has minimal delay and step that also depend on the Clock Frequency parameter.
DIM A AS WORD
A = 100
WAITMS A
WAIT 50