## CHAPTER 1

### Introduction

1.1 INTRODUCTION TO MCS BASIC-52

Welcome to MCS BASIC-52. This program functions as a BASIC interpreter occupying 8K of ROM in INTEL's 8052AH microcontroller. MCS BASIC-52 provides most of the features of "standard" BASICS, plus many additional features that apply to control environments and to the architecture of the 8052AH.

The design goal of MCS BASIC-52 was to develop a software program that would make it easy for a hardware/software designer to interact with the 8052 device; but, at the same time not limit the designer to the slow and sometimes awkward constructs of BASIC. This program is not a "toy" like many of the so called tiny basics. It is a powerful software tool that can significantly reduce the design time of many projects. MCS BASIC-52 is ideal for so called imbedded systems, where terminals are not attached to system, but the system controls and manipulates equipment and data.

MCS BASIC-52 offers many unique hardware and software features, including the ability to store and execute the user program out of an EPROM, the ability to process interrupts within the constructs of a BASIC program, plus an accurate real time clock. In addition, the arithmetic routines and I/O routines contained in MCS BASIC-52 can be accessed with assembly language CALL routines. This feature can be used to eliminate the need for the user to write these sometimes difficult and tedious programs.

All of the above are covered in this document. This is NOT a "How to Write Basic Programs" manual. Many excellent texts on this subject have been produced. Your local computer store can recommend many such texts.

The descriptions of many of the statements in this manual involve rather detailed discussions that relate to interfacing MCS BASIC-52 to assembly language programs. If the user is not interested in using assembly language with MCS BASIC-52 these discussions may be ignored. If you are only interested in programming the MCS BASIC-52 device in BASIC, you can treat all statements the same way they would be in any standard BASIC interpreter.

In reading this manual, you will find that some information may he repeated two or three times. This is not an accident. Years of experience have proven that one of the most frustrating experiences one encounters with manuals is trying to find a particular piece of information that the reader knows is in the manual, but can't remember where.

1.2 GETTING STARTED

If you are like most engineers, technicians, hobbyists and humans, and don't like to read manuals, this section is for you. The purpose of this section is to get you off on the right foot. If you are in the high anxiety mode and just want to see if the darn chip works, wire the device in the minimum hardware configuration as suggested in the hardware configuration chapter of this manual, apply power, and watch what happens. Nothing! That's because after power is applied to the MCS BASIC-52 device, the program initializes the 8052AH hardware and goes into an auto-BAUD search routine. You must touch the space bar on the serial input device in order to get MCS BASIC-52 to sign on. The message that will appear is *MCS-51 BASIC Vx.x*. If a space character is not the first character sent to the MCS BASIC-51 device after reset, you can spend a lot of time trying to figure out what went wrong. So do yourself a favor, read this section and touch the space bar before you call your local Intel field applications engineer. We received a number of questions asking how the auto-BAUD search routine worked. As a result this routine is listed in chapter 11 of this manual.

1.3 WHAT HAPPENS AFTER RESET?

After reset, MCS BASIC-52:

1) Clears the INTERNAL 8052AH memory

2) Initializes the internal registers and pointers

3) Tests, clears, and sizes the EXTERNAL memory

