The text of a manual · part 9 of 17
1771-DB Basic Module User Manual, pages 81 to 90
Pages 81 to 90 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 81
Chapter 5 Operating Functions
5.5.1 Dual Operand (dyadic) Operators (continued)
5.5.1.1 Comments on logical operators .AND.,.OR. and .XOR. These operators perform a BIT-WISE logical function on valid INTEGERS. This means both arguments for these operators are between 0 and 65535 (0FFFFH) inclusive. If they are not, the BASIC Module generates a BAD ARGUMENT ERROR. All non-integer values are truncated, not rounded. Use the following chart for bit manipulations on 16 bit values. Hex Decimal To set bit
0 1 2 3 4 5 6 7 10 11 12 13 14 15 16 17
.OR. VARIABLE WITH
0001H 0002H 0004H 0008H 0010H 0020H 0040H 0080H 0100H 0200H 0400H 0800H 1000H 2000H 4000H 8000H
1 2 4 8 16 32 64 128 256 512 1024 2048 4096 8192 16384 32768
0FFFEH 0FFFDH 0FFFBH 0FFF7H 0FFEFH 0FFDFH 0FFBFH 0FF7FH 0FEFFH 0FDFFH 0FBFFH 0F7FFH 0EFFFH 0DFFFH 0BFFFH 7FFFH
65534 65533 65531 65527 65519 65503 65471 65407 65279 65023 64511 63487 61439 57343 49151 32767
Example: 10 A= A .OR. 0100H:REM SET BIT 10 To clear bit
0 1 2 3 4 5 6 7 10 11 12 13 14 15 16 17
.AND. VARIABLE WITH
Example: 10 A= A .AND. 0F7FFH: REM CLR BIT 13 To clear all bits
.XOR. VARIABLE WITH ITSELF Example: 10 A = A.XOR.A : REM CLR A
5–44
Page 82
Chapter 5 Operating Functions
5.5.2 Unary Operators
We divide the unary operators into three groups; general purpose, log functions and trig functions.
5.5.2.1 General Purpose Operators Following are the general purpose operators. 5.5.2.1.1 ABS([expr]) Returns the absolute value of the expression. Examples: uPRINT ABS(5) 5 uPRINT ABS(–5) 5
5.5.2.1.2 NOT([expr]) Returns a 16 bit one’s complement of the expression. The expression must be a valid integer (i.e. between 0 and 65535 (0FFFFH) inclusive). Non-integers are truncated, not rounded. Examples: uPRINT NOT(65000) 535 uPRINT NOT(0) 65535
5.5.2.1.3 INT([expr]) Returns the integer portion of the expression. Examples: uPRINT INT(3.7) 3 100 uPRINT INT(100.876)
5.5.2.1.4 SGN([expr]) Returns a value of +1 if the argument is greater than zero, zero if the argument is equal to zero and –1 if the argument is less than zero. Examples: uPRINT SGN(52)
uPRINT SGN(0)
uPRINT SGN(–8)
1
0
–1
5.5.2.1.5 SQR([expr]) Returns the square root of the argument. The argument may not be less than zero. The result returned is accurate to within +/–a value of 5 on the least significant digit. Examples: uPRINT SQR(9) 3 uPRINT SQR(45)
6.7082035 uPRINT SQR(100) 10
5–45
Page 83
Chapter 5 Operating Functions
5.5.2 Unary Operators (continued)
5.5.2.1.6 RND Returns a pseudo-random number in the range between 0 and 1 inclusive. The RND operator uses a 16-bit binary seed and generates 65536 pseudo-random numbers before repeating the sequence. The numbers generated are specifically between 0/65535 and 65535/65535 inclusive. Unlike most BASICS, the RND operator in the BASIC Module does not require an argument or a dummy argument. If an argument is placed after the RND operator, a BAD SYNTAX error occurs. Examples: uPRINT Rnd .30278477 5.5.2.1.7 PI PI is a stored constant. In the BASIC Module PI is stored as 3.1415926.
5.5.2.2 Log Functions Following are the log functions. 5.5.2.2.1 LOG([expr]) Returns the natural logarithm of the argument. The argument must be greater than 0. This calculation is carried out to 7 significant digits. Examples: uPRINT LOG(12) 2.484906 uPRINT LOG(EXP(1)) 1
5.5.2.2.2 EXP([expr]) This function raises the number “e” (2.7182818) to the power of the argument. Examples: uPRINT EXP(1) 2.7182818 uPRINT EXP(LOG(2)) 2
5.5.2.3 Trig Functions 5.5.2.3.1 SIN([expr] Returns the sine of the argument. The argument is expressed in radians. Calculations are carried out to 7 significant digits. The argument must be between "200000. Examples: uPRINT SIN(PI/4) .7071067 0
5–46 uPRINT SIN(0)
Page 84
Chapter 5 Operating Functions
5.5.2 Unary Operators (continued)
5.5.2.3.2 COS([expr]) Returns the cosine of the argument. The argument is expressed in radians. Calculations are carried out to 7 significant digits. The argument must be between " 200000. Examples: uPRINT COS(PI/4) .7071067 uPRINT COS(0) 1
5.5.2.3.3 TAN([expr]) Returns the tangent of the argument. The argument is expressed in radians. The argument must be between "200000. Examples: uPRINT TAN(PI/4) 1 uPRINT TAN(0) 0
5.5.2.3.4 ATN([expr]) Returns the arctangent of the argument. The result is in radians. Calculations are carried out to 7 significant digits. The ATN operator returns a result between –PI/2 (3.1415926/2) and PI/2. Examples: uPRINT ATN(PI) 1.2626272 uPRINT ATN(1) .78539804
5.5.2.4 Comments on Trig Functions The SIN, COS and TAN operators use a Taylor series to calculate the function. These operators first reduce the argument to a value between 0 and PI/2. This reduction is accomplished by the following equation: reduced argument = (user arg/PI – INT(user arg/PI)) * PI The reduced argument, from the above equation, is between 0 and PI. The reduced argument is then tested to see if it is greater than PI/2. If it is, then it is subtracted from PI to yield the final value. If it is not, then the reduced argument is the final value.
5–47
Page 85
Chapter 5 Operating Functions
5.5.2 Unary Operators (continued)
Although this method of angle reduction provides a simple and economical means of generating the appropriate arguments for a Taylor series, there is an accuracy problem associated with this technique. The accuracy problem is noticed when the user argument is large (i.e. greater than 1000). This is because significant digits, in the decimal (fraction) portion of reduced argument are lost in the (user arg/PI – INT(user arg/PI)) expression. As a general rule, keep the arguments for the trigonometric functions as small as possible.
5.5.3 Understanding Precedence of Operators
You can write complex expressions using only a small number of parenthesis. To illustrate the precedence of operators examine the following equation: 4+3*2 =? In this equation multiplication has precedence over addition. Therefore, multiply (3*2) and then add 4. 4+3*2 = 10 When an expression is scanned from left to right an operation is not performed until an operator of lower or equal precedence is encountered. In the example you cannot perform addition because multiplication has higher precedence. Use parentheses if you are in doubt about the order of precedence. The precedence of operators from highest to lowest in the BASIC Module is: 1) Operators that use parenthesis () 2) Exponentiation (**) 3) Negation (–) 4) Multiplication (*) and division (/) 5) Addition (+) and subtraction (–) 6) Relational expressions (=, tu , u, u=, t, t=). 7) Logical and (.AND.) 8) Logical or (.OR.) 9) Logical XOR (.XOR.)
5–48
Page 86
Chapter 5 Operating Functions
5.5.4 How Relational Expressions Work
Relational expressions involve the operators =, tu, u, u=, t, and t=. These operators are typically used to “test” a condition. In the BASIC Module relational operations are typically used to “test” a condition. In the BASIC Module relational operators return a result of 665535 (OFFFFH) if the relational expression is true, and a result of 0, if the relation expression is false. The result is returned to the argument stack. Because of this, it is possible to display the result of a relational expression. Examples: PRINT 1=0 PRINT 1u0 PRINT A tu A PRINT A=A 0 65535 0 65535 You can “chain” relational expressions together using the logical operators .AND., .OR., and .XOR.. This makes it possible to test a complex condition with ONE statement. Example: u10 IF ([AuB].AND. [AuC]) .OR.[AuD] THEN Additionally, you can use the NOT([expr]) operator. Example: u10 IF NOT(AuB).AND.(AuC) THEN By “chaining” together relational expressions with logical operators, you can test particular conditions with one statement. Important: When using logical operators to link together relational expressions, you must be sure operations are performed in the proper sequence.
5–49
Page 87
Chapter 5 Operating Functions
5.6 Special Operators
Special operators include special function operators and system control values.
5.6.1 Special Function Operators
Special function operators directly manipulate the I/O hardware and the memory addresses on the processor.
5.6.1.1 GET Use the GET operator in the RUN mode. It returns a result of zero in the command mode. The GET operator reads the console input device. If a character is available from the console device, the value of the character is assigned to GET. After GET is read in the program, it is assigned the value of zero until another character is sent from the console device. The following example prints the decimal representation of any character sent from the console: Example: u10 A=GET u20 IF A tu 0 THEN PRINT A :REM ZERO MEANS NO ENTRY u30 GOTO 10 uRUN 65 49 24 50
(TYPE “A” ON CONSOLE) (TYPE “1” ON CONSOLE) (TYPE “CONTROL–X” ON CONSOLE) (TYPE “2” ON CONSOLE) The GET operator is read only once before it is assigned a value of zero. This guarantees that the first character entered is always read, independent of where the GET operator is placed in the program. There is no buffering of characters on the program port.
5.6.1.2 TIME Use the TIME operator to retrieve and/or assign a value to the real time clock resident in the BASIC Module. 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, increments once every 5 milliseconds. The units of time are in seconds.
5–50
Page 88
Chapter 5 Operating Functions
5.6.1 Special Function Operators (continued)
When TIME is assigned a value with a LET statement (i.e. TIME u 100), only the integer portion of TIME is changed. Example: uCLOCK1
(enable REAL TIME CLOCK)
uCLOCK0
(disable REAL TIME CLOCK)
uPRINT TIME (display TIME) 3.315 uTIME = 0
(set TIME = 0)
uPRINT TIME (display TIME) .315 (only the integer is changed) You can change the “fraction” portion of TIME by manipulating the contents of internal memory location 71 (47H). You can do this by using a DBY(71) statement. Note that each count in internal memory location 71 (47H) represents 5 milliseconds of TIME. Continuing with the example: uDBY(71) = 0
(fraction of TIME = 0)
uPRINT TIME 0 uDBY(71) = 3
(fraction of TIME = 3, 15 ms)
uPRINT TIME 1.5 E–2 Only the integer portion of TIME changes when a value is assigned. This allows you to generate accurate time intervals. For example, if 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. When the TIME interrupt occurs, the statement TIME 0 is executed, but the millisecond counter is not re-assigned a value. If interrupt latency exceeds 5 milliseconds, the clock remains accurate.
5–51
Page 89
Chapter 5 Operating Functions
5.6.2 System Control Values
The system control values determine how memory is allocated by the BASIC Module.
5.6.2.1 MTOP After reset, the BASIC Module sizes the external memory and assigns the last valid memory address to the system control value, MTOP. The module does not use any external RAM memory beyond the value assigned to MTOP. Examples: uPRINT MTOP 14335 or
PH0. MTOP 37FFH
5.6.2.2 LEN The system control value, LEN, tells you how many bytes of memory the currently selected program occupies. This is the length of the program and does not include the size of string memory, variables and array memory usage. You cannot assign LEN a value, it can only be read. A NULL program (i.e. no program) returns a LEN of 1. The 1 represents the end of program file character. Important: The BASIC Module does not require any “dummy” arguments for the system control values.
5.7 Data Transfer Support Routines
The BASIC Module communicates with the host processor using block-transfer communications. The host processor sends variable length blocks of data to the module. You can transfer a maximum of 64 words in and 64 words out per scan. The module responds with the requested block length of data. The module has an auxiliary processor dedicated to servicing of the block-transfers to and from the host processor. These support routines provide the communications link between the host processor and the BASIC processor. The block-transfer-read (BTR) buffer resides at address 7800H and is 128 bytes long. You can examine the values in the buffer using the XBY command, i.e. PRINT XBY(7800H). The block-transfer-write (BTW) buffer resides at one of two starting addresses; 7B00H or 7C00H . Examine addresses 7D0 AH and 7D0BH which point to the proper starting address. 7D0AH contains the high byte of the address and 7D0BH contains the low byte of the address. The BTW buffer is also 128 bytes long. You can also examine these values in the buffer using the XBY command.
5–52
Page 90
Chapter 5 Operating Functions
5.7 Data Transfer Support Routines (continued)
Example:
5.7.1 Update Block-Transfer-Read Buffer (timed) – CALL 2
This routine transfers the block-transfer-read (BTR) buffer to the auxiliary processor on the BASIC Module for use in the next BTR request from the host processor. If no data transfer occurs within 2 seconds the routine returns to BASIC without transferring data. This routine has no input arguments and one output argument (the status of the transfer). A non-zero returned means that no transfer occurred and the CALL timed out. A zero returned means a successful transfer.
u10 REM PRINT CONTENTS OF BTW BUFFER u20 C=(XBY(7D0AH)*100H)+XBY(7D0BH) u30 FOR I=0 TO 128 u40 PH0. XBY(C+I), u50 NEXT I u60 END
Example: u10 CALL 2 u20 POP X u30 IF X tu 0 PRINT “TRANSFER UNSUCCESSFUL”
5.7.2 Update Block-Transfer-Write Buffer (timed) – CALL 3
This routine transfers the block-transfer-write (BTW) buffer of the auxiliary processor on the BASIC Module to the BASIC BTW buffer. If no data transfer occurs within 2 seconds the routine returns to BASIC with no new data. This routine has no input arguments and one output argument (the status of the transfer). A non-zero returned means that no transfer occurred and the CALL timed out. A zero returned means a successful transfer. Example: u10 CALL 3 u20 POP X u30 IF X tu 0 PRINT “TRANSFER UNSUCCESSFUL”
5–53