Browse
(440) 299-7778
Menu
(440) 299-7778
One-year parts and labor warranty30-day money-back guaranteeContact

The text of a manual · part 7 of 17

1771-DB Basic Module User Manual, pages 61 to 70

Pages 61 to 70 of 167

The original document is the PDF (943 KB). This is its text as extracted, unedited; tables and drawings read best in the PDF, and each page heading opens it at that page.

For the Allen-Bradley 1771-DB

Page 61

Chapter 5 Operating Functions

5.4.12 Statements: GOSUB [ln num] – RETURN (continued)

RETURN Use this statement to “return” control to the statement following the most recently executed GO SUB STATEMENT. Use one return for each GOSUB to avoid overflowing the C-STACK. This means that a subroutine called by the GO SUB statement can call another subroutine with another GOSUB statement. Examples: SIMPLE SUBROUTINE NESTED SUBROUTINES u10 FOR I=1 TO 5 u20 GOSUB 100 u30 NEXT I u40 END u100 PRINT I u110 RETURN uRUN 1 2 3 4 5 READY u u10 FOR I=1 TO 3 u20 GOSUB 100 u30 NEXT I u40 END u100 REM USER SUBROUTINE HERE u105 PRINT I, u110 GOSUB 200 u200 PRINT I,I*I u210 RETURN uRUN 1 1 2 4 3 9 READY u

5.4.13 Statement: GOTO [ln num]

Mode: COMMAND AND/OR RUN Type: CONTROL The GOTO [ln num] statement causes BASIC to transfer control directly to the line number ([ln num]) following the GOTO statement. Example: u50 GOTO 100 If line 100 exists, this statement causes execution of the program to resume at line 100. If line number 100 does not exist the message ERROR: INVALID LINE NUMBER is printed to the console device.

5–24

Page 62

Chapter 5 Operating Functions

5.4.13 Statement: GOTO [ln num] (continued)

Unlike the RUN command the GOTO statement, if executed in the COMMAND mode, does not clear the variable storage space or interrupts. However, if the GOTO statement is executed in the COMMAND mode after a line is edited the module clears the variable storage space and all BASIC evoked interrupts. This is necessary because the variable storage and the BASIC program reside in the same RAM memory. Because of this editing a program can destroy variables.

5.4.14 Statements: ON [expr] GOTO [ln num], [ln num],...[ln num], ON [expr] GOSUB[ln num], [ln num],...[ln num]

Mode: RUN Type: CONTROL The value of the expression following the ON statement is the number in the line list that control is transferred to. Example: u10 ON Q GOTO 100,200,300 If Q is equal to 0, control is transferred to line number 100. If Q is equal to 1, control is transferred to line number 200. If Q is equal to 2, GOTO line 300, etc. All comments that apply to GOTO and GOSUB apply to the ON statement. If Q is less than ZERO a BAD ARGUMENT ERROR is generated. If Q is greater than the line number list following the GOTO or GOSUB statement, a BAD SYNTAX ERROR is generated. The ON statement provides “conditional branching” options within the constructs of a BASIC Module program.

5.4.15 Statements: IF – THEN – ELSE

Mode: RUN Type: CONTROL The IF statement sets up a conditional test. The general form of the IF – THEN – ELSE statement follows: [ln num] IF [rel expr] THEN valid statement ELSE valid statement Example: u10 IF A=100 THEN A=0 ELSE A=A+1

5–25

Page 63

Chapter 5 Operating Functions

5.4.15 Statements: IF – THEN – ELSE (continued)

Upon execution of line 10 IF A is equal to 100, THEN A is assigned a value of 0. IF A does not equal 100, A is assigned a value of A+1. If you want to transfer control to different line numbers using the IF statement, you may omit the GOTO statement. The following examples give the same results: u20 IF INT(A) u20 IF INT(A)

t10 THEN GOTO 100 ELSE GOTO 200 or t10 THEN 100 ELSE 200

You can replace the THEN statement with any valid BASIC Module statement, as shown below: u30 IF Atu 10 THEN PRINT A ELSE 10 u30 IF Atu 10 PRINT A ELSE 10 You may execute multiple statements following the THEN or ELSE if you use a colon to separate them. Example: u30 IF Atu 10 THEN PRINT A:GOTO 150 ELSE 10 u30 IF Atu 10 PRINT A:GOTO 150 ELSE 10 In these examples, if A does not equal 10 then both PRINT A and GOTO 150 are executed. If A10, then control passes to 10. You may omit the ELSE statement. If you omit the ELSE statement control passes to the next statement. Example: u20 IF A10 THEN 40 u30 PRINT A In this example, if A equals 10 then control passes to line number 40. If A does not equal 10 line number 30 is executed.

5.4.16 Statement: INPUT

