The Microcontroller Idea Book - grifo¨ COM

Additional PRINT options are CR, SPC, TAB, USING. PRINT#. C,R. Same as PRINT, but outputs to LPT (pin 8). BAUD and XTAL values affect the. PRINT# rate.
43KB taille 5 téléchargements 44 vues
The following material is excerpted from:

The Microcontroller Idea Book Circuits, Programs, & Applications featuring the 8052-BASIC Microcontroller by Jan Axelson

copyright 1994, 1997 by Jan Axelson ISBN 0-9650819-0-7 Published by Lakeview Research Distribution by International Thomson Publishing (ITP) in arrangement with Peer-to-Peer Communications. For more information, contact: Lakeview Research 2209 Winnebago St. Madison, WI 53704 USA Phone: 608-241-5824 Fax: 608-241-5848 Email: [email protected] World Wide Web: http://www.lvr.com

You may distribute this material if you agree to distribute it in full and unchanged and agree to charge no fee for such distribution with the exception of reasonable media charges. The author and publisher have used their best efforts in preparing this work and the materials in it. The author built and tested the electronic circuits described, ran and tested the computer programs presented, and reviewed all materials for completeness and accuracy. The author and publisher make no warranty with regard to the circuit schematics, program listings, and other materials in this work. The author and publisher take no responsibility for any damages resulting from any use of the material in this work.

Chapter 5

Quick Reference to BASIC-52 This quick reference to the BASIC-52 programming language lists the keywords alphabetically, along with brief descriptions of function and use. Conventions The reference uses the following typographic conventions: KEYWORDS (boldface uppercase) BASIC-52 keywords placeholders (italics) Variables, expressions, constants, or other information that you must supply [optional items] (enclosed in square brackets) Items that are not required repeating elements... (followed by ellipsis (three dots)) You may add more items with the same form as the preceding item.

C = command mode R = run mode

variable = expression Assigns a value to a variable

C,R

expression = expression Equivalence test (relational operator)

C,R

expression + expression Add

C,R

expression - expression Subtract

C,R

expression * expression Multiply

C,R

74

The Microcontroller Idea Book

Programming

expression / expression Divide

C,R

expression ** expression Raises first expression to value of second expression (exponent)

C,R

expression expression Inequality test (relational operator)

C,R

expression < expression Less than test (relational operator)

C,R

expression > expression Greater than test (relational operator)

C,R

expression = expression Greater than or equal test (relational operator)

C,R

? Same as PRINT ABS (expression) Returns the absolute value of expression

C,R

expression .AND. expression Logical AND

C,R

ASC(character) Returns the value of ASCII character

C,R

ATN(expression) Returns the arctangent of expression

C,R

BAUD expression Sets the baud rate for LPT (pin 8). For proper operation, XTAL must match the system’s crystal frequency.

C,R

CALL integer C,R Calls an assembly-language routine at the specified address in program memory. The Microcontroller Idea Book

75

Chapter 5

CBY(expression) Retrieves the value at expression in program, or code, memory.

C,R

CHR(expression) Converts expression to its ASCII character.

C,R

CLEAR Sets all variables to 0, resets all stacks and interrupts evoked by BASIC.

C,R

CLEARI Clears all interrupts evoked by BASIC. Disables ONTIME, ONEX1.

C,R

CLEARS C,R Resets BASIC-52’s stacks. Sets control stack = 0FEh, argument stack = 1FEh, internal stack = value in 3Eh in internal RAM. CLOCK0 Disables the real-time clock.

C,R

CLOCK1 Enables the real-time clock.

C,R

CONT Continues executing program after STOP or CONTROL+C.

C

COS(expression) Returns the cosine of expression

C,R

CR PRINT option. Causes a carriage return, but no line feed, on the host display. DATA expression [,...,expression] Specifies expressions to be retrieved by a READ statement.

R

DBY(expression) Retrieves or assigns a value at expression in internal data memory.

C,R

DIM array name [(size)] [,...array name(size)] Reserves storage for an array. Default size is 11 (0-10). Size limits are 0-254. Example: DIM B(100) Reserves storage for 100-element array B

C,R

76

The Microcontroller Idea Book

Programming

DO: [program statements]: UNTIL relational expression Executes all statements between DO and UNTIL until relational expression is true.

R

DO: [program statements]: WHILE relational expression Executes all statements between DO and WHILE until relational expression is false.

R

END Terminates program execution.

R

EXP (expression) Raises e (2.7182818) to the power of expression

C,R