BASIC then assigns the top of external RAM to the system control value MTOP and uses this number as the random number seed. BASIC assigns the default crystal value, 11.0592 Mhz, to the system control value - XTAL and uses this default value to calculate all time dependent functions, such as the EPROM programming timer and the interrupt driven real time clock. Finally. BASIC checks external memory location 8000H to see if the baud rate information is stored. If the baud rate is stored, MCS BASIC-52 initializes the baud rate generator (the 8052AH's special function register - T2CON) with this information and signs on. If it isn't stored, BASIC interrogates the serial port input and waits for a space character to be typed. This sounds like a lot, but on the 8052AH, it doesn't take much time.

MCS BASIC-52 initializes the 8052AH's Special Function Registers, TMOD, TCON, and T2CON with the following values:

TCON - 244 (0F4H)

TMOD - 16 (10H)

T2CON - 52 (34H)

After Reset the console device should display the followlng:

    *MCS-51(tm) BASIC Vx.x*
    READY
    >

To see if everything is OK after Reset, type the following:

    >PRINT XTAL, TMOD, TCON, T2CON

    (BASIC should respond)
    11059200 16 244 52

If it does, everything is working properly. If it does not make sure that the external memory, the serial port, and the oscillator are connected and working. Hardware debug begins here.

In the appendix of this manual is a quick reference guide. It provides a short description of all of the commands and statements implemented in MCS BASIC-52 You might want to use this section to gain a quick understanding of the MCS BASIC-52 software package. Those of you who are familiar with the BASIC language will notice that most of the statements and commands used in MCS BASIC-52 are standard, so getting started should not be a problem.

1.4 DEFINITION OF TERMS:

COMMANDS:

MCS BASIC-52 operates in two modes, the "command" or direct mode and the interpreter or "run" mode. MCS BASIC-52 commands can only be entered when the processor is in the command or direct mode. MCS BASIC-52 takes immediate action after a command has been entered. This document will use the terms "run" mode and "command" mode to refer to the two different modes of operation.

STATEMENTS:

A BASIC program is comprised of statements. Every statement begins with a line number, followed by the statement body, and terminated with a Carriage Return (cr), or a colon (:) in the case of multiple statements per line. Some statements can be executed in the command mode, others cannot. The description of statements section of this manual describes whether a statement can be executed in the command mode or only in the run mode.

There are three general types of statements in MCS BASIC-52: assignments, input/output, and control. The description of statements section of this manual explains what type is associated with each statement.

Every line in a program must have a statement line number ranging between 0 and 65535 inclusive.

Statement numbers are used by BASIC to order the program statements sequentially.

In any program, a statement number can be used only once.

Statements need not be entered in numerical order, because BASIC will automatically order them in ascending order.

A statement may contain no more than 72 characters in Version 1.0 and no more than 79 in Version 1.1.

Blanks (spaces) are ignored by BASIC and BASIC automatically inserts blanks during LIST.

More than one statement can be put on a line, if separated by a colon (:), but only one statement number is allowed per line.

FORMAT STATEMENTS:

Format Statements may only be used within the PRINT statement. The format statements include TAB([expr]), SPC([exprl), USlNG(special symbols), and CR (carriage return with no line feed). Details of the format statements are provided in the description of the PRINT statement section of this manual.

DATA FORMAT:

The range of numbers that can be represented in MCS BASIC-52 is:

+- 1E-127 to +-.99999999E+127.

There are eight digits of significance in MCS BASIC-52. Numbers are internally rounded to fit this precision. Numbers may be entered and displayed in four formats: integer, decimal, hexadecimal, and exponential. Example: 129, 34.98, 0A6EH, 1.23456E + 3

INTEGERS:

In MCS BASIC-52, integers are numbers that ranges from 0 to 65535 or 0FFFFH. All integers can be entered in either decimal or hexadecimal format and all hexadecimal numbers must begin with a valid digit (e.g. the number A000H must be entered 0A000H). When an operator, such as .AND. requires an integer, MCS BASIC-52 will truncate the fraction portion of number so it will fit the integer format. All line numbers used by MCS BASIC-52 are integers. This document will refer to integers and line numbers, respectively in the following manner:

[integer] - [ln num]

NOTE - Throughout this document the brackets [] are used only to indicate an integer, constant, etc. They are NOT entered when typing the actual number or variable.

CONSTANTS:

A constant is a real number that ranges from +-1E-127 to + .99999999E+127. A constant, of course, can be an integer. This document will refer to constants in the following manner:

[const]

OPERATORS:

An operator performs a pre-defined operation on variables and/or constants. Operators require either one or two operands. Typical two operand or dyadic operators include ADD (+), SUBTRACT (-), MULTIPLY (*), and DIVIDE (/). Operators that require only one operand are often referred to as unary operators. Some typical unary operators are SIN, COS, and ABS.

VARIABLES:

In Version 1.0 of MCS BASIC-52 a variable could be defined as either a letter, (i.e. A, X, I), a letter followed by a number, (i.e. Q1, T7, L3), a letter followed by a one dimensioned expression, (i.e. J(4), G(A+6), I(10*SIN(X))), or a letter followed by a number followed by a one dimensioned expression (i.e. A1(8), P7(DBY(9)), W8(A+B). In Version 1.1 variables can be defined in the same manner as in Version 1.0, however variables may also contain up to 8 letters or numbers including the underline character. This permits the user to use a more descriptive name for a given variable. Examples of valid variables in Version 1.1 of MCS BASIC-52 are as follows:

FRED
VOLTAGE1
I_11
ARRAY(ELE_1)

When using the expanded variable names available in Version 1.1 of MCS BASIC-52 it is important to note that 1) It takes longer for MCS BASIC-52 to process these expanded variable names and 2) The user may not use any keyword as part of a variable name (i.e. the variables TABLE and DIET could not be used because TAB and IE are reserved words). BAD SYNTAX errors will be generated if the user attempts to define a variable that contains a reserved word.

Variables can be up to 8 characters in length, however, only the first character, the last character, and the total number of characters are of significance. This lets the user better describe variables that are used in a program.

Variables that include a one dimensioned expression [expr] are often referred to as dimensioned or arrayed variables. Variables that only involve a letter or a letter and a number are called scalar variables. The details concerning dimensioned variables are covered in the description of the statement DIM. This document will refer to variables as [var].

MCS BASIC-5 allocates variables in a "static" manner. That means each time a variable is used, BASIC allocates a portion of memory (8 bytes) specifically for that variable. This memory cannot be de-allocated on a variable by variable basis. That means if you execute a statement like Q = 3, later on you cannot tell BASIC that the variable Q no longer exists so, please "free up" the 8 bytes of memory that belong to Q. Sorry. it doesn't work this way. The only way the user can clear the memory that is allocated to variables is to execute a CLEAR statement. This Statement "frees" all memory allocated to variables.

IMPORTANT NOTE:

Relative to a dimensioned variable, it takes MCS BASIC-52 a lot less time to find a scalar variable. That's because there is no expression to evaluate in a scalar variable. So, if you want to make a program run as fast as possible, use dimensioned variables only when you have to. Use scalars for intermediate variables, then assign the final result to a dimensioned variable.

EXPRESSIONS:

An expression is a logical mathematical formula that involves operators (both unary and dyadic), constants, and variables. Expressions can be simple or quite complex, i.e. 12*EXP(A)/100, H(I)+55, or (SIN(A)*SIN(A)+COS(A)*COS(A))/2. A "stand alone" variable [var] or constant [const] is also considered an "expression". This document will refer to expressions as [expr].

RELATIONAL EXPRESSIONS:

Relational expressions involve the operators equal (=), not equal (<>), greater than (>), less than (<), greater than or equal to (>=) and less than or equal to (<=). They are used in control statements to "test" a condition (i.e. IF A < 100 THEN . . .). Relational expressions always require two operands. This document will refer to relational expressions as [rel expr].

SPECIAL FUNCTION OPERATORS:

Virtually all of the special function registers on the 8052AH can be accessed by using the special function operators. The exceptions are PORTS 0, 2 and 3 and non-I/O associated registers such as ACC, B, and PSW. Other special function operators are XTAL and TIME. Details of the special function operators are covered in the section special function operators.

SYSTEM CONTROL VALUES:

The system control values include the following: LEN (which returns the length of the program), FREE (which designates how many bytes of RAM are not used that are allocated to BASIC), and MTOP (which is the last memory location that is assigned to BASIC). Details of the system control values are covered in the section system control values.

STACK STRUCTURE:

MCS BASIC-52 reserves the first 512 bytes of external data memory to implement two "software" stacks. These are the control stack and the arithmetic stack or argument stack. Understanding how the stacks work in MCS BASIC-52 is not necessary if the user wishes only to program in BASIC. However, understanding the stack structure is necessary if the user wishes to link MCS BASIC-52 to assembly language routines. The details of how to link to assembly language are covered in the assembly language linkage section of this manual.

CONTROL STACK : The control stack occupies locations 96 (60H) through 254 (0FEH) in external ram memory. This memory is used to store all information associated with loop control (i.e. DO-WHILE, DO-UNTIL, and FOR-NEXT) and basic subroutines (GOSUB). The stack is initialized to 254 (0FEH) and "grows down."

ARGUMENT STACK : The argument stack occupies locations 301 (12DH) through 510 (1FEH) in external ram memory. This stack stores all constants that MCS BASIC-52 is currently using. Operations such as ADD, SUBTRACT, MULTIPLY, and DIVIDE always operate on the first two numbers on the argument stack and return the result to the argument stack. The argument stack is initialized to 510 (1FEH) and "grows down" as more values are placed on the argument stack. Each floating point number placed on the argument stack requires 6 BYTES of storage.

INTERNAL STACK : The stack pointer on the 8052AH (special function register - SP) is initialized to 77 (4DH). The 8052AH's stack pointer "grows up" as values are placed on the stack. In MCS BASIC-52 the user has the option of placing the 8052AH's stack pointer anywhere (above location 77) in internal memory. The details of how to do this are covered in the assembly language linkage section of this manual.

LINE EDITOR:

MCS BASIC-52 contains a minimum level line editor. Once a line is entered the user may not change the line without re-typing the line. However, it is possible to delete characters while a line is in the process of being entered. This is done by entering a "rubout" or "delete" character (7FH). The rubout character will cause the last character entered to be erased from the text input buffer. Additionally, a control-D will cause the entire line to be erased. In Version 1.1 of MCS BASIC-52, Control-Q (X-ON) and Control S (X-OFF) recognition have been added to the serial port. The user is cautioned not to accidently type a Control-S when entering information because the MCS BASIC-52 will no longer respond to the console device. Control-Q is used to bring the console device back to life after Control-S is typed.

NOTE - In this document a carriage return is indicated by the symbol (cr). The carriage return is the RETURN key on most keyboards.

1.5 WHAT'S THE DIFFERENCE BETWEEN V1.0 AND V1.1

Thanks to feedback from many of the users of MCS BASIC-52, a number of changes and additions have been made to Version 1.1. All of these changes and additions were made to enhance the usefulness of the product and yet retain 100% compatibility, well almost 100% compatibility with the original version. To make things simple, all of the changes will be mentioned here and a reference will be provided as to where the reader of this manual may obtain more information about the change or addition.

The only change that has been made to V1.1 that is not compatible with V1.0 is with the IF-THEN- ELSE statement when used with multiple statements per line. In V1.0, the following two examples would function in the same manner.

EXAMPLE 1:

    10 IF A=B THEN C=A : A=A/2 : GOTO 100
    20 PRINT A

EXAMPLE 2:

    10 IF A=B THEN C=A
    12 A=A/2
    14 GOTO 100
    20 PRINT A

They function in the same manner because V1.0 treats the delimiter (:) exactly the same as a carriage return (cr) in every case. However, V1.1 executes the remainder of the line if and only if the test A = B proves to be true. This means in example1, IF A did equal B, V1.1 would then set C=A, then set A = A/2, then execute line 100. IF A did not equal B, V1.1 would then PRINT A and ignore the statements C=A: A=A/2: GOTO 100. V1.1 will execute example2 exactly the same way as V1.0. This same logical interpretation holds true for the ELSE statement as well. This example dictates a simple rule for maintaining IF-THEN-ELSE compatability between the two versions. If the delimiter (:) is not used in an IF-THEN-ELSE statement, V1.0 and V1.1 will treat the statements in the same manner!!

This change was made because most users of MCS BASIC-52 felt that the V1.1 interpretation of this statement was more useful because fewer GOTO statements need be employed in a typical program.

Additionally, V1.1 accepts inputs in either lower or upper case, whereas V1.0 converted lower case to upper case. V1.1 will however, convert keywords from lower case to upper case during the LlSTing of a program. Finally, MCS BASIC-52 V1.1 runs between 2% and 10% faster than V1.0. Typically, this should not cause any problems.

As far as the user is concerned, these are the only changes that may affect the operation of a typical program. Now, on to the additions.

ADDITIONS TO MCS BASIC-52 V1.1:

X-ON (control Q) and X-OFF (control S) have been added. These permit the user to stop (control S) and start (control Q) the display of characters during a LIST or PRINT. This feature also permits synchronization with external I/O (input/output) devices. The X-OFF (control S) functions on a line by line basis, not on a character by character basis.

Five new statements have been added. These include IDLE, LD@, ST@, PGM, and RROM. Details of these statements are listed under the description of statements section of this manual.

Six new reset options have been provided. They permit the user to assign the top of memory (MTOP) during reset, and allow the user to write specific reset programs in assembly language. Additionally, they provide an option where the memory will not be cleared during reset. More information on the specific reset options is detailed in the description of EPROM file commands under PROG1, PROG2, PROG3, PROG4, PROG5, and PROG6 commands, and in Chapter 11 of this manual.

The Timing of the EPROM programming algorithm has been significantly relaxed between the various strobes required for the EPROM programming function. This relaxed timing permits the user to program devices such as the 8751H and the 8748/9 using the EPROM programming capabilities of the MCS BASIC-52 device. Details of the timing changes are in Chapter 10 of this manual.

During EPROM programming, the INT0/DMA request pin of the MCS BASIC-52 device is treated as a ready input pin. This allows for a simple direct connection to EEPROM devices such as the 2817A. For normal EPROM programming, INT0 must be kept high or the programming hangs up. Details concerning the use of EEPROMs with the MCS BASIC-52 device are provided in Chapter 10 of this manual.

A "run-trap" option has been provided. This option traps the MCS BASIC-52 interpreter in the program run mode and will not permit the user to exit this mode. Details of this option are covered in Chapter 8.4 of this manual.

A user statement/command expansion option has been provided. This permits the user to easily add new or custom statements and commands to MCS BASIC-52. Details of this option are covered in Chapter 12 of this manual.

A number of new assembly language user "op-bytes" have been added. These permit the user to make better use of the statement/command expansion option previously described. Details of these new "op-bytes" are presented in Chapter 9.6 of this manual.

The length of the input buffer has been increased from 72 characters to 79 characters and the ERROR: LINE TOO LONG has been eliminated. Instead, when the cursor reaches the 79th position a bell character will be echoed everytime the user attempts to enter another character.

A new variation on the PRINT (including PH0. and PH1.) and LIST statements have been added. This new option is evoked with an @ character (example: PRINT@ or LIST@ and permits the user to write specific output drivers for these statements and commands. When the PRINT@ or LIST@ is evoked, MCS BASIC-52 calls external code memory location 403CH. The user must put the specific output driver in this location. More details of this option is in Description of Statements section of this manual.

The control stack has been made more "forgiving." This means that the user can execute a GOSUB to a subroutine that contains a FOR-NEXT loop and return from the subroutine without completing the FOR-NEXT loop. Version 1.0 would yield a C-STACK error under these circumstances, V 1.1 yields no error.

The question mark character ? is interpreted as a PRINT statement (example: PRINT 10+20 is the same as ? 10+20). The symbols P. remains a shorthand notation for PRINT just as in V1.0.

The FOR-NEXT statement can be executed in the direct mode. This lets the user write short routines in the direct mode to, for example, display a region of memory (example: FOR I = 200H TO 210H: PH0. XBY(I): NEXT I)

Variables can be up to 8 characters in length, however, only the first character, the last character, and the total number of characters are of significance. This lets the user better describe variables that are used in a program. Chapter 1.4 details the limitations on the expanded variables in Version 1.1.

The CALL statement vectors to locations 4100H through 41FFH if the CALL integer is between 0 and 7FH inclusive. This means that CALL 0 will vector to location 4100H, CALL 1 to location 4102H, CALL 2 to location 4104H, etc. This permits the user to easily generate assembly language CALL tables by using simple integers with the CALL statement. Anyway, CALL 0 through CALL 1FFFH was not too useful because these numbers vectored into the MCS BASIC-52 ROM.

The error message anomaly for an invalid line number on a GOTO or GOSUB statement has been eliminated on V1.1 of MCS BASIC-52. The correct line number is now processed and displayed by the error processor.

The FOR-TO-{STEP}-NEXT statement can be executed in the command mode in version 1.1 of MCS BASIC-52. Additionally, the NEXT statement does not require a variable in version 1.1. Details of these features are covered in the Description of Statements section of this manual.

The REM statement can be executed in the command mode. If the user is employing some type of upload/download routine with a computer, this lets the user insert REM statements, without line numbers in the text and not download them to the MCS BASIC-52 device. This helps to conserve memory.

Version 1.1 is also a little less "crashable" than version 1.0. This is due to a more extensive "type checking" on control transfer routines (i.e. GOTO, GOSUB).