Mode: RUN Type: INPUT/OUTPUT The INPUT statement allows you to enter data from the console during program execution. You may assign data to one or more variables with a single input statement. You must separate the variables with a comma. Example: uINPUT A,B

5–26

Page 64

Chapter 5 Operating Functions

5.4.16 Statement: INPUT (continued)

Causes a question mark (?) to print on the console device. This prompts you to input two numbers separated by a comma. If you do not enter enough data, the module prints TRY AGAIN on the console device. Example: u10 INPUT A,B u20 PRINT A,B uRUN ?1 TRY AGAIN ?1,2 1 2 READY You can write the INPUT statement so that a descriptive prompt tells you what to enter. The message to be printed is placed in quotes after the INPUT statement. If a comma appears before the first variable on the input list, the question mark prompt character is not displayed. Examples:

u10 INPUT“ENTER A NUMBER” A u20 PRINT SQR(A) u30 END uRUN u10 INPUT”ENTER A NUMBER–”,A u20 PRINT SQR(A) u30 END uRUN

ENTER A NUMBER ?100 10

ENTER A NUMBER–100 10 You can also assign strings with an INPUT statement. Strings are always terminated with a carriage return (cr). If more than one string input is requested with a single INPUT statement, the module prompts you with a question mark.

5–27

Page 65

Chapter 5 Operating Functions

5.4.16 Statement: INPUT (continued)

Examples:

u10 STRING 110,10 u20 INPUT “NAME:”,$(1) u30 PRINT “HI”,$(1) u40 END uRUN u10 STRING 110,10 u20 INPUT “NAMES:”,$(1),$(2) u30 PRINT “HI”,$(1),“ AND “,$(2) u40 END uRUN

NAME: SUSAN HI SUSAN

NAMES: BILL ?ANN HI BILL AND ANN

READY READY You can assign strings and variables with a single INPUT statement. Example: u10 STRING 100,10 u20 INPUT“NAME(CR), AGE – ”,$(1),A u30 PRINT “HELLO ”,$(1),“, YOU ARE ”,A,“YEARS OLD” u40 END uRUN NAME(CR), AGE – FRED ?15 HELLO FRED, YOU ARE 15 YEARS OLD READY u

5.4.17 Statement: LD@ [expr]

5–28

Mode: COMMAND AND/OR RUN This statement, along with CALL 77, allows you to save/retrieve variables to/from a protected area of memory. This protected area is not zeroed on power-up or when the RUN command is issued. The LD@ statement takes the variable stored at address [expr] and moves it to the top of the argument stack. For more information on protecting variables, see section 5.11.6, “Protected Variable Storage – CALL 77”.

Page 66

Chapter 5 Operating Functions

5.4.18 Statement: LET

Mode: COMMAND AND/OR RUN Type: ASSIGNMENT Use the LET statement to assign a variable to the value of an expression. The general form of LET is: LET [var] = [expr] Examples: LET A =10*SIN(B)/100 or LET A = A+1 Note that the = sign used in the LET statement is not an equality operator. It is a “replacement” operator. The statement should be read A is replaced by A plus one. The word LET is always optional, (i.e. LET A =2 is the same as A =2). When LET is omitted the LET statement is called an IMPLIED LET. We use the word LET to refer to both the LET statement and the IMPLIED LET statement. Also use the LET statement to assign the string variables: LET $(1)=“THIS IS A STRING” or LET $(2)=$(1) Before you can assign strings you must execute the STRING [expr], [expr] statement or else a MEMORY ALLOCATION ERROR occurs. See the following section 5.4.31 titled, “STRING”.

5–29

Page 67

Chapter 5 Operating Functions

5.4.19 Statement: ONERR [ln num)

Mode: RUN Type: CONTROL The ONERR[ln num] statement lets you handle arithmetic errors, if they occur, during program execution. Only ARITH. OVERFLOW, ARITH. UNDERFLOW, DIVIDE BY ZERO and BAD ARGUMENT errors are “trapped” by the ONE RR statement. All other errors are not “trapped”. If an arithmetic error occurs after the ONE RR statement is executed, the module interpreter passes control to the line number following the ONERR[ln num] statement. You handle the error condition in a manner suitable to the particular application. The ONERR command does not trap bad data entered during an input instruction. This yields a “TRY AGAIN” message or “EXTRA IGNORED” message. See Chapter 9 for an explanation of errors. With the ONERR[ln num] statement, you have the option of determining what type of error occurred. Do this by examining external memory location 257 (101H) after the error condition is trapped. The error codes are: ERROR CODE =10 – DIVIDE BY ZERO ERROR CODE =20 – ARITH. OVERFLOW ERROR CODE =30 – ARITH. UNDERFLOW ERROR CODE =40 – BAD ARGUMENT You can examine this location by using an XBY(257) statement. Example: PRINT XBY(257) or E XBY(257)