FOR counter variable = start-count expression C,R TO end-count expression [ STEP count-increment expression]: [program statements]: NEXT [counter variable] Executes all statements between FOR and NEXT the number of times specified by the counter and step expressions. FPROG, FPROG1-FPROG6 Like PROG, PROG1-PROG6, but using Intelligent programming algorithm.

C

FREE Returns the number of bytes of unused external data RAM.

C,R

GET Contains the ASCII code of a character received from the host computer’s keyboard. After a program reads the value of GET (For example, G=GET), GET returns to 0 until a new character arrives.

R

GOSUB line number Causes BASIC-52 to transfer program control to a subroutine beginning at line number. A RETURN statement returns control to the line number following the GOSUB statement.

R

GOTO line number Causes BASIC-52 to jump to line number in the current program.

C,R

IDLE Forces BASIC-52 to wait for ONTIME or ONEX1 interrupt.

R

The Microcontroller Idea Book

77

Chapter 5

IE Retrieves or assigns a value to the 8052’s special function register IE.

C,R

IF relational expression THEN program statements [ELSE] [program statements] If relational expression is true, executes program statements following THEN. If relational expression is false, executes program statements following ELSE, if used.

R

INPUT [“Prompt message”][,] variable [,variable] [,...variable] R Displays a question mark and optional prompt message on the host computer and waits for keyboard input. Stores input in variable(s). A comma before the first variable suppresses the question mark. INT(expression) Returns integer portion of expression.

C,R

IP Retrieves or assigns a value to the 8052’s special function register IP.

C,R

LD@ expression C,R Retrieves a 6-byte floating-point number and places it on the argument stack. Expression points to the most significant byte of the number. LEN Returns the number of bytes in the current program

C,R

[LET] variable = expression Assigns a variable to the value of expression. Use of LET is optional.

C,R

LIST[line number][-line number] Displays the current program on the host computer.

C,R

LIST# [line number][-line number] Writes the current program to LPT (pin 8).

C,R

LIST@ [line number][-line number] Writes the current program to a user-written assembly-language output driver at 40C3h. Setting bit 7 of internal data memory location 27H enables the driver.

C,R

78

The Microcontroller Idea Book

Programming LOG(expression) Returns natural logarithm of expression.

C,R

MTOP [=highest address in RAM program space] Assigns or reads the highest address BASIC-52 will use to store variables, strings, and RAM programs. Usually 7FFFh or lower, since EPROM space begins at 8000h.

C,R

NEW Erases current program in RAM; clears all variables.

C

NOT (expression) Returns 1’s complement (inverse) of expression.

C,R

NULL [integer] Sets the number (0-255) of NULL characters (ASCII 00) that BASIC-52 sends automatically after a carriage return. Only very slow printers or terminals need these extra nulls.

C

ON expression GOSUB line number [,line number] [,...,line number] R Transfers program control to a subroutine beginning at one of the line numbers in the list. The value of expression matches the position of the line number selected, with the first line number at position 0. Examples: X=1 ON X GOTO 100,200,400 Transfers program control to a subroutine at line 200 (position 1 in the list) X=0 ON X GOTO 800,300 Transfers program control to a subroutine at line 800 (position 0 in the list) ON expression GOTO line number [,line number] [,...,line number] Transfers program control to one of the line numbers in a list of numbers. The value of expression matches the position of the line number selected, with the first line number at position 0. Example:

R

X=0 ON X GOTO 800,300 Transfers program control to line 800 (position 0 in the list) The Microcontroller Idea Book

79

Chapter 5

ONERR line number R Passes control to line number following an arithmetic error. Arithmetic errors include ARITH. OVERFLOW, ARITH. UNDERFLOW, DIVIDE BY ZERO, and BAD ARGUMENT. ONEX1 line number R On interrupt 1 (pin 13), BASIC-52 finishes executing the current statement, and then passes control to an interrupt routine beginning at line number. The interrupt routine must end with RETI. ONTIME number of seconds, line number R When TIME = number of seconds, BASIC-52 passes control to an interrupt routine beginning at line number. The interrupt routine must end with RETI. CLOCK1 starts the timer. expression .OR. expression Logical OR

C,R

P. same as PRINT PCON Retrieves or assigns a value to the 8052’s special function register PCON.

C,R

