## CHAPTER 7

### Special Operators

7.1 SPECIAL FUNCTION OPERATORS

Special function operators are called special function operators because they directly manipulate the I/O hardware and the memory addresses on the 8052AH device. All special function operators, with the exception of CBY([expr]) and GET, can be placed on either side of the replacement operator (=) in a LET statement.

EXAMPLES:

    A=DBY(100)
    DBY(100) = A+2

Both of the above are valid statements in MCS BASIC-52. The special function operators in MCS BASIC-52 include the following:

CBY([expr])

The CBY([exprl) operator is used to retrieve data from the program or code memory address space of the 8052AH. Since code memory cannot be written into on the 8052AH, the CBY([expr]) operator cannot be assigned a value. It can only be read.

EXAMPLES:

    A = CBY(1000)

Causes the value in code memory space 1000 to be assigned to the variable A. The argument for the CBY([exprl) operator must be a valid integer (i.e. between 0 and 65535 (0FFFFH) ). If it is not, a BAD ARGUMENT error will occur.

DBY([expr])

The DBY([expr]) operator is used to retrieve or assign a value to the 8052AH's internal data memory. Both the value and argument in the DBY operator must be between 0 and 255 inclusive. This is because there are only 256 internal memory locations in the 8052AH and one byte can only represent a quantity between 0 and 255 inclusive.

EXAMPLES:

    A=DBY(B)
    DBY(250) = CBY(1000)

The first example would assign variable A the value that is in internal memory location B. B would have to be between 0 and 255. The second example would load internal memory location 250 with the same value that is in program memory location 1000.

XBY([expr])

The XBY([expr]) operator is used to retrieve or assign a value to the 8052AH's external data memory. The argument in the XBY([expr]) operator must be a valid integer (i.e. between 0 and 65535 (0FFFFH) ). The value assigned to the XBY([expr]) operator must be between 0 and 255. If it is not a BAD ARGUMENT error will occur.

EXAMPLES:

    XBY(4000H)=DBY(100)
    A=XBY(0F000H)

The first example would load external memory location 4000H with the same value that was in internal memory location 100. The second example would make the variable A equal to the value in external memory location 0F000H.

GET

The GET operator only produces a meaningful result when used in the run mode. It will always return a result of zero in the command mode. What GET does is read the console input device. Actually, it takes a "snapshot" of the console input device. If a character is available from the console device, the value of the character will be assigned to GET. After GET is read in the program, GET will be assigned the value of zero until another character is sent from the console device. The following example will print the decimal representation of any character sent from the console:

EXAMPLE:

    >10 A=GET
    >20 IF A<>0 THEN PRINT A
    >30 GOTO 10
    >RUN

     65 (TYPE "A" ON CONSOLE)
     49 (TYPE "1" ON CONSOLE)
     24 (TYPE "CONTROL-X" ON CONSOLE)
     50 (TYPE '2' ON CON8OLE)

The reason the GET operator can be read only once before it is assigned a value of zero is that this implementation guarantees that the first character entered will always be read, independent of where the GET operator is placed in the program.

The following operators directly manipulate the 8052AH's special function registers. Specific details of the operation of these registers is in the microcontroller users handbook, available from INTEL.

IE - Interrupt Enable

The IE operator is used to retrieve or assign a value to the 8052AH's special function register IE. Since the IE register on the 8052AH is a byte register, the value assigned to IE must be between 0 and 255. The IE register on the 8052AH contains an unused bit, bit IE.6. Since this bit is "undefined," it may be read as a random one or zero, so the user may want to mask this bit when reading the IE register. This can be done with a statement like A = IE .AND. 0BFH. The only statements in MCS BASIC-52 that write to the IE register are the CLOCK0, CLOCK1, ONEX1, CLEAR, and CLEARI statements.

EXAMPLES:

    IE = 81H
    A = IE.AND.0BFH

IP - Interrupt Priority

The IP operator is used to retrieve or assign a value to the 8052AH's special function register IP. Since the IP register on the 8052AH is a byte register, the value assigned to IP must be between 0 and 255. The IP register on the 8052AH contains two unused bits, bit IP.6 and IP.7. Since these bits are "un-defined," they may be read as a random 1 or 0, so the user may want to mask these bits when reading the IP register. This can be done with a statement such as B = IP .AND. 3FH. MCS BASIC-52 does not write to the IP register during initialization, so user can establish whatever interrupt priorities are required in a given application.

EXAMPLES:

    IP = 3
    A = IP.AND.3FH

PORT1

The PORT1 operator is used to retrieve or assign a value to the 8052AH's P1 I/O port. Since P1 on the 8052AH is a byte wide register, the value assigned to P1 must be between 0 and 255 inclusive. Certain bits on P1 have pre-defined functions. If the user does not implement any of the hardware associated with these pre-defined functions, The PORT1 instruction can be used in any manner appropriate in the application.

PCON - Power control

The PCON operator is used to retrieve or assign a value to the 8052AH's PCON register. In the 8052AH, only the most significant bit of the PCON register is used, all other bits are undefined. Setting this bit will double the baud rate if TIMER/COUNTER1 is used as the baud rate generator for the serial port. PCON is a byte register.

RCAP2 - Reload/capture control for timer 2
RCAP2 is disabled in version 1.2 and will return a BAD SYNTAX error.

The RCAP2 operator is used to retrieve and/or assign a value to the 8052AH's special function registers RCAP2H and RCAP2L. This operator treats RCAP2H and RCAP2L as a 16-bit register pair. RCAP2H is the high byte and RCAP2L is the low byte. The RCAP2H and RCAP2L registers are the reload/capture registers for TIMER2. The user must use caution when writing to RCAP2 register because RCAP2 controls the BAUD rate of the serial port on the MCS BASIC-52 device. The following can be used to determine what BAUD rate the MCS BASIC-52 device is operating at:

BAUD = XTAL/(32*(65536-RCAP2) )

T2CON - Timer 2 control
T2CON is disabled in version 1.2 and will return a BAD SYNTAX error.

The T2CON operator is used to retrieve and/or assign a value to the 8052AH's special function register T2CON. The T2CON is a byte register that controls TIMER2's mode of operation and determines which timer (TIMER1 or TIMER2) is used as the 8052AH's baud rate generator. MCS BASIC-52 initializes T2CON with the value 52 (34H) and assumes that its value is never changed. Randomly changing the value of T2CON, without knowing what you are doing can "crash" the serial port on the 8052AH. Beware!

TCON - Timer control

The TCON operator is used to retrieve and/or assign value to the 8052AH's special function register TCON. TCON is a byte register that is used to enable or disable TIMER0 and TIMER1, plus the interrupts that are associated with these timers. Additionally, TCON determines whether the external interrupt pins on the 8052AH are operating in a level sensitive or edge-triggered mode. MCS BASIC-52 initializes TCON with the value 244 (0F4H) and assumes that it is never changed. The value 244 (0F4H) places both TIMER0 and TIMER1 in the run (enabled) mode. If the user disables the operation of TIMER0, by clearing bit 4 in the TCON register, the real time clock will not work. If the user disables the operation of TIMER1, by clearing bit 6 in the TCON register, the EPROM programming routines, the software serial port, and the PWM statement will not work. Use caution when changing TCON!!!

TMOD - Timer mode

The TMOD operator is used to retrieve and/or assign a value to the 8052AH's special function register TMOD. TMOD is a byte register that controls TIMER0 and TlMER1's mode of operation. MCS BASIC-52 initializes the TCON register with a value of 16 (10H). The value 16 (10H) places TIMER0 in mode 0, which is a 13-bit counter mode and TIMER1 in mode 1, which is a 16-bit counter mode . MCS BASIC-52 assumes that the modes of these two timer/counters are never changed. If the user changes the mode of TIMER0, the real time clock will not operate properly. If the user changes the mode of TIMER1, EPROM programming. the software serial port, and the PWM statement will not work properly. If the user does not use these features available in MCS BASIC-52, either timer/counter can be placed in any mode required by the specific application.

TIME

The TIME operator is used to retrieve and/or assign a value to the real time clock resident in MCS BASIC-52. After reset, TIME is equal to 0. The CLOCK1 statement enables the real time clock. When the real time clock is enabled, the special function operator, TIME will increment once every 5 milliseconds. The TIME operator uses TIMER0 and the interrupts associated with TIMER0 on the 8052AH. The unit of TIME is seconds and the appropriate XTAL value must be assigned to insure that the TIME operator is accurate.

When TIME is assigned a value with a LET statement (i.e. TIME = 100), only the integer portion of TIME will be changed.

EXAMPLE:

    >CLOCK1         (enable REAL TIME CLOCK)
    >CLOCK0         (disable REAL TIME CLOCK)
    >PRINT TIME     (display TIME)
     3.315

    >TIME = 0       (set TIME = 0)
    >PRINT TIME     (display TIME)
     .315           (only the integer is changed)

The "fraction" portion of TIME can be changed by manipulating the contents of internal memory location 71 (47H). This is accomplished by a DBY(71) statement. Note that each count in internal memory location 71 (47H) represents 5 milliseconds of TIME.

Continuing with the EXAMPLE:

    >DBY(71) = 0    (fraction of TIME = 0)
    >PRINT TIME
     0

    >DBY(71) = 3    (fraction of TIME = 3, 15 ms)
    >PRINT TIME
     1.5 E-2

The reason only the integer portion of TIME is changed when assigned a value is that it allows the user to generate accurate time intervals. For instance, let's say you want to create an accurate 12 hour clock. There are 43200 seconds in a 12 hour period, so an ONTIME 43200,[ln num] statement is used. Now, when the TIME interrupt occurs the statement TIME = 0 is executed, but the millisecond counter is not re-assigned a value so if interrupt latency happens to exceed 5 milliseconds, the clock will still remain accurate.

TIMER0

The TIMER0 operator is used to retrieve or assign a value to the 8052AH's special function registers TH0 and TL0. This operator treats the byte registers TH0 and TL0 as a 16-bit register pair. TH0 is the high byte and TL0 is the low byte. MCS BASIC-52 uses TH0 and TL0 to implement the real time clock function. If the user does not implement the real time clock function (i.e. does not use the statement CLOCK1) in the BASIC program TH0 and TL0 may be used in any manner suitable to the particular application.

TIMER1

The TIMER1 operator is used to retrieve or assign a value to the 8052AH's special function registers TH1 and TL1. This operator treats the byte registers TH1 and TL1 as a 16-bit register pair TH1 is the high byte and TL1 is the low byte. MCS BASIC-52 uses TH1 and TL1 to implement the timings for the software serial port, the EPROM programming feature, and the PWM statement. If the user does not use any of these features TH1 and TL1 may be used in any manner suitable to the particular application.

TIMER2
TIMER2 is disabled in version 1.2 and will return a BAD SYNTAX error.

The TIMER2 operator is used to retrieve or assign a value to the 8052AH's special function registers TH2 and TL2. This operator treats the byte registers TH2 and TL2 as a 16-bit register pair. TH2 is the high byte and TL2 is the low byte. MCS BASIC-52 uses TH2 and TL2 to generate the baud rate for the serial port. If the user does not use TIMER2 to clock the serial port, TH2 and TL2 may be used in any manner suitable to the particular application.

XTAL

The XTAL operator tells MCS BASIC-52 what frequency the system is operating at. The XTAL operator is used by MCS BASIC-52 to calculate the real time clock reload value, the PROM programming timing, and the software serial port baud rate generation. The XTAL value is expressed in Hz. So,

XTAL = 9000000

would set the XTAL value to 9 MHz.

7.2 EXAMPLES OF MANIPULATING SPECIAL FUNCTION VALUES

Using the logical operators available in MCS BASIC-52. it is possible to write to or read from any byte of the special function registers that MCS BASIC-52 treats as a register pair:

EXAMPLE:

    WRITING TO THE HIGH 3YTE
    >TIMER0 = (TIMER0 .AND. 00FFH)+ INT(256*(USER BYTE))

EXAMPLE:

    WRITING TO THE LOW BYTE
    >TIMER0 = (TIMER0 .AND. 0FF00H) + (USER BYTE)

EXAMPLE:

    READING HIGH BYTE
    >PH0. INT(TIMER0/256)

EXAMPLE:

    READING LOW BYTE
    >PH0. TIMER0 .AND. 0FFH

TIMER1 can function as the baud rate generator for MCS BASIC-52. To assign TIMER1 as the baud rate generator, the following instructions must be executed:

    >TMOD = 32                           - TIMER1 in auto reload mode
    >TIMER1 = 256*(256-(65536-RCAP2)/12) - load TIMER1
    >T2CON = 0                           - use TIMER1 as baud rate gen

This sequence of instructions can be executed in either the direct mode or as part of a program. When TIMER1 is used as the baud rate generator. TIMER2 can be used in any way suitable to the application. The PROG, FPROG, LIST#, PRINT# and PWM commands/statements cannot be used when TIMER1 functions as the baud rate generator for the MCS BASIC-52 device. Certain crystals may not be able to use TIMER1 as the baud rate generator, especially at high (above 2400) baud rates.

7.3 SYSTEM CONTROL VALUES

The system control values determine or reveal how memory is allocated by MCS BASIC-52.

MTOP

After reset, MCS BASIC-52 sizes the external memory and assigns the last valid memory address to the system control value, MTOP. MCS BASIC-52 will not use any external RAM memory beyond the value assigned to MTOP. If the user wishes to allocate some external memory for an assembly language routine the LET statement can be used (e.g. MTOP = user address). If the user assigns a value to MTOP that is greater than the last valid memory address, a MEMORY ALLOCATION error will be generated.

EXAMPLES:

    >PRINT MTOP
     2047

    >MTOP=2000
    >PRINT MTOP
     2000

LEN

The system control value, LEN. tells the user how many bytes of memory thc current selected program occupies. Obviously, LEN cannot be assigned a value, it can only be read. A null program (i.e. no program) will return a LEN of 1. The 1 represents the end of program file character.

FREE

The system control value, FREE, tells the user how many bytes of RAM memory are available to the user. When the current selected is in RAM memory, the following relationship will always hold true.

FREE = MTOP - LEN - 511

NOTE: Unlike some BASICS, MCS BASIC-52 does not require any "dummy" arguments for the system control values.