5–30

Page 68

Chapter 5 Operating Functions

5.4.20 Statement: ONTIME [expr],[ln num]

Mode: RUN Type: CONTROL Your BASIC Module can process a line in milliseconds while the timer/counters on the microprocessor operate in microseconds. You must use the ONTIME [expr], [In num] statement because of this incompatibility between the timer/counters on the microprocessor and the BASIC Module. The ONTIME statement generates an interrupt every time the special function operator, TIME, is equal to or greater than the expression following the ON TIME statement. Only the integer portion of TIME is compared to the integer portion of the expression. This comparison is performed at the end (CR or:) of each line of BASIC. The interrupt forces a GOSUB to the line number [ln num] ) following the expression ([expr]) in the ONTIME statement. Important: The ONTIME statement does not interrupt an input command or a CALL routine. Since the ONTIME statement uses the special function operator, TIME, you must execute the CLOCK1 statement for ONTIME to operate. If CLOCK1 is not executed the special function operator, TIME, does not increment. You can generate periodic interrupts by executing the ONTIME statement again in the interrupt routine: Example:

u10 TIME=0 : CLOCK1 : ONTIME 2,100: DO u20 WHILE TIMEt10 : END u100 PRINT “TIMER INTERRUPT AT –”,TIME,“SECONDS” u110 ONTIME TIME+2,100 : RETI uRUN TIMER INTERRUPT AT – 2.045 SECONDS TIMER INTERRUPT AT – 4.045 SECONDS TIMER INTERRUPT AT – 6.045 SECONDS TIMER INTERRUPT AT – 8.045 SECONDS TIMER INTERRUPT AT – 10.045 SECONDS READY The terminal used in this example runs at 4800 baud. This baud rate allows about 45 milliseconds to print the message TIMER INTERRUPT AT –“ ”. The resulting printed time is 45 milliseconds greater than the planned time.

5–31

Page 69

Chapter 5 Operating Functions

5.4.20 Statement: ONTIME [expr], [ln num] (continued)

If you do not want this delay, you should assign a variable to the special function operator, TIME, at the beginning of the interrupt routine. Example: u10 TIME=0 : CLOCK1 : ONTIME 2,100: DO u20 WHILE TIME t10: END u100 A=TIME u110 PRINT “TIMER INTERRUPT AT –”,A,“SECONDS” u120 ONTIME A+2,100 : RETI uRUN TIMER INTERRUPT AT – 2 SECONDS TIMER INTERRUPT AT – 4 SECONDS TIMER INTERRUPT AT – 6 SECONDS TIMER INTERRUPT AT –8 SECONDS TIMER INTERRUPT AT – 10 SECONDS READY Important: You must exit the ONTIME interrupt routine with a RET I statement. Failure to do this “locks-out” all future interrupts. The ONTIME statement eliminates the need for you to “test” the value of the TIME operator periodically throughout the BASIC program.

5.4.21 Statement: PRINT or P.

Mode: COMMAND and/or RUN Type: INPUT/OUTPUT The PRINT statement directs the BASIC Module to output to the console device. You may print the value of expressions, strings, literal values, variables or text strings. You may combine the various forms in the print list by separating them with commas. If the list is terminated with a comma, the carriage return/line feed is suppressed. P. is a “shorthand” notation for PRINT. Examples: uPRINT 10*10,3*3 u PRINT “MCS–51″ u PRINT 5,1E3 100 9 MCS–51 5 1000 Important: Values are printed next to one another with two intervening blanks. A PRINT statement with no arguments sends a carriage return/line feed sequence to the console device.

5–32

Page 70

Chapter 5 Operating Functions

5.4.22 Special Print Formatting Statements

The following sections list and describe the special print formatting statements.

5.4.22.1 PRINT TAB([expr]) Use the TAB([expr]) function in the PRINT statement to cause data to print out in exact locations on the output device. TAB([expr]) tells the BASIC Module which position to begin printing the next value in the print list. If the printhead or cursor is on or beyond the specified TAB position, the module ignores the TAB function. Example: uPRINT TAB(5),“X”,TAB(10),“Y” X Y

5.4.22.2 PRINT SPC([expr]) Use the SPC([expr]) function in the PRINT statement to cause the BASIC Module to output the number of spaces in the SPC argument. Example: u PRINT A,SPC(5),B Use the above statement to place an additional 5 spaces between the A and B in addition to the two that would normally print.

5.4.22.3 PRINT CR Use CR in a PRINT statement to force a carriage return, but no line feed. You can use CR to create one line on a CRT device that is repeatedly updated. Example: u 10 FOR I=1 TO 1000 u20 PRINT I,CR, u30 NEXT I The above example causes the output to remain on one line only. No line feed is ever sent to the console device.

5–33