PGM C,R Programs an EPROM, EEPROM, or NV RAM with data from memory. The following data must be stored in internal data memory in the locations listed: 1Bh,19h High byte, low byte of first address of data to program 1Ah,18h High byte, low byte of first address to be programmed - 1 1Fh,1Eh High byte, low byte indicating number of bytes to program 40h,41h High byte, low byte indicating width of programming pulse. High byte = ((65536 - pulse width in seconds * XTAL/12) / 256. Low byte = ((65536 - pulse width in seconds * XTAL/12) .AND. 0FFh. 26h For Intelligent programming, set bit 3. For 50-millisecond programming, clear bit 3. PH0. C,R Same as PRINT, but displays values in hexadecimal format. Uses two digits to display values less than 0FFh. PH0.# Same as PRINT#, but displays values in PH0. hexadecimal format 80

C,R

The Microcontroller Idea Book

Programming

PH0.@ Same as PRINT@, but outputs values in PH0. hexadecimal format.

C,R

PH1. Same as PRINT, but displays values in hexadecimal format. Always displays four digits.

C,R

PH1.# Same as PRINT#, but displays values in PH1. hexadecimal format.

C,R

PH1.@ Same as PRINT@, but outputs values in PH1. hexadecimal format.

C,R

PI Constant equal to 3.1415926.

C,R

POP variable [,...variable] Assigns the value of the top of the argument stack to variable.

C,R

PORT1 Retrieves or assigns a value to PORT1 (pins 1-8).

C,R

PRINT [expression] [,...expression] [,] C,R Displays the value of expression(s) on the host computer. A comma at the end of the statement suppresses the CARRIAGE RETURN/LINEFEED. Values are separated by two spaces. Additional PRINT options are CR, SPC, TAB, USING. PRINT# C,R Same as PRINT, but outputs to LPT (pin 8). BAUD and XTAL values affect the PRINT# rate. PRINT@ C,R Same as PRINT, but outputs to a user-defined output driver. Requires an assembly-language output routine at 403Ch in external program memory. Setting bit 7 of internal data memory location 24h enables the output routine. PROG Stores the current RAM program in the EPROM space.

The Microcontroller Idea Book

C

81

Chapter 5 PROG1 Saves the serial-port baud rate. On power-up or reset, BASIC-52 boots without having to receive a space character. The terminal’s baud rate must match the stored value.

C

PROG2 C Like PROG1, but on power-up or reset, BASIC-52 also begins executing the first program in the EPROM space. PROG3 Like PROG1, but also saves MTOP. On power-up or reset, BASIC-52 clears memory only to MTOP.

C

PROG4 Like PROG2, but also saves MTOP. On power-up or reset, BASIC-52 clears memory only to MTOP.

C

PROG5 C Like PROG3, but also reads 5Fh in external data memory on power-up or reset. If 5Fh contains 0A5h, BASIC-52 doesn’t clear external data memory. If data memory location 5Eh contains 34h, BASIC-52 will automatically begin executing a program in external data memory. PROG6 C Like PROG5, but if external data memory location contains 5Fh, BASIC-52 calls a user-written assembly-language reset routine beginning at program memory 4039h. PUSH expression [,...expression] Places the values of expression(s) sequentially on BASIC-52’s argument stack.

C,R

PWM expression1, expression2, expression3 C,R Outputs a pulse-width modulated (PWM) sequence of pulses on pin 3. Expression1 is the width of each high pulse, expressed in clock cycles. Expression2 is the width of each low pulse, expressed in clock cycles. Expression3 is the number of PWM cycles output. One clock cycle = 12/XTAL. One PWM cycle = one high pulse plus one low pulse. Expression1 and Expression2 must each be at least 25. Maximum for each Expression is 65535. RAM Selects the current program in the RAM space.

82

C

The Microcontroller Idea Book

Programming RCAP2 C,R Retrieves or assigns a value to the 8052’s special function registers RCAP2H and RCAP2L. READ variable [,...,variable] Retrieves the expressions in a DATA statement and assigns each expression to a variable.

R

REM C,R Introduces a comment, or remark. BASIC-52 ignores all text after REM in a program line. RESTORE Resets READ pointer to the first expression in the DATA statement.

R

RETI R Returns program control to the line number following the most recently executed ONEX1 or ONTIME statement. RETURN R Returns program control to the line number following the most recently executed GOSUB statement. RND Returns a pseudo-random number between 0 and 1 inclusive.

C,R

ROM [program number] Selects a program in the EPROM space (beginning at 8000h). Default program number is 1.

C

RROM [program number] C,R Changes to ROM mode and runs the specified program. Default program number is 1. RUN Executes the current program. Clears all variables.

R

SGN (expression) Returns +1 if expression >=0, zero if expression = 0, and -1 if expression